-
Notifications
You must be signed in to change notification settings - Fork 0
/
villager.gd
47 lines (34 loc) · 893 Bytes
/
villager.gd
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
extends CharacterBody2D
enum {
IDLE,
NEW_DIR,
MOVE
}
const SPEED = 30;
var current_state = IDLE;
var current_dir = Vector2.RIGHT;
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity: int = ProjectSettings.get_setting("physics/2d/default_gravity")
func _ready() -> void:
randomize();
func _process(delta: float) -> void:
match current_state:
IDLE:
pass;
NEW_DIR:
current_dir = _choose([Vector2.RIGHT, Vector2.LEFT]);
MOVE:
_move(delta);
func _physics_process(delta: float) -> void:
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta
move_and_slide()
func _move(delta):
position += current_dir * SPEED * delta;
func _choose(array):
array.shuffle();
return array.front();
func _on_timer_timeout() -> void:
$Timer.wait_time = _choose([1, 2, 3]);
current_state = _choose([IDLE, NEW_DIR, MOVE]);