public static WorldState CreateNewWorldState(int width, int height, int maxRockets, int aliensWidth, int aliensHeight, int initialLives) { GameConfigState gameConfigState = new GameConfigState(width, height, maxRockets, aliensWidth, aliensHeight); PlayerState playerState = new PlayerState(width / 2); AliensState aliensState = CreateNewAliensState(width, height, aliensWidth, aliensHeight); AliensMovementState aliensMovementState = new AliensMovementState(AliensMovementState.MovementDirection.Right); RocketsState rocketsState = new RocketsState(new List <Vector2i>()); BombsState bombsState = new BombsState(new List <Vector2i>()); GameProgressState gameProgressState = new GameProgressState(0, initialLives, false); WorldState worldState = new WorldState(gameConfigState, playerState, aliensState, aliensMovementState, rocketsState, bombsState, gameProgressState); return(worldState); }
public WorldState(GameConfigState gameConfigState, PlayerState playerState, AliensState aliensState, AliensMovementState aliensMovementState, RocketsState rocketsState, BombsState bombsState, GameProgressState gameProgressState) { GameConfigState = gameConfigState; PlayerState = playerState; AliensState = aliensState; AliensMovementState = aliensMovementState; RocketsState = rocketsState; BombsState = bombsState; GameProgressState = gameProgressState; }
public static void TickAliens(GameConfigState gameConfigState, AliensState aliensState, AliensMovementState aliensMovementState, out AliensState newAliensState, out AliensMovementState newAliensMovementState, out AliensFiringInput newAliensFiringInput) { Vector2i topLeft, bottomRight; aliensState.GetAbsoluteBoundingBox(out topLeft, out bottomRight); AliensMovementState.MovementDirection nextMovementDirection = ChooseNewAliensMovementDirection(gameConfigState, aliensMovementState, topLeft, bottomRight); Dictionary <AliensMovementState.MovementDirection, Vector2i> movementDirectionToDelta = new Dictionary <AliensMovementState.MovementDirection, Vector2i> { { AliensMovementState.MovementDirection.Left, new Vector2i(-1, 0) }, { AliensMovementState.MovementDirection.Right, new Vector2i(1, 0) }, { AliensMovementState.MovementDirection.Down, new Vector2i(0, 1) }, }; Vector2i movementDelta = movementDirectionToDelta[nextMovementDirection]; newAliensState = new AliensState(aliensState.TopLeft + movementDelta, aliensState.RelativePositions); newAliensMovementState = new AliensMovementState(nextMovementDirection); TickAliensFiring(aliensState, out newAliensFiringInput); }
public static WorldState Tick(WorldState worldState, PlayerInput playerInput) { PlayerState newPlayerState = TickPlayer(worldState.GameConfigState, worldState.PlayerState, playerInput); AliensState newAliensState; AliensMovementState newAliensMovementState; AliensFiringInput newAliensFiringInput; bool createNewAliens = (worldState.AliensState.RelativePositions.Count == 0); if (createNewAliens) { newAliensState = CreateNewAliensState(worldState.GameConfigState.Width, worldState.GameConfigState.Height, worldState.GameConfigState.AliensWidth, worldState.GameConfigState.AliensHeight); newAliensMovementState = new AliensMovementState(AliensMovementState.MovementDirection.Right); newAliensFiringInput = new AliensFiringInput(false, 0); } else { TickAliens(worldState.GameConfigState, worldState.AliensState, worldState.AliensMovementState, out newAliensState, out newAliensMovementState, out newAliensFiringInput); } RocketsState newRocketsState = TickRockets(worldState.GameConfigState, worldState.RocketsState, worldState.PlayerState, playerInput); BombsState newBombsState = TickBombs(worldState.GameConfigState, worldState.BombsState, worldState.PlayerState, worldState.AliensState, newAliensFiringInput); List <Tuple <int, int> > alienRocketCollisions = FindAlienRocketCollisions(newAliensState, newRocketsState); List <int> playerBombCollisions = FindPlayerBombCollisions(worldState.GameConfigState, newBombsState, newPlayerState); AliensState newAliensState2 = ResolveAlienCollisions(newAliensState, alienRocketCollisions); RocketsState newRocketsState2 = ResolveRocketCollisions(newRocketsState, alienRocketCollisions); GameProgressState newGameProgressState = TickGameProgress(worldState.GameConfigState, worldState.GameProgressState, newAliensState2, alienRocketCollisions.Count, playerBombCollisions.Count != 0); return(new WorldState(worldState.GameConfigState, newPlayerState, newAliensState2, newAliensMovementState, newRocketsState2, newBombsState, newGameProgressState)); }
private static AliensMovementState.MovementDirection ChooseNewAliensMovementDirection(GameConfigState gameConfigState, AliensMovementState aliensMovementState, Vector2i topLeft, Vector2i bottomRight) { switch (aliensMovementState.PreviousMovementDirection) { case AliensMovementState.MovementDirection.Left: if (topLeft.X == 0) { return(AliensMovementState.MovementDirection.Down); } else { return(aliensMovementState.PreviousMovementDirection); } case AliensMovementState.MovementDirection.Right: if (bottomRight.X == gameConfigState.Width) { return(AliensMovementState.MovementDirection.Down); } else { return(aliensMovementState.PreviousMovementDirection); } case AliensMovementState.MovementDirection.Down: if (topLeft.X == 0) { return(AliensMovementState.MovementDirection.Right); } else { return(AliensMovementState.MovementDirection.Left); } default: throw new NotImplementedException(); } }