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); } }
public Peer(IUnityThreadListener unityListener, UserData userData, Simulation simulation, bool isHost) : base(unityListener, userData) { _isHost = isHost; _simulation = simulation ?? throw new ArgumentNullException(nameof(simulation)); }
/// <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)); }
/// <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) { }