enemy.gd 703 B

123456789101112131415161718192021222324252627282930
  1. class_name Enemy extends Node2D
  2. ## Movement speed in pixels per second.
  3. const MOVEMENT_SPEED = 75.0
  4. const DAMAGE_PER_SECOND = 15.0
  5. ## The node we should be "attacking" every frame.
  6. ## If [code]null[/code], nobody is in range to attack.
  7. var attacking: Player = null
  8. func _process(delta: float):
  9. if is_instance_valid(attacking):
  10. attacking.health -= delta * DAMAGE_PER_SECOND
  11. position.x += MOVEMENT_SPEED * delta
  12. # The enemy went outside of the window. Move it back to the left.
  13. if position.x >= 732:
  14. position.x = -32
  15. func _on_attack_area_body_entered(body: PhysicsBody2D):
  16. if body is Player:
  17. attacking = body
  18. func _on_attack_area_body_exited(_body: PhysicsBody2D):
  19. attacking = null