Skip to content

Commit

Permalink
implement transit instead of ad-hoc fades and improve singleton usage
Browse files Browse the repository at this point in the history
  • Loading branch information
backwardspy committed Sep 9, 2020
1 parent 86efc76 commit 0f3fc12
Show file tree
Hide file tree
Showing 22 changed files with 121 additions and 198 deletions.
3 changes: 1 addition & 2 deletions mobs/Spawner.gd
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ onready var _spawn_points := get_children()
onready var _player := get_node(player_path)
onready var _upgrade_shop: CanvasItem = get_node(upgrade_shop_path)
onready var _spawn_timer: Timer = get_node("../SpawnTimer")
onready var _score_tracker: ScoreTracker = get_node("/root/ScoreTracker")

var _endless_mode := false
var _wave_difficulty := 1
Expand Down Expand Up @@ -67,7 +66,7 @@ func _end_wave():

func _on_mob_death():
_died_this_wave += 1
_score_tracker.add_mob_kill()
ScoreTracker.add_mob_kill()

if not _endless_mode and _died_this_wave >= _max_mobs:
_end_wave()
Expand Down
3 changes: 1 addition & 2 deletions player/BonusMan.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ extends Node

export var initial_bonus := 30

onready var _wallet: Wallet = $"/root/Wallet"
onready var _label: Label = $"../UI/Control/ScrapBonusBox/BonusLabel"
onready var _timer: Timer = $Timer

Expand All @@ -28,4 +27,4 @@ func _on_Spawner_wave_started(difficulty: int):
func _on_Spawner_wave_ended():
_timer.stop()
if _bonus > 0:
_wallet.add_scrap(_bonus)
Wallet.add_scrap(_bonus)
6 changes: 2 additions & 4 deletions player/Player.gd
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ var _dead := false
var _weapons_added := 0

onready var _health := max_health
onready var _wallet: Wallet = get_node("/root/Wallet")
onready var _score_tracker: ScoreTracker = get_node("/root/ScoreTracker")

func hurt(damage: float):
if _dead:
Expand Down Expand Up @@ -121,8 +119,8 @@ func _process(dt: float):
func _on_LootSuccArea_body_entered(body):
if body is Scrap:
body.succ(self)
_wallet.add_scrap(1)
_score_tracker.add_scrap_earned(1)
Wallet.add_scrap(1)
ScoreTracker.add_scrap_earned(1)

func _on_UpgradeShop_upgrade_purchased(upgrade: Upgrade, _final: bool):
_apply_upgrade(upgrade)
Expand Down
1 change: 1 addition & 0 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ config/icon="res://icon.png"

Wallet="*res://player/Wallet.gd"
ScoreTracker="*res://player/ScoreTracker.gd"
Transit="*res://transit/Transit.tscn"

[display]

Expand Down
6 changes: 1 addition & 5 deletions states/About.tscn
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[gd_scene load_steps=5 format=2]
[gd_scene load_steps=4 format=2]

[ext_resource path="res://ui/ui_theme.tres" type="Theme" id=1]
[ext_resource path="res://effects/Fadein.tscn" type="PackedScene" id=2]
[ext_resource path="res://ui/about/About.gd" type="Script" id=3]
[ext_resource path="res://ui/title_font.tres" type="DynamicFont" id=4]

Expand Down Expand Up @@ -93,8 +92,5 @@ Copyright (c) 2020 backwardspy"
__meta__ = {
"_edit_use_anchors_": false
}

[node name="Fadein" parent="." instance=ExtResource( 2 )]
visible = false
[connection signal="pressed" from="BackButton" to="." method="back_to_menu"]
[connection signal="meta_clicked" from="AboutInfo" to="." method="_on_AboutInfo_meta_clicked"]
24 changes: 2 additions & 22 deletions states/Game.gd
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
extends Node2D

onready var _score_tracker: ScoreTracker = get_node("/root/ScoreTracker")
onready var _blackout: ColorRect = $UI/Blackout
onready var _tween: Tween = $Tween

var _in_endless_wave := false

func handle_player_death():
Expand All @@ -12,27 +8,11 @@ func handle_player_death():

func quit_to(scene: String):
$MusicMan.fade_out()

