Skip to content

Commit

Permalink
feat: aircraft dropping boxes
Browse files Browse the repository at this point in the history
  • Loading branch information
lbovet committed Aug 4, 2021
1 parent 877563d commit 704f43e
Show file tree
Hide file tree
Showing 34 changed files with 713 additions and 54 deletions.
49 changes: 49 additions & 0 deletions Aircraft.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
extends Area2D

export (PackedScene) var Box

var speed = 400

var obstacles = 0
var dropAllowed = false
var dropped = false

func _ready():
$DropTimer.start(0.9 + (0.2 - randf()*0.4))

func _physics_process(delta):
position += -transform.y * speed * delta
if not dropped and dropAllowed and obstacles == 0:
drop()

func _on_VisibilityNotifier2D_screen_exited():
queue_free()

func drop():
var box = Box.instance()
box.global_position = global_position
box.apply_central_impulse(-transform.y * 500)
box.rotation = rotation + rand_range(-PI / 2, PI / 2)
var rnd = randf()
if rnd < 0.5:
box.setContent(1)
elif rnd < 0.9:
box.setContent(2)
else:
box.setContent(3)
box.z_index = z_index - 1
box.collision_layer = 128
box.collision_mask = 128
box.set_angular_velocity(5)
get_parent().add_child(box)
box.setScale(Vector2(1.5, 1.5))
dropped = true

func _on_DropTimer_timeout():
dropAllowed = true

func _on_Aircraft_body_entered(body):
obstacles += 1

func _on_Aircraft_body_exited(body):
obstacles -= 1
29 changes: 29 additions & 0 deletions Aircraft.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[gd_scene load_steps=5 format=2]

[ext_resource path="res://images/plane.png" type="Texture" id=1]
[ext_resource path="res://Aircraft.gd" type="Script" id=2]
[ext_resource path="res://Box.tscn" type="PackedScene" id=3]

[sub_resource type="RectangleShape2D" id=1]
extents = Vector2( 15.8861, 14.1277 )

[node name="Aircraft" type="Area2D"]
z_index = 10
script = ExtResource( 2 )
Box = ExtResource( 3 )

[node name="Sprite" type="Sprite" parent="."]
scale = Vector2( 0.063, 0.063 )
texture = ExtResource( 1 )

