コード例 #1
0
        private void StopMoving(bool wonFlag, Racing3DGameModel.GameoverReason stopReason, bool showGameOverUI = true)
        {
            OpponentCar.StopMoving();
            ControllableCar.StopMoving();

            if (showGameOverUI)
            {
                view.ShowGameOverUI(wonFlag, stopReason);
            }
        }
コード例 #2
0
        public void StartGame()
        {
            Application.targetFrameRate = 60;
            var self = GameServices.RealTime.GetSelf();

            selfId = self.ParticipantId;
            var opponent = GameServices.RealTime.GetConnectedParticipants().Where(p => !p.ParticipantId.Equals(selfId)).FirstOrDefault();

            opponentId = opponent.ParticipantId;
            model      = new Racing3DGameModel(selfId, opponentId);
            IsPlaying  = true;

            ClearOldPowerUps();
            ClearSideObjects();

            if (model.IsHost)
            {
                var powerUpsPosition = GenerateRandomPositions();
                InstantiatePowerUps(powerUpsPosition);
                model.PowerUpsPosition = powerUpsPosition;

                var sideObjectsPosition = GenerateRandomSidePositions();
                InstantiateSideObjects(sideObjectsPosition);
                model.SideObjectsPosition = sideObjectsPosition;

                isStartMessageReceived = false;
                shouldSendStartMessage = true;
            }

            ControllableCar.ResetValues();
            ControllableCar.Controllable = true;
            OpponentCar.ResetValues();
            OpponentCar.Controllable = false;

            cameraControl.transform.position = ControllableCar.PrepareCameraPosition;
            view.ShowPrepareText();
            view.ShowInGameUI();
            LoadInfos(self, opponent);
        }
コード例 #3
0
 private void StartMoving()
 {
     ControllableCar.StartMoving(true);
     OpponentCar.StartMoving(false);
     cameraControl.StartFollowing(ControllableCar.gameObject);
 }
コード例 #4
0
        private void OnDataReceived(string senderId, byte[] data)
        {
            var message = model.FromByteArray(data);

            if (message == null)
            {
                Debug.LogError("Failed to parse data!!!");
                return;
            }

            Debug.Log("[OnDataReceived] Type: " + message.Type);

            /// Opponent sent ready message, this also means you're a host...
            if (message.Type == Racing3DGameModel.MessageTypes.Ready)
            {
                isStartMessageReceived = true;

                if (!model.IsHost)
                {
                    return;
                }

                stopwatch.Stop();
                var delay = stopwatch.Elapsed.TotalSeconds;
                view.StartCounting((float)delay + readyMessageDelay, StartMoving);
                Debug.Log("Stopwatch's delay: " + delay);
                stopwatch.Reset();
                return;
            }

            /// Opponent sent start game message, this also means you're a guest...
            if (message.Type == Racing3DGameModel.MessageTypes.StartGame)
            {
                var startMessage = message as Racing3DGameModel.StartGameMessage;

                if (!model.IsHost)
                {
                    InstantiatePowerUps(startMessage.PowerUpsPosition);
                    InstantiateSideObjects(startMessage.SideObjectsPosition);
                    SendReadyMessage();
                }

                view.StartCounting(onFinish: StartMoving);
                return;
            }

            /// Opponent moved...
            if (message.Type == Racing3DGameModel.MessageTypes.Move)
            {
                var moveMessage = message as Racing3DGameModel.MoveMessage;
                OpponentCar.Move(moveMessage.Direction);
                return;
            }

            /// Opponent start using nitro...
            if (message.Type == Racing3DGameModel.MessageTypes.UseNitro)
            {
                OpponentCar.UseNitro();
                return;
            }

            /// Opponent hit an powerUp...
            if (message.Type == Racing3DGameModel.MessageTypes.HitPowerUp)
            {
                OpponentCar.CreateHitPowerUpEffect();
                StopMoving(true, Racing3DGameModel.GameoverReason.OpponentHitPowerUp, IsPlaying);
                IsPlaying = false;
                return;
            }

            /// Opponent has finished the race...
            if (message.Type == Racing3DGameModel.MessageTypes.FinishRace)
            {
                StopMoving(false, Racing3DGameModel.GameoverReason.OpponentFinishRace, IsPlaying);
                IsPlaying = false;
                return;
            }

            /// Opponent request a rematch...
            if (message.Type == Racing3DGameModel.MessageTypes.RematchRequest)
            {
                view.ShowRematchRequestedUI();
                return;
            }

            /// Opponent accepted or denied the rematch...
            if (message.Type == Racing3DGameModel.MessageTypes.RematchResponse)
            {
                var response = message as Racing3DGameModel.RematchResponseMaessage;
                view.ShowRematchResponsedUI(response.Accepted);

                if (response.Accepted)
                {
                    StartGame();
                }

                return;
            }
        }