public override void Update(GameTime gameTime) { PositionDifference = Position - playerPosition; playerPosition -= Position; Rotation = (float)Math.Atan2(playerPosition.Y, playerPosition.X); // Checks if Fire Fighters in Fire Range if (PositionDifference.Length() <= 150) { LinearVelocity = 0; // fires weapon if Fire Rate is ready if (canFire) { FireWeapon(); } } //Moves if out of range of player if (PositionDifference.Length() > 150) { LinearVelocity = 1; } //Update timer fireTimer.Update(gameTime.ElapsedGameTime); base.Update(gameTime); }
public PoliceEnemy(Game game, SpriteBatch spriteBatch, Texture2D texture, Vector2 position, int seed, bool add = false) : base(game, spriteBatch, texture, position, add) { // Fire Rate timer fireTimeSpan = TimeSpan.FromSeconds(2); fireTimer = new Timer(); fireTimer.OnExpire += () => canFire = true; fireTimer.Start(fireTimeSpan); // Change direction timer turnTimeSpan = TimeSpan.FromSeconds(1); turnTimer = new Timer(); turnTimer.OnExpire += () => canTurn = true; turnTimer.Start(turnTimeSpan); // Pedestrian Kill timer pedestrianKillTimeSpan = TimeSpan.FromSeconds(5); pedestrianKillTimer = new Timer(); pedestrianKillTimer.OnExpire += () => pedestrianKilled = false; // enemy type enemyType = EnemyType.Police; Health = 200; //Random number random = new Random(); //State Machine stateMachine = new StateMachine <PoliceStates>(); //Actions for States Action policeWalkingAction = () => { LinearVelocity = 1; if (Position.X < 0 || Position.X > 2000) { Position.X = 1000; Position.Y = 1000; } if (Position.Y < 0 || Position.Y > 2000) { Position.X = 1000; Position.Y = 1000; } if (canTurn) { Rotation = (random.Next(3) - 1) * (random.Next(10)); canTurn = false; turnTimer.Start(turnTimeSpan); } }; Action policeChasingAction = () => { playerPosition -= Position; Rotation = (float)Math.Atan2(playerPosition.Y, playerPosition.X); LinearVelocity = 2; }; Action policeDonutingAction = () => { IEnumerable <PlayerBullet> bullets = Game.Components.OfType <PlayerBullet>(); foreach (PlayerBullet bullet in bullets) { if (bullet.weaponType == WeaponType.DonutGun) { donutPosition = bullet.Position; donutPosition -= Position; Rotation = (float)Math.Atan2(donutPosition.Y, donutPosition.X); LinearVelocity = 2; } } }; //Transitions between States Func <bool> policeFindPlayerTransition = () => { return(PositionDifference.Length() <= 200); }; Func <bool> policeLosePlayerTransition = () => { return(PositionDifference.Length() > 200); }; Func <bool> policeFindDonutTransition = () => { IEnumerable <PlayerBullet> bullets = Game.Components.OfType <PlayerBullet>(); foreach (PlayerBullet bullet in bullets) { if (bullet.weaponType == WeaponType.DonutGun) { return(true); } } return(false); }; Func <bool> policeNoMoreDonutTransition = () => { IEnumerable <PlayerBullet> bullets = Game.Components.OfType <PlayerBullet>(); foreach (PlayerBullet bullet in bullets) { if (bullet.weaponType == WeaponType.DonutGun) { return(false); } } return(true); }; Func <bool> policeKillPedestrianTransition = () => { //currentPedestrianCount = Game.Components.OfType<Pedestrian>().Count(); //if (currentPedestrianCount < previousPedestrianCount) //{ // if (!pedestrianKillTimer.Running) // { // pedestrianKillTimer.Start(pedestrianKillTimeSpan); // pedestrianKilled = true; // } //} //update timer you idiot if (pedestrianKilled) { return(true); } else { return(false); } }; stateMachine.AddState(PoliceStates.Walking, policeWalkingAction); stateMachine.AddState(PoliceStates.Chasing, policeChasingAction); stateMachine.AddState(PoliceStates.Donuting, policeDonutingAction); stateMachine.AddTransition(PoliceStates.Walking, PoliceStates.Chasing, policeFindPlayerTransition); stateMachine.AddTransition(PoliceStates.Chasing, PoliceStates.Walking, policeLosePlayerTransition); stateMachine.AddTransition(PoliceStates.Walking, PoliceStates.Donuting, policeFindDonutTransition); stateMachine.AddTransition(PoliceStates.Chasing, PoliceStates.Donuting, policeFindDonutTransition); stateMachine.AddTransition(PoliceStates.Donuting, PoliceStates.Walking, policeNoMoreDonutTransition); stateMachine.AddTransition(PoliceStates.Chasing, PoliceStates.Walking, policeKillPedestrianTransition); }