enemy.gd 725 B

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