summaryrefslogtreecommitdiff
path: root/Units/miner.gd
blob: ac8d0b40aebdb43e5ced44be7906ca321d6cc3a2 (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
extends Unit


var direction := Vector2.RIGHT


func _ready():
	$AnimationPlayer.play("mine")
	scale = Vector2(direction.x, 1)
	
	$RayGround.force_raycast_update()
	$RayForward.force_raycast_update()


func _physics_process(delta: float) -> void:
	if not is_on_floor():
		velocity += get_gravity() * delta * Game.speed
	
	velocity.x = direction.x * 100 * delta * Game.speed
	
	if not $RayGround.is_colliding() and not $RayForward.is_colliding():
		var default = load("res://Units/Default.tscn").instantiate()
		default.global_position = global_position
		default.direction = direction
		get_tree().current_scene.add_child(default)
		queue_free()
	
	move_and_slide()


func mine() -> void:
	var points = []
	for point: Vector2 in $PickaxePolygon.polygon:
		points.append(
			global_position +
			($PickaxePolygon.position * Vector2(direction.x, 1)) +
			(point.rotated($PickaxePolygon.rotation) * Vector2(direction.x, 1))
		)
	
	(func():
		Game.erase_map(
			PackedVector2Array(points),
			get_tree().current_scene.get_node("GroundCollision"),
			get_tree().current_scene.get_node("Map")
		)
	).call_deferred()