日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Unity3D学习:飞碟游戏进化版

發(fā)布時(shí)間:2023/12/3 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Unity3D学习:飞碟游戏进化版 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

上一個(gè)做的飛碟游戲雖然是功能也齊全,但是我感覺架構(gòu)不是很好有點(diǎn)紊亂,不利于后期維護(hù)以及改進(jìn),所以我按照一個(gè)完整的架構(gòu)重新做了一次,思路更清晰,而且還添加了更多的功能。這次的飛碟游戲是兩個(gè)關(guān)卡,50分上第二關(guān),100分通第二關(guān),而且可以任意切換模式(運(yùn)動(dòng)學(xué)非剛體模式和物理剛體模式,前者沒有剛體組件,后者加剛體組件)

講講規(guī)則先:一開始就是第一關(guān),默認(rèn)模式為運(yùn)動(dòng)學(xué)非剛體模式,還是按空格發(fā)射飛碟(一次一個(gè)),一個(gè)發(fā)射期間不能發(fā)射第二個(gè),一個(gè)飛碟如果沒打中會(huì)在一定時(shí)間內(nèi)消失,飛碟消失才能發(fā)射下一個(gè)飛碟,鼠標(biāo)左鍵射擊子彈,按數(shù)字鍵2切換模式為物理剛體模式,按數(shù)字鍵1切換到運(yùn)動(dòng)學(xué)非剛體模式。

上個(gè)效果圖(由于比較難射中,所以我把飛碟大小弄大一些,分?jǐn)?shù)顯示被遮住了):


接下來講一下設(shè)計(jì)思路,游戲架構(gòu)如下


DiskModule類掛在飛碟預(yù)設(shè)上,用來處理觸發(fā)器事件(飛碟碰到子彈或者地板時(shí)觸發(fā))

DiskFactory類專門生產(chǎn)飛碟,所有生產(chǎn)飛碟的細(xì)節(jié)都在這里實(shí)現(xiàn),會(huì)根據(jù)每個(gè)關(guān)卡生產(chǎn)不同大小以及顏色的飛碟,并且回收飛碟以便再次加以利用

Recorder類只負(fù)責(zé)記錄分?jǐn)?shù)或者重置分?jǐn)?shù)

Userface類處理用戶界面邏輯

CCActionManager類實(shí)現(xiàn)運(yùn)動(dòng)學(xué)的動(dòng)作,比如運(yùn)動(dòng)學(xué)非剛體的飛碟發(fā)射

PhysicsActionManager類實(shí)現(xiàn)物理剛體學(xué)動(dòng)作,比如子彈的發(fā)射以及剛體飛碟的發(fā)射

SceneController類加載場(chǎng)景以及調(diào)用各個(gè)其他類來實(shí)現(xiàn)功能,比如Userface調(diào)用子彈或者飛碟射擊要通過SceneController根據(jù)不同模式以及關(guān)卡來調(diào)用相應(yīng)的動(dòng)作管理器

具體代碼如下,(注釋已經(jīng)把代碼思想寫的很清楚):

