Unity-牧师与魔鬼
2020-12-13 16:10
标签:fir instance this oss sse net message static 变量 Priests and Devils 个人github 阅读以下游戏脚本 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! play the game 程序要求: Priests, devils, stone, river, boat 实现思路: Model: Coast, Boat, Character View: UserGUI Controller: BoatController, CoastController, CharacterController, FirstController, Director 给定 Model 属性和值域 枚举变量是很好的状态标记辅助变量,可以避免记忆整数标识的状态,而采用易懂的变量名替代。 View 实现 实现各 Controller 和 Director Director 采取单例模式 Controller 代码见 github。 其中,需要注意,上下船动作虽然简单,但也应该分解为,人物上/下船,船上/下人物,人物上/下岸,岸上/下人物。因为这个动作的结果会对三个 GameObject 造成影响,所以我们要通知三个 GameObject 让其进行状态更新。 这一点尤其要注意。 GameObject 运动脚本 实现参考博客 原文:大专栏 Unity-牧师与魔鬼 Unity-牧师与魔鬼 标签:fir instance this oss sse net message static 变量 原文地址:https://www.cnblogs.com/wangziqiang123/p/11618283.html
|动作|结果|
|——-|—————|
|点击人物|上/下船, 上/下岸|
|点击船只|过河|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64public enum Location { left, right }
public class {
public Moveable mScript;
public GameObject Role { get; set; }
public CoastController Coast { get; set; }
public bool IsOnBoat { get; set; }
public string Name {
get {
return Role.name;
}
set {
Role.name = value;
}
}
public (string _name) {
if (_name.Contains("priest")) {
// Instantiate priest
} else {
// Instantiate devil
}
}
}
public class Coast {
readonly GameObject coast;
readonly Vector3 departure;
public readonly Vector3 destination;
public readonly Vector3[] positions;
public CharacterController[] characters;
public Location Location { get; set; }
public Coast(string _location) {
if (_location == "right") {
// Instantiate right coast
coast.name = "departure";
Location = Location.right;
} else {
// Instantiate left coast
coast.name = "destination";
Location = Location.left;
}
}
}
public class Boat {
public readonly Moveable mScript;
public readonly Vector3 departure;
public readonly Vector3 destination;
public readonly Vector3[] departures;
public readonly Vector3[] destinations;
public CharacterController[] passenger = new CharacterController[2];
public GameObject _Boat { get; set; }
public Location Location { get; set; }
public Boat() {
// Instantiate boat gameobject
}
}
见 github 代码项目的 prehab 文件夹
UserGUI 代码见 github。
1
2
3
4
5
6
7
8
9
10
11
12public class Director : System.Object {
private static Director _instance;
public ISceneController CurrentSceneController { get; set; }
public static Director GetInstance() {
// variable ?? (some code)
// 这是个语法糖,表示判断前面的 variable 是否为空
// 不为空则返回 variable
// 为空则执行括号内的 some code
return _instance ?? (_instance = new Director());
}
}
上一篇:闲话js作用域