[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
position = Vector2( -0.374573, -82.9523 )
scale = Vector2( 2.04304, 1.96886 )
shape = SubResource( 1 )

[node name="DropTimer" type="Timer" parent="."]
one_shot = true

[connection signal="body_entered" from="." to="." method="_on_Aircraft_body_entered"]
[connection signal="body_exited" from="." to="." method="_on_Aircraft_body_exited"]
[connection signal="timeout" from="DropTimer" to="." method="_on_DropTimer_timeout"]
6 changes: 6 additions & 0 deletions Barrel.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
extends RigidBody2D


func _on_PickArea_body_entered(body):
if body.is_in_group("tank") and body.life > 0:
queue_free()
30 changes: 30 additions & 0 deletions Barrel.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[gd_scene load_steps=5 format=2]

[ext_resource path="res://images/nitro.png" type="Texture" id=1]
[ext_resource path="res://Barrel.gd" type="Script" id=2]

[sub_resource type="RectangleShape2D" id=1]
extents = Vector2( 2.76727, 4.33193 )

[sub_resource type="CapsuleShape2D" id=2]
radius = 8.06976
height = 33.491

[node name="Barrel" type="RigidBody2D"]
script = ExtResource( 2 )

[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
scale = Vector2( 3.12, 2.6 )
shape = SubResource( 1 )

[node name="Sprite" type="Sprite" parent="."]
scale = Vector2( 0.063, 0.063 )
texture = ExtResource( 1 )

[node name="PickArea" type="Area2D" parent="."]

[node name="CollisionShape2D" type="CollisionShape2D" parent="PickArea"]
scale = Vector2( 2.06014, -0.801646 )
shape = SubResource( 2 )

[connection signal="body_entered" from="PickArea" to="." method="_on_PickArea_body_entered"]
78 changes: 78 additions & 0 deletions Box.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
extends RigidBody2D

export (PackedScene) var Shell

enum Content { SHELL=0, SMALL_SHELLS=1, BIG_SHELL=2, SPRING=3 }

var content = null
var targetScale = Vector2(1, 1)
var scaleDelta = Vector2(0.4, 0.4)
var blinks = 5
var shells = 0
var life = 8

func _ready():
content = $Sprite/Content.frame

func setScale(scale):
self.scale = scale
scaleDelta = scale - targetScale

func _physics_process(delta):
if scale.length() > 1:
var ratio = $FallTimer.time_left / $FallTimer.wait_time
scale = targetScale + ratio * scaleDelta

func setContent(content):
$Sprite/Content.frame = content
self.content = content

func hit(damage, source):
life -= damage
if life < 0:
if content != Content.SPRING:
fire(source)
queue_free()

func fire(source, angle=0):
var s = Shell.instance()
if content == Content.BIG_SHELL:
s.setBig()
elif content == Content.SMALL_SHELLS:
s.setSmall()
if angle == 0:
fire(source, randf() * PI + PI / 3)
fire(source, -randf() * PI + PI / 3)
s.transform = transform
s.rotate(angle)
get_parent().add_child(s)
s.setSource(source)

func _on_PickArea_body_entered(body):
if body.is_in_group("tank") and body.life > 0:
collision_layer = 256
collision_mask = 256
$PickTimer.start()
z_index = 0
if content == Content.SPRING:
body.addSpring()
if content == Content.SHELL:
body.addPoints(shells)
if content == Content.BIG_SHELL:
body.addBigShell()
if content == Content.SMALL_SHELLS:
body.addSmallShells()

func _on_FallTimer_timeout():
z_index = 1
collision_layer = 1
set_angular_velocity(0)

func _on_PickTimer_timeout():
if blinks % 2:
modulate.a = 0
else:
modulate.a = 1
blinks -= 1
if blinks == 0:
queue_free()
63 changes: 63 additions & 0 deletions Box.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
[gd_scene load_steps=10 format=2]

[ext_resource path="res://images/box.png" type="Texture" id=1]
[ext_resource path="res://images/shell.png" type="Texture" id=2]
[ext_resource path="res://images/small-shells.png" type="Texture" id=3]
[ext_resource path="res://images/spring.png" type="Texture" id=4]
[ext_resource path="res://images/big-shell.png" type="Texture" id=5]
[ext_resource path="res://Box.gd" type="Script" id=6]
[ext_resource path="res://Shell.tscn" type="PackedScene" id=7]

[sub_resource type="SpriteFrames" id=1]
animations = [ {
"frames": [ ExtResource( 2 ), ExtResource( 3 ), ExtResource( 5 ), ExtResource( 4 ) ],
"loop": true,
"name": "default",
"speed": 5.0
} ]

[sub_resource type="CapsuleShape2D" id=3]
radius = 10.7314
height = 44.8859

[node name="Box" type="RigidBody2D" groups=[
"heavy",
"target",
]]
z_index = 1
z_as_relative = false
script = ExtResource( 6 )
Shell = ExtResource( 7 )

[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="."]
position = Vector2( 0, -2 )
scale = Vector2( 0.063, 0.063 )
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 )

[node name="Sprite" type="Sprite" parent="."]
scale = Vector2( 0.063, 0.063 )
texture = ExtResource( 1 )

[node name="Content" type="AnimatedSprite" parent="Sprite"]
modulate = Color( 0.776471, 0.776471, 0.776471, 1 )
position = Vector2( 0, -7 )
rotation = 0.207694
frames = SubResource( 1 )

[node name="PickArea" type="Area2D" parent="."]

[node name="CollisionShape2D" type="CollisionShape2D" parent="PickArea"]
scale = Vector2( 2.06014, -0.801646 )
shape = SubResource( 3 )

[node name="FallTimer" type="Timer" parent="."]
wait_time = 0.6
one_shot = true
autostart = true

[node name="PickTimer" type="Timer" parent="."]
wait_time = 0.06

[connection signal="body_entered" from="PickArea" to="." method="_on_PickArea_body_entered"]
[connection signal="timeout" from="FallTimer" to="." method="_on_FallTimer_timeout"]
[connection signal="timeout" from="PickTimer" to="." method="_on_PickTimer_timeout"]
3 changes: 3 additions & 0 deletions Bush.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ radius = 19.2516
[node name="Bush" type="Area2D" groups=[
"target",
]]
z_index = 4
z_as_relative = false
script = ExtResource( 2 )

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

[node name="Particles2D" type="Particles2D" parent="."]
z_as_relative = false
emitting = false
amount = 40
lifetime = 4.3
Expand Down
1 change: 0 additions & 1 deletion Controls.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ extends Node2D
export (int) var player

func _ready():
print("p"+str(player)+"_left")
$Left/TouchScreenButton.action = "p"+str(player)+"_left"
$Button/TouchScreenButton.action = "p"+str(player)+"_button"
$Right/TouchScreenButton.action = "p"+str(player)+"_right"
31 changes: 30 additions & 1 deletion Field.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
extends Area2D

export (PackedScene) var Aircraft

signal tank_removed(tank)
signal game_over

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

func update():
print("updating ", len(candidates))
if len(candidates) < 2:
if len(candidates) == 1:
winner = candidates[0]
$Airfcrafts.stop()
emit_signal("game_over")
else:
var zeroes=0
Expand All @@ -28,6 +30,7 @@ func update():
maybeWinner = c
if zeroes > len(candidates) -1:
winner = maybeWinner
$Airfcrafts.stop()
emit_signal("game_over")

func _on_Tank1_dead():
Expand All @@ -53,3 +56,29 @@ func _on_Tank3_dead():

func _on_Tank3_zero():
update()

func spawnAircraft():
var spawn_location
if randf() > 0.5:

spawn_location = $Airfcrafts/UpperAircraftSpawner/PathFollow2D
else:
spawn_location = $Airfcrafts/LowerAircraftSpawner/PathFollow2D

spawn_location.offset = randi()

var aircraft = Aircraft.instance()
add_child(aircraft)

# Set the direction perpendicular to the path direction.
var direction = spawn_location.rotation + PI

# Set the position to a random location.
aircraft.position = spawn_location.position

# Add some randomness to the direction.
direction += rand_range(-PI / 8, PI / 8)
aircraft.rotation = direction

func _on_Airfcrafts_timeout():
spawnAircraft()
Loading

0 comments on commit 704f43e

Please sign in to comment.