_blackout.visible = true
var _r := _tween.interpolate_property(
_blackout,
"color:a",
0,
1,
3,
Tween.TRANS_LINEAR,
Tween.EASE_IN
)
_r = _tween.start()
yield(_tween, "tween_all_completed")

var err := get_tree().change_scene(scene)
if err:
push_error("failed to change to %s scene: %s" % [scene, err])
Transit.change_scene(scene, 1.0)

func _ready():
randomize()
get_node("/root/Wallet").empty()
Wallet.empty()

func _on_Player_died():
call_deferred("handle_player_death")
Expand Down
23 changes: 4 additions & 19 deletions states/Game.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=27 format=2]
[gd_scene load_steps=26 format=2]

[ext_resource path="res://player/Player.tscn" type="PackedScene" id=1]
[ext_resource path="res://sprites.png" type="Texture" id=2]
Expand All @@ -12,7 +12,6 @@
[ext_resource path="res://ui/game/UpgradeShop.tscn" type="PackedScene" id=10]
[ext_resource path="res://ui/GameAnimations.gd" type="Script" id=11]
[ext_resource path="res://tileset.tres" type="TileSet" id=12]
[ext_resource path="res://effects/Fadein.tscn" type="PackedScene" id=13]
[ext_resource path="res://music/TremLoadingloopl.ogg" type="AudioStream" id=14]
[ext_resource path="res://effects/MusicMan.gd" type="Script" id=15]
[ext_resource path="res://upgrades/UpgradeManager.tscn" type="PackedScene" id=16]
Expand Down Expand Up @@ -117,8 +116,6 @@ script = ExtResource( 5 )
[node name="WorldEnvironment" type="WorldEnvironment" parent="."]
environment = SubResource( 1 )

[node name="Tween" type="Tween" parent="."]

[node name="MusicMan" type="Node" parent="."]
script = ExtResource( 15 )

Expand Down Expand Up @@ -319,27 +316,15 @@ upgrade_manager_path = NodePath("../../../UpgradeManager")
[node name="PauseMenu" parent="UI/Control" instance=ExtResource( 18 )]
pause_mode = 2
visible = false

[node name="Blackout" type="ColorRect" parent="UI"]
visible = false
anchor_right = 1.0
anchor_bottom = 1.0
color = Color( 0, 0, 0, 0 )
__meta__ = {
"_edit_use_anchors_": false
}

[node name="Fadein" parent="UI" instance=ExtResource( 13 )]
visible = false
[connection signal="timeout" from="BonusMan/Timer" to="BonusMan" method="_on_Timer_timeout"]
[connection signal="died" from="Player" to="." method="_on_Player_died"]
[connection signal="health_changed" from="Player" to="UI/Control/HealthBar" method="_on_Player_health_changed"]
[connection signal="wave_ended" from="Spawner" to="MusicMan" method="to_idle"]
[connection signal="wave_ended" from="Spawner" to="BonusMan" method="_on_Spawner_wave_ended"]
[connection signal="wave_ended" from="Spawner" to="MusicMan" method="to_idle"]
[connection signal="wave_started" from="Spawner" to="AnimationPlayer" method="_on_Spawner_wave_started"]
[connection signal="wave_started" from="Spawner" to="." method="_on_Spawner_wave_started"]
[connection signal="wave_started" from="Spawner" to="MusicMan" method="to_battle"]
[connection signal="wave_started" from="Spawner" to="BonusMan" method="_on_Spawner_wave_started"]
[connection signal="wave_started" from="Spawner" to="AnimationPlayer" method="_on_Spawner_wave_started"]
[connection signal="wave_started" from="Spawner" to="MusicMan" method="to_battle"]
[connection signal="timeout" from="SpawnTimer" to="Spawner" method="spawn"]
[connection signal="upgrade_purchased" from="UI/Control/UpgradeShop" to="Player" method="_on_UpgradeShop_upgrade_purchased"]
[connection signal="upgrade_purchased" from="UI/Control/UpgradeShop" to="Spawner" method="_on_UpgradeShop_upgrade_purchased"]
Expand Down
21 changes: 1 addition & 20 deletions states/GameEnd.gd
Original file line number Diff line number Diff line change
@@ -1,26 +1,7 @@
extends Control

onready var _tween: Tween = $Tween
onready var _blackout: ColorRect = $Blackout

