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 override void DoPhysics() { Input.Cursor.Update(gameTime); // Rotation regarding the cursor class _physics.rotation = OwnMath.CalculateAngleBetweenPoints(Position.ToPoint(), Input.Cursor.Position.ToPoint()); // Make sure that the player does not go out of bounds Position = new Vector2(MathHelper.Clamp(Position.X, 80, gameWidth - 80), MathHelper.Clamp(Position.Y, 80, gameHeight - 80)); }
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; }