Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save and load system #23

Merged
merged 3 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions globals/GameManager.gd
Original file line number Diff line number Diff line change
@@ -1,18 +1 @@
extends Node

const XP_PER_SECOND: int = 5

const TREES = {
0: {
"id": "Tree1",
"name": "Tree 1"
},
1: {
"id": "Tree2",
"name": "Tree 2"
},
2: {
"id": "Tree3",
"name": "Tree 3"
},
}
39 changes: 39 additions & 0 deletions globals/GameStorage.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
extends Node

const SAVE_PATH = "user://savegame.json"

func _ready() -> void:
load_progress()
save_progress()

func save_progress():
var data = {
"player": PlayerLS.get_data(),
"trees": TreeLS.trees,
"selectedTree": TreeLS.selected
}

var file = FileAccess.open(SAVE_PATH, FileAccess.WRITE)
var jsonData = JSON.stringify(data)

file.store_line(jsonData)

func load_progress():
if not FileAccess.file_exists(SAVE_PATH):
print("Save file not exists")
return

var file = FileAccess.open(SAVE_PATH, FileAccess.READ)
var file_content = file.get_as_text()
var json = JSON.new()

json.parse(file_content)

var data = json.data

TreeLS.trees = data.trees
TreeLS.selected = data.selectedTree
PlayerLS.xp = data.player.xp
PlayerLS.level = data.player.level
PlayerLS.xpNextLevel = data.player.xpNextLevel

8 changes: 8 additions & 0 deletions globals/LevelSystem/Player.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ var xp = 0 # Current XP
var level = 1 # Current Level
var xpNextLevel = 100 # XP to the next level

func get_data() -> Dictionary:
var data = {
"xp": xp,
"level": level,
"xpNextLevel": xpNextLevel
}
return data

func add_xp() -> void:
xp += XP_PER_SECOND
_check_level_up()
Expand Down
21 changes: 14 additions & 7 deletions globals/LevelSystem/Tree.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,24 @@ extends Node
const XP_PER_SECOND: int = 5

var trees = {
"Tree1": {"xp": 0, "level": 1, "xpNextLevel": 100, "maxLevel": 10},
"Tree2": {"xp": 0, "level": 1, "xpNextLevel": 100, "maxLevel": 10},
"Tree3": {"xp": 0, "level": 1, "xpNextLevel": 100, "maxLevel": 10},
"Tree1": {"id": 0, "name": "Tree 1", "xp": 0, "level": 1, "xpNextLevel": 100, "maxLevel": 10},
"Tree2": {"id": 1, "name": "Tree 2", "xp": 0, "level": 1, "xpNextLevel": 100, "maxLevel": 10},
"Tree3": {"id": 2, "name": "Tree 3", "xp": 0, "level": 1, "xpNextLevel": 100, "maxLevel": 10},
}
var select = "Tree1"
var selected = "Tree1"

func get_select_tree():
return trees[select]
return trees[selected]

func find_tree_by_id(target_id: int) -> Dictionary:
for key in trees.keys():
var tree = trees[key]
if tree.id == target_id:
return {"key": key, "tree": tree}
return {}

func set_selected_tree(name: String) -> void:
select = name
selected = name

func add_xp() -> void:
var tree = get_select_tree()
Expand All @@ -32,4 +39,4 @@ func _check_level_up() -> void:

func _on_level_up() -> void:
var tree = get_select_tree()
print(select, " subiu de nível! Novo nível: ", tree["level"])
print(selected, " subiu de nível! Novo nível: ", tree["level"])
1 change: 1 addition & 0 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ config/icon="res://icon.svg"

[autoload]

GameStorage="*res://globals/GameStorage.gd"
GameManager="*res://globals/GameManager.gd"
Utils="*res://globals/Utils.gd"
PlayerLS="*res://globals/LevelSystem/Player.gd"
Expand Down
19 changes: 12 additions & 7 deletions scenes/game/game.gd
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,28 @@ func _on_xp_timer_timeout() -> void:
PlayerLS.add_xp()
TreeLS.add_xp()
_update_ui()
GameStorage.save_progress()

func _update_ui() -> void:
var tree = TreeLS.get_select_tree()
player_level_status.update_ui(PlayerLS.level, PlayerLS.xp, PlayerLS.xpNextLevel)
tree_level_status.update_ui(tree["level"], tree["xp"], tree["xpNextLevel"])

func _populate_option_button():
var TREES = GameManager.TREES
var TREES = TreeLS.trees
var selected = TreeLS.selected

for key in TREES.keys():
var tree_data = TREES[key]
tree_selector.add_item(tree_data["name"], key)
var tree = TREES[key]
tree_selector.add_item(tree.name, tree.id)

tree_selector.selected = TREES[selected].id

func _on_option_button_item_selected(index: int) -> void:
var TREES = GameManager.TREES
var seletec = TREES[index]
var seletec = TreeLS.find_tree_by_id(index)

TreeLS.set_selected_tree(seletec.key)
tree_name.text = seletec.tree.name

TreeLS.set_selected_tree(seletec["id"])
tree_name.text = seletec["name"]
_update_ui()
GameStorage.save_progress()