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


var direction := Vector2.RIGHT

var last_position: Vector2


func _ready():
	$AnimationPlayer.play("mine")
	scale = Vector2(direction.x, 1)
	
	$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 $RayForward.is_colliding():
		# TODO: erase step gerade aus
		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()
	
	last_position = global_position
	move_and_slide()
	
	if last_position == global_position: # jumping over fraction pixel collisions, left over after mining
		position.y -= 10 * delta * Game.speed
		position.x += 10 * direction.x * delta * Game.speed


func mine() -> void:
	var points = []
	for point: Vector2 in $Polygon.polygon:
		points.append(
			global_position +
			($Polygon.position * Vector2(direction.x, 1)) +
			(point.rotated($Polygon.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()