2024-08-15 21:29:01 +02:00
|
|
|
class_name Main
|
2024-09-06 12:34:18 +02:00
|
|
|
extends Node
|
2024-07-09 22:33:38 +02:00
|
|
|
|
|
|
|
@onready var message_container:Control = %MessagesContainer
|
|
|
|
@onready var message_textedit:TextEdit = %MessageTextEdit
|
2024-07-20 11:20:18 +02:00
|
|
|
|
|
|
|
@onready var root_ui:RootUI = %RootUI
|
|
|
|
|
2024-08-15 23:43:05 +02:00
|
|
|
var _picked_up_level_items:Array[NodePath] = []
|
|
|
|
|
2024-07-20 11:20:18 +02:00
|
|
|
func save_game():
|
2024-08-15 22:15:24 +02:00
|
|
|
var player:Player = find_child("Player", true, false)
|
|
|
|
|
|
|
|
if player == null:
|
|
|
|
push_error("Cannot load game: no player found!")
|
|
|
|
return
|
|
|
|
|
2024-07-20 11:20:18 +02:00
|
|
|
var save_game:SaveGame = SaveGame.new()
|
|
|
|
|
2024-08-15 23:21:31 +02:00
|
|
|
save_game.level = root_ui.current_level_resource
|
2024-08-15 23:43:05 +02:00
|
|
|
save_game.level_pickup_items = _picked_up_level_items
|
2024-07-20 11:20:18 +02:00
|
|
|
save_game.player_transform = player.global_transform
|
|
|
|
|
2024-08-02 01:03:40 +02:00
|
|
|
for node:Node in get_tree().get_nodes_in_group("quest_state"):
|
|
|
|
save_game.quest_states[node.name] = {}
|
2024-08-15 22:15:24 +02:00
|
|
|
|
2024-08-02 01:03:40 +02:00
|
|
|
for property in node.get_property_list():
|
|
|
|
if (property.usage & PROPERTY_USAGE_STORAGE != 0) and (property.usage & PROPERTY_USAGE_SCRIPT_VARIABLE) != 0:
|
|
|
|
save_game.quest_states[node.name][property.name] = node.get(property.name)
|
|
|
|
|
2024-09-08 10:54:39 +02:00
|
|
|
for item_stack in player.inventory.get_item_stacks():
|
|
|
|
save_game.inventory.append({"item": item_stack.item, "count": item_stack.count})
|
2024-08-15 20:24:41 +02:00
|
|
|
|
2024-07-20 11:20:18 +02:00
|
|
|
ResourceSaver.save(save_game, "user://savegame.tres")
|
|
|
|
root_ui.activate_ui_panel(%GameUI)
|
|
|
|
|
|
|
|
func load_game():
|
2024-08-15 23:43:05 +02:00
|
|
|
_picked_up_level_items.clear()
|
|
|
|
|
2024-08-15 23:21:31 +02:00
|
|
|
var save_game:SaveGame = load("user://savegame.tres") as SaveGame
|
|
|
|
var level_resource:PackedScene = load(save_game.level)
|
|
|
|
root_ui.load_scene(level_resource)
|
|
|
|
|
2024-08-15 22:15:24 +02:00
|
|
|
var player:Player = find_child("Player", true, false)
|
|
|
|
|
|
|
|
if player == null:
|
|
|
|
push_error("Cannot load game: no player found!")
|
|
|
|
return
|
2024-07-20 11:20:18 +02:00
|
|
|
|
|
|
|
player.global_transform = save_game.player_transform
|
|
|
|
|
2024-08-15 23:43:05 +02:00
|
|
|
# Quest state
|
2024-08-02 01:03:40 +02:00
|
|
|
for node:Node in get_tree().get_nodes_in_group("quest_state"):
|
|
|
|
for property in node.get_property_list():
|
|
|
|
if (property.usage & PROPERTY_USAGE_STORAGE != 0) and (property.usage & PROPERTY_USAGE_SCRIPT_VARIABLE) != 0:
|
|
|
|
node.set(property.name, save_game.quest_states[node.name][property.name])
|
|
|
|
|
2024-08-15 23:43:05 +02:00
|
|
|
# Inventory
|
2024-08-15 20:24:41 +02:00
|
|
|
player.inventory.clear()
|
2024-09-08 10:54:39 +02:00
|
|
|
var inventory_item_stacks:Array[ItemStack] = player.inventory.get_item_stacks()
|
|
|
|
|
|
|
|
assert(inventory_item_stacks.size() >= save_game.inventory.size())
|
|
|
|
|
|
|
|
for i in range(save_game.inventory.size()):
|
|
|
|
inventory_item_stacks[i].item = save_game.inventory[i]["item"]
|
|
|
|
inventory_item_stacks[i].count = save_game.inventory[i]["count"]
|
2024-08-15 20:24:41 +02:00
|
|
|
|
2024-08-15 23:43:05 +02:00
|
|
|
# Picked up items
|
|
|
|
for item_path in save_game.level_pickup_items:
|
|
|
|
if get_tree().root.has_node(item_path):
|
|
|
|
var item_node = get_tree().root.get_node(item_path)
|
|
|
|
_picked_up_level_items.append(item_path)
|
|
|
|
item_node.queue_free()
|
|
|
|
|
2024-07-20 11:20:18 +02:00
|
|
|
root_ui.activate_ui_panel(%GameUI)
|
2024-08-02 00:06:50 +02:00
|
|
|
|
2024-08-15 23:43:05 +02:00
|
|
|
func on_item_pickup(node_path:NodePath):
|
|
|
|
_picked_up_level_items.append(node_path)
|