游戏设计要求:
- 创建一个地图和若干巡逻兵(使用动画);
- 每个巡逻兵走一个3~5个边的凸多边型,位置数据是相对地址。即每次确定下一个目标位置,用自己当前位置为原点计算;
- 巡逻兵碰撞到障碍物,则会自动选下一个点为目标;
- 巡逻兵在设定范围内感知到玩家,会自动追击玩家;
- 失去玩家目标后,继续巡逻;
- 计分:玩家每次甩掉一个巡逻兵计一分,与巡逻兵碰撞游戏结束;
- 程序设计要求:
- 必须使用订阅与发布模式传消息
- 工厂模式生产巡逻兵
实现的代码地址为:Github
视频的地址为:腾讯视频
首先是找到巡逻兵的资源,我是在Asset Store上找到的资源,大家自己找自己喜欢的就好了。
然后进行具体的代码实现,首先在实现好对各个物体的具体控制类之后,然后就是实现GameEventManager类,也是来进行订阅者模式的代码。
public class GameEventManager : MonoBehaviour
{
public delegate void EscapeEvent(GameObject patrol);
public static event EscapeEvent OnGoalLost;
public delegate void FollowEvent(GameObject patrol);
public static event FollowEvent OnFollowing;
public delegate void GameOverEvent();
public static event GameOverEvent GameOver;
public delegate void WinEvent();
public static event WinEvent Win;
public void PlayerEscape(GameObject patrol) {
if (OnGoalLost != null) {
OnGoalLost(patrol);
}
}
public void FollowPlayer(GameObject patrol) {
if (OnFollowing != null) {
OnFollowing(patrol);
}
}
public void OnPlayerCatched() {
if (GameOver != null) {
GameOver();
}
}
public void TimeIsUP() {
if (Win != null) {
Win();
}
}
}
然后这次也有一些比较需要实现的内容,镜头跟随的内容,没有这个的话,游戏进行的体验不是非常的好。
public class CameraFollowAction : MonoBehaviour
{
public GameObject player;
public float smothing = 5f;
Vector3 offset;
void Start() {
offset = new Vector3(0, 5, -5);
}
void FixedUpdate() {
Vector3 target = player.transform.position + offset;
transform.position = Vector3.Lerp(transform.position, target, smothing * Time.deltaTime);
}
}