unity3d-射击飞碟小游戏

unity3d-射击飞碟小游戏

通过简单工厂和射线完成一个简单的设计飞碟小游戏

游戏要求

  • 写个用鼠标打飞碟的游戏。
  • 游戏要分多个 round , 飞碟数量每个 round 都是 n 个,但色彩,大小;发射位置,速度,角度,每次发射数量可以变化。
  • 游戏过程中,仅能创建 n 个飞碟, 且不容许初始化阶段生成任何飞碟。 飞碟线路计算请使用 mathf 类。 向下加速度 a 是常数。 飞碟被用户击中,则回收。并按你定义的规则计算分数。飞碟落地(y < c),则自动回收。
  • 请画 UML 图,描述主要对象及其关系。

说实话,不是很会画uml图,所以暂时先保留

游戏实现

这次的游戏可以在地面攻击目标的基础上进行开发,通过之前写好的简单工厂来回收和管理飞碟。至于游戏的分局主要就是通过添加一个UserInterface的脚本来进行选择,以及显示时间和分数。

简单工厂—管理飞碟

关于飞碟的生成和回收管理,我们一样主要通过简单工厂里的三个函数来进行管理。placeTarget完成在一个位置生成飞碟对象,内部通过调用GenGameObject的类来进行生成调度对象,ApplyPlaceTarget是在拿到游戏对象之后对其进行的具体操作。也是通过一个placeTargetAction的类来控制其动作的。hideMark就是将回收后的物体放回指定位置的函数。这就是在放置目标的脚本的基础上进行修改的。

此外,我还添加了score来记录分数,round来记录局数,state来记录游戏的状态。以及设置了相应的函数hitTarget()在击中飞碟对象之后分数+1,missTarget()未击中飞碟飞碟落地之后分数-1。以及相应的函数来获取这些值。

在AttackAction.cs中

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
namespace Com.Mygame {
public enum State { OVER, PLAYING };
public class AttackManager : System.Object {
private static AttackManager _instance;
private GenGameObject _gen_game_obj;
private int score;
private int round;
public State state;
void Start () {
score = 0;
}
public void hitTarget() {
score += 1;
}
public void missTarget() {
score -= 1;
}
public int getScore () {
return score;
}
public void setRound (int r) {
round = r;
}
public static AttackManager GetInstance(){
if (_instance == null) {
_instance = new AttackManager();
}
return _instance;
}
public GenGameObject getGenGameObject() {
return _gen_game_obj;
}
internal void setGenGameObject(GenGameObject ggo) {
if (_gen_game_obj == null) {
_gen_game_obj = ggo;
}
}
public GameObject placeTarget (Vector3 position){
GameObject target = _gen_game_obj.genTarget (position);
if (target == null)
return null;
return ApplyPlaceTarget(target, position);
}
public GameObject ApplyPlaceTarget (GameObject target, Vector3 position) {
if (target != null) {
placeTargetAction ac = target.AddComponent<placeTargetAction> ();
ac.setting (target, position);
ac.setRound (round);
forceManager fc = target.AddComponent<forceManager> ();
fc.setting (target);
}
return target;
}
public void hideMark (GameObject target) {
if (target != null) {
Debug.Log ("hide");
target.transform.position = new Vector3 (0, 40, 0);
placeTargetAction ac = target.GetComponent<placeTargetAction> ();
ac.setRound (round);
forceManager fc = target.AddComponent<forceManager> ();
fc.setting (target);
if (_gen_game_obj.getUsed ().Count > 0) {
_gen_game_obj.getUnused ().Enqueue (_gen_game_obj.getUsed ().Dequeue ());
}
}
}
}
public class placeTargetAction : MonoBehaviour {
private Vector3 position;
private GameObject target;
private int round;
public void setting (GameObject target, Vector3 position){
this.position = position;
this.target = target;
}
public void setRound(int round) {
this.round = round;
}
void Start () {
transform.position = position;
}
void Update () {
if (round == 1) {
target.GetComponent<Renderer> ().material.color = Color.red;
} else if (round == 2) {
target.GetComponent<Renderer> ().material.color = Color.blue;
} else if (round == 3) {
target.GetComponent<Renderer> ().material.color = Color.yellow;
}
}
}
}