func _on_RichTextLabel_meta_clicked(meta: String):
OS.shell_open(meta)

func _on_MenuButton_pressed():
focus_mode = Control.FOCUS_NONE
_blackout.visible = true
_tween.interpolate_property(
_blackout,
"color:a",
0,
1,
3,
Tween.TRANS_LINEAR,
Tween.EASE_IN
)
_tween.start()
yield(_tween, "tween_all_completed")

var err := get_tree().change_scene("res://states/Menu.tscn")
if err:
push_error("failed to change to menu scene: %s" % err)
Transit.change_scene("res://states/Menu.tscn", 1.0)
17 changes: 1 addition & 16 deletions states/GameEnd.tscn
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
[gd_scene load_steps=8 format=2]
[gd_scene load_steps=7 format=2]

[ext_resource path="res://ui/ui_theme.tres" type="Theme" id=1]
[ext_resource path="res://ui/title_font.tres" type="DynamicFont" id=2]
[ext_resource path="res://states/GameEnd.gd" type="Script" id=3]
[ext_resource path="res://effects/Fadein.tscn" type="PackedScene" id=4]
[ext_resource path="res://ui/game_end/ScrapLabel.gd" type="Script" id=5]
[ext_resource path="res://ui/game_end/KillsLabel.gd" type="Script" id=6]
[ext_resource path="res://ui/game_end/Score.gd" type="Script" id=7]
Expand All @@ -20,8 +19,6 @@ __meta__ = {
"_edit_use_anchors_": false
}

[node name="Tween" type="Tween" parent="."]

[node name="GameTextBox" type="VBoxContainer" parent="."]
anchor_left = 0.5
anchor_right = 0.5
Expand Down Expand Up @@ -120,17 +117,5 @@ text = "Return to Menu"
__meta__ = {
"_edit_use_anchors_": false
}

[node name="Fadein" parent="." instance=ExtResource( 4 )]
visible = false

[node name="Blackout" type="ColorRect" parent="."]
visible = false
anchor_right = 1.0
anchor_bottom = 1.0
color = Color( 0, 0, 0, 0 )
__meta__ = {
"_edit_use_anchors_": false
}
[connection signal="meta_clicked" from="MetaTextBox/RichTextLabel" to="." method="_on_RichTextLabel_meta_clicked"]
[connection signal="pressed" from="MenuButton" to="." method="_on_MenuButton_pressed"]
34 changes: 5 additions & 29 deletions states/GameOver.gd
Original file line number Diff line number Diff line change
@@ -1,36 +1,12 @@
extends Control

onready var _tween: Tween = $Tween

func _fade_to_scene(path: String):
focus_mode = Control.FOCUS_NONE

$Blackout.visible = true

var _r := _tween.interpolate_property(
$Blackout,
"color:a",
0,
1,
0.5,
Tween.TRANS_LINEAR,
Tween.EASE_OUT
)

_r = _tween.start()
yield(_tween, "tween_all_completed")
var err := get_tree().change_scene(path)
if err:
push_error("failed to change scene to %s: %s" % [path, err])

func retry():
_fade_to_scene("res://states/Game.tscn")
Transit.change_scene("res://states/Game.tscn", 1.0)

func menu():
_fade_to_scene("res://states/Menu.tscn")
Transit.change_scene("res://states/Menu.tscn", 1.0)

func _ready():
var scores: ScoreTracker = get_node("/root/ScoreTracker")
$ControlsBox/ScrapEarned.text = "You earned %s Scrap" % scores.get_scrap_earned()
$ControlsBox/MobsKilled.text = "You killed %s drones" % scores.get_mobs_killed()
$ControlsBox/Score.text = "Score: %s" % scores.calculate_score()
$ControlsBox/ScrapEarned.text = "You earned %s Scrap" % ScoreTracker.get_scrap_earned()
$ControlsBox/MobsKilled.text = "You killed %s drones" % ScoreTracker.get_mobs_killed()
$ControlsBox/Score.text = "Score: %s" % ScoreTracker.calculate_score()
13 changes: 1 addition & 12 deletions states/GameOver.tscn
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
[gd_scene load_steps=5 format=2]
[gd_scene load_steps=4 format=2]

