项目实训(二十)太空陨石游戏的游戏管理
文章目录
- 前言
- 一、场景的加载
- 二、生成行星
- 三、游戏结束并计算分数,并得出排名
前言
AsteroidsGameManager: 管理游戏的脚本,添加计时器,计时结束后,委托事件触发,实例化自己的角色,主机生成游戏内容,生成行星。判断游戏结束(通过生命数)||全员加载进来(然后开始倒计时),游戏结束后通过判断分数计算冠军,并通过在线socket记录信息。
`
一、场景的加载
检查是否都加载了场景
private bool CheckAllPlayerLoadedLevel()//检查是否都加载了场景{foreach (Player p in PhotonNetwork.PlayerList){object playerLoadedLevel;if (p.CustomProperties.TryGetValue(AsteroidsGame.PLAYER_LOADED_LEVEL, out playerLoadedLevel)){if ((bool) playerLoadedLevel){continue;}}return false;}return true;}
public override void OnEnable()//添加计时委托{base.OnEnable();CountdownTimer.OnCountdownTimerHasExpired += OnCountdownTimerIsExpired;//给委托加一个方法,这个方法会执行这里的“开始游戏”}public void Start()//通知其他人我加载好了{Hashtable props = new Hashtable{{AsteroidsGame.PLAYER_LOADED_LEVEL, true}};PhotonNetwork.LocalPlayer.SetCustomProperties(props);//“通知其他人”我加载好了场景}
二、生成行星
private IEnumerator SpawnAsteroid()//开始生成行星:PhotonNetwork.InstantiateRoomObject("BigAsteroid",{while (true){yield return new WaitForSeconds(Random.Range(AsteroidsGame.ASTEROIDS_MIN_SPAWN_TIME, AsteroidsGame.ASTEROIDS_MAX_SPAWN_TIME));Vector2 direction = Random.insideUnitCircle;Vector3 position = Vector3.zero;if (Mathf.Abs(direction.x) > Mathf.Abs(direction.y)){// Make it appear on the left/right sideposition = new Vector3(Mathf.Sign(direction.x) * Camera.main.orthographicSize * Camera.main.aspect, 0, direction.y * Camera.main.orthographicSize);}else{// Make it appear on the top/bottomposition = new Vector3(direction.x * Camera.main.orthographicSize * Camera.main.aspect, 0, Mathf.Sign(direction.y) * Camera.main.orthographicSize);}// Offset slightly so we are not out of screen at creation time (as it would destroy the asteroid right away)position -= position.normalized * 0.1f;Vector3 force = -position.normalized * 1000.0f;Vector3 torque = Random.insideUnitSphere * Random.Range(500.0f, 1500.0f);object[] instantiationData = {force, torque, true};PhotonNetwork.InstantiateRoomObject("BigAsteroid", position, Quaternion.Euler(Random.value * 360.0f, Random.value * 360.0f, Random.value * 360.0f), 0, instantiationData);}}
游戏开始后,全部加载好后,计时结束后,委托事件触发这里,实例化自己的角色,主机生成游戏内容
private void StartGame()//全部加载好后,计时结束后,委托事件触发这里------实例化自己的角色,主机生成游戏内容{Debug.Log("StartGame!");// on rejoin, we have to figure out if the spaceship exists or not// if this is a rejoin (the ship is already network instantiated and will be setup via event) we don't need to call PN.Instantiatefloat angularStart = (360.0f / PhotonNetwork.CurrentRoom.PlayerCount) * PhotonNetwork.LocalPlayer.GetPlayerNumber();float x = 20.0f * Mathf.Sin(angularStart * Mathf.Deg2Rad);//转弧度制float z = 20.0f * Mathf.Cos(angularStart * Mathf.Deg2Rad);Vector3 position = new Vector3(x, 0.0f, z);Quaternion rotation = Quaternion.Euler(0.0f, angularStart, 0.0f);PhotonNetwork.Instantiate("Spaceship", position, rotation, 0); // avoid this call on rejoin (ship was network instantiated before)if (PhotonNetwork.IsMasterClient){StartCoroutine(SpawnAsteroid());}}
三、游戏结束并计算分数,并得出排名
private IEnumerator EndOfGame(float waitTime){//isGameStart = false;yield return new WaitForSeconds(waitTime);recordPanel.SetActive(true);GameObject winnerGo = null;int currentMaxScore = -9999;Photon.Realtime.Player winner = null;foreach (Photon.Realtime.Player p in PhotonNetwork.PlayerList){GameObject recordEntryGo = Instantiate(recordEntryPrefab, vlg.transform);recordEntryGo.GetComponent<RecordEntry>().InitializeOnlyScore(p.NickName, p.GetScore());recordEntryGo.transform.SetParent(vlg.transform);//计算综合分数决定冠军if (p.GetScore() > currentMaxScore){winnerGo = recordEntryGo;currentMaxScore = p.GetScore();winner = p;}}winnerGo.GetComponent<RecordEntry>().ShowWinnerIcon();
---
用此方法可以得到下一关的生命数
PhotonNetwork.LocalPlayer.SetCustomProperties(props);//下一关生命
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
