-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tie.gd
93 lines (80 loc) · 2.31 KB
/
Tie.gd
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
extends Area2D
signal hit
export var speed = 300
export var h_speed = 200
export var fire_delay = 0.7
export var turn_delay = 0.4
var screen_size
var TieExplosion = preload("res://TieExplosion.tscn")
var TieBlast = preload("res://TieBlast.tscn")
var dead = false
var ms_since_last_shot = 0.5
var ms_since_dir_change = 0
var last_shot_side
var h_dir
# Called when the node enters the scene tree for the first time.
func _ready():
screen_size = get_viewport_rect().size
position.x = randi() % int(screen_size.x)
position.y = -30
if randi() % 2 == 0:
h_dir = 1
last_shot_side = 0
else:
h_dir = -1
last_shot_side = 1
# Called every frame. 'delta' is the elapsed time since the previous framase.
func _process(delta):
if !dead:
ms_since_dir_change += delta
ms_since_last_shot += delta
position.y += speed * delta
position.x += h_speed * delta * h_dir
position.x = clamp(position.x, 8, screen_size.x - 40)
if ms_since_dir_change >= turn_delay && (randi() % 90 == 0):
ms_since_dir_change = 0
if h_dir == 1:
h_dir = -1
else:
h_dir = 1
if position.x >= screen_size.x - 40:
h_dir = -1
if position.x <= 8:
h_dir = 1
if ms_since_last_shot > fire_delay && (randi() % 65 == 0) && position.y < (screen_size.y / 2):
_fire_shot($Blasters/BlasterSound1)
_fire_shot($Blasters/BlasterSound1)
$Blasters/Timer.start()
func _on_VisibilityNotifier2D_screen_entered():
$AudioStreamPlayer2D.play()
func _on_VisibilityNotifier2D_screen_exited():
queue_free()
func _on_Tie_body_entered(body):
_die()
body.queue_free()
func _fire_shot(blaster_sound):
ms_since_last_shot = 0
var new_blaster = TieBlast.instance()
new_blaster.position.x = self.position.x - 10 + (last_shot_side * 20)
new_blaster.position.y = self.position.y + 45
$Blasters.add_child(new_blaster)
blaster_sound.stop()
blaster_sound.play()
if (last_shot_side == 1):
last_shot_side = 0
else:
last_shot_side = 1
func _on_Timer_timeout():
_fire_shot($Blasters/BlasterSound2)
_fire_shot($Blasters/BlasterSound2)
func _on_Tie_area_entered(area):
if "Xwing" in area.name:
_die()
func _die():
$AnimatedSprite.hide()
emit_signal("hit", position)
# Must be deferred as we can't change physics properties on a physics callback.
$CollisionPolygon2D.set_deferred("disabled", true)
if !dead:
add_child(TieExplosion.instance())
dead = true