public static void HandlePlayerMovement(PlayingState playingState, GraphicsDeviceManager graphics, GameTime gameTime, KeyboardState previousKeyboardState, 
            MouseState previousMouseState, GamePadState previousGamepadState, FollowCamera followCam, TileMap tileMap, LevelCollisionDetection levelCollisionDetection)
        {
            List<Guid> moveableEntities = playingState.Entities.Where(x => (x.ComponentFlags & ComponentMasks.PlayerInput) == ComponentMasks.PlayerInput).Select(x => x.ID).ToList();
            foreach (Guid id in moveableEntities)
            {
                var directionComponent = playingState.DirectionComponents[id];

                var positionComponent = playingState.PositionComponents[id];
                var aabbComponent = playingState.AABBComponents[id];
                Vector2 position = positionComponent.Position;
                Rectangle boundedBox = aabbComponent.BoundedBox;

                VelocityComponent velocityComponent = playingState.VelocityComponents[id];

                AccelerationComponent accelerationComponent = playingState.AccelerationComponents[id];

                #region gamepad controls
                GamePadState currentGamePadState = GamePad.GetState(PlayerIndex.One);

                //position.X += currentGamePadState.ThumbSticks.Left.X * playerMoveSpeed;
                //position.Y -= currentGamePadState.ThumbSticks.Left.Y * playerMoveSpeed;
                //previousGamepadState = currentGamePadState;
                #endregion


                #region keyboard controls
                KeyboardState CurrentKeyboardState = Keyboard.GetState();
                if (CurrentKeyboardState.IsKeyDown(Keys.Left) || CurrentKeyboardState.IsKeyDown(Keys.A) || currentGamePadState.DPad.Left == ButtonState.Pressed)
                {
                    velocityComponent.xVelocity -= accelerationComponent.xAcceleration;
                    velocityComponent.xVelocity = MathHelper.Clamp(velocityComponent.xVelocity, -1 * velocityComponent.xTerminalVelocity, 0);
                    
                    boundedBox.X += (int)velocityComponent.xVelocity;
                    boundedBox = levelCollisionDetection.CheckWallCollision(boundedBox, Direction.Left);
                }

                if (CurrentKeyboardState.IsKeyDown(Keys.Right) || CurrentKeyboardState.IsKeyDown(Keys.D) || currentGamePadState.DPad.Right == ButtonState.Pressed)
                {
                    velocityComponent.xVelocity += accelerationComponent.xAcceleration;
                    velocityComponent.xVelocity = MathHelper.Clamp(velocityComponent.xVelocity, 0, velocityComponent.xTerminalVelocity);
                                        
                    boundedBox.X += (int)velocityComponent.xVelocity;
                    boundedBox = levelCollisionDetection.CheckWallCollision(boundedBox, Direction.Right);
                }

                //move player x axis



                if (CurrentKeyboardState.IsKeyDown(Keys.Up) || CurrentKeyboardState.IsKeyDown(Keys.W) || currentGamePadState.DPad.Up == ButtonState.Pressed)
                {

                    velocityComponent.yVelocity -= accelerationComponent.yAcceleration;
                    velocityComponent.yVelocity  = MathHelper.Clamp(velocityComponent.yVelocity, -1 * velocityComponent.yTerminalVelocity, 0 );
                    
                    boundedBox.Y += (int)velocityComponent.yVelocity;
                    boundedBox = levelCollisionDetection.CheckWallCollision(boundedBox, Direction.Up);
                }



                if (CurrentKeyboardState.IsKeyDown(Keys.Down) || CurrentKeyboardState.IsKeyDown(Keys.S) || currentGamePadState.DPad.Down == ButtonState.Pressed)
                {
                    velocityComponent.yVelocity += accelerationComponent.yAcceleration;
                    velocityComponent.yVelocity = MathHelper.Clamp(velocityComponent.yVelocity, 0, velocityComponent.yTerminalVelocity);
                    position.Y += velocityComponent.yVelocity;

                    boundedBox.Y += (int)velocityComponent.yVelocity;
                    boundedBox = levelCollisionDetection.CheckWallCollision(boundedBox, Direction.Down);
                }

                if (CurrentKeyboardState.IsKeyUp(Keys.Down) && CurrentKeyboardState.IsKeyUp(Keys.S) && CurrentKeyboardState.IsKeyUp(Keys.Up) && CurrentKeyboardState.IsKeyUp(Keys.W))
                {
                    velocityComponent.yVelocity = 0;
                }
                if (CurrentKeyboardState.IsKeyUp(Keys.Left) && CurrentKeyboardState.IsKeyUp(Keys.A) && CurrentKeyboardState.IsKeyUp(Keys.D) && CurrentKeyboardState.IsKeyUp(Keys.Right))
                {
                    velocityComponent.xVelocity = 0;
                }

                previousKeyboardState = CurrentKeyboardState;

                #endregion


                #region mouse controls
                //face player towards mouse

                MouseState currentMouseState = Mouse.GetState();

                position = new Vector2(boundedBox.X + (boundedBox.Width / 2), boundedBox.Y + (boundedBox.Height / 2));

                Vector2 mouseLocation = new Vector2(currentMouseState.X + followCam.Center.X, currentMouseState.Y + followCam.Center.Y);

                Vector2 direction = mouseLocation - position;
                direction.Normalize();

                directionComponent.Direction = (float)Math.Atan2((double)direction.Y, (double)direction.X);


                //if (currentMouseState.LeftButton == ButtonState.Pressed)
                //{
                //    List<Guid> weaponEntities = playingState.Entities.Where(x => (x.ComponentFlags & ComponentMasks.Weapon) == ComponentMasks.Weapon).Select(x => x.ID).ToList();
                //    foreach (Guid wid in weaponEntities)
                //    {

                //        if (playingState.OwnerComponents.ContainsKey(wid))
                //        {
                //            OwnerComponent ownerComponent = playingState.OwnerComponents[wid];
                //            if (ownerComponent.OwnerID == id)
                //            {
                //                Guid projectileId = playingState.CreateEntity();
                //                playingState.Entities.Where(x => x.ID == projectileId).First().ComponentFlags = ComponentMasks.Projectile;


                //                playingState.DirectionComponents[projectileId] = new DirectionComponent() { Direction = directionComponent.Direction };
                //                playingState.DisplayComponents[projectileId] = new DisplayComponent() { Source = new Rectangle(21, 504, destination.Width, destination.Height) };
                //                playingState.SpeedComponents[projectileId] = new SpeedComponent() { Speed = 15f };
                //                playingState.PositionComponents[projectileId] = new PositionComponent() { Position = position, Destination = new Rectangle((int)position.X, (int)position.Y, destination.Width, destination.Height) };
                //                playingState.DamageComponents[projectileId] = new DamageComponent() { Damage = 10 };
                //                playingState.OwnerComponents[projectileId] = new OwnerComponent() { OwnerID = wid };
                //            }
                //        }
                //    }
                //}
                



                previousMouseState = currentMouseState;
                #endregion
                
                playingState.DirectionComponents[id] = directionComponent;

                positionComponent.Position = position;
                aabbComponent.BoundedBox = boundedBox;
                playingState.PositionComponents[id] = positionComponent;
                playingState.AABBComponents[id] = aabbComponent;
                playingState.VelocityComponents[id] = velocityComponent;
                playingState.AccelerationComponents[id] = accelerationComponent;
            }
        }
Exemple #2
0
        public void ImportTMX(ContentManager content, string filepath, int tilesize, SpriteFont font, PlayingState playingState)
        {
            TileMap = new TileMap();

            var map = new TmxMap(filepath);
            TileMap.RowCount = map.Width;
            TileMap.ColumnCount = map.Height;


            //get and set tile map details
            var tileMap = map.Tilesets["Basic"];
            CreateTileMap(tileMap);

            //get and set level layer
            var levelLayer = map.Layers["Level"];
            CreateTileArray(levelLayer);

            //get and set level bounding rects
            var boudingRects = map.ObjectGroups["Level Rects"].Objects;
            CreateBoundingRects(boudingRects, playingState);

            //get and set player spawn
            var playersSpawn = map.ObjectGroups["Player Spawn"].Objects;
            CreatePlayerSpawn(playersSpawn, playingState);

            //get and set enemy spawns
            var enemySpawns = map.ObjectGroups["Enemy Spawns"].Objects;
            CreateEnemySpawns(enemySpawns, playingState);

            this.Font = font;
        }      
 public LevelCollisionDetection(TileMap tileMap, int tileSize)
 {
     this.TileMap = tileMap;
     this.TileSize = tileSize;
 }