private void OnDataReceived(string senderId, byte[] data) { var message = model.FromByteArray(data); if (message == null) { Debug.LogError("Failed to parse data!!!"); return; } /// Opponent sent ready message, this also means you're a host... if (message.Type == Racing3DGameModel.MessageTypes.Ready) { 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; } }