1、智能巡逻兵
- 提交要求:
- 游戏设计要求:
- 创建一个地图和若干巡逻兵(使用动画);
- 每个巡逻兵走一个3~5个边的凸多边形,位置数据是相对地址。即每次确定下一个目标位置,用自己当前位置为原点计算;
- 巡逻兵碰撞到障碍物,则会自动选下一个点为目标;
- 巡逻兵在设定范围内感知到玩家,则会自动追击玩家;
- 失去玩家目标后,继续巡逻;
- 计分:玩家每次甩掉一个巡逻兵计一分,与巡逻兵碰撞游戏结束;
- 程序设计要求:
- 必须使用订阅与发布模式传消息
- subject:OnLostGoal
- Publisher:?
- Subscriber:?
- 工厂模式生产巡逻兵
- 必须使用订阅与发布模式传消息
- 友善提示1:生产3~5个边的凸多边形
- 随机生成矩形
- 在矩形每个边上随机找点,可得到3~4的凸多边形
- 5?
代码说明:
- SSDirector:用来管理游戏场景的导演类。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SSDirector : System.Object
{
private static SSDirector _instance;
public ISceneController CurrentScenceController {
get; set; }
public static SSDirector GetInstance()
{
if (_instance == null)
{
_instance = new SSDirector();
}
return _instance;
}
}
- FirstController:游戏的第一个场景,用来加载资源。运用观察者模式,在游戏场景中订阅了分数以及游戏结束的事件,并在该类中实现具体事件。此外还提供了UserGUI可以使用的接口。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum GameState : int {
GAMEOVER, RUNNING, RESTART }
public class FirstController : MonoBehaviour, ISceneController, IUserAction
{
PatrolFactory df = null;
GameState gamestate;
private GameObject player;
private int score;
void Awake()
{
SSDirector director = SSDirector.GetInstance();
director.CurrentScenceController = this;
df = Singleton<PatrolFactory>.Instance;
director.CurrentScenceController.LoadResources();
gamestate = GameState.RUNNING;
score = 0;
this.gameObject.AddComponent<UserGUI>();
this.gameObject.AddComponent<GameEventManager>();
}
public void LoadResources()
{
//产生迷宫
GameObject maze = Instantiate<GameObject>(Resources.Load<GameObject>("prefabs/maze"));
maze.name = "maze";
//产生玩家
player = Instantiate<GameObject>(Resources.Load<GameObject>("prefabs/FreeVoxelGirlBlackhairPrefab 1"));
player.name = "player";
player.transform.position = new Vector3(0, 0, 2);
df.GetPatrol(new Vector3(-13, 0, 8), PatrolDirection.RIGHT);
df.GetPatrol(new Vector3(3, 0, 8), PatrolDirection.DOWN);
df.GetPatrol(new Vector3(13, 0, 2), PatrolDirection.LEFT