mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-01 10:15:45 +00:00
Game saving and loading
This commit is contained in:
parent
fbd00dcd15
commit
73b0847948
9 changed files with 83 additions and 26 deletions
|
|
@ -1,4 +1,4 @@
|
|||
[gd_resource type="Resource" script_class="MapsDatabase" load_steps=7 format=3 uid="uid://blf2ii0j3fqil"]
|
||||
[gd_resource type="Resource" script_class="MapsDatabase" load_steps=8 format=3 uid="uid://blf2ii0j3fqil"]
|
||||
|
||||
[ext_resource type="Resource" uid="uid://b4u3cs6ietmow" path="res://Resources/Maps/Tutorial.tres" id="1_jm16h"]
|
||||
[ext_resource type="Script" uid="uid://denacldr388km" path="res://Scripts/Resources/MapsDatabase.cs" id="1_p3u82"]
|
||||
|
|
@ -6,14 +6,9 @@
|
|||
[ext_resource type="Resource" uid="uid://37h0tjja4sns" path="res://Resources/Maps/RebelBase.tres" id="3_b8vvn"]
|
||||
[ext_resource type="Resource" uid="uid://mx860r8t37g1" path="res://Resources/Maps/Level2.tres" id="4_axa7o"]
|
||||
[ext_resource type="Resource" uid="uid://b0xtpdb4by8mm" path="res://Resources/Maps/Level3.tres" id="5_8yujj"]
|
||||
[ext_resource type="Resource" uid="uid://b6per1p85bqpn" path="res://Resources/Maps/Level4.tres" id="6_2lffu"]
|
||||
|
||||
[resource]
|
||||
script = ExtResource("1_p3u82")
|
||||
Maps = Dictionary[int, Object]({
|
||||
0: ExtResource("1_jm16h"),
|
||||
1: ExtResource("2_2lffu"),
|
||||
2: ExtResource("3_b8vvn"),
|
||||
3: ExtResource("4_axa7o"),
|
||||
4: ExtResource("5_8yujj")
|
||||
})
|
||||
Maps = Array[Object]([ExtResource("1_jm16h"), ExtResource("2_2lffu"), ExtResource("3_b8vvn"), ExtResource("4_axa7o"), ExtResource("5_8yujj"), ExtResource("6_2lffu")])
|
||||
metadata/_custom_type_script = "uid://denacldr388km"
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ metadata/_custom_type_script = "uid://mja0rk7n2kln"
|
|||
|
||||
[resource]
|
||||
script = ExtResource("1_5stb7")
|
||||
LevelId = 0
|
||||
MapName = &"Tutorial"
|
||||
MapDescription = &"A VR simulation for training"
|
||||
ScenePath = &"uid://bpnahwxmjgb6t"
|
||||
|
|
|
|||
|
|
@ -20,9 +20,9 @@
|
|||
[ext_resource type="Resource" uid="uid://byo74ews118nl" path="res://Resources/Music/No_Reason.tres" id="13_fh7cp"]
|
||||
[ext_resource type="FontFile" uid="uid://cyk27h5aox1n7" path="res://fonts/sector_034.ttf" id="14_yxkem"]
|
||||
|
||||
[sub_resource type="AtlasTexture" id="AtlasTexture_hy5wk"]
|
||||
[sub_resource type="AtlasTexture" id="AtlasTexture_ogjye"]
|
||||
atlas = ExtResource("9_riqfo")
|
||||
region = Rect2(288, 0, 32, 16)
|
||||
region = Rect2(64, 0, 32, 16)
|
||||
|
||||
[sub_resource type="Theme" id="Theme_c7h4d"]
|
||||
|
||||
|
|
@ -67,7 +67,7 @@ offset_top = 40.995
|
|||
offset_right = 186.42
|
||||
offset_bottom = 56.995
|
||||
rotation = 3.14159
|
||||
texture = SubResource("AtlasTexture_hy5wk")
|
||||
texture = SubResource("AtlasTexture_ogjye")
|
||||
|
||||
[node name="MarginContainer" type="MarginContainer" parent="."]
|
||||
anchors_preset = 15
|
||||
|
|
@ -216,7 +216,7 @@ grow_vertical = 2
|
|||
theme_override_styles/panel = ExtResource("7_rrcx7")
|
||||
|
||||
[connection signal="pressed" from="MarginContainer/GridContainer/GameButtons/StartButton" to="." method="_on_start_button_pressed"]
|
||||
[connection signal="pressed" from="MarginContainer/GridContainer/GameButtons/ContinueButton" to="." method="_on_debug_button_pressed"]
|
||||
[connection signal="pressed" from="MarginContainer/GridContainer/GameButtons/ContinueButton" to="." method="OnContinuePressed"]
|
||||
[connection signal="pressed" from="MarginContainer/GridContainer/GameButtons/DebugButton" to="." method="_on_debug_button_pressed"]
|
||||
[connection signal="pressed" from="MarginContainer/GridContainer/GameButtons/ExitButton" to="." method="_on_exit_button_pressed"]
|
||||
[connection signal="pressed" from="MarginContainer/GridContainer/Options/OptionsButton" to="." method="OnOptionsButtonPressed"]
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
[ext_resource type="Script" uid="uid://doxmbokehw8ci" path="res://Scripts/GameManager.cs" id="1_6sq7s"]
|
||||
[ext_resource type="PackedScene" uid="uid://c4pr2707hbeph" path="res://Scenes/Actors/fsm_player.tscn" id="2_cicvv"]
|
||||
[ext_resource type="Script" uid="uid://epnwjptvks3t" path="res://Scripts/Resources/LootItem.cs" id="3_8anae"]
|
||||
[ext_resource type="Script" uid="uid://mja0rk7n2kln" path="res://Scripts/Resources/MapStartDataResource.cs" id="4_tgwif"]
|
||||
[ext_resource type="TileSet" uid="uid://6k28roiljylj" path="res://Tilesets/factory_tileset.tres" id="5_vmlpc"]
|
||||
[ext_resource type="Script" uid="uid://krean0uywtms" path="res://Scripts/TilemapAvoidance.cs" id="6_sv42c"]
|
||||
|
|
@ -17,6 +16,7 @@
|
|||
[ext_resource type="PackedScene" uid="uid://cqrkisw6jg24w" path="res://Scenes/Props/MainFrame1.tscn" id="15_8rpjd"]
|
||||
[ext_resource type="PackedScene" uid="uid://doqmmuuuprl3s" path="res://Scenes/Interactable/Computer.tscn" id="16_r67ss"]
|
||||
[ext_resource type="PackedScene" uid="uid://bc64lr3vlwchq" path="res://Scenes/Door_Vertical.tscn" id="17_8n6yg"]
|
||||
[ext_resource type="Resource" uid="uid://mx860r8t37g1" path="res://Resources/Maps/Level2.tres" id="18_8anae"]
|
||||
[ext_resource type="PackedScene" uid="uid://dx1urm7nttkqk" path="res://Scenes/Actors/level_teleporter.tscn" id="18_tbppe"]
|
||||
[ext_resource type="PackedScene" uid="uid://crph24e6e0v0q" path="res://Scenes/Interactable/Control_Pad.tscn" id="19_nxss7"]
|
||||
[ext_resource type="PackedScene" uid="uid://cyy5i38sf7fid" path="res://Scenes/Props/Locker.tscn" id="20_jrxvj"]
|
||||
|
|
@ -31,7 +31,7 @@
|
|||
[sub_resource type="Resource" id="Resource_53p3c"]
|
||||
script = ExtResource("4_tgwif")
|
||||
EggIndex = 0
|
||||
StartingEquipment = Array[ExtResource("3_8anae")]([null])
|
||||
StartingEquipment = [null]
|
||||
|
||||
[sub_resource type="Resource" id="Resource_2ugqu"]
|
||||
script = ExtResource("11_esmuk")
|
||||
|
|
@ -180,6 +180,7 @@ position = Vector2(292, 408)
|
|||
[node name="LevelTeleporter" parent="Tilemaps/Actors" instance=ExtResource("18_tbppe")]
|
||||
position = Vector2(112, 304)
|
||||
LevelPath = "res://Scenes/Maps/Level2.tscn"
|
||||
Map = ExtResource("18_8anae")
|
||||
SaveInventory = true
|
||||
|
||||
[node name="ControlPad" parent="Tilemaps" node_paths=PackedStringArray("Targets") instance=ExtResource("19_nxss7")]
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
[gd_scene load_steps=51 format=4 uid="uid://bpnahwxmjgb6t"]
|
||||
[gd_scene load_steps=52 format=4 uid="uid://bpnahwxmjgb6t"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://doxmbokehw8ci" path="res://Scripts/GameManager.cs" id="1_qxyfr"]
|
||||
[ext_resource type="Resource" uid="uid://b4u3cs6ietmow" path="res://Resources/Maps/Tutorial.tres" id="2_0xhda"]
|
||||
|
|
@ -33,6 +33,7 @@
|
|||
[ext_resource type="FontFile" uid="uid://cc3bdt3rt8a1o" path="res://fonts/Silver.ttf" id="25_covcf"]
|
||||
[ext_resource type="PackedScene" uid="uid://dx1urm7nttkqk" path="res://Scenes/Actors/level_teleporter.tscn" id="25_mfnhx"]
|
||||
[ext_resource type="LabelSettings" uid="uid://buk3e7bbwmnv1" path="res://Resources/Styles/Hud_Text_Style.tres" id="26_r1dm5"]
|
||||
[ext_resource type="Resource" uid="uid://xtg062tcon1j" path="res://Resources/Maps/Level1.tres" id="28_xlcit"]
|
||||
[ext_resource type="PackedScene" uid="uid://cnhgvn2salyxl" path="res://Scenes/Items/Icicle_Repeater.tscn" id="32_covcf"]
|
||||
[ext_resource type="PackedScene" uid="uid://cmr6fclttrpkb" path="res://Scenes/Items/Ammo1.tscn" id="33_0xhda"]
|
||||
|
||||
|
|
@ -325,6 +326,7 @@ vertical_alignment = 1
|
|||
[node name="LevelTeleporter" parent="Parallax2D/Tilemaps" instance=ExtResource("25_mfnhx")]
|
||||
position = Vector2(999, -40)
|
||||
LevelPath = "res://Scenes/test.tscn"
|
||||
Map = ExtResource("28_xlcit")
|
||||
IsEnabled = true
|
||||
|
||||
[node name="Label" type="Label" parent="Parallax2D/Tilemaps"]
|
||||
|
|
@ -342,6 +344,7 @@ horizontal_alignment = 1
|
|||
[node name="LevelTeleporter2" parent="Parallax2D/Tilemaps" instance=ExtResource("25_mfnhx")]
|
||||
position = Vector2(135, 423)
|
||||
LevelPath = "res://Scenes/test.tscn"
|
||||
Map = ExtResource("28_xlcit")
|
||||
IsEnabled = true
|
||||
|
||||
[node name="IcicleRepeater" parent="Parallax2D/Tilemaps" instance=ExtResource("32_covcf")]
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
|
|
@ -1,8 +1,10 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Cirno.Scripts.Resources;
|
||||
using Cirno.Scripts.Utils;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
using GTweens.Builders;
|
||||
using GTweensGodot.Extensions;
|
||||
|
||||
|
|
@ -201,6 +203,8 @@ public partial class GlobalState : Node
|
|||
_fader.TweenModulateAlpha(0, 1f).PlayUnpausable();
|
||||
}
|
||||
|
||||
private readonly string SaveNameFile = "user://savegame.save";
|
||||
|
||||
public void SaveGame()
|
||||
{
|
||||
var items = InventoryManager.Instance.Save();
|
||||
|
|
@ -213,16 +217,52 @@ public partial class GlobalState : Node
|
|||
{ "Level", SessionSettings.LevelNumber }
|
||||
};
|
||||
|
||||
var saveFile = FileAccess.Open("user://savegame.save", FileAccess.ModeFlags.Write);
|
||||
var saveFile = FileAccess.Open(SaveNameFile, FileAccess.ModeFlags.Write);
|
||||
|
||||
var jsonString = Json.Stringify(serializedSavedata);
|
||||
|
||||
saveFile.StoreLine(jsonString);
|
||||
|
||||
GD.Print("Saved data successfully");
|
||||
}
|
||||
|
||||
public void LoadGame()
|
||||
public bool LoadGame()
|
||||
{
|
||||
if (!FileAccess.FileExists(SaveNameFile))
|
||||
{
|
||||
return false; // Error! We don't have a save to load.
|
||||
}
|
||||
using var saveFile = FileAccess.Open(SaveNameFile, FileAccess.ModeFlags.Read);
|
||||
|
||||
var jsonString = saveFile.GetLine();
|
||||
var json = new Json();
|
||||
|
||||
var parseResult = json.Parse(jsonString);
|
||||
if (parseResult != Error.Ok)
|
||||
{
|
||||
GD.Print($"JSON Parse Error: {json.GetErrorMessage()} in {jsonString} at line {json.GetErrorLine()}");
|
||||
return false;
|
||||
}
|
||||
|
||||
var deserializedSaveData = new Dictionary<string, Variant>((Dictionary)json.Data);
|
||||
|
||||
Dictionary<string, int> items = (Dictionary<string, int>)deserializedSaveData["Items"];
|
||||
|
||||
int levelNumber = (int)deserializedSaveData["Level"];
|
||||
|
||||
var levelData = _mapsDatabase.Maps.FirstOrDefault(x => x.LevelId == levelNumber);
|
||||
if (levelData is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
this.SessionSettings.NewSession();
|
||||
SessionSettings.LevelNumber = levelNumber;
|
||||
SessionSettings.Items = items;
|
||||
|
||||
this.GotoScene(levelData);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void ResizeCursor(float scale)
|
||||
|
|
|
|||
|
|
@ -50,11 +50,19 @@ public partial class MainMenu : CanvasLayer
|
|||
|
||||
private void _on_start_button_pressed()
|
||||
{
|
||||
if (GameScene != null)
|
||||
if (GameScene == null) return;
|
||||
GlobalState.Session.NewSession();
|
||||
GlobalState.Session.AllowSaving = true;
|
||||
GlobalState.Instance.GotoScene(GameScene);
|
||||
//GetTree().ChangeSceneToFile(GameScene);
|
||||
}
|
||||
|
||||
private void OnContinuePressed()
|
||||
{
|
||||
if (GameScene == null) return;
|
||||
if (GlobalState.Instance.LoadGame())
|
||||
{
|
||||
GlobalState.Session.NewSession();
|
||||
GlobalState.Instance.GotoScene(GameScene);
|
||||
//GetTree().ChangeSceneToFile(GameScene);
|
||||
GlobalState.Session.AllowSaving = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue