Skip to content

Commit ebfc369

Browse files
author
myjimnelson
authored
Load game works from main menu (#30)
* Added file dialog, unsure how to chg scene w data * Checkpoint toward loading file Having to use a global singleton to pass file path * Loading works! Imports if a .SAV chosen, otherwise tries a C7 load (untested)
1 parent 887c147 commit ebfc369

File tree

5 files changed

+64
-6
lines changed

5 files changed

+64
-6
lines changed

C7/Game.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ enum GameState {
3232
private KinematicBody2D Player;
3333

3434
Stopwatch loadTimer = new Stopwatch();
35+
GlobalSingleton Global;
36+
3537

3638
public override void _EnterTree()
3739
{
@@ -41,7 +43,9 @@ public override void _EnterTree()
4143
// Called when the node enters the scene tree for the first time.
4244
public override void _Ready()
4345
{
44-
controller = CreateGame.createGame(genBasicTerrainNoiseMap);
46+
Global = GetNode<GlobalSingleton>("/root/GlobalSingleton");
47+
controller = CreateGame.createGame(Global.LoadGamePath, Global.DefaultBicPath);
48+
Global.ResetLoadGamePath();
4549
var map = MapInteractions.GetWholeMap();
4650
Civ3Map baseTerrainMap = new Civ3Map(map.numTilesWide, map.numTilesTall);
4751
baseTerrainMap.Civ3Tiles = map.tiles;

C7/GlobalSingleton.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Godot;
2+
3+
/****
4+
Need to pass values from one scene to another, particularly when loading
5+
a game in main menu. This script is set to auto load in project settings.
6+
See https://docs.godotengine.org/en/stable/getting_started/step_by_step/singletons_autoload.html
7+
****/
8+
public class GlobalSingleton : Node
9+
{
10+
// Will have main menu file picker set this and Game.cs pass it to C7Engine.createGame
11+
// which then should blank it again to prevent reloading same if going back to main menu
12+
// and back to game
13+
public string LoadGamePath;
14+
// For now this needs to get passed to QueryCiv3 when importing.
15+
public string DefaultBicPath { get => Util.GetCiv3Path() + @"/Conquests/conquests.biq"; }
16+
// This is the 'static map' used in lieu of terrain generation
17+
public string DefaultGamePath { get => @"./c7-static-map-save.json"; }
18+
public void ResetLoadGamePath()
19+
{
20+
LoadGamePath = DefaultGamePath;
21+
}
22+
}

C7/MainMenu.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,21 @@ public class MainMenu : Node2D
99
ImageTexture InactiveButton;
1010
ImageTexture HoverButton;
1111
TextureRect MainMenuBackground;
12+
Util.Civ3FileDialog LoadDialog;
13+
GlobalSingleton Global;
1214

1315
// Called when the node enters the scene tree for the first time.
1416
public override void _Ready()
1517
{
1618
GD.Print("Hello world!");
19+
// To pass data between scenes, putting path string in a global singleton and reading it later in createGame
20+
Global = GetNode<GlobalSingleton>("/root/GlobalSingleton");
21+
Global.ResetLoadGamePath();
1722
DisplayTitleScreen();
23+
LoadDialog = new Util.Civ3FileDialog();
24+
LoadDialog.RelPath = @"Conquests/Saves";
25+
LoadDialog.Connect("file_selected", this, nameof(_on_FileDialog_file_selected));
26+
GetNode<CanvasLayer>("CanvasLayer").AddChild(LoadDialog);
1827
}
1928

2029
private void DisplayTitleScreen()
@@ -28,7 +37,7 @@ private void DisplayTitleScreen()
2837
AddButton("New Game", 160, "StartGame");
2938
AddButton("Quick Start", 195, "StartGame");
3039
AddButton("Tutorial", 230, "StartGame");
31-
AddButton("Load Game", 265, "StartGame");
40+
AddButton("Load Game", 265, "LoadGame");
3241
AddButton("Load Scenario", 300, "StartGame");
3342
AddButton("Hall of Fame", 335, "HallOfFame");
3443
AddButton("Preferences", 370, "Preferences");
@@ -73,6 +82,13 @@ public void StartGame()
7382
GetTree().ChangeScene("res://C7Game.tscn");
7483
}
7584

85+
public void LoadGame()
86+
{
87+
GD.Print("Real Load button pressed");
88+
PlayButtonPressedSound();
89+
LoadDialog.Popup_();
90+
}
91+
7692
public void showCredits()
7793
{
7894
GD.Print("Credits button pressed");
@@ -102,4 +118,11 @@ private void PlayButtonPressedSound()
102118
player.Stream = wav;
103119
player.Play();
104120
}
121+
private void _on_FileDialog_file_selected(string path)
122+
{
123+
GD.Print("Loading " + path);
124+
Global.LoadGamePath = path;
125+
GetTree().ChangeScene("res://C7Game.tscn");
126+
127+
}
105128
}

C7/project.godot

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ run/main_scene="res://MainMenu.tscn"
1919
boot_splash/image="res://icon.png"
2020
config/icon="res://icon.png"
2121

22+
[autoload]
23+
24+
GlobalSingleton="*res://GlobalSingleton.cs"
25+
2226
[display]
2327

2428
window/size/height=768

C7Engine/EntryPoints/CreateGame.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,16 @@ public class CreateGame
1111
* quickly. By keeping all the client-callable APIs in the EntryPoints folder,
1212
* hopefully it won't be too much of a goose hunt to refactor it later if we decide to do so.
1313
**/
14-
public static Player createGame(GameMap.TerrainNoiseMapGenerator terrainGen)
14+
public static Player createGame(string loadFilePath, string defaultBicPath)
1515
{
16-
// I think this path is relative from the C7 folder, not C7GameEngine, if that matters
17-
// TODO: Unsure if this will work when exported for distribution
18-
C7SaveFormat save = C7SaveFormat.Load(@"./c7-static-map-save.json");
16+
C7SaveFormat save;
17+
if (loadFilePath.EndsWith("SAV", System.StringComparison.CurrentCultureIgnoreCase))
18+
{
19+
save = ImportCiv3.ImportSav(loadFilePath, defaultBicPath);
20+
}
21+
else {
22+
save = C7SaveFormat.Load(loadFilePath);
23+
}
1924
EngineStorage.setGameData(save.GameData);
2025
// possibly do something with save.Rules here when it exists
2126
// and maybe consider if we have any need to keep a reference to the save object handy...probably not

0 commit comments

Comments
 (0)