Files
godot-demo-projects/loading/serialization/enemy.gd
Hugo Locurcio bac1e69164 Use static typing in all demos (#1063)
This leads to code that is easier to understand and runs
faster thanks to GDScript's typed instructions.

The untyped declaration warning is now enabled on all projects
where type hints were added. All projects currently run without
any untyped declration warnings.

Dodge the Creeps and Squash the Creeps demos intentionally don't
use type hints to match the documentation, where type hints haven't
been adopted yet (given its beginner focus).
2024-06-01 12:12:18 +02:00

30 lines
725 B
GDScript

class_name Enemy
extends Node2D
## Movement speed in pixels per second.
const MOVEMENT_SPEED = 75.0
const DAMAGE_PER_SECOND = 15.0
## The node we should be "attacking" every frame.
## If [code]null[/code], nobody is in range to attack.
var attacking: Player = null
func _process(delta: float) -> void:
if is_instance_valid(attacking):
attacking.health -= delta * DAMAGE_PER_SECOND
position.x += MOVEMENT_SPEED * delta
# The enemy went outside of the window. Move it back to the left.
if position.x >= 732:
position.x = -32
func _on_attack_area_body_entered(body: PhysicsBody2D) -> void:
if body is Player:
attacking = body
func _on_attack_area_body_exited(_body: PhysicsBody2D) -> void:
attacking = null