summaryrefslogtreecommitdiff
path: root/Units/climber.gd
blob: 5c1f0c2d25ca827b154ccd9246bdf010c7681cc2 (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 Unit


var speed := 1000.0
var direction := Vector2.RIGHT

var was_climbing := false
var post_climb_position_fix := 3


func _physics_process(delta: float) -> void:
	if $RayWallLeft.is_colliding() or $RayWallRight.is_colliding():
		process_climbing(delta)
	elif was_climbing:
		var default = load("res://Units/Default.tscn").instantiate()
		default.global_position = global_position + (post_climb_position_fix * direction)
		default.direction = direction
		get_tree().current_scene.add_child(default)
		queue_free()
	else:
		process_walking(delta)
	
	move_and_slide()


func process_walking(delta: float) -> void:
	if not is_on_floor():
		velocity.x = 0
		velocity += get_gravity() * delta * Game.speed
		if velocity.y > (get_gravity().y / 10):
			$AnimatedSprite.play("fall")
	
	if is_on_floor():
		if $RayWallLeft.is_colliding() or $RayHeadLeft.is_colliding():
			direction = Vector2.RIGHT
		elif $RayWallRight.is_colliding() or $RayHeadRight.is_colliding():
			direction = Vector2.LEFT
		
		if (
			($RaySlopeLeft.is_colliding() and not $RayWallLeft.is_colliding()) or
			($RaySlopeRight.is_colliding() and not $RayWallRight.is_colliding())
		):
			position.y -= 10 * delta * Game.speed
			position.x += 10 * direction.x * delta * Game.speed
		
		velocity.x = direction.x * speed * delta * Game.speed
		
		$AnimatedSprite.play("walk")
		$AnimatedSprite.flip_h = direction.x < 0


func process_climbing(delta: float) -> void:
	was_climbing = true
	
	velocity.y = -((speed / 2) * delta * Game.speed)
	
	$AnimatedSprite.play("climb")