save system

This commit is contained in:
zoe 2021-10-01 16:23:48 +02:00
parent ad8de1dbb7
commit ffcccb8c2e
29 changed files with 286 additions and 77 deletions

View file

@ -83,7 +83,7 @@ func spawn_trajectory_dots():
can_spawn_trajectory_dot = true
trajectoryTimer.start()
func reset_ball():
GameStats.multiplier = 1
GameStats.set_multiplier(1)
# delete the ball when the level is won, so it doesn't respawn
if GameStats.level_won:
queue_free()

View file

@ -98,5 +98,6 @@ anims/reset = SubResource( 2 )
[node name="AudioStreamPlayer" type="AudioStreamPlayer" parent="."]
stream = ExtResource( 5 )
bus = "SFX"
[connection signal="timeout" from="TrajectoryTimer" to="." method="_on_TrajectoryTimer_timeout"]

View file

@ -93,5 +93,6 @@ texture = ExtResource( 7 )
stream = ExtResource( 8 )
volume_db = 4.0
autoplay = true
bus = "SFX"
[connection signal="timeout" from="KillTimer" to="." method="_on_KillTimer_timeout"]

View file

@ -1,5 +1,6 @@
extends KinematicBody2D
signal level_just_won
enum {IDLE, MOVEUP, MOVEDOWN}
var state = IDLE
@ -8,6 +9,7 @@ func _on_Killzone_body_entered(body):
body.reset_ball()
state = MOVEUP
GameStats.add_balls(-1)
if body.has_method("explode"):
if (body.has_method("set_particle_direction")):
body.set_particle_direction(Vector2(0, -10))
@ -28,4 +30,6 @@ func movedown():
func _on_ZoneDetection_area_entered(_area):
if (state == MOVEUP):
state = MOVEDOWN
if GameStats.level_won:
emit_signal("level_just_won")
else: state = IDLE

View file

@ -1,7 +1,10 @@
[gd_scene load_steps=3 format=2]
[gd_scene load_steps=6 format=2]
[ext_resource path="res://Ball/sprite.png" type="Texture" id=1]
[ext_resource path="res://HUD/BallCounter.gd" type="Script" id=2]
[ext_resource path="res://HUD/Multiplier.gd" type="Script" id=3]
[ext_resource path="res://Menu/Theme.tres" type="Theme" id=4]
[ext_resource path="res://HUD/Score.gd" type="Script" id=5]
[node name="HUD" type="CanvasLayer"]
@ -19,3 +22,33 @@ centered = false
region_enabled = true
region_rect = Rect2( 0, 0, 16, 16 )
script = ExtResource( 2 )
[node name="Multiplier" type="Label" parent="."]
margin_left = 336.0
margin_top = 176.0
margin_right = 376.0
margin_bottom = 190.0
grow_horizontal = 0
grow_vertical = 0
theme = ExtResource( 4 )
text = "x 1"
align = 2
script = ExtResource( 3 )
__meta__ = {
"_edit_use_anchors_": false
}
[node name="Score" type="Label" parent="."]
margin_left = 336.0
margin_top = 192.0
margin_right = 376.0
margin_bottom = 210.0
grow_horizontal = 0
grow_vertical = 0
theme = ExtResource( 4 )
text = "500"
align = 2
script = ExtResource( 5 )
__meta__ = {
"_edit_use_anchors_": false
}

1
HUD/HighScoreLabel.gd Normal file
View file

@ -0,0 +1 @@
extends Label

7
HUD/Multiplier.gd Normal file
View file

@ -0,0 +1,7 @@
extends Label
func _ready():
var _connected = GameStats.connect("multiplier_changed", self, "update_text")
func update_text(value):
text = "x" + String(value)

8
HUD/Score.gd Normal file
View file

@ -0,0 +1,8 @@
extends Label
func _ready():
update_text(GameStats.score)
var _connected = GameStats.connect("score_changed", self, "update_text")
func update_text(value):
text = String(value)

View file

@ -13,3 +13,4 @@ bus = "SFX"
[node name="Select" type="AudioStreamPlayer" parent="."]
stream = ExtResource( 2 )
bus = "SFX"

View file