创建调度飞碟对象

在简单工厂中所需要的游戏的对象,都是直接通过向GenGameObject这个类里面的方法来直接拿的。实现的主要方法也与放置目标的类似,当游戏对象不够而用户申请的时候就生成,之后就只在使用和未使用的游戏对象中进行通过队列进行调度,不再进一步生成或摧毁对象。

设置飞碟数目

在这里我设置飞碟数目num最多为10个并且在生成的时候会用if(used.Count + unused.Count < num)决策进行检测,设置为public在unity3d的视窗中也可以自己进行设置。

设置预设

这里我就使用一个圆筒来代替飞碟,通过材质球来添加颜色,不添加也行因为在关卡部分也会进行设置。

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
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Com.Mygame;
public class GenGameObject : MonoBehaviour {
public GameObject prefab;
public int num = 10; // target number
Queue<GameObject> unused = new Queue<GameObject>();
Queue<GameObject> used = new Queue<GameObject>();
// Use this for initialization
void Start () {
AttackManager.GetInstance ().setGenGameObject (this);
}
public GameObject genTarget(Vector3 position) {
GameObject target;
if (used.Count + unused.Count < num) {
if (unused.Count == 0) {
target = Instantiate (prefab, position, Quaternion.identity) as GameObject;
used.Enqueue (target);
} else {
target = unused.Dequeue ();
}
} else {
target = null;
}
return target;
}
public Queue<GameObject> getUnused() {
return unused;
}
public Queue<GameObject> getUsed() {
return used;
}
// Update is called once per frame
void Update () {
}
}

用户交互

主要添加一个UserInterface.cs来进行对游戏关卡的设置还有分数和时间的显示。分数的计算,就直接在简单工厂中进行会来的比较方便,这里只需要获得工厂对象再将它的分数显示出来就可以了。以及交互对局数和状态的设置。

界面上就可以看到这种效果。

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
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Com.Mygame;
public class UserInterface : MonoBehaviour {
private float time;
AttackManager my;
// Use this for initialization..
void Start () {
my = AttackManager.GetInstance();
my.setRound(1);
time = 30;
my.state = State.OVER;
}
// Update is called once per frame
void Update () {
if (time > 0) {
time -= Time.deltaTime;
} else {
my.state = State.OVER;
}
}
void OnGUI() {
if (GUILayout.Button("level1")) {
my.setRound(1);
my.state = State.PLAYING;
}
if (GUILayout.Button("level2")) {
my.setRound(2);
my.state = State.PLAYING;
}
if (GUILayout.Button("level3")) {
my.setRound(3);
my.state = State.PLAYING;
}
GUILayout.Label ("Score: " + my.getScore());
GUILayout.Label ("time: " + Mathf.Ceil(time));
}
}

Ray射线—打飞碟

完成以上的步骤之后,我们已经可以调度好飞碟对象,并且有用户交互的界面了。现在最重要的就是需要一个方法把飞碟打下来。

在我的理解中,打飞碟的方法应该有两种,一种是用子弹作为碰撞体来碰撞飞碟触发条件。一种就是直接用射线来完成触发条件。由于暂时还未加入对子弹,所以我暂时使用Ray射线来碰撞飞碟。通过简单修改放置攻击目标的代码就可以完成了。

这里将预设的标签设置为target,就只有在射线碰撞到有这个标签的游戏对象之后才会触发隐藏游戏对象的条件还有得分方法。

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
public class AttackAction : MonoBehaviour {
private GameObject cam;
private AttackManager myFactory;
// Use this for initialization
void Start () {
cam = GameObject.Find ("Main Camera");
myFactory = AttackManager.GetInstance ();
}
void Update () {
myFactory.placeTarget (new Vector3(0, 40, 0));
if (Input.GetMouseButtonDown (0)) {
//create ray, origin is camera, and direction to mousepoint
Ray ray = cam.GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
//Return the ray's hit
RaycastHit hit;
if (Physics.Raycast (ray, out hit)) {
print (hit.transform.gameObject.name);
print (hit.point);
if (hit.collider.gameObject.tag.Contains ("target")) {
myFactory.hideMark (hit.collider.gameObject);
myFactory.hitTarget ();
print ("hit");
}
}
}
}
}

