Skip to content

Commit 704f43e

Browse files
committed
feat: aircraft dropping boxes
1 parent 877563d commit 704f43e

34 files changed

+713
-54
lines changed

Aircraft.gd

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
extends Area2D
2+
3+
export (PackedScene) var Box
4+
5+
var speed = 400
6+
7+
var obstacles = 0
8+
var dropAllowed = false
9+
var dropped = false
10+
11+
func _ready():
12+
$DropTimer.start(0.9 + (0.2 - randf()*0.4))
13+
14+
func _physics_process(delta):
15+
position += -transform.y * speed * delta
16+
if not dropped and dropAllowed and obstacles == 0:
17+
drop()
18+
19+
func _on_VisibilityNotifier2D_screen_exited():
20+
queue_free()
21+
22+
func drop():
23+
var box = Box.instance()
24+
box.global_position = global_position
25+
box.apply_central_impulse(-transform.y * 500)
26+
box.rotation = rotation + rand_range(-PI / 2, PI / 2)
27+
var rnd = randf()
28+
if rnd < 0.5:
29+
box.setContent(1)
30+
elif rnd < 0.9:
31+
box.setContent(2)
32+
else:
33+
box.setContent(3)
34+
box.z_index = z_index - 1
35+
box.collision_layer = 128
36+
box.collision_mask = 128
37+
box.set_angular_velocity(5)
38+
get_parent().add_child(box)
39+
box.setScale(Vector2(1.5, 1.5))
40+
dropped = true
41+
42+
func _on_DropTimer_timeout():
43+
dropAllowed = true
44+
45+
func _on_Aircraft_body_entered(body):
46+
obstacles += 1
47+
48+
func _on_Aircraft_body_exited(body):
49+
obstacles -= 1

Aircraft.tscn

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
[gd_scene load_steps=5 format=2]
2+
3+
[ext_resource path="res://images/plane.png" type="Texture" id=1]
4+
[ext_resource path="res://Aircraft.gd" type="Script" id=2]
5+
[ext_resource path="res://Box.tscn" type="PackedScene" id=3]
6+
7+
[sub_resource type="RectangleShape2D" id=1]
8+
extents = Vector2( 15.8861, 14.1277 )
9+
10+
[node name="Aircraft" type="Area2D"]
11+
z_index = 10
12+
script = ExtResource( 2 )
13+
Box = ExtResource( 3 )
14+
15+
[node name="Sprite" type="Sprite" parent="."]
16+
scale = Vector2( 0.063, 0.063 )
17+
texture = ExtResource( 1 )
18+
19+
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
20+
position = Vector2( -0.374573, -82.9523 )
21+
scale = Vector2( 2.04304, 1.96886 )
22+
shape = SubResource( 1 )
23+
24+
[node name="DropTimer" type="Timer" parent="."]
25+
one_shot = true
26+
27+
[connection signal="body_entered" from="." to="." method="_on_Aircraft_body_entered"]
28+
[connection signal="body_exited" from="." to="." method="_on_Aircraft_body_exited"]
29+
[connection signal="timeout" from="DropTimer" to="." method="_on_DropTimer_timeout"]

Barrel.gd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
extends RigidBody2D
2+
3+
4+
func _on_PickArea_body_entered(body):
5+
if body.is_in_group("tank") and body.life > 0:
6+
queue_free()

Barrel.tscn

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[gd_scene load_steps=5 format=2]
2+
3+
[ext_resource path="res://images/nitro.png" type="Texture" id=1]
4+
[ext_resource path="res://Barrel.gd" type="Script" id=2]
5+
6+
[sub_resource type="RectangleShape2D" id=1]
7+
extents = Vector2( 2.76727, 4.33193 )
8+
9+
[sub_resource type="CapsuleShape2D" id=2]
10+
radius = 8.06976
11+
height = 33.491
12+
13+
[node name="Barrel" type="RigidBody2D"]
14+
script = ExtResource( 2 )
15+
16+
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
17+
scale = Vector2( 3.12, 2.6 )
18+
shape = SubResource( 1 )
19+
20+
[node name="Sprite" type="Sprite" parent="."]
21+
scale = Vector2( 0.063, 0.063 )
22+
texture = ExtResource( 1 )
23+
24+
[node name="PickArea" type="Area2D" parent="."]
25+
26+
[node name="CollisionShape2D" type="CollisionShape2D" parent="PickArea"]
27+
scale = Vector2( 2.06014, -0.801646 )
28+
shape = SubResource( 2 )
29+
30+
[connection signal="body_entered" from="PickArea" to="." method="_on_PickArea_body_entered"]

