Skip to content

Commit

Permalink
Issue-48: WIP, Save MapManager, PlayerManager, Current Scene
Browse files Browse the repository at this point in the history
  • Loading branch information
Tysterman74 committed Jul 25, 2024
1 parent ba7ac80 commit d0bd2d1
Show file tree
Hide file tree
Showing 17 changed files with 166 additions and 24 deletions.
4 changes: 3 additions & 1 deletion #Scenes/Events/heal/0.tscn
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[gd_scene load_steps=3 format=3 uid="uid://lsl3jisfwkv3"]
[gd_scene load_steps=4 format=3 uid="uid://lsl3jisfwkv3"]

[ext_resource type="Script" path="res://#Scenes/SceneScripts/HealScene.gd" id="1_074vy"]
[ext_resource type="PackedScene" uid="uid://bam77cwf4emyr" path="res://#Scenes/TopBarOverlay.tscn" id="1_cp1mw"]
[ext_resource type="Script" path="res://#Scenes/Events/skipEventButton.gd" id="2_1m5v4"]

Expand All @@ -10,6 +11,7 @@ anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_074vy")

[node name="TextEdit" type="TextEdit" parent="."]
layout_mode = 0
Expand Down
4 changes: 3 additions & 1 deletion #Scenes/Events/shop/0.tscn
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[gd_scene load_steps=3 format=3 uid="uid://b8hbst1klk5ng"]
[gd_scene load_steps=4 format=3 uid="uid://b8hbst1klk5ng"]

[ext_resource type="PackedScene" uid="uid://bam77cwf4emyr" path="res://#Scenes/TopBarOverlay.tscn" id="1_7gwvp"]
[ext_resource type="Script" path="res://#Scenes/SceneScripts/ShopScene.gd" id="1_u1m6k"]
[ext_resource type="Script" path="res://#Scenes/Events/skipEventButton.gd" id="2_owx5q"]

[node name="Control" type="Control"]
Expand All @@ -10,6 +11,7 @@ anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_u1m6k")

[node name="TextEdit" type="TextEdit" parent="."]
layout_mode = 0
Expand Down
6 changes: 6 additions & 0 deletions #Scenes/MainMenu.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ grow_horizontal = 0
grow_vertical = 0
theme_override_constants/separation = 7

[node name="Continue" type="Button" parent="VBoxContainer"]
layout_mode = 2
theme_override_font_sizes/font_size = 40
text = "Continue"

[node name="Start" type="Button" parent="VBoxContainer"]
layout_mode = 2
theme_override_font_sizes/font_size = 40
Expand All @@ -56,6 +61,7 @@ layout_mode = 2
theme_override_font_sizes/font_size = 40
text = "Quit"

[connection signal="pressed" from="VBoxContainer/Continue" to="." method="_on_continue_pressed"]
[connection signal="pressed" from="VBoxContainer/Start" to="." method="_on_start_pressed"]
[connection signal="pressed" from="VBoxContainer/Options" to="." method="_on_options_pressed"]
[connection signal="pressed" from="VBoxContainer/Quit" to="." method="_on_quit_pressed"]
4 changes: 4 additions & 0 deletions #Scenes/SceneScripts/HealScene.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
extends Control

func _ready() -> void:
SaveManager.execute_save()
4 changes: 4 additions & 0 deletions #Scenes/SceneScripts/MainMenu.gd
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ func _on_start_pressed() -> void:
# TODO : load the last scene the player was in
pass

func _on_continue_pressed() -> void:
if PlayerManager.has_saved_data():
SceneManager.load_scene_data()
#SceneManager.goto_scene("res://#Scenes/MapUI.tscn")

## Scene to be loaded when option button is pressed
func _on_options_pressed() -> void:
Expand Down
4 changes: 4 additions & 0 deletions #Scenes/SceneScripts/ShopScene.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
extends Control