碰撞体—回收飞碟

除了通过ray打掉的飞碟之后,还会有一部分的飞碟是掉落到地板上的。这时候我们就需要把飞碟的预设设置为碰撞体,地板也设置为碰撞体,并且会触发碰撞条件。在飞碟碰撞到地板之后触发条件,进行回收和扣分。

ufo

plane

这个时候我们可以在plane上挂在一段新的代码用作触发函数。other就是与地面碰撞的地板。或者直接将这段脚本加入AttackAction中。

1
2
3
4
5
void OnTriggerEnter(Collider other){
Destroy (other.GetComponent<forceManager> ());
myFactory.hideMark (other.gameObject);
myFactory.missTarget ();
}

刚体—飞碟受力

要使对象受物理引擎控制,简单的办法就是给它加个 Rigidbody ,对象就会受到重力影响,而且会和世界中的其他对象碰撞。

所以为了完成飞碟的碰撞和掉落以及向不同方向飞,我们还需要给他加上刚体部件才能够顺利受力力完成碰撞和掉落。

在Com.Mygame中加入,forceManager来管理受力,使每次发射出去都受到不同方向的力就能使飞碟不同方向地飞。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class forceManager : MonoBehaviour {
private GameObject target;
private Rigidbody rb;
private int force;
public void setting (GameObject target) {
this.target = target;
}
void Start () {
rb = target.GetComponent<Rigidbody> ();
force = Random.Range (-5, 5);
}
void Update () {
rb.AddForce (new Vector3 (force, 0), ForceMode.Force);
}
}

在上面的OnTriggerEnter函数中,触发之后就会删除这个部件,下一次重新调度之后 ApplyPlaceTarget中来又重新添加上。这样子就可以保证上以不同的方向出发。

游戏效果

三种颜色代表了不同的关卡,整个游戏过程中只生成了10个游戏对象。当然界面有一点丑,整体脚本已经写好。可以进一步地进行优化。

脚本