@ -6,6 +6,7 @@ export var unlock_points_required = 0
func _ready():
if unlock_points_required > GameStats.unlock_points:
disabled = true
text = String(unlock_points_required - GameStats.unlock_points) + " more points"
func _on_SceneSelectButton_pressed():
GameStats.set_current_scene(scene)

View file

@ -7,8 +7,16 @@
[ext_resource path="res://Menu/Buttons/ButtonSFX.tscn" type="PackedScene" id=5]
[node name="PlayButton" type="TextureButton"]
margin_right = 40.0
margin_bottom = 40.0
anchor_left = 1.0
anchor_top = 1.0
anchor_right = 1.0
anchor_bottom = 1.0
margin_left = -384.0
margin_top = -216.0
margin_right = -352.0
margin_bottom = -184.0
size_flags_horizontal = 0
size_flags_vertical = 0
texture_normal = ExtResource( 3 )
texture_pressed = ExtResource( 1 )
texture_hover = ExtResource( 2 )
@ -21,5 +29,5 @@ __meta__ = {
[connection signal="focus_entered" from="." to="ButtonSFX" method="on_hover"]
[connection signal="mouse_entered" from="." to="ButtonSFX" method="on_hover"]
[connection signal="pressed" from="." to="." method="_on_PlayButton_pressed"]
[connection signal="pressed" from="." to="ButtonSFX" method="on_select"]
[connection signal="pressed" from="." to="." method="_on_PlayButton_pressed"]

View file

@ -21,5 +21,5 @@ __meta__ = {
[connection signal="focus_entered" from="." to="ButtonSFX" method="on_hover"]
[connection signal="mouse_entered" from="." to="ButtonSFX" method="on_hover"]
[connection signal="pressed" from="." to="ButtonSFX" method="on_select"]
[connection signal="pressed" from="." to="." method="_on_RestartButton_pressed"]
[connection signal="pressed" from="." to="ButtonSFX" method="on_select"]

View file

@ -1,12 +1,26 @@
extends VBoxContainer
extends PanelContainer
onready var stage_label = $CenterContainer/HBoxContainer/VBoxContainer/StageName
onready var score = $CenterContainer/HBoxContainer/VBoxContainer/ScoreLabel
onready var play = $CenterContainer/HBoxContainer/PlayButton
func _ready():
var _game_stats = GameStats.connect("current_stage_changed", self, "update_info")
func update_info(stage):
$HBoxContainer/PlayButton.disabled = false
play.disabled = false
var Stage = load(stage)
stage = Stage.instance()
$ScoreLabel.text = stage.stage_name
$HBoxContainer/PlayButton.visible = true
score.text = String(stage.high_score)
play.visible = true
var filename = "user://" + stage.stage_name + ".dat"
var file = File.new()
if file.file_exists(filename):
var err = file.open(filename, File.READ)
if err == OK:
var data = file.get_var()
file.close()
score.text = String(data.high_score)
stage_label.text = String(stage.stage_name)

View file

@ -1,50 +1,65 @@
[gd_scene load_steps=3 format=2]
[gd_scene load_steps=5 format=2]
[ext_resource path="res://Menu/LevelInfo.gd" type="Script" id=1]
[ext_resource path="res://Menu/Theme.tres" type="Theme" id=2]
[ext_resource path="res://Menu/Buttons/PlayButton.tscn" type="PackedScene" id=6]
[node name="LevelInfo" type="VBoxContainer"]
margin_left = 3.0
margin_right = 150.0
margin_bottom = 165.0
alignment = 2
[sub_resource type="StyleBoxFlat" id=1]
bg_color = Color( 0.847059, 0.74902, 0.847059, 1 )
[node name="LevelInfo" type="PanelContainer"]
anchor_right = 1.0
anchor_bottom = 1.0
theme = ExtResource( 2 )
custom_styles/panel = SubResource( 1 )
script = ExtResource( 1 )
__meta__ = {
"_edit_use_anchors_": false
}
[node name="ScoreLabel" type="Label" parent="."]
margin_top = 115.0
margin_right = 147.0
margin_bottom = 129.0
[node name="CenterContainer" type="CenterContainer" parent="."]
margin_right = 384.0
margin_bottom = 216.0
[node name="HBoxContainer" type="HBoxContainer" parent="CenterContainer"]
margin_left = 133.0
margin_top = 88.0
margin_right = 250.0
margin_bottom = 128.0
alignment = 1
[node name="VBoxContainer" type="VBoxContainer" parent="CenterContainer/HBoxContainer"]
margin_right = 117.0
margin_bottom = 40.0
alignment = 1
[node name="ScoreLabel" type="Label" parent="CenterContainer/HBoxContainer/VBoxContainer"]
margin_right = 117.0
margin_bottom = 18.0
theme = ExtResource( 2 )
text = "high score"
align = 2
valign = 3
__meta__ = {
"_edit_use_anchors_": false
}
[node name="StageName" type="Label" parent="CenterContainer/HBoxContainer/VBoxContainer"]
margin_top = 22.0
margin_right = 117.0
margin_bottom = 40.0
theme = ExtResource( 2 )
text = "select stage"
align = 2
valign = 3
[node name="StageName" type="Label" parent="."]
margin_top = 133.0
margin_right = 147.0
margin_bottom = 147.0
text = "..."
align = 2
valign = 3
[node name="HBoxContainer" type="HBoxContainer" parent="."]
margin_top = 151.0
margin_right = 147.0
margin_bottom = 165.0
alignment = 2
[node name="GoodLuck!!!" type="Label" parent="HBoxContainer"]
margin_left = 135.0
margin_right = 147.0
margin_bottom = 14.0
text = "..."
align = 2
valign = 3
[node name="PlayButton" parent="HBoxContainer" instance=ExtResource( 6 )]
[node name="PlayButton" parent="CenterContainer/HBoxContainer" instance=ExtResource( 6 )]
visible = false
margin_left = 94.0
margin_right = 126.0
anchor_left = 0.0
anchor_top = 0.0
anchor_right = 0.0
anchor_bottom = 0.0
margin_left = 121.0
margin_top = 0.0
margin_right = 153.0
margin_bottom = 32.0
disabled = true

View file

@ -105,7 +105,6 @@ __meta__ = {
[node name="LevelInfo" parent="Control/TabContainer/Levels/HBoxContainer" instance=ExtResource( 7 )]
margin_left = 157.0
margin_right = 172.0
alignment = 1
[node name="LevelList" type="ScrollContainer" parent="Control/TabContainer/Levels/HBoxContainer"]
margin_left = 176.0

View file

@ -54,19 +54,19 @@ anchor_bottom = 0.0
margin_right = 32.0
margin_bottom = 32.0
[node name="LevelSelectIconButton" parent="Pause/PanelContainer/HBoxContainer/VBoxContainer" instance=ExtResource( 6 )]
[node name="RestartButton" parent="Pause/PanelContainer/HBoxContainer/VBoxContainer" instance=ExtResource( 1 )]
margin_left = 36.0
margin_right = 68.0
margin_bottom = 32.0
[node name="LevelSelectIconButton" parent="Pause/PanelContainer/HBoxContainer/VBoxContainer" instance=ExtResource( 6 )]
margin_left = 72.0
margin_right = 104.0
margin_bottom = 32.0
texture_normal = ExtResource( 3 )
texture_pressed = ExtResource( 5 )
texture_hover = ExtResource( 4 )
[node name="RestartButton" parent="Pause/PanelContainer/HBoxContainer/VBoxContainer" instance=ExtResource( 1 )]
margin_left = 72.0
margin_right = 104.0
margin_bottom = 32.0
[node name="Resume" type="TextureButton" parent="Pause/PanelContainer/HBoxContainer/VBoxContainer"]
margin_left = 108.0
margin_right = 140.0
@ -93,8 +93,8 @@ custom_icons/grabber_highlight = ExtResource( 14 )
custom_icons/grabber = ExtResource( 13 )
bus_channel = "Music"
[connection signal="pressed" from="Pause/PanelContainer/HBoxContainer/VBoxContainer/LevelSelectIconButton" to="Pause" method="_on_pause_button_pressed"]
[connection signal="pressed" from="Pause/PanelContainer/HBoxContainer/VBoxContainer/RestartButton" to="Pause" method="_on_pause_button_pressed"]
[connection signal="pressed" from="Pause/PanelContainer/HBoxContainer/VBoxContainer/LevelSelectIconButton" to="Pause" method="_on_pause_button_pressed"]
[connection signal="focus_entered" from="Pause/PanelContainer/HBoxContainer/VBoxContainer/Resume" to="Pause/PanelContainer/HBoxContainer/VBoxContainer/Resume/ButtonSFX" method="on_hover"]
[connection signal="mouse_entered" from="Pause/PanelContainer/HBoxContainer/VBoxContainer/Resume" to="Pause/PanelContainer/HBoxContainer/VBoxContainer/Resume/ButtonSFX" method="on_hover"]
[connection signal="pressed" from="Pause/PanelContainer/HBoxContainer/VBoxContainer/Resume" to="Pause" method="_on_pause_button_pressed"]

View file

@ -1,4 +1,4 @@
[gd_scene load_steps=10 format=2]
[gd_scene load_steps=11 format=2]
[ext_resource path="res://Menu/MenuPage.tscn" type="PackedScene" id=1]
[ext_resource path="res://Menu/Buttons/RestartButton.tscn" type="PackedScene" id=2]
@ -8,6 +8,7 @@
[ext_resource path="res://Menu/Icons/Home/homeIcon1.png" type="Texture" id=6]
[ext_resource path="res://Menu/Icons/Home/homeIcon2.png" type="Texture" id=7]
[ext_resource path="res://Menu/Icons/Home/homeIcon3.png" type="Texture" id=8]
[ext_resource path="res://Menu/WinScreenScoreLabel.gd" type="Script" id=9]
[sub_resource type="Animation" id=1]
resource_name = "win"
@ -40,8 +41,8 @@ script = ExtResource( 4 )
visible = false
[node name="GridContainer" parent="Control/Panel/CenterContainer" index="0"]
margin_top = 55.0
margin_bottom = 109.0
margin_top = 44.0
margin_bottom = 120.0
[node name="ButtonRow" parent="Control/Panel/CenterContainer/GridContainer" index="1"]
margin_bottom = 54.0
@ -59,6 +60,14 @@ texture_normal = ExtResource( 6 )
texture_pressed = ExtResource( 8 )
texture_hover = ExtResource( 7 )
[node name="WinScreenScoreLabel" type="Label" parent="Control/Panel/CenterContainer/GridContainer" index="2"]
margin_top = 58.0
margin_right = 86.0
margin_bottom = 76.0
text = "700"
align = 1
script = ExtResource( 9 )
[node name="AnimationPlayer" type="AnimationPlayer" parent="." index="1"]
anims/win = SubResource( 1 )

View file

@ -0,0 +1,8 @@
extends Label
func _ready():
GameStats.connect("score_changed", self, "update_score_text")
text = String(GameStats.score)
func update_score_text(value):
text = String(value)

View file

@ -49,5 +49,6 @@ shape = SubResource( 2 )
anims/spawn = SubResource( 3 )
[node name="ScoreCounter" parent="." instance=ExtResource( 5 )]
base_multiplier = 0
[connection signal="timeout" from="GraceTimer" to="." method="_on_GraceTimer_timeout"]

View file

@ -45,5 +45,6 @@ script = ExtResource( 5 )
[node name="GraceTimer" parent="." instance=ExtResource( 3 )]
[node name="ScoreCounter" parent="." instance=ExtResource( 7 )]
base_multiplier = 0
[connection signal="timeout" from="GraceTimer" to="." method="_on_GraceTimer_timeout"]

View file

@ -15,13 +15,11 @@ func make_green():
# get the main scene of current peg and add new peg to it
var main = get_tree().current_scene
main.add_child(green_peg)
# add score
$ScoreCounter.trigger()
#finally delete the old peg
GameStats.pinks_left -= 1
GameStats.add_to_multiplier()
win_level()
Engine.time_scale = 1
queue_free()

View file

@ -11,7 +11,6 @@ func make_green():
$ScoreCounter.trigger()
main.add_child(green_bar)
GameStats.add_to_multiplier()
queue_free()
func bouncy():

View file

@ -18,8 +18,6 @@ func make_green():
var main = get_tree().current_scene
main.add_child(green_peg)
# increase multiplyier
GameStats.add_to_multiplier()
#finally delete the old peg
queue_free()

View file

@ -1,17 +1,16 @@
extends Node2D
export var base_score = 1
export var base_multiplier = 1
func trigger():
add_score()
add_multiplier()
func add_score():
var score = base_score * GameStats.multiplier
GameStats.score += score
#Todo remove
print(GameStats.score)
GameStats.set_score_to(GameStats.score + base_score * GameStats.multiplier)
var main = get_tree().current_scene
var Scorelabel = preload("res://Pegs/ScoreLabel.tscn")
@ -20,6 +19,9 @@ func add_score():
scorelabel.global_position = global_position
scorelabel.trigger(String(score))
func add_multiplier():
GameStats.add_to_multiplier(base_multiplier)
func delete():
queue_free()

View file

@ -34,6 +34,7 @@ tracks/1/keys = {
[node name="ScoreLabel" type="Node2D"]
z_index = 1000
z_as_relative = false
script = ExtResource( 2 )
[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
@ -50,7 +51,7 @@ size_flags_horizontal = 3
size_flags_vertical = 5
theme = ExtResource( 1 )
text = "69"
max_lines_visible = 1
align = 1
__meta__ = {
"_edit_use_anchors_": false
}

View file

@ -1,6 +1,14 @@
extends Node2D
export var stage_name = "test stage"
export var high_score = 0
export var already_beaten = false
func _ready():
load_level()
$Killzone.connect("level_just_won", self, "save_level")
GameStats.multiplier = 1
print(high_score)
func _physics_process(_delta):
fast_forward()
@ -10,3 +18,46 @@ func fast_forward():
Engine.time_scale = 3
if (Input.is_action_just_released("fast_forward")):
Engine.time_scale = 1
func load_level():
var filename = give_filename()
var file = File.new()
if file.file_exists(filename):
var err = file.open(filename, File.READ)
if err == OK:
var data = file.get_var()
file.close()
load_data(data)
func load_data(data):
already_beaten = data.already_beaten
high_score = data.high_score
func save_level():
print("saving game")
if !already_beaten:
GameStats.unlock_points += 10
already_beaten = true
else: GameStats.unlock_points += 1
if GameStats.score > high_score:
print("New High score!")
high_score = GameStats.score
var data = {
"high_score" : high_score,
"already_beaten" : already_beaten
}
var file = File.new()
var err = file.open(give_filename(), File.WRITE)
if err == OK:
file.store_var(data)
file.close()
else: print("something went wrong while saving your high scores! please check file permissions!")
Saves.save_game()
func give_filename():
var save_path = "user://" + String(stage_name) + ".dat"
return save_path

View file

@ -2,6 +2,8 @@ extends Node
signal balls_changed()
signal current_stage_changed(scene)
signal multiplier_changed(value)
signal score_changed(value)
var unlock_points = 10
var max_balls = 8
@ -37,19 +39,33 @@ func restart_level():
reset_board()
func reset_board():
multiplier = 1
score = 0
balls_left = max_balls
pinks_left = 0
level_won = false
multiplier = 1
score = 0
func set_current_scene(stage):
current_stage = stage
emit_signal("current_stage_changed", current_stage)
func add_to_multiplier():
func add_to_multiplier(value):
if value + GameStats.multiplier > GameStats.multiplier:
var SFXPlayer = load("res://SFX/SFXPlayer.tscn")
var sfx_player = SFXPlayer.instance()
var main = get_tree().current_scene
main.add_child(sfx_player)
multiplier += 1
multiplier += value
emit_signal("multiplier_changed", multiplier)
func set_multiplier(value):
multiplier = value
emit_signal("multiplier_changed", multiplier)
func set_score_to(value):
score = value
emit_signal("score_changed", score)

31
Stats/Saves.gd Normal file
View file

@ -0,0 +1,31 @@
extends Node
var save_path = "user://save.dat"
func _ready():
load_game()
func save_game():
var data = {
"unlock_points" : GameStats.unlock_points
}
print(data)
var file = File.new()
var err = file.open(save_path, File.WRITE)
if err == OK:
file.store_var(data)
file.close()
func load_game():
var file = File.new()
if file.file_exists(save_path):
var err = file.open(save_path, File.READ)
if err == OK:
var data = file.get_var()
file.close()
load_unlock_points(data)
func load_unlock_points(data):
GameStats.unlock_points = data.unlock_points

View file

@ -22,6 +22,7 @@ config/icon="res://icon.png"
GameStats="*res://Stats/GameStats.gd"
Music="*res://Music/Music.tscn"
Saves="*res://Stats/Saves.gd"
[display]