using System.Collections; using System.Collections.Generic; using UnityEngine;public class Director : System.Object {private static Director _instance;public SceneController _Controller { get; set; }public static Director getinstance(){if (_instance == null){_instance = new Director();}return _instance;}public int getFPS(){return Application.targetFrameRate;}public void setFPS(int fps){Application.targetFrameRate = fps;} }
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Userface : MonoBehaviour {private SceneController _Controller;public Camera _camera;public Text Score;//顯示分?jǐn)?shù)public Text Round;//顯示關(guān)卡public Text Mode;//顯示模式void Start () {_Controller = (SceneController)FindObjectOfType(typeof(SceneController));}// Update is called once per framevoid Update () {if(Input.GetKeyDown("1"))//按下1切換到運(yùn)動(dòng)學(xué)模式{if(!_Controller.isFlying){Debug.Log("NonPhy mode");_Controller._mode = false;}}if(Input.GetKeyDown("2"))//按下2切換到物理剛體模式{if (!_Controller.isFlying){Debug.Log("Phy mode");_Controller._mode = true;}}if(Input.GetKeyDown("space"))//按下空格發(fā)射飛碟{_Controller.ShootDisk();}if (Input.GetMouseButtonDown(0)&&!_Controller.isShooting)//按下左鍵發(fā)射子彈{Ray mouseRay = _camera.ScreenPointToRay(Input.mousePosition);_Controller.ShootBullet(mouseRay.direction);}Score.text = "Score:" + _Controller.getRecorder().getScore();Round.text = "Round:" + _Controller.getRound();if (_Controller._mode == false) Mode.text = "Mode:CCAction";else Mode.text = "Mode:PhysicsAction";} }
using System.Collections; using System.Collections.Generic; using UnityEngine;public class Recorder : MonoBehaviour {private int _score = 0;public void AddScore(string name)//根據(jù)碰撞物名稱加減分{if (name == "Plane") _score -= 10;else if (name == "bullet(Clone)") _score += 10;}public int getScore(){return _score;}public void reset(){_score = 0;} }
using System.Collections; using System.Collections.Generic; using UnityEngine;public class DiskFactory : MonoBehaviour {public List<GameObject> Using; //儲(chǔ)存正在使用的public List<GameObject> Used; //儲(chǔ)存空閑的 private SceneController _Controller;public GameObject DiskPrefab;void Start () {Using = new List<GameObject>();Used = new List<GameObject>();_Controller = (SceneController)FindObjectOfType(typeof(SceneController));}public GameObject getDisk(bool isPhy,int _Round){GameObject t;if (Used.Count == 0){t = GameObject.Instantiate(DiskPrefab) as GameObject;}else{t = Used[0];Used.Remove(t);}t.GetComponent<MeshCollider>().isTrigger = true;t.SetActive(true);if (isPhy)//物理學(xué)模式加剛體組件{if (t.GetComponent<Rigidbody>() == null)t.AddComponent<Rigidbody>();}else if (!isPhy)//運(yùn)動(dòng)學(xué)模式去除剛體組件{if (t.GetComponent<Rigidbody>())Destroy(t.GetComponent<Rigidbody>());}Using.Add(t);if(_Round==1)//第一關(guān)的飛碟形式{t.transform.localScale *= 2;t.GetComponent<Renderer>().material.color = Color.green;}//第二關(guān)為初始大小以及紅色return t;}private void freeDisk(int round)//把場(chǎng)景中inactive的飛碟回收{(diào)for (int i = 0; i < Using.Count; i++){GameObject t = Using[i];if (!t.activeInHierarchy){Using.RemoveAt(i);Used.Add(t);Destroy(t.GetComponent<Rigidbody>());if (round==1)t.transform.localScale /= 2;t.GetComponent<Renderer>().material.color = Color.red;}}}void Update () {freeDisk(_Controller.getRound());} }
using System.Collections; using System.Collections.Generic; using UnityEngine;public class DiskModule : MonoBehaviour {private string _sth = "";private float _time = 8f;private SceneController _Controller;void Start () {_Controller = (SceneController)FindObjectOfType(typeof(SceneController));}void OnTriggerEnter(Collider other)//觸發(fā)器事件{if(_sth==""){_sth = other.gameObject.name;Debug.Log(_sth);if (_sth == "bullet(Clone)"){this._time = 0;//直接回收_Controller._explosion.GetComponent<Renderer>().material.color = this.GetComponent<Renderer>().material.color;_Controller._explosion.transform.position = this.transform.position;_Controller._explosion.Play();//播放爆炸粒子}_Controller.getRecorder().AddScore(_sth);_Controller.isShooting = false;}}void Update () {if (_Controller.isFlying){if (_time > 0) _time -= Time.deltaTime;else if (_time <= 0)//回收飛碟{GetComponent<MeshCollider>().isTrigger = false;this.gameObject.SetActive(false);_time = 8f;_sth = "";_Controller.isFlying = false;}}} }
using System.Collections; using System.Collections.Generic; using UnityEngine;public class SceneController : MonoBehaviour {private CCActionManager _CCAM;private PhysicsActionManager _PhyAM;private DiskFactory _Factory;private Recorder _Recorder;private int _Round = 1;public GameObject _bullet;//子彈public ParticleSystem _explosion;//爆炸粒子public bool _mode = false;//標(biāo)記模式public bool isShooting = false;//判斷子彈是否正在飛public bool isFlying = false;//判斷飛碟是否正在飛private float _time = 1f;void Start () {_bullet = GameObject.Instantiate(_bullet) as GameObject;_explosion = GameObject.Instantiate(_explosion) as ParticleSystem;Director _director = Director.getinstance();_director._Controller = this;_CCAM = (CCActionManager)FindObjectOfType(typeof(CCActionManager));_PhyAM = (PhysicsActionManager)FindObjectOfType(typeof(PhysicsActionManager));_Recorder = (Recorder)FindObjectOfType(typeof(Recorder));_Factory = (DiskFactory)FindObjectOfType(typeof(DiskFactory));_director.setFPS(60);_director._Controller = this;}public DiskFactory getFactory(){return _Factory;}public int getRound(){return _Round;}public Recorder getRecorder(){return _Recorder;}public CCActionManager getCCActionManager(){return _CCAM;}public PhysicsActionManager getPhysicsActionManager(){return _PhyAM;}public void ShootDisk(){if(!isFlying){GameObject _disk = _Factory.getDisk(_mode, _Round);//根據(jù)不同關(guān)卡以及不同模式生產(chǎn)不同的飛碟if (!_mode) _CCAM.ShootDisks(_disk);//根據(jù)不同模式調(diào)用不同動(dòng)作管理器的動(dòng)作函數(shù)else _PhyAM.ShootDisks(_disk);isFlying = true;}}public void ShootBullet(Vector3 _dir)//調(diào)用動(dòng)作管理器的射擊子彈函數(shù){isShooting = true;_PhyAM.ShootBullets(_bullet, _dir);}private void AddRound()//判斷是否通關(guān){if (_Recorder.getScore() >= 50&&_Round == 1){_Round++;}else if (_Recorder.getScore() > 100&& _Round == 2){_Round = 1;_Recorder.reset();}}void Update () {AddRound();if(isShooting){if(_time>0){_time -= Time.deltaTime;if(_time<=0){isShooting = false;_time = 1f;}}}} }
using System.Collections; using System.Collections.Generic; using UnityEngine;public class PhysicsActionManager : MonoBehaviour {private SceneController _Controller;void Start () {_Controller = (SceneController)FindObjectOfType(typeof(SceneController));}public void ShootBullets(GameObject _bullet, Vector3 _dir)//發(fā)射子彈{_bullet.transform.position = new Vector3(0, 2, 0);_bullet.GetComponent<Rigidbody>().velocity = Vector3.zero; // 子彈剛體速度重置_bullet.GetComponent<Rigidbody>().AddForce(_dir * 500f, ForceMode.Impulse);}public void ShootDisks(GameObject _disk)//發(fā)射飛碟{_disk.transform.position = new Vector3(0, 2, 2);float _dx = Random.Range(-20f, 20f);Vector3 _dir = new Vector3(_dx, 30, 20);_disk.transform.up = _dir;if (_Controller.getRound() == 1){_disk.GetComponent<Rigidbody>().AddForce(_dir*20f, ForceMode.Force);} else if(_Controller.getRound() == 2){_disk.GetComponent<Rigidbody>().AddForce(_dir*30f, ForceMode.Force);}}void Update () {} }
using System.Collections; using System.Collections.Generic; using UnityEngine;public class CCActionManager : MonoBehaviour {private SceneController _Controller;private GameObject temp;void Start () {_Controller = (SceneController)FindObjectOfType(typeof(SceneController));}public void ShootDisks(GameObject _disk)//發(fā)射飛碟{_disk.transform.position = new Vector3(Random.Range(-1f, 1f), 2f, 2f);temp = _disk;}void Update () {if(_Controller.isFlying&&!_Controller._mode){if (_Controller.getRound() == 1){temp.transform.position = Vector3.MoveTowards(temp.transform.position, new Vector3(0, 2, 100), 0.1f);}else if (_Controller.getRound() == 2){temp.transform.position = Vector3.MoveTowards(temp.transform.position, new Vector3(0, 2, 100), 0.2f);}}} }


總結(jié)

以上是生活随笔為你收集整理的Unity3D学习:飞碟游戏进化版的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。