summaryrefslogtreecommitdiff
path: root/Enemies/walking_enemy.gd
blob: be36e2d469860567dfad97be27d200952f36aed5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
extends Enemy


@export var acceleration: float = 10.0

enum MovementType {
	BACK_FORTH,
	FORWARD,
}
@export var movement_type: MovementType = MovementType.BACK_FORTH

@export var back_forth_time: float = 1.0


func _ready() -> void:
	super._ready()
	
	if movement_type == MovementType.BACK_FORTH:
		$BackForthTimer.wait_time = back_forth_time
		$BackForthTimer.start()


func _physics_process(delta: float) -> void:
	$AnimatedSprite2D.play()
	
	if not is_on_floor():
		velocity += get_gravity() * delta
	
	velocity.x = move_toward(velocity.x, direction * speed, acceleration)
	
	move_and_slide()
	if get_last_slide_collision():
		if $RayLeft.is_colliding():
			direction = 1
		elif $RayRight.is_colliding():
			direction = -1


func _on_back_forth_timer_timeout() -> void:
	direction = -direction