private void RaiseOnNewGameState() { foreach (Delegate d in NewGameState.GetInvocationList()) { if (d.Target is ISynchronizeInvoke) { (d.Target as ISynchronizeInvoke).BeginInvoke(d, new object[] { CurrentGameState }); } else { d.DynamicInvoke(CurrentGameState); } } foreach (Delegate d in ChangedMapState.GetInvocationList()) { if (CurrentGameState.Map.GameState != previousMapState) { if (d.Target is ISynchronizeInvoke) { (d.Target as ISynchronizeInvoke).BeginInvoke(d, new object[] { CurrentGameState.Map.GameState }); } else { d.DynamicInvoke(CurrentGameState.Map.GameState); } } } }
// Start is called before the first frame update void Start() { states["Title"] = new TitleState(); states["Play"] = new PlayState(); states["NewGame"] = new NewGameState(); states["Pause"] = new PauseState(); TransitionState("Title"); }
private void LogActionsTaken() { for (int t = 0; t < Constants.TANK_COUNT; t++) { MobileState tankState = NewGameState.GetMobileState(t); if (tankState.IsActive) { Tank tank = Game.Current.Elements[t] as Tank; TankAction actionTaken = TankActionsTaken[t]; LogDebugMessage("Player {0} tank {1} took action {2} and is now at {3}", tank.PlayerNumber, tank.Number, actionTaken, tankState.Pos); } } DebugHelper.WriteLine(); }
public void DoAfterUpdatingTheState(bool stateUpdateCompletedSuccessfully) { // Find any mobile states unaccounted for, and remove them: for (int i = 0; i < AllMobileStatesAccountedFor.Length; i++) { // Ignore if accounted for: if (AllMobileStatesAccountedFor[i]) { continue; } // Ignore if not active: MobileState mobiState = NewGameState.GetMobileState(i); if (!mobiState.IsActive) { continue; } Element element = Game.Current.Elements[i]; mobiState = mobiState.Kill(); NewGameState.SetMobileState(i, ref mobiState); LogDebugMessage("Deactivating {0} {1} @{2}, which was not accounted for", element.ElementType, i, mobiState.Pos); } // Record the actual actions taken by the players: Turn prevTurn = Game.Current.CurrentTurn.PreviousTurn; if (prevTurn != null) { prevTurn.TankActionsTakenAfterPreviousTurn = TankActionsTaken; } Game.Current.CurrentTurn.GameState = NewGameState; LogActionsTaken(); LogDebugMessage("New game state: {0}", NewGameState); DebugHelper.WriteLine(); CheckGameState(); // Clear the variables, so the callback can be used again without risk of debris from the previous time: TankActionsTaken = null; PrevGameState = null; NewGameState = null; }
public NewGameStateTest() { gameMock = new Mock <IGame>(); Mock <IGameBoard> gameBoardMock = new Mock <IGameBoard>(); gameBoardMock.SetupGet(x => x.Size).Returns(10); gameMock.SetupGet(x => x.Board).Returns(gameBoardMock.Object); var serviceProviderMock = new Mock <IServiceProvider>(); serviceProviderMock .Setup(x => x.GetService(It.IsAny <Type>())) .Returns <Type>((serviceType) => (serviceType == typeof(RunningGameState) ? (IGameState) new RunningGameState(null, null, null, NullLogger <RunningGameState> .Instance) : (IGameState) new EndedGameState(NullLogger <EndedGameState> .Instance))); shipCoordinatesGeneratorMock = new Mock <IShipCoordinatesGenerator>(); target = new NewGameState(serviceProviderMock.Object, shipCoordinatesGeneratorMock.Object, NullLogger <NewGameState> .Instance); }
public void UpdateTankState(int unitId, TankAction tankAction, Point newPos, Direction newDir, bool isActive) { bool tankFound = false; for (int t = 0; t < Constants.TANK_COUNT; t++) { Tank tank = Game.Current.Elements[t] as Tank; if (tank.Id == unitId) { MobileState newMobileState = new MobileState(newPos, newDir, isActive); NewGameState.SetMobileState(t, ref newMobileState); TankActionsTaken[tank.Index] = tankAction; tankFound = true; AllMobileStatesAccountedFor[tank.Index] = true; break; } } /* TODO: Else find a likely matching tank in the previous game state based on position and direction: */ #if DEBUG if (!tankFound) { LogDebugMessage("ERROR! No existing tank could be found with id {0} at position ({1}, {2})", unitId, newPos.X, newPos.Y); #if THROW_HARNESS_ERRORS throw new InvalidOperationException( String.Format( "No existing tank could be found with id {0} at position ({1}, {2})", unitId, newPos.X, newPos.Y) ); #endif } #endif }
void OnReceived(IAsyncResult result) { // this is what had been passed into BeginReceive as the second parameter: UdpClient socket = result.AsyncState as UdpClient; // points towards whoever had sent the message: IPEndPoint source = new IPEndPoint(0, 0); // get the actual message and fill out the source: byte[] message = socket.EndReceive(result, ref source); // do what you'd like with `message` here: string returnData = Encoding.ASCII.GetString(message); //dropGameState = new DropGameState(); dropGameState = new GameState(); newGameState = new NewGameState(); lastestGameState = new GameState(); latestMessage = JsonUtility.FromJson <Message>(returnData); //Debug.Log("Got THIS: " + latestMessage.cmd.ToString() + " And THIS: " + returnData); try { switch (latestMessage.cmd) { case commands.NEW_CLIENT: newGameState = JsonUtility.FromJson <NewGameState>(returnData); //SpawnPlayers(); break; case commands.UPDATE: lastestGameState = JsonUtility.FromJson <GameState>(returnData); //UpdatePlayers(); break; case commands.EXIT: //dropGameState = JsonUtility.FromJson<DropGameState>(returnData); //ListOfDroppedPlayers latestDroppedPlayer = JsonUtility.FromJson<ListOfDroppedPlayers>(returnData); //foreach (string player in latestDroppedPlayer.droppedPlayers) //{ // droppedPlayers.Add(player); //} dropGameState = JsonUtility.FromJson <GameState>(returnData); foreach (Player player in dropGameState.players) { deletedPlayers.Add(player); } break; case commands.LIST: listGameState = JsonUtility.FromJson <GameState>(returnData); break; } } catch (Exception e) { Debug.Log(e.ToString()); } // schedule the next receive operation once reading is done: socket.BeginReceive(new AsyncCallback(OnReceived), socket); }
private void CheckGameState() { GameState calculatedGameState = PrevGameState.Clone(); calculatedGameState.Tick = Game.Current.CurrentTurn.Tick; if (calculatedGameState is MutableGameState) { MutableGameStateEngine.ApplyAllActions((MutableGameState)calculatedGameState, TankActionsTaken); } else if (calculatedGameState is ImmutableGameState) { ImmutableGameStateEngine.ApplyActions((ImmutableGameState)calculatedGameState, TankActionsTaken); } else { throw new InvalidOperationException( String.Format( "The calculated game state on turn {0} can't be cast to a known game state type", Game.Current.CurrentTurn.Tick ) ); } calculatedGameState.Outcome |= Outcome.InProgress; Game.Current.CurrentTurn.GameStateCalculatedByGameStateEngine = calculatedGameState; string reasonDifferent; if (!GameState.AreGameStatesEquivalent(NewGameState, calculatedGameState, out reasonDifferent)) { LogDebugMessage( "ERROR! The calculated game state does not match the new harness game state. \r\nReason: {0}", reasonDifferent); #if THROW_HARNESS_ERRORS throw new ApplicationException( String.Format( "The calculated game state does not match the new harness game state. \r\nReason: {0}", reasonDifferent ) ); #endif // Workaround for a bug in the Entelect test harness on tick 1. // Copy calculated game state into retrieved game state: if (NewGameState.Tick == 1) { BitMatrix destWalls = NewGameState.Walls; BitMatrix srcWalls = calculatedGameState.Walls; // Copy walls from calculated game state: for (int x = destWalls.TopLeft.X; x <= destWalls.BottomRight.X; x++) { for (int y = destWalls.TopLeft.Y; y <= destWalls.BottomRight.Y; y++) { destWalls[x, y] = srcWalls[x, y]; } } // Copy mobile states: for (int i = 0; i < Constants.MOBILE_ELEMENT_COUNT; i++) { MobileState calculatedMobileState = calculatedGameState.GetMobileState(i); NewGameState.SetMobileState(i, ref calculatedMobileState); } } } }
public void UpdateBulletState(int bulletId, Point bulletPos, Direction bulletDir, bool isActive) { MobileState newMobileState = new MobileState(bulletPos, bulletDir, isActive); // Look for the bullet: int i = 0; bool bulletFound = false; for (int b = Constants.MIN_BULLET_INDEX; b <= Constants.MAX_BULLET_INDEX; b++) { if (Game.Current.CurrentTurn.BulletIds[i] == bulletId) { NewGameState.SetMobileState(b, ref newMobileState); bulletFound = true; AllMobileStatesAccountedFor[b] = true; break; } i++; } if (!bulletFound) { /* Find a tank which has the same direction as the bullet, * and where the bullet is in the correct position for a newly fired bullet: */ for (int t = 0; t < Constants.TANK_COUNT; t++) { MobileState tankState = NewGameState.GetMobileState(t); if (tankState.Dir == bulletDir) { Point tankFiringPoint = tankState.GetTankFiringPoint(); if (tankFiringPoint == bulletPos) { Tank tank = Game.Current.Elements[t] as Tank; int bulletIndex = tank.Bullet.Index; NewGameState.SetMobileState(bulletIndex, ref newMobileState); Game.Current.CurrentTurn.BulletIds[t] = bulletId; AllMobileStatesAccountedFor[bulletIndex] = true; bulletFound = true; break; } } } /* TODO: Find a likely matching bullet in the previous game state based on position and direction: * if (!bulletFound) * { * // Find a bullet which has the same direction and is two spaces before this bullet: * * } */ #if DEBUG if (!bulletFound) { LogDebugMessage("ERROR! The {0}bullet with id {1} at {2} could not be matched to a tank", isActive ? String.Empty : "dead ", bulletId, bulletPos); #if THROW_HARNESS_ERRORS throw new InvalidOperationException( string.Format( "The {0}bullet with id {1} at {2} could not be matched to a tank", isActive ? String.Empty : "dead ", bulletId, bulletPos ) ); #endif } #endif } }