Project Training (20) Game Management of Space Meteorite Game


foreword

AsteroidsGameManager: Manage the script of the game, add a timer, after the timer is over, the delegate event is triggered, instantiate your own character, the host generates the game content, and generates the planet. Judging the end of the game (by the number of lives)||All members are loaded in (then the countdown starts), after the game is over, the champion is calculated by judging the score, and the information is recorded through the online socket.


`

1. Loading the scene

Check if all scenes are loaded

 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);//“通知其他人”我加载好了场景
        }

2. Generate planets

  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 side
                    position = 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/bottom
                    position = 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);
            }
        }

After the game starts, after everything is loaded, and after the timer ends, the delegate event triggers here, instantiates your own character, and the host generates the game content

 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.Instantiate

            
            float 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());
            }
        }

3. The game is over and the score is calculated, and the ranking is obtained

 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();
---

Use this method to get the number of lives in the next level

 PhotonNetwork.LocalPlayer.SetCustomProperties(props);//下一关生命

Guess you like

Origin blog.csdn.net/qq_45856546/article/details/125210269
Recommended