Ejemplo n.º 1
0
        public static BombsState TickBombs(GameConfigState gameConfigState, BombsState bombsState, PlayerState playerState, AliensState aliensState, AliensFiringInput aliensFiringInput)
        {
            List <Vector2i> newPositions = bombsState.Positions.Select(position => position + new Vector2i(0, 1)).Where(position => position.Y < gameConfigState.Height).ToList();

            if (aliensFiringInput.Fire)
            {
                newPositions.Add(aliensState.TopLeft + aliensState.RelativePositions[aliensFiringInput.AlienIndex]);
            }
            return(new BombsState(newPositions));
        }
Ejemplo n.º 2
0
        public static RocketsState TickRockets(GameConfigState gameConfigState, RocketsState rocketsState, PlayerState playerState, PlayerInput playerInput)
        {
            List <Vector2i> newPositions = rocketsState.Positions.Select(position => position + new Vector2i(0, -1)).Where(position => position.Y >= 0).ToList();

            if (playerInput == PlayerInput.Fire && newPositions.Count < gameConfigState.MaxRockets)
            {
                newPositions.Add(new Vector2i(playerState.Position, gameConfigState.Height - 1));
            }
            return(new RocketsState(newPositions));
        }
Ejemplo n.º 3
0
        private static List <int> FindPlayerBombCollisions(GameConfigState gameConfigState, BombsState bombsState, PlayerState playerState)
        {
            Vector2i playerPosition = new Vector2i(playerState.Position, gameConfigState.Height - 1);

            List <int> collisions = bombsState.Positions.Select((position, index) => new Tuple <Vector2i, int>(position, index))
                                    .Where(positionAndIndex => playerPosition == positionAndIndex.Item1)
                                    .Select(positionAndIndex => positionAndIndex.Item2)
                                    .ToList();

            return(collisions);
        }
Ejemplo n.º 4
0
        public static GameProgressState TickGameProgress(GameConfigState gameConfigState, GameProgressState gameProgressState, AliensState aliensState, int aliensKilled, bool playerCollidedWithBomb)
        {
            Vector2i topLeft, bottomRight;

            aliensState.GetAbsoluteBoundingBox(out topLeft, out bottomRight);

            int  newScore    = gameProgressState.Score + aliensKilled;
            int  newLives    = Math.Max(gameProgressState.Lives - (playerCollidedWithBomb ? 1 : 0), 0);
            bool newGameOver = gameProgressState.GameOver || (newLives == 0 || (bottomRight.Y >= gameConfigState.Height));

            return(new GameProgressState(newScore, newLives, newGameOver));
        }
Ejemplo n.º 5
0
        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);
        }
Ejemplo n.º 6
0
        public static PlayerState TickPlayer(GameConfigState gameConfigState, PlayerState playerState, PlayerInput playerInput)
        {
            switch (playerInput)
            {
            case PlayerInput.MoveLeft:
                return(new PlayerState(Math.Max(playerState.Position - 1, 0)));

            case PlayerInput.MoveRight:
                return(new PlayerState(Math.Min(playerState.Position + 1, gameConfigState.Width - 1)));

            default:
                return(playerState);
            }
        }
Ejemplo n.º 7
0
        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;
        }
Ejemplo n.º 8
0
        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);
        }
Ejemplo n.º 9
0
        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();
            }
        }