AttackAction.cs

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Com.Mygame;
namespace Com.Mygame {
public enum State { OVER, PLAYING };
public class AttackManager : System.Object {
private static AttackManager _instance;
private GenGameObject _gen_game_obj;
private int score;
private int round;
public State state;
void Start () {
score = 0;
}
public void hitTarget() {
score += 1;
}
public void missTarget() {
score -= 1;
}
public int getScore () {
return score;
}
public void setRound (int r) {
round = r;
}
public static AttackManager GetInstance(){
if (_instance == null) {
_instance = new AttackManager();
}
return _instance;
}
public GenGameObject getGenGameObject() {
return _gen_game_obj;
}
internal void setGenGameObject(GenGameObject ggo) {
if (_gen_game_obj == null) {
_gen_game_obj = ggo;
}
}
public GameObject placeTarget (Vector3 position){
GameObject target = _gen_game_obj.genTarget (position);
if (target == null)
return null;
return ApplyPlaceTarget(target, position);
}
public GameObject ApplyPlaceTarget (GameObject target, Vector3 position) {
if (target != null) {
placeTargetAction ac = target.AddComponent<placeTargetAction> ();
ac.setting (target, position);
ac.setRound (round);
forceManager fc = target.AddComponent<forceManager> ();
fc.setting (target);
}
return target;
}
public void hideMark (GameObject target) {
if (target != null) {
Debug.Log ("hide");
target.transform.position = new Vector3 (0, 40, 0);
placeTargetAction ac = target.GetComponent<placeTargetAction> ();
ac.setRound (round);
forceManager fc = target.AddComponent<forceManager> ();
fc.setting (target);
if (_gen_game_obj.getUsed ().Count > 0) {
_gen_game_obj.getUnused ().Enqueue (_gen_game_obj.getUsed ().Dequeue ());
}
}
}
}
public class placeTargetAction : MonoBehaviour {
private Vector3 position;
private GameObject target;
private int round;
public void setting (GameObject target, Vector3 position){
this.position = position;
this.target = target;
}
public void setRound(int round) {
this.round = round;
}
void Start () {
transform.position = position;
}
void Update () {
if (round == 1) {
target.GetComponent<Renderer> ().material.color = Color.red;
} else if (round == 2) {
target.GetComponent<Renderer> ().material.color = Color.blue;
} else if (round == 3) {
target.GetComponent<Renderer> ().material.color = Color.yellow;
}
}
}
public class forceManager : MonoBehaviour {
private GameObject target;
private Rigidbody rb;
private int force;
public void setting (GameObject target) {
this.target = target;
}
void Start () {
rb = target.GetComponent<Rigidbody> ();
force = Random.Range (-5, 5);
}
void Update () {
rb.AddForce (new Vector3 (force, 0), ForceMode.Force);
}
}
}
public class AttackAction : MonoBehaviour {
private GameObject cam;
private AttackManager myFactory;
// Use this for initialization
void Start () {
cam = GameObject.Find ("Main Camera");
myFactory = AttackManager.GetInstance ();
}
void Update () {
myFactory.placeTarget (new Vector3(0, 40, 0));
if (Input.GetMouseButtonDown (0)) {
//create ray, origin is camera, and direction to mousepoint
Ray ray = cam.GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
//Return the ray's hit
RaycastHit hit;
if (Physics.Raycast (ray, out hit)) {
print (hit.transform.gameObject.name);
print (hit.point);
if (hit.collider.gameObject.tag.Contains ("target")) {
myFactory.hideMark (hit.collider.gameObject);
myFactory.hitTarget ();
print ("hit");
}
}
}
}
void OnTriggerEnter(Collider other){
Destroy (other.GetComponent<forceManager> ());
myFactory.hideMark (other.gameObject);
myFactory.missTarget ();
}
}

GenGameObject.cs

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
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Com.Mygame;
public class GenGameObject : MonoBehaviour {
public GameObject prefab;
public int num = 10; // target number
Queue<GameObject> unused = new Queue<GameObject>();
Queue<GameObject> used = new Queue<GameObject>();
// Use this for initialization
void Start () {
AttackManager.GetInstance ().setGenGameObject (this);
}
public GameObject genTarget(Vector3 position) {
GameObject target;
if (used.Count + unused.Count < num) {
if (unused.Count == 0) {
target = Instantiate (prefab, position, Quaternion.identity) as GameObject;
used.Enqueue (target);
} else {
target = unused.Dequeue ();
}
} else {
target = null;
}
return target;
}
public Queue<GameObject> getUnused() {
return unused;
}
public Queue<GameObject> getUsed() {
return used;
}
// Update is called once per frame
void Update () {
}
}

UserInterface.cs

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
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Com.Mygame;
public class UserInterface : MonoBehaviour {
private float time;
AttackManager my;
// Use this for initialization..
void Start () {
my = AttackManager.GetInstance();
my.setRound(1);
time = 30;
my.state = State.OVER;
}
// Update is called once per frame
void Update () {
if (time > 0) {
time -= Time.deltaTime;
} else {
my.state = State.OVER;
}
}
void OnGUI() {
if (GUILayout.Button("level1")) {
my.setRound(1);
my.state = State.PLAYING;
}
if (GUILayout.Button("level2")) {
my.setRound(2);
my.state = State.PLAYING;
}
if (GUILayout.Button("level3")) {
my.setRound(3);
my.state = State.PLAYING;
}
GUILayout.Label ("Score: " + my.getScore());
GUILayout.Label ("time: " + Mathf.Ceil(time));
}
}
您的支持将鼓励我继续创作!