public static void UpdateEnemyPosition(Guid enemyId, AIComponent enemyAIComponent, PlayingState playingState, LevelCollisionDetection levelCollisionDetection) { #region set our variables var CurrentPosition = playingState.PositionComponents[enemyId]; var AABBComponent = playingState.AABBComponents[enemyId]; var Acceleration = playingState.AccelerationComponents[enemyId]; var AdjustedActive = new Vector2(enemyAIComponent.ActivePath.First.Value.TilePosition.X, enemyAIComponent.ActivePath.First.Value.TilePosition.Y); #endregion //update enemy position #region move right if (CurrentPosition.Position.X < AdjustedActive.X) { if ((CurrentPosition.Position.X + (int)Acceleration.xAcceleration) > AdjustedActive.X) { CurrentPosition.Position.X = (int)AdjustedActive.X; //update our bounded box to new position AABBComponent.BoundedBox.X = (int)CurrentPosition.Position.X - (AABBComponent.BoundedBox.Width / 2); AABBComponent.BoundedBox = levelCollisionDetection.CheckWallCollision(AABBComponent.BoundedBox, Direction.Right); } else { CurrentPosition.Position.X += (int)Acceleration.xAcceleration; //update our bounded box to new position AABBComponent.BoundedBox.X = (int)CurrentPosition.Position.X - (AABBComponent.BoundedBox.Width / 2); AABBComponent.BoundedBox = levelCollisionDetection.CheckWallCollision(AABBComponent.BoundedBox, Direction.Right); } //this.Position = mapCollisionDetection.CheckWallCollision(this.Position, this.Width, this.Height, Direction.Right); } #endregion #region move left else if (CurrentPosition.Position.X > AdjustedActive.X) { if ((CurrentPosition.Position.X - Acceleration.xAcceleration) < AdjustedActive.X) { CurrentPosition.Position.X = (int)AdjustedActive.X; //update our bounded box to new position AABBComponent.BoundedBox.X = (int)CurrentPosition.Position.X - (AABBComponent.BoundedBox.Width / 2); AABBComponent.BoundedBox = levelCollisionDetection.CheckWallCollision(AABBComponent.BoundedBox, Direction.Left); } else { CurrentPosition.Position.X -= (int)Acceleration.xAcceleration; //update our bounded box to new position AABBComponent.BoundedBox.X = (int)CurrentPosition.Position.X - (AABBComponent.BoundedBox.Width / 2); AABBComponent.BoundedBox = levelCollisionDetection.CheckWallCollision(AABBComponent.BoundedBox, Direction.Left); } //this.Position = mapCollisionDetection.CheckWallCollision(this.Position, this.Width, this.Height, Direction.Left); } #endregion #region move up if (CurrentPosition.Position.Y > AdjustedActive.Y) { if (CurrentPosition.Position.Y - (int)Acceleration.yAcceleration < AdjustedActive.Y) { CurrentPosition.Position.Y = (int)AdjustedActive.Y; //update our bounded box to new position AABBComponent.BoundedBox.Y = (int)CurrentPosition.Position.Y - (AABBComponent.BoundedBox.Height / 2); AABBComponent.BoundedBox = levelCollisionDetection.CheckWallCollision(AABBComponent.BoundedBox, Direction.Up); } else { CurrentPosition.Position.Y -= (int)Acceleration.yAcceleration; //update our bounded box to new position AABBComponent.BoundedBox.Y = (int)CurrentPosition.Position.Y - (AABBComponent.BoundedBox.Height / 2); AABBComponent.BoundedBox = levelCollisionDetection.CheckWallCollision(AABBComponent.BoundedBox, Direction.Up); } //this.Position = mapCollisionDetection.CheckWallCollision(this.Position, this.Width, this.Height, Direction.Up); } #endregion #region move down else if (CurrentPosition.Position.Y < AdjustedActive.Y) { if (CurrentPosition.Position.Y + (int)Acceleration.yAcceleration > AdjustedActive.Y) { CurrentPosition.Position.Y = (int)AdjustedActive.Y; //update our bounded box to new position AABBComponent.BoundedBox.Y = (int)CurrentPosition.Position.Y - (AABBComponent.BoundedBox.Height / 2); AABBComponent.BoundedBox = levelCollisionDetection.CheckWallCollision(AABBComponent.BoundedBox, Direction.Down); } else { CurrentPosition.Position.Y += (int)Acceleration.yAcceleration; //update our bounded box to new position AABBComponent.BoundedBox.Y = (int)CurrentPosition.Position.Y - (AABBComponent.BoundedBox.Height / 2); AABBComponent.BoundedBox = levelCollisionDetection.CheckWallCollision(AABBComponent.BoundedBox, Direction.Down); } //this.Position = mapCollisionDetection.CheckWallCollision(this.Position, this.Width, this.Height, Direction.Down); } #endregion //update our position based on our new bounded box after collision #region save component changes CurrentPosition.Position = new Vector2(AABBComponent.BoundedBox.X + (AABBComponent.BoundedBox.Width / 2), AABBComponent.BoundedBox.Y + (AABBComponent.BoundedBox.Height / 2)); playingState.PositionComponents[enemyId] = CurrentPosition; playingState.AIComponents[enemyId] = enemyAIComponent; playingState.AABBComponents[enemyId] = AABBComponent; #endregion }
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; } }