[ext_resource path="res://states/GameOver.gd" type="Script" id=1]
[ext_resource path="res://ui/ui_theme.tres" type="Theme" id=2]
[ext_resource path="res://ui/title_font.tres" type="DynamicFont" id=3]
[ext_resource path="res://effects/Fadein.tscn" type="PackedScene" id=4]

[node name="Root" type="Control"]
anchor_right = 1.0
Expand All @@ -17,8 +16,6 @@ __meta__ = {
"_edit_use_anchors_": false
}

[node name="Tween" type="Tween" parent="."]

[node name="ControlsBox" type="VBoxContainer" parent="."]
anchor_left = 0.5
anchor_top = 0.5
Expand Down Expand Up @@ -74,13 +71,5 @@ text = "Menu"
__meta__ = {
"_edit_use_anchors_": false
}

[node name="Fadein" parent="." instance=ExtResource( 4 )]

[node name="Blackout" type="ColorRect" parent="."]
visible = false
anchor_right = 1.0
anchor_bottom = 1.0
color = Color( 0, 0, 0, 0 )
[connection signal="pressed" from="ControlsBox/Retry" to="." method="retry"]
[connection signal="pressed" from="ControlsBox/QuitToMenu" to="." method="menu"]
37 changes: 2 additions & 35 deletions states/Menu.gd
Original file line number Diff line number Diff line change
@@ -1,43 +1,10 @@
extends Control

onready var _tween := $Tween

func fade_to_scene(path: String):
focus_mode = Control.FOCUS_NONE

$Blackout.visible = true

_tween.interpolate_property(
$Blackout,
"color:a",
0,
1,
0.5,
Tween.TRANS_LINEAR,
Tween.EASE_OUT
)

_tween.interpolate_property(
$AudioStreamPlayer,
"volume_db",
0,
-80,
0.5,
Tween.TRANS_LINEAR,
Tween.EASE_IN
)

_tween.start()
yield(_tween, "tween_all_completed")
var err = get_tree().change_scene(path)
if err:
push_error("failed to load scene %s: %s" % [path, err])

func start_game():
fade_to_scene("res://states/Game.tscn")
Transit.change_scene("res://states/Game.tscn", 1.0)

func about():
fade_to_scene("res://states/About.tscn")
Transit.change_scene("res://states/About.tscn")

func quit_game():
get_tree().quit()
16 changes: 2 additions & 14 deletions states/Menu.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=15 format=2]
[gd_scene load_steps=14 format=2]

[ext_resource path="res://states/Menu.gd" type="Script" id=1]
[ext_resource path="res://ui/ui_theme.tres" type="Theme" id=2]
Expand All @@ -12,7 +12,6 @@
[ext_resource path="res://ui/menu/TitleBob.gd" type="Script" id=10]
[ext_resource path="res://music/Factory.ogg" type="AudioStream" id=11]
[ext_resource path="res://tileset.tres" type="TileSet" id=12]
[ext_resource path="res://effects/Fadein.tscn" type="PackedScene" id=13]
[ext_resource path="res://ui/question_mark.png" type="Texture" id=14]

[node name="Root" type="Control"]
Expand All @@ -31,8 +30,6 @@ __meta__ = {
stream = ExtResource( 11 )
autoplay = true

[node name="Tween" type="Tween" parent="."]

[node name="TileMap" type="TileMap" parent="."]
tile_set = ExtResource( 12 )
cell_size = Vector2( 16, 16 )
Expand Down Expand Up @@ -143,16 +140,7 @@ flat = true
__meta__ = {
"_edit_use_anchors_": false
}

[node name="Blackout" type="ColorRect" parent="."]
visible = false
anchor_right = 1.0
anchor_bottom = 1.0
color = Color( 0, 0, 0, 0 )

[node name="Fadein" parent="." instance=ExtResource( 13 )]
visible = false
[connection signal="pressed" from="Buttons/StartGame" to="." method="start_game"]
[connection signal="pressed" from="Buttons/QuitGame" to="." method="quit_game"]
[connection signal="pressed" from="Buttons/QuitGame" to="." method="start_game"]
[connection signal="pressed" from="Buttons/QuitGame" to="." method="quit_game"]
[connection signal="pressed" from="About" to="." method="about"]
Loading

0 comments on commit 0f3fc12

Please sign in to comment.