summaryrefslogtreecommitdiff
path: root/player/opponent.gd
blob: 7fc2ebff2ac450bbdaf6cc0c268bd1d6686167d6 (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
55
56
57
extends Character


var current_direction := Direction.Left

@export var random_start_type := false


func _ready() -> void:
	super._ready()
	
	var rng = RandomNumberGenerator.new()
	rng.seed = ((get_tree().current_scene as Stage).scene_file_path as String).md5_buffer().decode_u64(0) + Time.get_ticks_msec()
	
	$AnimatedSprite2D.modulate = Color(max(0.1, rng.randf()), max(0.1, rng.randf()), max(0.1, rng.randf()))
	
	speed_modifier = 0.5
	
	if random_start_type:
		current_type = Type.values().pick_random()
	
	idle()


func _process(_delta: float) -> void:
	if current_state == State.Idle or current_state == State.Walk:
		walk(current_direction)
		
		if is_on_wall(current_direction):
			change_direction()
		
		if not is_on_floor():
			fall()


func is_below_ledge() -> bool:
	return $RayUpLeft2.is_colliding() or $RayUpRight2.is_colliding()


func change_direction():
	if current_direction == Direction.Left:
		current_direction = Direction.Right
	else:
		current_direction = Direction.Left


func _on_vertical_timer_timeout() -> void:
	if current_state == State.Idle or current_state == State.Walk:
		if randi_range(0, 100) > 75:
			change_direction()
		
		if randi_range(0, 100) > 75 and is_below_ledge():
			await jump()
		elif randi_range(0, 100) > 50 and not is_on_bottom_floor():
			await fall()
	
	$VerticalTimer.start()