public override void DoPhysics() { // get direction to the current target Vector2 direction = OwnMath.GetDirectionToPoint(Position.ToPoint(), targetPos.ToPoint()); // get the angle between the zombie and the player float angle = OwnMath.CalculateAngleBetweenPoints(Position.ToPoint(), targetPos.ToPoint()); float distance = direction.Length(); // distance equals the length of the vector direction.Normalize(); // normalize the direction to units // move the zombie and set the animation if the zombie didnt reach the player // basically follow the player if (distance > ATTACK_DISTANCE) { currentAnimationKey = MOVE_ANIMATION_KEY; _physics.position.X += direction.X * _physics.speed; _physics.position.Y += direction.Y * _physics.speed; } else // if reached invoke the attack according to the fire rate { currentAnimationKey = ATTACK_ANIMATION_KEY; // set the animation to attack attackTimer += gameTime.ElapsedGameTime.Milliseconds; // increase time if (attackTimer >= ATTACK_ANIMATION_TIMER) { // Attack zombieAttack.Invoke(); attackTimer = 0; // reset timer after reaching max } } _physics.rotation = angle; }
public Bullet(Texture2D Texture, Player player) { Random r = new Random(); this.Texture = Texture; this.player = player; damage = player.Damage; Console.WriteLine(damage); // spawn from the origin of the player Position = player.Position; // calculcate which direction the bullet will go direction = OwnMath.GetDirectionToPoint(player.Position.ToPoint(), player.Input.Cursor.Position.ToPoint()); // get the rotation to the mouse pointer (equal be the player._physics.rotation) rotation = OwnMath.CalculateAngleBetweenPoints(player.Position.ToPoint(), player.Input.Cursor.Position.ToPoint()); // default speed speed = 5f; }
public void Update(GameTime gameTime) { SpawnZombie(gameTime); // if any blood is animated then we make it disapear bloodAnimations.RemoveAll((b) => { return(b.Active == false); }); // animate the bullets the player fired foreach (var bullet in bulletsFired) { bullet.Update(gameTime); } // animate all blood that is activated foreach (var blood in bloodAnimations) { blood.Update(gameTime); } // checks and remove the zombie with 0 health (or dead) from the list for (int i = 0; i < zombies.Count; i++) { if (!zombies[i].IsAlive) { // increase kill points zombiesKilledCounter++; // remove zombie from zombie list zombies.RemoveAt(i); // when removing from a list, the zombie count decreases i--; } } // update the zombie (important to do it after, so we wont animate the dead zombies) foreach (var zombie in zombies) { zombie.Update(gameTime, currentPlayer.Position); } // Checks the collision between the bullets and the zombies for (int i = 0; i < bulletsFired.Count; i++) { // the current bullet var bullet = bulletsFired[i]; for (int j = 0; j < zombies.Count; j++) { // current zombie var zombie = zombies[j]; // the distance between the zombie and the bullet var dist = OwnMath.GetDirectionToPoint(bullet.Position.ToPoint(), zombie.Position.ToPoint()); // if the distance is less than 20 (about zombie width) // and the bullet is not removed already for other reason if (dist.Length() < 20 && bulletsFired.Count > i) { // bullet hits, add points to the fighter ability charge currentPlayer.abilityCharge += (int)bullet.damage; bulletsFired.RemoveAt(i); zombie.TakeDamage(bullet.damage); bloodAnimations.Add(new Blood(Content, zombie.Position)); } } for (int j = 0; j < CurrentRoom.collideables.Count; j++) { var col = CurrentRoom.collideables[j]; if (bullet.Rectangle.Intersects(col.Rectangle)) { bulletsFired.Remove(bullet); if (col.TakeDamage()) { CurrentRoom.collideables.Remove(col); if (col.Type == Component.ObjectType.SPAWN_POINT) { CurrentRoom.spawnPlaces.Remove(col.Position); } } } } // if the bullet reached the screen bounds, remove it from the list if (bullet.Position.X > width - 50 || bullet.Position.Y > height - 50 || bullet.Position.X < 50 || bullet.Position.Y < 50) { // if the bullet is not removed already for other reason if (bulletsFired.Count > i) { bulletsFired.RemoveAt(i); } } } if (CurrentRoom.DoorOpened) { Rectangle r1 = new Rectangle((int)currentPlayer.Position.X, (int)currentPlayer.Position.Y, currentPlayer.GetAnimation().Width / 2, currentPlayer.GetAnimation().Height / 2); if (r1.Intersects(CurrentRoom.Exit.Rectangle)) { Data.SetScore(Data.GetScore() + zombiesKilledCounter); CurrentRoom.LoadMap(); ResetGame(); } } }