プロジェクト研修(20) 宇宙隕石ゲームのゲーム運営


序文

AsteroidsGameManager: ゲームのスクリプトを管理し、タイマーを追加します。タイマーが経過すると、デリゲート イベントがトリガーされ、独自のキャラクターをインスタンス化し、ホストがゲーム コンテンツを生成し、惑星を生成します。ゲームの終了判定(ライフ数による)||全員がロードされ(カウントダウンが開始)、ゲーム終了後、スコアを判定してチャンピオンが計算され、その情報がオンラインを通じて記録されます。ソケット。


`

1. シーンのロード

すべてのシーンがロードされているかどうかを確認する

 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. 惑星を生成する

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

ゲームが開始され、すべてがロードされ、タイマーが終了した後、ここでデリゲート イベントがトリガーされ、独自のキャラクターがインスタンス化され、ホストがゲーム コンテンツを生成します。

 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. ゲームが終了し、スコアが計算され、ランキングが取得されます

 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);//下一关生命

おすすめ

転載: blog.csdn.net/qq_45856546/article/details/125210269