游戏内容要求:
-
游戏有 n 个 round,每个 round 都包括10 次 trial。
-
每个 trial 的飞碟的色彩、大小、发射位置、速度、角度、同时出现的个数都可能不同。它们由该 round 的 ruler 控制。
-
每个 trial 的飞碟有随机性,总体难度随 round 上升。
-
鼠标点中得分,得分规则按色彩、大小、速度不同计算,规则可自由设定
游戏的要求:
- 使用带缓存的工厂模式管理不同飞碟的生产与回收,该工厂必须是场景单实例的!具体实现见参考资源 Singleton 模板类。
- 近可能使用前面 MVC 结构实现人机交互与游戏模型分离。
游戏效果图
详细介绍:
一、UML图
- 结构上参考了课件的描述,采用工厂+单实例+对象池的方法,结合前面学习的MVC方法,将人机交互和游戏模型分离
- 代码结构:
Director: 常规的Director,一个游戏只有一个
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Director : System.Object
{
//导演,控制场景切换
private static Director _instance;
public SceneController currentSceneController { get; set; }
public bool running { get; set; }
public static Director getInstance()
{
if (_instance == null)
{
_instance = new Director();
}
return _instance;
}
public int getFPS()
{
return Application.targetFrameRate;
}
//让游戏以指定的帧率执行
public void setFPS(int fps)
{
Application.targetFrameRate = fps;
}
public void NextScene()
{
}
}
DiskData: 给飞盘添加属性
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Attributes
{
//飞盘大小
public float size;
//飞盘颜色
public Color color;
//发射飞盘的位置
public Vector3 position;
//飞盘的发射方向
public Vector3 direction;
//飞盘速度
public float speed;
//飞碟材料
public Material material;
//打击飞盘的分数
public int score;
//构造函数
public Attributes(float size, Color color, Vector3 position, Vector3 direction, float speed, Material material)
{
this.size = size;
this.color = color;
this.material = material;
this.position = position;
this.direction = direction;
this.speed = speed;
this.score = ((int)(this.speed + 10 - this.size));
}
}
public class DiskData : MonoBehaviour
{
Attributes _attributes;
public Attributes attributes
{
get
{
return _attributes;
}
set
{
_attributes = value;
this.gameObject.GetComponent<Transform>().position = value.position;
this.gameObject.GetComponent<Transform>().localScale = new Vector3(1, 1, 1) * value.size;
//this.gameObject.transform.GetChild(0).gameObject.GetComponent<Renderer>().material.color = value.color;
//this.gameObject.GetComponent<Renderer>().material = value.material;
this.GetComponentsInChildren<Renderer>()[0].materials[0] = value.material;
Transform[] father = this.gameObject.GetComponentsInChildren<Transform>();
foreach (var child in father)
{
child.GetComponent<Renderer>().material = value.material;
}
this.gameObject.transform.GetChild(0).GetComponent<MeshRenderer>().material = value.material;
Debug.Log("this.gameObject.GetComponentsInChildren<Renderer>()[0].materials[0]: " + value.material.name + this.gameObject.transform.GetChild(0).GetComponent<MeshRenderer>().material.name);
//Debug.Log("this.gameObject.GetComponent<Renderer>().