打飞碟小游戏
本次打飞碟小游戏,基本上是在上两周面向对象上编程实现的一个小游戏。在课程群里面,老师分享了一个优秀博客,此处引入,作为借鉴。
在本次作业中,我的脚本结构主要是参考上一次动作分离版牧师与魔鬼,保留SSDirector、SSAction与SSActionManager。在这次写代码的过程中,我发现我定义的接口太混乱,所以将所有的接口又再次提出放到了Interface中,这样比较方便debug。在本次代码中,新引入的就是Singleton,以及DiskData与DiskFactory。在此处的DiskFactory是用来管理飞碟出场的,并且是单例的;DiskData是用来对每一个飞碟加入自己的属性,即得分与大小比例,然后加入飞碟的预制中。
以下是上周动作分离设计的UML图:
在这个游戏中,稍稍改良,在CCActionManager改为FlyActionManager,下面的CCActionSequence改为了一个动作发生——UFOFlyAction。
在这位同学的博客中,我借鉴了他的爆炸的预设和skybox,好吧,是因为我实在懒得挑了,而且这个天空盒的画风真的太赞了~向这位同学学习学习~
游戏简介
在这个游戏中,一共有三个关卡,第一个关卡只有红色的飞碟,一个飞碟是1分。当得到15分后,即进入第二关,新加入黄色飞碟,击中一个得2分。当得到30分之后,即进入第三关,新加入蓝色飞碟,击中一个得3分。当错过了10个飞碟的话,游戏结束。
具体函数实现
- 首先介绍FirstController:
继承MonoBehavior,ISceneController,IUserAction。主要函数如下:
void Start ()
{
//获取各个实例及各个类
SSDirector director = SSDirector.getInstance();
director.currentScenceController = this;
diskFactory = Singleton<DiskFactory>.Instance;
scoreRecorder = Singleton<ScoreRecorder>.Instance;
actionManager = gameObject.AddComponent<FlyActionManager>() as FlyActionManager;
userGUI = gameObject.AddComponent<UserGUI>() as UserGUI;
}
void Update ()
{
if(isGameStart)
{
if (isGameOver)
{
CancelInvoke("LoadResources");
}
if (!isPlayGame)
{
InvokeRepeating("LoadResources", 1f, speed);
isPlayGame = true;
}
SendDisk();
if (scoreRecorder.score >= scoreRound2 && round == 1)
{
round = 2;
speed = speed - 0.6f;
CancelInvoke("LoadResources");
isPlayGame = false;
}
else if (scoreRecorder.score >= scoreRound3 && round == 2)
{
round = 3;
speed = speed - 0.5f;
CancelInvoke("LoadResources");
isPlayGame = false;
}
}
}
//加载资源
public void LoadResources()
{
diskQueue.Enqueue(diskFactory.GetDisk(round));
}
//发送飞碟
private void SendDisk()
{
float position_x = 16;
if (diskQueue.Count != 0)
{
GameObject disk = diskQueue.Dequeue();
diskNotHit.Add(disk);
disk.SetActive(true);
float ran_y = Random.Range(1f, 4f);
float ran_x = Random.Range(-1f, 1f) < 0 ? -1 : 1;
disk.GetComponent<DiskData>().direction = new Vector3(ran_x, ran_y, 0);
Vector3 position = new Vector3(-disk.GetComponent<DiskData>().direction.x * position_x, ran_y, 0);
disk.transform.position = position;
float power = Random.Range(10f, 15f);
float angle = Random.Range(15f, 28f);
actionManager.UFOFly(disk,angle,power);
}
for (int