//constructor public AI_Object(BaseKeys keys, Vector2 position, Color color, float rotation, Vector2 scale, SpriteEffects effects, float layerDepth, Hero hero, GameObject enemy, Boolean isRandomAi) : base(position, color, rotation, scale, effects, layerDepth, hero) { this.keys = keys; Game1.event_update += update; velocity = new Vector2(0, 0); isJumping = false; isHitting = false; isDucking = false; isGameOver = false; HitCount = 0; positionBeforeJump = Vector2.Zero; isMovementAllowedRight = true; isMovementAllowedLeft = true; isInputAllowed = false; animationRandomizer = new Random(); aiRandom = new Random(); movementChangeRate = ((float)randomMovementDirection.Next(2, 10)) / 10; this.isRandomAi = isRandomAi; this.enemy = enemy; }
void JumpingRandom(GameTime gameTime) { if (!jumpingGotRandom) // randomize a jumping random { jumpRandom = jumpRandomizer.Next(0, 101); jumpingGotRandom = true; } if (jumpRandom < 15) // 15 percent chance to jump { jump = true; } if (jump) // if jumping, wait until the timer expires then request a new random { jumpingTimer += (float)gameTime.ElapsedGameTime.TotalSeconds; if (jumpingTimer > jumpRate) { jump = false; this.keys = new BotKeys(false, false, false, false, false); jumpingTimer = 0; jumpingGotRandom = false; } } else // wait until the timer expires and then request a new timer if the ai randomized not to jum { jumpingTimer += (float)gameTime.ElapsedGameTime.TotalSeconds; if (jumpingTimer > jumpRate) { jumpingTimer = 0; jumpingGotRandom = false; } } }
/// <summary> /// making the ai hit /// </summary> void Hit() { this.keys = new BotKeys(false, false, false, true, false); attackingTimer += (float)Global.gameTime.ElapsedGameTime.TotalSeconds; if (attackingTimer > attackingRate) { this.keys = new BotKeys(false, false, false, false, false); attackingTimer = 0; } }
/// <summary> /// making the ai jump /// </summary> void Jump() { jump = true; RandomMovement(Global.gameTime); if (jump) // if jumping, wait until the timer expires then request a new random { jumpingTimer += (float)Global.gameTime.ElapsedGameTime.TotalSeconds; if (jumpingTimer > jumpRate) { jump = false; this.keys = new BotKeys(false, false, false, false, false); jumpingTimer = 0; } } }
/// <summary> /// if the enemy is likely to hit, the ai has a 50 percent chance to either hit back or duck the hit. /// </summary> void DuckOrHit() { if (!duckOrAttackGotRandom && duckOrAttackRandom.Next(0, 101) < 50) { Hit(); } else { this.keys = new BotKeys(false, false, false, false, true); duckOrAttackGotRandom = true; if (duckingTimer > duckingRate) { this.keys = new BotKeys(false, false, false, false, false); duckingTimer = 0; duckOrAttackGotRandom = false; } } }
//GameObject constructor public OnlineChar(Vector2 position, Color color, float rotation, Vector2 scale, SpriteEffects effects, float layerDepth, Hero hero) : base(position, color, rotation, scale, effects, layerDepth, hero) { this.keys = new BotKeys(false, false, false, false, false); velocity = new Vector2(0, 0); isJumping = false; isHitting = false; isDucking = false; isGameOver = false; HitCount = 0; positionBeforeJump = Vector2.Zero; isMovementAllowedRight = true; isMovementAllowedLeft = true; isInputAllowed = true; animationRandomizer = new Random(); }
//GameObject constructor public GameObject(BaseKeys keys, Vector2 position, Color color, float rotation, Vector2 scale, SpriteEffects effects, float layerDepth, Hero hero, Boolean isAI) : base(position, color, rotation, scale, effects, layerDepth, hero) { this.keys = keys; Game1.event_update += update; velocity = new Vector2(0, 0); isJumping = false; isHitting = false; isDucking = false; isGameOver = false; HitCount = 0; positionBeforeJump = Vector2.Zero; isMovementAllowedRight = true; isMovementAllowedLeft = true; isInputAllowed = false; animationRandomizer = new Random(); }
void DuckingRandom(GameTime gameTime) { if (enemy.isHitting && closeCombat) // if the enemy is hitting in close combat { if (!duckingGotRandom) // generate a random for the ducking { duckingRandom = aiRandom.Next(0, 101); duckingGotRandom = true; } if (duckingRandom < 75) // 75 percent chance to duck { ducking = true; this.keys = new BotKeys(false, false, false, false, true); } } if (ducking) // if the randomizer chose ducking, duck until the ducking timer expires, then request a new random { duckingTimer += (float)gameTime.ElapsedGameTime.TotalSeconds; if (duckingTimer > duckingRate) { ducking = false; this.keys = new BotKeys(false, false, false, false, false); duckingTimer = 0; duckingGotRandom = false; } } else // if not ducking, do not duck until the ducking timer expires and then request a new random { duckingTimer += (float)gameTime.ElapsedGameTime.TotalSeconds; if (duckingTimer > duckingRate) { duckingTimer = 0; duckingGotRandom = false; } } }
void AttackRandom(GameTime gameTime) { if (!attackGotRandom) // generate a random for the attack { attackRandom = attackRandomizer.Next(0, 101); attackGotRandom = true; } if (attackRandom < 60) // 60 percent chance to attack { attacking = true; this.keys = new BotKeys(false, false, false, true, false); } else { RandomMovement(Global.gameTime); //if attacking was not the result of the random, move the character } if (attacking) // if the randomizer chose attacking, attack until the attacking timer expires, then request a new random { attackingTimer += (float)gameTime.ElapsedGameTime.TotalSeconds; if (attackingTimer > attackingRate) { attacking = false; this.keys = new BotKeys(false, false, false, false, false); attackingTimer = 0; attackGotRandom = false; } } else // if not attacking, do not attack until the attacking timer expires and then request a new random { attackingTimer += (float)gameTime.ElapsedGameTime.TotalSeconds; if (attackingTimer > attackingRate) { attackingTimer = 0; attackGotRandom = false; } } }
void RandomMovement(GameTime gameTime) { if (!movementGotRandom) // randomize a movement direction { movementRandom = randomMovementDirection.Next(0, 101); movementGotRandom = true; } if (this.Position.X > this.enemy.Position.X) { if (movementRandom < 90) // 90% chance to move in the direction of the enemy, 10% chance to move backwards { if (jump) { this.keys = new BotKeys(true, false, true, false, false); //if the character is jumping, jump while moving } else { this.keys = new BotKeys(true, false, false, false, false); } } else { if (jump) { this.keys = new BotKeys(false, true, true, false, false); //if the character is jumping, jump while moving } else { this.keys = new BotKeys(false, true, false, false, false); } } } else { if (movementRandom < 90) // 90% chance to move in the direction of the enemy, 10% chance to move backwards { if (jump) { this.keys = new BotKeys(false, true, true, false, false); //if the character is jumping, jump while moving } else { this.keys = new BotKeys(false, true, false, false, false); } } else { if (jump) { this.keys = new BotKeys(true, false, true, false, false); //if the character is jumping, jump while moving } else { this.keys = new BotKeys(true, false, false, false, false); } } } movementChangeTimer += (float)gameTime.ElapsedGameTime.TotalSeconds; // increase the movement timer if (movementChangeTimer > movementChangeRate) // if the movement timer reached its rate { movementChangeRate = ((float)randomMovementDirection.Next(2, 10)) / 10; //radnomize another rate between 0.2 seconds and a second movementChangeTimer = 0; // reset the timer movementGotRandom = false; //request a new random //here we make the ai randomize a new direction every time the random rate ends } }