summaryrefslogtreecommitdiff
path: root/guide.gd
blob: aae9deb898ad5c7de95f4b8a645354a15e99ac25 (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
extends Area2D


@export var force: float = 10.0

var current_direction := Vector2.UP: set = set_current_direction
var base_gravity: float = gravity


func set_current_direction(value: Vector2) -> void:
	current_direction = value
	
	gravity_direction = current_direction
	gravity = base_gravity
	
	if current_direction == Vector2.UP:
		$AnimationPlayer.play("up")
		gravity = base_gravity * 0.25
	elif current_direction == Vector2.DOWN:
		$AnimationPlayer.play("down")
	elif current_direction == Vector2.LEFT:
		$AnimationPlayer.play("left")
	elif current_direction == Vector2.RIGHT:
		$AnimationPlayer.play("right")


func get_impulse() -> Vector2:
	return Vector2(force, force)


func apply_guiding_impulse(body: Node2D) -> void:
	if (
		current_direction == Vector2.LEFT or
		current_direction == Vector2.RIGHT or
		(current_direction == Vector2.UP and body.global_position.y > global_position.y)
	):
		body.apply_central_impulse(
			(global_position - body.global_position).normalized() *
			get_impulse()
		)


func _on_timer_timeout() -> void:
	current_direction = [
		Vector2.UP,
		Vector2.DOWN,
		Vector2.LEFT,
		Vector2.RIGHT,
	].pick_random()
	$Timer.wait_time = randf_range(5.0, 10.0)


func _on_body_entered(body: Node2D) -> void:
	apply_guiding_impulse(body)