func _ready() -> void:
SaveManager.execute_save()
1 change: 1 addition & 0 deletions #Scenes/SceneScripts/TestingScene.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ extends Node2D
## Before, the testing scene was the main scene so it was properly started by the phase manager
## Now that the main scene is the main menu and we load the testing scene from there, we init to properly init the game state
func _ready() -> void:
SaveManager.execute_save()
PhaseManager.initialize_game()
1 change: 1 addition & 0 deletions Dialog/EventDialogueWindow.gd
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ var dialogue_line: DialogueLine:


func _ready() -> void:
SaveManager.execute_save()
balloon.hide()
Engine.get_singleton("DialogueManager").mutated.connect(_on_mutated)

Expand Down
2 changes: 1 addition & 1 deletion Event/EventMob.gd
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ func on_event_ended() -> void:
super()
print("Mob event ended")
# TODO show reward screen


2 changes: 1 addition & 1 deletion Global/DEBUG_VAR.gd
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ extends Node
const DEBUG_FREE_MOVEMENT: bool = false

## Player can skip the placeholder events by pressing on the skip button
const DEBUG_SKIP_EVENT: bool = false
const DEBUG_SKIP_EVENT: bool = true

## Allow player to add and remove items from the inventory with buttons [br]
## in the inventory UI
Expand Down
14 changes: 14 additions & 0 deletions Managers/InventoryManager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ var relic_component : InventoryRelicComponent = InventoryRelicComponent.new()
## This is for the UI [br]
## The current UI is a placeholder and wil be changed in the future

func _ready() -> void:
SaveManager.start_save.connect(_save_inventory)

var inventory_HUD : PackedScene = preload("res://InventoryComponents/InventoryHUD/inventory_hud.tscn")
var instanced_inventory_HUD : Node

Expand Down Expand Up @@ -40,3 +43,14 @@ func reset_inventory() -> void:
torch_component = InventoryTorchComponent.new()
consumable_component = InventoryConsumablesComponent.new()
relic_component = InventoryRelicComponent.new()

func _save_inventory() -> void:
var config_file: ConfigFile = SaveManager.config_file
config_file.set_value("InventoryManager", "gold_component", gold_component)
config_file.set_value("InventoryManager", "torch_component", torch_component)
config_file.set_value("InventoryManager", "consumable_component", consumable_component)
config_file.set_value("InventoryManager", "relic_component", relic_component)

var error: Error = config_file.save("user://save_data.ini")
if error:
print("Error saving inventory data: ", error)
32 changes: 28 additions & 4 deletions Managers/MapManager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,35 @@ func create_map(map_floors_width: Array[int] = map_width_array) -> MapBase:
## Create a map with a width array

func _ready() -> void:
map_width_array = [1, 3, 5, 7, 9, 11, 9, 7, 5, 3, 1]
current_map = create_map()

if (!_load_map_data()):
map_width_array = [1, 3, 5, 7, 9, 11, 9, 7, 5, 3, 1]
current_map = create_map()
SaveManager.start_save.connect(_save_map_data)

## checks if the map exists

func is_map_initialized() -> bool:
return current_map != null

func set_room_light_data(room: RoomBase) -> void:
current_map.rooms[room.room_position.y][room.room_position.x].light_data = room.light_data

func _save_map_data() -> void:
var config_file: ConfigFile = SaveManager.config_file
config_file.set_value("MapManager", "map_width_array", map_width_array)
config_file.set_value("MapManager", "current_map", current_map)

var error: Error = config_file.save("user://save_data.ini")
if error:
print("Error saving player data: ", error)

func _load_map_data() -> bool:
var config_file: ConfigFile = SaveManager.config_file
var error: Error = config_file.load("user://save_data.ini")
if error:
print("Loading Error: ", error)
return false

map_width_array = config_file.get_value("MapManager", "map_width_array")
current_map = config_file.get_value("MapManager", "current_map")
return true

35 changes: 32 additions & 3 deletions Managers/PlayerManager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,40 @@ var player_persistent_data: PlayerPersistentData = null:


func _ready() -> void:
player = null
is_player_initial_position_set = false

if (!_load_player()):
player = null
is_player_initial_position_set = false
SaveManager.start_save.connect(_save_player)


func _save_player() -> void:
var config_file: ConfigFile = SaveManager.config_file
config_file.set_value("Player", "player", player)
config_file.set_value("Player", "position", player_position)
config_file.set_value("Player", "player_room", player_room)
config_file.set_value("Player", "player_persistent_data", player_persistent_data)

var error: Error = config_file.save("user://save_data.ini")
if error:
print("Error saving player data: ", error)

func _load_player() -> bool:
var config_file: ConfigFile = SaveManager.config_file
var error: Error = config_file.load("user://save_data.ini")
if error:
print("Loading Error: ", error)
return false

player = config_file.get_value("Player", "player")
player_position = config_file.get_value("Player", "position")
player_room = config_file.get_value("Player", "player_room")
player_persistent_data = config_file.get_value("Player", "player_persistent_data") as PlayerPersistentData
return true


func has_saved_data() -> bool:
var config_file: ConfigFile = SaveManager.config_file
return config_file.has_section_key("Player", "position")

func set_player(in_player: Player) -> void:
player = in_player
Expand Down
18 changes: 18 additions & 0 deletions Managers/SaveManager.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
extends Node

signal start_save
signal load_data

var config_file: ConfigFile

func _ready() -> void:
config_file = ConfigFile.new()

func execute_save() -> void:
start_save.emit()

func clear_data() -> void:
config_file.clear()
var error: Error = config_file.save("user://save_data.ini")
if error:
print("Error saving player data: ", error)
28 changes: 27 additions & 1 deletion Managers/SceneManager.gd
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
extends Node

var current_scene: Node = null
var current_event: EventBase = null
var current_event_path: String = ""

func _ready() -> void:
var root: Node = get_tree().root
current_scene = root.get_child(root.get_child_count() - 1)
PhaseManager.on_defeat.connect(on_defeat)
PhaseManager.on_event_win.connect(on_event_win)
SaveManager.start_save.connect(_save_scene_data)

func goto_scene(path: String) -> void:
# This function will usually be called from a signal callback,
Expand Down Expand Up @@ -57,7 +60,9 @@ func goto_scene_map(event: EventBase, selection: int) -> void:
var event_type_name: String = actual_event.get_event_name()
# go search the scene of the given event with the given selection
var path: String = "res://#Scenes/Events/%s/%d.tscn" % [event_type_name, selection]


current_event_path = path
current_event = actual_event
call_deferred("_deferred_goto_scene_map", actual_event, path)

## Load the scene and start the event
Expand All @@ -75,3 +80,24 @@ func on_defeat() -> void:
## Give rewards, allow player to move on the map
func on_event_win() -> void:
PlayerManager.player_room.room_event.on_event_ended()

func _save_scene_data() -> void:
for children: Node in current_scene.get_children():
children.owner = current_scene
var packed_scene: PackedScene = PackedScene.new()
packed_scene.pack(current_scene)
ResourceSaver.save(packed_scene, "user://current_scene.tscn")

func load_scene_data() -> void:
call_deferred("_deferred_load_current_scene_from_data")

func _deferred_load_current_scene_from_data() -> void:
current_scene.free()

var scene: Resource = ResourceLoader.load("user://current_scene.tscn")
current_scene = scene.instantiate()
get_tree().root.add_child(current_scene)
# Optionally, to make it compatible with the SceneTree.change_scene_to_file() API.
get_tree().current_scene = current_scene


30 changes: 18 additions & 12 deletions UI/MapUI.gd
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,26 @@ func _add_torch_to_current_location() -> void:
var accessible_room_positions: Array[RoomBase] = MapMovement.get_all_accessible_rooms_in_range(PlayerManager.player_position, _LIGHT_FLOOR_RANGE, MapManager.current_map.rooms)
for room: RoomBase in accessible_room_positions:
room.light_data.increase_light_by_torch()
MapManager.set_room_light_data(room)

