summaryrefslogtreecommitdiff
path: root/Units/default.gd
blob: bd571a8b8155bdf99655687d078387c80fe160aa (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
class_name UnitDefault
extends Unit


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


func _physics_process(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 $RayWallLeft.is_colliding():
		direction = Vector2.RIGHT
	elif $RayWallRight.is_colliding():
		direction = Vector2.LEFT
	
	if is_on_floor():
		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
	
	move_and_slide()


func _on_input_event(_viewport: Node, event: InputEvent, _shape_idx: int) -> void:
	if event.is_action_pressed("select") and Game.selected_unit_type:
		get_viewport().set_input_as_handled()
		
		var unit = Game.selected_unit_type.instantiate()
		unit.position = global_position
		
		if "direction" in unit:
			unit.direction = direction
		
		get_tree().current_scene.add_child(unit)
		queue_free()