Skip to content

Commit

Permalink
feat: save energy state
Browse files Browse the repository at this point in the history
  • Loading branch information
myin142 committed Aug 14, 2023
1 parent d7d2add commit 934cbc6
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 11 deletions.
4 changes: 2 additions & 2 deletions godot/addons/save-system/SaveManager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ func load_from_slot(slot: int):
var file = FileAccess.open(file_name, FileAccess.READ)
var data
if file == null:
logger.error("Failed to load data from %s: %s" % [file_name, file.get_open_error()])
logger.error("Failed to load data from %s" % [file_name])
else:
data = file.get_var()
if data:
logger.debug("Load %s" % str(data))
file.close()
file.close()

return data

Expand Down
14 changes: 14 additions & 0 deletions godot/src/Energy.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
extends Node

@export var max_energy := 5
@onready var energy := max_energy : set = _set_energy

func _set_energy(v):
energy = v
GameManager.energy_updated.emit(energy)

func use_energy():
if energy > 0:
self.energy -= 1
return true
return false
33 changes: 26 additions & 7 deletions godot/src/game_manager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,40 @@ extends Node

signal energy_updated(energy)

const SAVE_SLOT = 0

@onready var save_manager := $SaveManager
@onready var energy := $Energy

var points := 0

var max_energy := 5
@onready var energy := max_energy : set = _set_energy
func _ready():
_load_game()

func _set_energy(v):
energy = v
energy_updated.emit(energy)
func _exit_tree():
_save_game()

func scored(score: int):
points += floor(score / 100)

func start_game():
if energy > 0:
self.energy -= 1
if energy.use_energy():
SceneManager.change_scene("res://src/game.tscn")
else:
print("No energy")

func get_energy():
return energy.energy

func _save_game():
save_manager.save_to_slot(SAVE_SLOT, {
"energy": get_energy()
})

func _load_game():
var data = save_manager.load_from_slot(SAVE_SLOT)
if data == null:
return

if "energy" in data:
energy.energy = data.energy
10 changes: 9 additions & 1 deletion godot/src/game_manager.tscn
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
[gd_scene load_steps=2 format=3 uid="uid://cn81olkklufoh"]
[gd_scene load_steps=4 format=3 uid="uid://cn81olkklufoh"]

[ext_resource type="Script" path="res://src/game_manager.gd" id="1_hnxtg"]
[ext_resource type="Script" path="res://addons/save-system/SaveManager.gd" id="2_etxdi"]
[ext_resource type="Script" path="res://src/Energy.gd" id="3_vf6rt"]

[node name="GameManager" type="Node"]
script = ExtResource("1_hnxtg")

[node name="SaveManager" type="Node" parent="."]
script = ExtResource("2_etxdi")

[node name="Energy" type="Node" parent="."]
script = ExtResource("3_vf6rt")
2 changes: 1 addition & 1 deletion godot/src/menu/energy_container.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class_name EnergyContainer
extends Control

func _ready():
set_energy(GameManager.energy)
set_energy(GameManager.get_energy())

func set_energy(v):
for i in get_child_count():
Expand Down

0 comments on commit 934cbc6

Please sign in to comment.