public static void CheckForEnemy(Guid enemyId, PlayingState playingState, LevelCollisionDetection levelCollisionDetection) { AIComponent enemyAIComponent = playingState.AIComponents[enemyId]; //determine if the player is in line of site IEnumerable<Guid> playerEntities = playingState.Entities.Where(x => (x.ComponentFlags & ComponentMasks.Player) == ComponentMasks.Player).Select(x => x.ID); foreach (Guid playerid in playerEntities) { //get the enemy direction DirectionComponent enemyDirectionComponent = playingState.DirectionComponents[enemyId]; float direction = enemyDirectionComponent.Direction; if (enemyAIComponent.ActiveState.Peek() != AIState.ATTACK) { bool playerSeen = LineOfSiteRayCast.CalculateLineOfSight(playingState.PositionComponents[enemyId].Position, playingState.PositionComponents[playerid].Position, enemyAIComponent.LineOfSight, playingState.DirectionComponents[enemyId].Direction, playingState); if (playerSeen) { //set our path goal enemyAIComponent.EntityToAttack = playerid; enemyAIComponent.ActiveState.Push(AIState.ATTACK); playingState.AIComponents[enemyId] = enemyAIComponent; UpdateEnemyPath(enemyId, playingState); AttackEnemy(enemyId, playingState, levelCollisionDetection); } } } }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); // TODO: use this.Content to load your game content here spriteSheet = Content.Load<Texture2D>("Graphics\\UIpackSheet_transparent"); spriteFont = Content.Load<SpriteFont>("Fonts\\kooten"); levelRenderer = new LevelRenderer(); levelRenderer.ImportTMX(Content, "Engine\\World\\Levels\\testmap.tmx", tileSize, spriteFont, playingState); levelCollisionDetection = new LevelCollisionDetection(levelRenderer.TileMap, tileSize); }
public void CreateEnemySpawns(TmxList<TmxObject> enemySpawns, PlayingState playingState) { LevelCollisionDetection levelCollisionDetection = new LevelCollisionDetection(TileMap, TileMap.TileSize); foreach (TmxObject enemypawn in enemySpawns) { Guid id = playingState.CreateEntity(); playingState.Entities.Where(x => x.ID == id).First().ComponentFlags = ComponentMasks.Enemy; playingState.DirectionComponents[id] = new DirectionComponent() { Direction = (float)(enemypawn.Rotation * (Math.PI / 180)) }; playingState.DisplayComponents[id] = new DisplayComponent() { Source = new Rectangle(343, 470, TileMap.TileSize, TileMap.TileSize) }; playingState.HealthComponents[id] = new HealthComponent() { Health = 15 }; playingState.PositionComponents[id] = new PositionComponent() { Position = new Vector2((float)enemypawn.X + (TileMap.TileSize / 2), (float)enemypawn.Y + (TileMap.TileSize / 2)) }; playingState.VelocityComponents[id] = new VelocityComponent() { xVelocity = 0f, yVelocity = 0f, xTerminalVelocity = 3f, yTerminalVelocity = 3f }; playingState.AccelerationComponents[id] = new AccelerationComponent() { xAcceleration = 3f, yAcceleration = 3f }; playingState.DamageComponents[id] = new DamageComponent() { AttackRange = 1 * TileMap.TileSize, Damage = 5 }; //our origin is going to offset our bounded box playingState.AABBComponents[id] = new AABBComponent() { BoundedBox = new Rectangle((int)enemypawn.X, (int)enemypawn.Y, TileMap.TileSize, TileMap.TileSize) }; //parse patrol paths string patrolPaths = enemypawn.Properties["PatrolPath"]; List<Vector2> PatrolVectors = new List<Vector2>(); if (!string.IsNullOrWhiteSpace(patrolPaths)) { string[] newPaths = patrolPaths.Split('|'); for (int i = 0; i < newPaths.Length; i++) { newPaths[i] = newPaths[i].Replace("(", ""); newPaths[i] = newPaths[i].Replace(")", ""); int x = Int32.Parse(newPaths[i].Split(',')[0]); int y = Int32.Parse(newPaths[i].Split(',')[1]); //center our points too x = (x * TileMap.TileSize) + (TileMap.TileSize / 2); y = (y * TileMap.TileSize) + (TileMap.TileSize / 2); PatrolVectors.Add(new Vector2(x, y)); } } playingState.AIComponents[id] = new AIComponent() { ActiveState = new Stack<AIState>(), LineOfSight = 6 * TileMap.TileSize, PatrolPath = PatrolVectors, ActivePath = new LinkedList<Tile>(), Astar = TileMap.aStar, AITree = AIPawnSystem.CreatePawnTree(id, playingState, levelCollisionDetection, TileMap.TileSize, TileMap.TileSize) }; playingState.AIComponents[id].ActiveState.Push(AIState.STILL); playingState.LabelComponents[id] = new LabelComponent() { Label = "", Position = new Vector2((float)enemypawn.X, (float)(enemypawn.Y - 5)) }; } }
public static void ProceedWithNextPath(Guid enemyId, PlayingState playingState, LevelCollisionDetection levelCollisionDetection) { UpdateEnemyPath(enemyId, playingState); UpdateActivePath(enemyId, playingState, levelCollisionDetection); }
public static void AttackEnemy(Guid enemyId, PlayingState playingState, LevelCollisionDetection levelCollisionDetection) { //update enemy ai pathing UpdateEnemyPath(enemyId, playingState); UpdateActivePath(enemyId, playingState, levelCollisionDetection); }
public static void UpdateEnemeyAI(PlayingState playingState, GameTime gameTime, LevelCollisionDetection levelCollisionDetection, int tileSize) { float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds; timer -= elapsed; if (timer < 0) { //Timer expired, execute action UpdateTimedEvents = true; timer = TIMER; //Reset Timer } else { UpdateTimedEvents = false; } IEnumerable<Guid> enemyEntites = playingState.Entities.Where(x => (x.ComponentFlags & ComponentMasks.Enemy) == ComponentMasks.Enemy).Select(x => x.ID); foreach (Guid enemyid in enemyEntites) { //get current enemey ai state AIComponent enemyAIComponent = playingState.AIComponents[enemyid]; enemyAIComponent.AITree.Tick(new TimeData((float)gameTime.ElapsedGameTime.TotalSeconds)); //if (enemyAI.ActiveState.Peek() == AIState.ATTACK) //{ // AttackEnemy(enemyid, playingState, levelCollisionDetection); //} //else //{ // //handle pathing // if (enemyAI.PatrolPath.Count() > 0) // { // //if we have a patrol path and we are not attacking, lets get back to patrolling // if (enemyAI.ActiveState.Peek() != AIState.PATROL && enemyAI.ActiveState.Peek() != AIState.ATTACK) // { // enemyAI.ActiveState.Push(AIState.PATROL); // } // if (enemyAI.ActiveState.Peek() == AIState.PATROL) // { // //lets do our patrol pathing // ProceedWithNextPath(enemyid, playingState, levelCollisionDetection); // } // } // //check to see if we see the enemy, if so put ourselves in attack mode // if (enemyAI.ActiveState.Peek() != AIState.ATTACK) // { // if (UpdateTimedEvents) // { // CheckForEnemy(enemyid, playingState, levelCollisionDetection); // } // } //} } }
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 UpdateActivePath(Guid enemyId, PlayingState playingState, LevelCollisionDetection levelCollisionDetection) { AIComponent enemyAIComponent = playingState.AIComponents[enemyId]; if (enemyAIComponent.ActivePath != null && enemyAIComponent.ActivePath.Count > 0) { 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); //create rectangle for our path goal var AdjustRect = new Rectangle((int)AdjustedActive.X, (int)AdjustedActive.Y, AABBComponent.BoundedBox.Width, AABBComponent.BoundedBox.Height); //use magnitiude if (Vector2.Distance(CurrentPosition.Position, AdjustedActive) <= Acceleration.xAcceleration) { //remove the first element in our active path enemyAIComponent.ActivePath.RemoveFirst(); if (enemyAIComponent.ActivePath.Count > 0) { AdjustedActive = new Vector2(enemyAIComponent.ActivePath.First.Value.TilePosition.X, enemyAIComponent.ActivePath.First.Value.TilePosition.Y); //update our direction var direction = AdjustedActive - new Vector2(AABBComponent.BoundedBox.X, AABBComponent.BoundedBox.Y); direction.Normalize(); DirectionComponent enemyDirection = playingState.DirectionComponents[enemyId]; enemyDirection.Direction = (float)Math.Atan2((double)direction.Y, (double)direction.X); playingState.DirectionComponents[enemyId] = enemyDirection; } } else { UpdateEnemyPosition(enemyId, enemyAIComponent, playingState, levelCollisionDetection); } } }
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; } }