summaryrefslogtreecommitdiff
path: root/scenes/levels/pixel/PixelDefender.gd
blob: 5f8d5167ea35c7cdf0645e692b22cdcf5ed62e6c (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
extends Level


const _bullet_scene = preload("res://scenes/levels/pixel/bullet.tscn")
const _enemy_scene = preload("res://scenes/levels/pixel/spaceship.tscn")


func _new_bullet(velocity, team) -> Bullet:
	var bullet = _bullet_scene.instance()
	bullet.velocity = velocity
	bullet.team = team
	$BulletHolder.add_child(bullet)
	return bullet

func _new_enemy():
	var enemy = _enemy_scene.instance()
	enemy.position = Vector2(500 + randi() % 80, (randf() * 600) - 350)
	enemy.scale.x *= -1
	enemy.team = Bullet.CHAOS
	$Enemies.add_child(enemy)
	enemy.connect("area_shape_entered", self, "_on_enemy_area_shape_entered", [enemy])
	return enemy

func start():
	.start()
	t = 0
	$Enemies.get_children().clear()
	var enemy_count = 6
	if Settings.difficulty == Settings.Difficulty.HARD:
		enemy_count += 7
	for i in range(5):
		_new_enemy()

var t = 0
var bullet_timer = 1.5
var enemy_speed = 1

func _process(delta):
	if not running:
		return
	t += delta
	
	while t > bullet_timer:
		t -= bullet_timer
		var b = _new_bullet(10, Bullet.ORDER)
		b.position = $spaceship.position + Vector2(16, 0)
		BackgroundMusic.bullet_sound()
		for e in $Enemies.get_children():
			b = _new_bullet(10, Bullet.CHAOS)
			b.position = e.position - Vector2(16, 0)
	if $Enemies.get_child_count() == 0:
		finish_level()
		return
	for e in $Enemies.get_children():
		e.position -= Vector2(enemy_speed, 0)
		if e.position.x < $spaceship.position.x:
			lose_all_hearts()
			return
	
	$spaceship.position.y = max(0, min(get_viewport().get_mouse_position().y, get_viewport_rect().size.y)) - 360

func _on_enemy_area_shape_entered(area_id, area, area_shape, local_shape, e):
	if area is Bullet and area.team == Bullet.CHAOS:
		return
	if area is Bullet:
		area.queue_free()
	elif area is Spaceship and area.team == Bullet.ORDER:
		e.queue_free()

func _on_spaceship_area_shape_entered(area_id, area, area_shape, local_shape):
	if area is Bullet:
		if area.team == Bullet.CHAOS:
			lose_heart()
	else:
		lose_all_hearts()