Box.gd

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
extends RigidBody2D
2+
3+
export (PackedScene) var Shell
4+
5+
enum Content { SHELL=0, SMALL_SHELLS=1, BIG_SHELL=2, SPRING=3 }
6+
7+
var content = null
8+
var targetScale = Vector2(1, 1)
9+
var scaleDelta = Vector2(0.4, 0.4)
10+
var blinks = 5
11+
var shells = 0
12+
var life = 8
13+
14+
func _ready():
15+
content = $Sprite/Content.frame
16+
17+
func setScale(scale):
18+
self.scale = scale
19+
scaleDelta = scale - targetScale
20+
21+
func _physics_process(delta):
22+
if scale.length() > 1:
23+
var ratio = $FallTimer.time_left / $FallTimer.wait_time
24+
scale = targetScale + ratio * scaleDelta
25+
26+
func setContent(content):
27+
$Sprite/Content.frame = content
28+
self.content = content
29+
30+
func hit(damage, source):
31+
life -= damage
32+
if life < 0:
33+
if content != Content.SPRING:
34+
fire(source)
35+
queue_free()
36+
37+
func fire(source, angle=0):
38+
var s = Shell.instance()
39+
if content == Content.BIG_SHELL:
40+
s.setBig()
41+
elif content == Content.SMALL_SHELLS:
42+
s.setSmall()
43+
if angle == 0:
44+
fire(source, randf() * PI + PI / 3)
45+
fire(source, -randf() * PI + PI / 3)
46+
s.transform = transform
47+
s.rotate(angle)
48+
get_parent().add_child(s)
49+
s.setSource(source)
50+
51+
func _on_PickArea_body_entered(body):
52+
if body.is_in_group("tank") and body.life > 0:
53+
collision_layer = 256
54+
collision_mask = 256
55+
$PickTimer.start()
56+
z_index = 0
57+
if content == Content.SPRING:
58+
body.addSpring()
59+
if content == Content.SHELL:
60+
body.addPoints(shells)
61+
if content == Content.BIG_SHELL:
62+
body.addBigShell()
63+
if content == Content.SMALL_SHELLS:
64+
body.addSmallShells()
65+
66+
func _on_FallTimer_timeout():
67+
z_index = 1
68+
collision_layer = 1
69+
set_angular_velocity(0)
70+
71+
func _on_PickTimer_timeout():
72+
if blinks % 2:
73+
modulate.a = 0
74+
else:
75+
modulate.a = 1
76+
blinks -= 1
77+
if blinks == 0:
78+
queue_free()

Box.tscn

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
[gd_scene load_steps=10 format=2]
2+
3+
[ext_resource path="res://images/box.png" type="Texture" id=1]
4+
[ext_resource path="res://images/shell.png" type="Texture" id=2]
5+
[ext_resource path="res://images/small-shells.png" type="Texture" id=3]
6+
[ext_resource path="res://images/spring.png" type="Texture" id=4]
7+
[ext_resource path="res://images/big-shell.png" type="Texture" id=5]
8+
[ext_resource path="res://Box.gd" type="Script" id=6]
9+
[ext_resource path="res://Shell.tscn" type="PackedScene" id=7]
10+
11+
[sub_resource type="SpriteFrames" id=1]
12+
animations = [ {
13+
"frames": [ ExtResource( 2 ), ExtResource( 3 ), ExtResource( 5 ), ExtResource( 4 ) ],
14+
"loop": true,
15+
"name": "default",
16+
"speed": 5.0
17+
} ]
18+
19+
[sub_resource type="CapsuleShape2D" id=3]
20+
radius = 10.7314
21+
height = 44.8859
22+
23+
[node name="Box" type="RigidBody2D" groups=[
24+
"heavy",
25+
"target",
26+
]]
27+
z_index = 1
28+
z_as_relative = false
29+
script = ExtResource( 6 )
30+
Shell = ExtResource( 7 )
31+
32+
[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="."]
33+
position = Vector2( 0, -2 )
34+
scale = Vector2( 0.063, 0.063 )
35+
polygon = PoolVector2Array( -203.652, -271.332, -96.4444, -272.383, -96.4261, -311.874, 95.2381, -312.128, 95.2381, -273.189, 203.106, -273.434, 203.398, 270.991, 95.1389, 272.042, 94.5607, 310.2, -94.2023, 309.61, -95.0871, 271.858, -203.036, 271.858 )
36+
37+
[node name="Sprite" type="Sprite" parent="."]
38+
scale = Vector2( 0.063, 0.063 )
39+
texture = ExtResource( 1 )
40+
41+
[node name="Content" type="AnimatedSprite" parent="Sprite"]
42+
modulate = Color( 0.776471, 0.776471, 0.776471, 1 )
43+
position = Vector2( 0, -7 )
44+
rotation = 0.207694
45+
frames = SubResource( 1 )
46+
47+
[node name="PickArea" type="Area2D" parent="."]
48+
49+
[node name="CollisionShape2D" type="CollisionShape2D" parent="PickArea"]
50+
scale = Vector2( 2.06014, -0.801646 )
51+
shape = SubResource( 3 )
52+
53+
[node name="FallTimer" type="Timer" parent="."]
54+
wait_time = 0.6
55+
one_shot = true
56+
autostart = true
57+
58+
[node name="PickTimer" type="Timer" parent="."]
59+
wait_time = 0.06
60+
61+
[connection signal="body_entered" from="PickArea" to="." method="_on_PickArea_body_entered"]
62+
[connection signal="timeout" from="FallTimer" to="." method="_on_FallTimer_timeout"]
63+
[connection signal="timeout" from="PickTimer" to="." method="_on_PickTimer_timeout"]

