public void UpdateState(Map gameMap) { // pick the nearest enemy and shoot if (gameMap.Enemies.Count == 0) { return; } float minDist = float.MaxValue; IHasHp nearestEnemy = null; foreach (IEnemy enemy in gameMap.Enemies) { float dist = (Position - enemy.Position).LengthSquared(); if (minDist == float.MaxValue || dist < minDist) { nearestEnemy = enemy; minDist = dist; } } Attack(nearestEnemy); long now = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond; if (shooting && now > stopShootingAnimation) { shooting = false; Shot = null; } }
/// <summary> /// Checks if it's time for attack and attacks the given entity. /// </summary> /// <param name="entity">Entity to attack.</param> private void Attack(IHasHp entity) { if (NextAttack == NO_ATTACK) { entity.TakeHit(Damage); NextAttack = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond + (long)(1000 / AttackSpeed); shooting = true; stopShootingAnimation = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond + 250; Shot = new Point[] { Position.ToPoint() + ShootingPoint, entity.Position.ToPoint() + entity.Center }; } else { long now = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond; if (now >= NextAttack) { entity.TakeHit(Damage); NextAttack = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond + (long)(1000 / AttackSpeed); shooting = true; stopShootingAnimation = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond + 250; Shot = new Point[] { Position.ToPoint() + ShootingPoint, entity.Position.ToPoint() + entity.Center }; } } }
/// <summary> /// Checks if it's time for attack and attacks the given entity. /// </summary> /// <param name="entity">Entity to attack.</param> private void Attack(IHasHp entity) { if (NextAttack == NO_ATTACK) { entity.TakeHit(Damage); NextAttack = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond + (long)(1000 / AttackSpeed); } else { long now = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond; if (now >= NextAttack) { entity.TakeHit(Damage); NextAttack = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond + (long)(1000 / AttackSpeed); } } }