Unity3d学习之路-牧师与魔鬼
游戏基本介绍
游戏规则:
Priests and Devils is a puzzle game in which you will help the Priests and Devils to cross the river within the time limit. There are 3 priests and 3 devils at one side of the river. They all want to get to the other side of this river, but there is only one boat and this boat can only carry two persons each time. And there must be one person steering the boat from one side to the other side. In the flash game, you can click on them to move them and click the go button to move the boat to the other direction. If the priests are out numbered by the devils on either side of the river, they get killed and the game is over. You can try it in many > ways. Keep all priests alive! Good luck!
游戏中提及的事物(Objects):
魔鬼,牧师,船,河流,两边的陆地用表格列出玩家动作表(规则表):
玩家动作 | 执行条件 | 执行结果 |
---|---|---|
点击牧师/魔鬼 | 游戏未结束,船没有正在移动,与船在相同的一边 | 牧师/魔鬼移动 |
点击船 | 游戏未结束且船上至少有一人 | 船移动 |
- 本游戏架构参考下图:
MVC模式真的很好用,用过都说好,不过本次游戏只创建了三个脚本,所以接口和导演是和GameModels放在同一个脚本的同一命名空间下。
游戏制作流程
搭建场景
在Unity官方的Asset Store输入需要查找的资源,找到了免费的模型且模型带有动画(Asset Store中有很多免费的资源但是注意版权哦),然后布置场景,把它们放在场景中,记录他们不同状态的时候的坐标,方便接下来使用。创建各类构建基本的架构
1.SSDirector
利用单例模式创建导演,和Cocos2d-x一样一个游戏导演只能有一个,这里继承于System.Object,保持导演类一直存在,不被Unity内存管理而管理,导演类类似于生活中的导演,安排场景,场景切换,都是靠它来指挥。
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;
}
}
2.ISceneController
这是一个场景的控制器的接口,算是导演与场景控制器沟通的接口,利用这个接口,得知当前场景是由哪个控制,然后向场景控制器传达要求等。
public interface ISceneController
{
void LoadResources(); //加载场景
}
3.IUserAction
用户进行操作后与游戏中发生响应的接口,用户通过键盘、鼠标等对游戏发出指令,这个指令会触发游戏中的一些行为,(比如在这个游戏中,点击角色让角色移动,这个角色移动就是用户动作后触发的行为),由IUserAction来声明
public interface IUserAction //用户互动会发生的事件
{
void MoveBoat(); //移动船
void Restart(); //重新开始
void MoveRole(RoleModel role); //移动角色
int Check(); //检测游戏结束
}
4.FirstController
这是一个控制器,对场景中的具体对象进行操作(其实也是使用GameModels给出的函数进行控制),可以看到这个控制器继承了两个接口类并实现了它们的方法,控制器是场景中各游戏对象行为改变的核心
public class Controllor : MonoBehaviour, ISceneController, IUserAction
{
public LandModel start_land; //开始陆地
public LandModel end_land; //结束陆地
public BoatModel boat; //船
private RoleModel[] roles; //角色
UserGUI user_gui;
void Start ()
{
SSDirector director = SSDirector.GetInstance(); //得到导演实例
director.Curre