前言
这是中山大学软件工程学院2023年3D游戏编程与设计的第六次作业,也欢迎大家学习参考交流
github个人主页: innitinnit (github.com)
目录
游戏要求/规则
1、游戏有3个 round,每个 round 都包括10 次 trial;
2、每个 trial 的飞碟的色彩、大小、发射位置、速度、角度、同时出现的个数都可能不同。它们由该 round 的 ruler 控制;
3、每个 trial 的飞碟有随机性,总体难度随 round 上升;
4、鼠标点中得分,得分规则按色彩、大小、速度不同计算,规则可自由设定
游戏实现原理UML图
游戏文件结构
游戏关键代码
裁判类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Judger : MonoBehaviour
{
private int score;
private int miss;
void Start()
{
score = 0;
miss = 0;
}
public int getScore()
{
return score;
}
public bool checkGame()
{
if (miss >= 10)
{
return false;
}
return true;
}
public void hit(GameObject disk)
{
if (disk.GetComponent<DiskInfo>().color == Color.white)
{
score += 1;
}
else if (disk.GetComponent<DiskInfo>().color == Color.red)
{
score += 2;
}
else
{
score += 3;
}
}
public void Miss()
{
Debug.Log("Miss one!");
miss += 1;
}
public void restart()
{
miss = 0;
score = 0;
}
}
飞碟工厂
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DiskFactory : MonoBehaviour
{
public GameObject disk = null;
private List<DiskInfo> activeList = new List<DiskInfo>();
private List<DiskInfo> freeList = new List<DiskInfo>();
int rand1 = 6, rand2 = 9, rand3 = 13;
public int difficulty = 0;
public GameObject GetDisk(int round)
{
GameObject newDisk = null;
if (freeList.Count > 0)
{
newDisk = freeList[0].gameObject;
freeList.Remove(freeList[0]);
}
else
{
newDisk = Instantiate(Resources.Load<GameObject>("Prefabs/Disk"), Vector3.zero, Quaternion.identity);
newDisk.AddComponent<DiskInfo>();
}
switch (round)
{
case 1:
{
difficulty = Random.Range(0, rand1);
break;
}
case 2:
{
difficulty = Random.Range(0, rand2);
break;
}
case 3:
{
difficulty = Random.Range(0, rand3);
break;
}
}
if (difficulty < rand1 - 1)
{
newDisk.GetComponent<DiskInfo>().color = Color.white;
newDisk.GetComponent<DiskInfo>().speed = 5.0f;
float RanX = Random.Range(-1f, 1f) < 0 ? -2 : 2;
newDisk.GetComponent<DiskInfo>().target = new Vector3(RanX, 1, 0);
newDisk.GetComponent<Renderer>().material.color = Color.white;
}
else if (difficulty < rand2 - 1)
{
newDisk.GetComponent<DiskInfo>().color = Color.red;
newDisk.GetComponent<DiskInfo>().speed = 7.0f;
float RanX = Random.Range(-1f, 1f) < 0 ? -2 : 2;
newDisk.GetComponent<DiskInfo>().target = new Vector3(RanX, 1, 0);
newDisk.GetComponent<Renderer>().material.color = Color.red;
}
else
{
newDisk.GetComponent<DiskInfo>().color = Color.black;
newDisk.GetComponent<DiskInfo>().speed = 9.0f;
float RanX = Random.Range(-1f, 1f) < 0 ? -2 : 2;
newDisk.GetComponent<DiskInfo>().target = new Vector3(RanX, 1, 0);
newDisk.GetComponent<Renderer>().material.color = Color.black;
}
activeList.Add(newDisk.GetComponent<DiskInfo>());
return newDisk;
}
public void freeDisk(GameObject disk)
{
DiskInfo tmp = null;
foreach (DiskInfo i in activeList)
{
if (disk.GetInstanceID() == i.gameObject.GetInstanceID())
{
tmp = i;
break;
}
}
if (tmp != null)
{
tmp.gameObject.SetActive(false);
tmp.hit = 0;
freeList.Add(tmp);
activeList.Remove(tmp);
}
}
}
public class DiskInfo : MonoBehaviour
{
public Vector3 pos;
public Color color;
public float speed;
public Vector3 target;
public int hit = 0;
}
飞行动作类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FlyAction : SSAction
{
public float g = 9.8f;
public Vector3 to;//初速度方向
public float v; //初速度
public float v_down = 0;
public float time;
private FlyAction() { }
public static FlyAction GetSSAction(Vector3 target, float speed)
{
FlyAction action = ScriptableObject.CreateInstance<FlyAction>();
action.to = target;
action.v = speed;
return action;
}
public override void Update()
{
time += Time.fixedDeltaTime;
this.transform.position += Vector3.down * (float)(v_down * Time.fixedDeltaTime + 0.5 * g * (Time.fixedDeltaTime) * (Time.fixedDeltaTime));
this.transform.position += to * v * Time.fixedDeltaTime;
v_down = time * g;
if (this.transform.position.y <= -5)
{
this.destroy = true;
if (this.transform.position.y > -15)
{
Singleton<Judger>.Instance.Miss();
}
this.callBack.SSActionEvent(this);
}
}
public override void Start()
{
}
}