HW5-打飞碟
一、游戲說明
飛碟游戲
游戲內(nèi)容:
游戲涉及的知識點(diǎn):
二、相關(guān)知識
1.為什么需要工廠對象
- 游戲?qū)ο蟮膭?chuàng)建與銷毀高成本,必須減少銷毀次數(shù)。如游戲中子彈
- 屏蔽創(chuàng)建與銷毀的業(yè)務(wù)邏輯,使程序易于擴(kuò)展
2.diskFactory的設(shè)計
- 前面游戲,由導(dǎo)演、場記、運(yùn)動管理師、演員構(gòu)成。
- 新游戲中,場記請了記分員、飛碟管理員
- 其中記分員按飛碟的數(shù)據(jù)計分,記分員擁有計分規(guī)則
- 場記只需要管理出飛碟規(guī)則與管理碰撞就可以了
3.帶緩存工廠模式的實現(xiàn)
- getDisk(ruler) 偽代碼
IF (free list has disk) THEN
a_disk = remove one from list ELSE
a_disk = clone from Prefabs
ENDIF
Set DiskData of a_disk with the ruler Add a_disk to used list
Return a_disk - FreeDisk(disk)
Find disk in used list
IF (not found) THEN THROW exception Move disk from used to free list
三、關(guān)鍵實現(xiàn)分析
1.首先是飛碟信息的存儲類
掛在飛碟上制作為預(yù)制
2.飛碟工廠類
主要有的功能為:飛碟預(yù)制的生產(chǎn)、飛碟的選擇、未使用飛碟的回收
生產(chǎn):
//在空閑的飛碟列表中嘗試找到符合tag的飛碟for(int i=0;i<free.Count;i++){if(free[i].tag == tag){disk_prefab = free[i].gameObject;free.Remove(free[i]);break;}} //如果空閑列表中沒有所需求的預(yù)制飛碟,則需要生產(chǎn)預(yù)制飛碟if(disk_prefab == null){if (tag == "disk1"){disk_prefab = Instantiate(Resources.Load<GameObject>("Prefabs/disk1"), new Vector3(0, start_y, 0), Quaternion.identity);}else if (tag == "disk2"){disk_prefab = Instantiate(Resources.Load<GameObject>("Prefabs/disk2"), new Vector3(0, start_y, 0), Quaternion.identity);}else{disk_prefab = Instantiate(Resources.Load<GameObject>("Prefabs/disk3"), new Vector3(0, start_y, 0), Quaternion.identity);} //設(shè)定顏色、位置等屬性float ran_x = Random.Range(-1f, 1f) < 0 ? -1 : 1;disk_prefab.GetComponent<Renderer>().material.color = disk_prefab.GetComponent<DiskData>().color;disk_prefab.GetComponent<DiskData>().direction = new Vector3(ran_x, start_y, 0);disk_prefab.transform.localScale = disk_prefab.GetComponent<DiskData>().scale;} //添加到使用的列表中used.Add(disk_prefab.GetComponent<DiskData>());飛碟的選擇:
不同的回合對應(yīng)不同的飛碟
飛碟回收:
未被擊中的飛碟,將其回收至空閑列表中,并將其移出正在使用的飛碟列表
3.場景控制FirstSceneControl類
負(fù)責(zé)游戲場景中的各種事件的處理,以及游戲規(guī)則如游戲開始結(jié)束等的執(zhí)行。
發(fā)送飛碟的處理:
擊中飛碟的相關(guān)判斷:
public void Hit(Vector3 pos){Ray ray = Camera.main.ScreenPointToRay(pos);RaycastHit[] hits;hits = Physics.RaycastAll(ray);bool not_hit = false;for (int i = 0; i < hits.Length; i++){RaycastHit hit = hits[i];if (hit.collider.gameObject.GetComponent<DiskData>() != null){for (int j = 0; j < disk_notshot.Count; j++){if (hit.collider.gameObject.GetInstanceID() == disk_notshot[j].gameObject.GetInstanceID()){not_hit = true;}}if(!not_hit){return;}disk_notshot.Remove(hit.collider.gameObject);score_recorder.Record(hit.collider.gameObject);Transform explode = hit.collider.gameObject.transform.GetChild(0);explode.GetComponent<ParticleSystem>().Play();StartCoroutine(WaitingParticle(0.08f, hit, disk_factory, hit.collider.gameObject));break;}}}回收飛碟
IEnumerator WaitingParticle(float wait_time, RaycastHit hit, DiskFactory disk_factory, GameObject obj){yield return new WaitForSeconds(wait_time);hit.collider.gameObject.transform.position = new Vector3(0, -9, 0);disk_factory.FreeDisk(obj);}4.飛碟飛行動作
public static diskFlyAction GetSSAction(Vector3 direction, float angle, float power){diskFlyAction action = CreateInstance<diskFlyAction>();if (direction.x == -1){action.start_vector = Quaternion.Euler(new Vector3(0, 0, -angle)) * Vector3.left * power;}else{action.start_vector = Quaternion.Euler(new Vector3(0, 0, angle)) * Vector3.right * power;}return action;}5.GUI界面渲染
void OnGUI (){bold_style.normal.textColor = new Color(1, 0, 0);bold_style.fontSize = 16;text_style.normal.textColor = new Color(0,0,0, 1);text_style.fontSize = 16;score_style.normal.textColor = new Color(1,0,1,1);score_style.fontSize = 16;over_style.normal.textColor = new Color(1, 0, 0);over_style.fontSize = 25;if (game_start){if (Input.GetButtonDown("Fire1")){Vector3 pos = Input.mousePosition;action.Hit(pos);}GUI.Label(new Rect(10, 5, 200, 50), "分?jǐn)?shù):", text_style);GUI.Label(new Rect(55, 5, 200, 50), action.GetScore().ToString(), score_style);GUI.Label(new Rect(Screen.width - 120, 5, 50, 50), "血量:", text_style);//顯示血量for (int i = 0; i < life; i++){GUI.Label(new Rect(Screen.width - 75 + 10 * i, 5, 50, 50), "+", bold_style);}if (life == 0){high_score = high_score > action.GetScore() ? high_score : action.GetScore();GUI.Label(new Rect(Screen.width / 2 - 20, Screen.width / 2 - 250, 100, 100), "游戲結(jié)束", over_style);GUI.Label(new Rect(Screen.width / 2 - 10, Screen.width / 2 - 200, 50, 50), "你獲得的分?jǐn)?shù):", text_style);GUI.Label(new Rect(Screen.width / 2 + 50, Screen.width / 2 - 200, 50, 50), high_score.ToString(), text_style);if (GUI.Button(new Rect(Screen.width / 2 - 20, Screen.width / 2 - 150, 100, 50), "重新開始")){life = 6;action.ReStart();return;}action.GameOver();}}else{if (GUI.Button(new Rect(Screen.width / 2 - 20, Screen.width / 2-290, 100, 50), "開始游戲")){game_start = true;action.BeginGame();}}}四、成品
視頻
最后感謝師兄的博客給了很大的幫助!不然真的很難實現(xiàn)。
總結(jié)
- 上一篇: unity3d实现简单的打飞碟游戏
- 下一篇: Unity3D学习:飞碟游戏进化版