Exemple #1
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);
            }
        }
Exemple #2
0
 public Peer(IUnityThreadListener unityListener, UserData userData, Simulation simulation, bool isHost)
     : base(unityListener, userData)
 {
     _isHost     = isHost;
     _simulation = simulation ?? throw new ArgumentNullException(nameof(simulation));
 }
Exemple #3
0
 /// <param name="commandsDispatcher">Dispatcher for handling commands received from remote server</param>
 public BasePeer(IDispatcher commandsDispatcher, UserData userData)
 {
     _userData           = userData;
     _commandsDispatcher = commandsDispatcher ?? throw new ArgumentNullException(nameof(commandsDispatcher));
 }
Exemple #4
0
 /// <summary>
 /// <see cref="ImmediateDispatcher"/> will be used by default so messages from remote server can be handled
 /// in background thread. Pass <see cref="IDispatcher"/> in overload constructor to override this behaviour.
 /// </summary>
 /// <param name="userData"></param>
 public BasePeer(UserData userData) :
     this(new ImmediateDispatcher(), userData)
 {
 }
 protected ThreadSafePeer(IUnityThreadListener unityListener, UserData userData)
     : base(new UnityDispatcher(unityListener), userData)
 {
 }