-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathICBM.tscn
190 lines (130 loc) · 4.2 KB
/
ICBM.tscn
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
[gd_scene load_steps=4 format=2]
[ext_resource path="res://assets/icbm.png" type="Texture" id=1]
[sub_resource type="GDScript" id=1]
script/source = "# ICBM Scene
extends Node2D
var Explode = preload(\"res://Explode.tscn\")
var rng = RandomNumberGenerator.new()
var targets setget set_targets
signal icbm_hit
var attack_color = Color(0, 0, 100)
var phase = 0
var start_loc = Vector2(0, 0)
var end_loc = Vector2(0, 0)
var cur_loc = null
var missile_speed = 2
var mirv = false
var mirv_child = false
var mirv_target_y
var mirv_splits
var parent_start_loc
var parent_split_loc
var bomber_child = false
var mirv_can_split = true
var mirv_too_low = false
signal mirv_splitting(start_loc, end_loc, can_sub_split)
signal mirv_hit(splits)
var missile_dir = Vector2(0,0)
# Called when the node enters the scene tree for the first time.
func _ready():
rng.randomize()
if missile_speed == 0:
missile_speed = 4
if not mirv_child and not bomber_child:
var x = rng.randf_range(0, 1000)
start_loc = Vector2(x, 0)
var offset = randi() % targets.size()
end_loc = targets[offset] # Random base or city location
var viewport = get_viewport_rect().size
if mirv and start_loc.y + 10 < 5 * viewport.y / 8:
mirv_target_y = rng.randf_range(max(start_loc.y + 10, viewport.y / 4), 5 * viewport.y / 8)
elif mirv:
mirv_too_low = true
$ICBMArea.position = start_loc
var velocity = start_loc.direction_to(end_loc)
var angle = rad2deg(velocity.angle()) + 90
$ICBMArea.set_rotation_degrees(angle)
var _err = $ICBMArea.connect(\"area_entered\", self, \"icbm_hit\")
# Called every frame. 'delta' is the elapsed time since the previous frame.
# Use this to size the explosion...
func _process(_delta):
if phase == 0:
move_missile()
elif phase == 1:
on_explode()
func _draw():
if cur_loc != null && phase == 0:
# draw_line(start_loc, end_loc, Color(30,30,30)) # debug
draw_line(start_loc, cur_loc, attack_color)
func on_explode():
$ICBMArea/CollisionShape2D/Scud.visible = false
var explode_instance = Explode.instance()
explode_instance.position = $ICBMArea.position
add_child(explode_instance)
phase = 2
explode_instance.connect(\"explode_end\", self, \"_on_icbm_explode\")
update()
func move_missile():
var old_position = $ICBMArea.position
var velocity = start_loc.direction_to(end_loc) * missile_speed
cur_loc = old_position + velocity
$ICBMArea.position = cur_loc
update()
if mirv and mirv_can_split and not mirv_too_low and cur_loc.y > mirv_target_y:
mirv_can_split = false
var target
var mirv_targets = targets.duplicate(true) # Create clone to not mess with original array
var viewport = get_viewport_rect().size
for _i in range(mirv_splits):
target = randi() % mirv_targets.size()
emit_signal(\"mirv_splitting\", cur_loc, mirv_targets[target], cur_loc.y < (3 * viewport.y / 4))
mirv_targets.remove(target)
# on_end()
elif cur_loc.y > end_loc.y:
phase = 1
func icbm_hit(_object):
if phase == 0:
phase = 1
func _on_icbm_explode():
if mirv and mirv_can_split:
emit_signal(\"mirv_hit\", mirv_splits)
else:
emit_signal(\"icbm_hit\")
on_end()
func on_end():
queue_free()
func set_targets(new_targets):
targets = new_targets
func set_color(new_value):
attack_color = new_value
func set_speed(new_value):
missile_speed = new_value
func set_mirv(new_boolean):
mirv = new_boolean
func set_mirv_child(new_boolean):
mirv_child = new_boolean
func set_mirv_splits(new_value):
mirv_splits = new_value
func set_start_loc(new_value):
start_loc = new_value
func set_end_loc(new_value):
end_loc = new_value
func set_parent_start_loc(new_value):
parent_start_loc = new_value
func set_parent_split_loc(new_value):
parent_split_loc = new_value
func set_bomber_child(new_boolean):
bomber_child = new_boolean
"
[sub_resource type="RectangleShape2D" id=2]
extents = Vector2( 1.94781, 6.59768 )
[node name="ICBM" type="Node2D"]
script = SubResource( 1 )
[node name="ICBMArea" type="Area2D" parent="."]
collision_mask = 4
[node name="CollisionShape2D" type="CollisionShape2D" parent="ICBMArea"]
shape = SubResource( 2 )
[node name="Scud" type="Sprite" parent="ICBMArea/CollisionShape2D"]
position = Vector2( 0.118118, 0.833971 )
scale = Vector2( 0.3, 0.3 )
texture = ExtResource( 1 )