Example #1
0
        private void OnPlayerAdded(Player player)
        {
            if (!player.IslocalPlayer)
            {
                return;
            }

            if (_localPlayer != null)
            {
                _localPlayer.Moved -= OnLocalPlayerMoved;
            }

            _localPlayer        = player;
            _localPlayer.Moved += OnLocalPlayerMoved;
        }
Example #2
0
        private async void StartGame(bool isHost)
        {
            if (Started)
            {
                throw new NotSupportedException("Game is already started");
            }

            _isHost = isHost;
            Started = true;

            var tempLogin   = Guid.NewGuid().ToString().Substring(0, 5);
            var userData    = new UserData(tempLogin);
            var localPlayer = new Player(islocalPlayer: true, tempLogin);

            _simulation = new Simulation.Domain.Simulation(localPlayer.Id);
            _simulationView.SetSimulation(_simulation);

            _simulation.PlayerAdded += OnPlayerAdded;
            localPlayer.Moved       += OnLocalPlayerMoved;

            _localPeer = new Peer(unityListener: this, userData: userData, simulation: _simulation, isHost: isHost);

            try
            {
                await _localPeer.ConnectWithMasterServerAsync();

                if (!isHost)
                {
                    await _localPeer.BroadcastEventAsync((int)CustomEventCode.FullUpdateRequest);
                }
            }
            catch (Exception e)
            {
                Debug.LogException(e);
                return;
            }

            if (_isHost)
            {
                _simulation.AddPlayer(localPlayer);
            }
        }