Bush.tscn

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ radius = 19.2516
5050
[node name="Bush" type="Area2D" groups=[
5151
"target",
5252
]]
53+
z_index = 4
54+
z_as_relative = false
5355
script = ExtResource( 2 )
5456

5557
[node name="Sprite" type="AnimatedSprite" parent="."]
@@ -60,6 +62,7 @@ frames = SubResource( 1 )
6062
shape = SubResource( 2 )
6163

6264
[node name="Particles2D" type="Particles2D" parent="."]
65+
z_as_relative = false
6366
emitting = false
6467
amount = 40
6568
lifetime = 4.3

Controls.gd

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ extends Node2D
44
export (int) var player
55

66
func _ready():
7-
print("p"+str(player)+"_left")
87
$Left/TouchScreenButton.action = "p"+str(player)+"_left"
98
$Button/TouchScreenButton.action = "p"+str(player)+"_button"
109
$Right/TouchScreenButton.action = "p"+str(player)+"_right"

Field.gd

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
extends Area2D
22

3+
export (PackedScene) var Aircraft
4+
35
signal tank_removed(tank)
46
signal game_over
57

@@ -11,10 +13,10 @@ func _ready():
1113
var winner = null
1214

1315
func update():
14-
print("updating ", len(candidates))
1516
if len(candidates) < 2:
1617
if len(candidates) == 1:
1718
winner = candidates[0]
19+
$Airfcrafts.stop()
1820
emit_signal("game_over")
1921
else:
2022
var zeroes=0
@@ -28,6 +30,7 @@ func update():
2830
maybeWinner = c
2931
if zeroes > len(candidates) -1:
3032
winner = maybeWinner
33+
$Airfcrafts.stop()
3134
emit_signal("game_over")
3235

3336
func _on_Tank1_dead():
@@ -53,3 +56,29 @@ func _on_Tank3_dead():
5356

5457
func _on_Tank3_zero():
5558
update()
59+
60+
func spawnAircraft():
61+
var spawn_location
62+
if randf() > 0.5:
63+
64+
spawn_location = $Airfcrafts/UpperAircraftSpawner/PathFollow2D
65+
else:
66+
spawn_location = $Airfcrafts/LowerAircraftSpawner/PathFollow2D
67+
68+
spawn_location.offset = randi()
69+
70+
var aircraft = Aircraft.instance()
71+
add_child(aircraft)
72+
73+
# Set the direction perpendicular to the path direction.
74+
var direction = spawn_location.rotation + PI
75+
76+
# Set the position to a random location.
77+
aircraft.position = spawn_location.position
78+
79+
# Add some randomness to the direction.
80+
direction += rand_range(-PI / 8, PI / 8)
81+
aircraft.rotation = direction
82+
83+
func _on_Airfcrafts_timeout():
84+
spawnAircraft()

0 commit comments

Comments
 (0)