current_player_room.room.set_torch_active()
MapManager.set_room_light_data(current_player_room.room)

light_overlay.queue_redraw()
SaveManager.execute_save()

func _ready() -> void:
# allows map to be closed if any of the room button on the map is pressed
SignalBus.clicked_next_room_on_map.connect(_on_room_clicked)

var current_map: MapBase = MapManager.current_map
_draw_map_ui(current_map.rooms)

if(!PlayerManager.is_player_initial_position_set):
scroll_container.scroll_to_bottom(0)
else:
scroll_container.scroll_to_fauna(0)

func _draw_map_ui(rooms: Array[Array]) -> void:
var accessible_rooms_by_player: Array[RoomBase] = []
# Godot not happy and telling me current_map.rooms is an Array and not an Array[RoomBase]
# because we can't have nested typing in array, so need to use assign for type conversion
Expand All @@ -75,11 +86,10 @@ func _ready() -> void:
else:
# If the player hasn't selected a room yet, take the currently accessible rooms
# (basically the rooms on the first floor) and increase the light on those rooms.
accessible_rooms_by_player.assign(current_map.rooms[0])
for room: RoomBase in current_map.rooms[0]:
accessible_rooms_by_player.assign(rooms[0])
for room: RoomBase in rooms[0]:
if room != null:
room.light_data.increase_light_by_player_movement()

# Create New Room Object to append to the room container
var new_room: Control = room_ui.instantiate()
var new_room_texture_button: TextureButton = Helpers.get_first_child_node_of_type(new_room, TextureButton)
Expand Down Expand Up @@ -115,7 +125,8 @@ func _ready() -> void:
var start_position_for_next_room_y: float = room_container.position.y + room_container.get_custom_minimum_size().y - new_room_size.y - _padding_offset
var position_for_next_room: Vector2 = Vector2(start_position_for_next_room_x, start_position_for_next_room_y)

room_ui_array.resize(MapManager.map_width_array.size())
#room_ui_array.resize(MapManager.map_width_array.size())
room_ui_array.resize(rooms.size())

# Get the offset if we had to adjust the X position due to having to set a minimum width if the map is too small.
var offset_x: float = room_container.get_custom_minimum_size().x / 2 - _get_combined_room_width(new_room_texture_button) / 2
Expand All @@ -127,8 +138,8 @@ func _ready() -> void:
if (get_combined_room_height(new_room_texture_button) < _MINIMUM_ROOM_HEIGHT):
offset_y = room_container.get_custom_minimum_size().y / 2 - get_combined_room_height(new_room_texture_button) / 2

for floor_index: int in range(current_map.rooms.size()):
var floor_array: Array = current_map.rooms[floor_index]
for floor_index: int in range(rooms.size()):
var floor_array: Array = rooms[floor_index]
# When we're done populating a floor and we go to the next index, reset the X start position
position_for_next_room.x = start_position_for_next_room_x
for room: RoomBase in floor_array:
Expand Down Expand Up @@ -165,11 +176,6 @@ func _ready() -> void:

light_overlay = LightOverlay.new(room_container, room_ui_array)
room_container.add_child(light_overlay)

if(!PlayerManager.is_player_initial_position_set):
scroll_container.scroll_to_bottom(0)
else:
scroll_container.scroll_to_fauna(0)

# Get the width of room nodes, by getting the size of what a room is w/ some offset
# multiplying that by the max number in the map_width_array to get the width of the largest floor then add offset
Expand Down
1 change: 1 addition & 0 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ config/icon="res://Art/Menus/fauna_icon-1.png"

[autoload]

SaveManager="*res://Managers/SaveManager.gd"
SceneManager="*res://Managers/SceneManager.gd"
SignalBus="*res://Global/SignalBus.gd"
DebugVar="*res://Global/DEBUG_VAR.gd"
Expand Down

0 comments on commit d0bd2d1

Please sign in to comment.