Restructure loading of game
This commit is contained in:
@@ -19,26 +19,20 @@ public partial class Game : Node3D, IGame
|
||||
|
||||
IGameRepo IProvide<IGameRepo>.Value() => GameRepo;
|
||||
|
||||
IGame IProvide<IGame>.Value() => this;
|
||||
IPlayer IProvide<IPlayer>.Value() => _player;
|
||||
|
||||
IPlayer IProvide<IPlayer>.Value() => Player;
|
||||
|
||||
IMap IProvide<IMap>.Value() => Map;
|
||||
IMap IProvide<IMap>.Value() => _map;
|
||||
|
||||
private static SimpleInjector.Container _container;
|
||||
|
||||
public IInstantiator Instantiator { get; set; } = default!;
|
||||
|
||||
public IGameState GameState { get; set; } = default!;
|
||||
|
||||
public IGameRepo GameRepo { get; set; } = default!;
|
||||
|
||||
public GameState.IBinding GameBinding { get; set; } = default!;
|
||||
|
||||
[Dependency] public IAppRepo AppRepo => this.DependOn<IAppRepo>();
|
||||
|
||||
#region Nodes
|
||||
[Node] private IMap Map { get; set; } = default!;
|
||||
[Node] private Node PauseContainer { get; set; } = default!;
|
||||
|
||||
[Node] private InGameUI InGameUI { get; set; } = default!;
|
||||
|
||||
@@ -49,10 +43,6 @@ public partial class Game : Node3D, IGame
|
||||
[Node] private IPauseMenu PauseMenu { get; set; } = default!;
|
||||
|
||||
[Node] private Timer DoubleEXPTimer { get; set; } = default!;
|
||||
|
||||
[Node] private IPlayer Player { get; set; } = default!;
|
||||
|
||||
[Node] private MainMenu MainMenu { get; set; } = default!;
|
||||
#endregion
|
||||
|
||||
#region Save
|
||||
@@ -72,19 +62,28 @@ public partial class Game : Node3D, IGame
|
||||
|
||||
private EffectService _effectService;
|
||||
|
||||
private IInstantiator _instantiator;
|
||||
private Player _player;
|
||||
private Map _map;
|
||||
|
||||
public void Setup()
|
||||
{
|
||||
_container = new SimpleInjector.Container();
|
||||
Module.Bootstrap(_container);
|
||||
|
||||
_instantiator = new Instantiator(GetTree());
|
||||
_player = _instantiator.LoadAndInstantiate<Player>("res://src/player/Player.tscn");
|
||||
PauseContainer.AddChild(_player);
|
||||
|
||||
_map = _instantiator.LoadAndInstantiate<Map>("res://src/map/Map.tscn");
|
||||
PauseContainer.AddChild(_map);
|
||||
|
||||
GameRepo = _container.GetInstance<IGameRepo>();
|
||||
GameState = _container.GetInstance<IGameState>();
|
||||
GameState.Set(GameRepo);
|
||||
GameState.Set(AppRepo);
|
||||
GameState.Set(Player);
|
||||
GameState.Set(Map);
|
||||
GameState.Set(_player);
|
||||
GameState.Set(_map);
|
||||
GameState.Set(InGameUI);
|
||||
Instantiator = new Instantiator(GetTree());
|
||||
RescuedItems = new RescuedItemDatabase();
|
||||
|
||||
GameChunk = new SaveChunk<GameData>(
|
||||
@@ -95,12 +94,12 @@ public partial class Game : Node3D, IGame
|
||||
|
||||
PlayerData = new PlayerData()
|
||||
{
|
||||
PlayerStats = Player.Stats,
|
||||
Inventory = Player.Inventory
|
||||
PlayerStats = _player.Stats,
|
||||
Inventory = _player.Inventory
|
||||
},
|
||||
MapData = new MapData()
|
||||
{
|
||||
FloorScenes = Map.FloorScenes
|
||||
FloorScenes = _map.FloorScenes
|
||||
},
|
||||
RescuedItems = new RescuedItemDatabase()
|
||||
{
|
||||
@@ -145,13 +144,6 @@ public partial class Game : Node3D, IGame
|
||||
GameBinding
|
||||
.Handle((in GameState.Output.InitializeGame _) =>
|
||||
{
|
||||
InitializeGame();
|
||||
Map.LoadMap();
|
||||
GameRepo.Resume();
|
||||
InGameUI.Show();
|
||||
InGameUI.PlayerInfoUI.Activate();
|
||||
Player.TeleportPlayer(Map.CurrentFloor.GetPlayerSpawnPoint());
|
||||
Player.Activate();
|
||||
})
|
||||
.Handle((in GameState.Output.LoadGameFromFile _) =>
|
||||
{
|
||||
@@ -209,24 +201,24 @@ public partial class Game : Node3D, IGame
|
||||
.Handle((in GameState.Output.LoadNextFloor _) =>
|
||||
{
|
||||
FloorClearMenu.FadeOut();
|
||||
Map.SpawnNextFloor();
|
||||
if (Player.EquippedWeapon.Value.ItemTag == ItemTag.BreaksOnChange)
|
||||
_map.SpawnNextFloor();
|
||||
if (_player.EquippedWeapon.Value.ItemTag == ItemTag.BreaksOnChange)
|
||||
{
|
||||
var itemToDestroy = Player.EquippedWeapon.Value;
|
||||
Player.Unequip(itemToDestroy);
|
||||
Player.Inventory.Remove(itemToDestroy);
|
||||
var itemToDestroy = _player.EquippedWeapon.Value;
|
||||
_player.Unequip(itemToDestroy);
|
||||
_player.Inventory.Remove(itemToDestroy);
|
||||
}
|
||||
if (Player.EquippedArmor.Value.ItemTag == ItemTag.BreaksOnChange)
|
||||
if (_player.EquippedArmor.Value.ItemTag == ItemTag.BreaksOnChange)
|
||||
{
|
||||
var itemToDestroy = Player.EquippedArmor.Value;
|
||||
Player.Unequip(itemToDestroy);
|
||||
Player.Inventory.Remove(itemToDestroy);
|
||||
var itemToDestroy = _player.EquippedArmor.Value;
|
||||
_player.Unequip(itemToDestroy);
|
||||
_player.Inventory.Remove(itemToDestroy);
|
||||
}
|
||||
if (Player.EquippedAccessory.Value.ItemTag == ItemTag.BreaksOnChange)
|
||||
if (_player.EquippedAccessory.Value.ItemTag == ItemTag.BreaksOnChange)
|
||||
{
|
||||
var itemToDestroy = Player.EquippedAccessory.Value;
|
||||
Player.Unequip(itemToDestroy);
|
||||
Player.Inventory.Remove(itemToDestroy);
|
||||
var itemToDestroy = _player.EquippedAccessory.Value;
|
||||
_player.Unequip(itemToDestroy);
|
||||
_player.Inventory.Remove(itemToDestroy);
|
||||
}
|
||||
FloorClearMenu.FadeOut();
|
||||
})
|
||||
@@ -248,27 +240,31 @@ public partial class Game : Node3D, IGame
|
||||
|
||||
DoubleEXPTimer.Timeout += DoubleEXPTimer_Timeout;
|
||||
|
||||
MainMenu.NewGame += OnNewGame;
|
||||
MainMenu.LoadGame += OnLoadGame;
|
||||
MainMenu.Quit += OnQuit;
|
||||
|
||||
DeathMenu.NewGame += OnContinueGame;
|
||||
DeathMenu.QuitGame += OnQuit;
|
||||
|
||||
GameRepo.IsPaused.Sync += IsPaused_Sync;
|
||||
|
||||
_effectService = new EffectService(this, Player, Map);
|
||||
_effectService = new EffectService(this, _player, _map);
|
||||
}
|
||||
|
||||
MainMenu.Show();
|
||||
public void OnReady()
|
||||
{
|
||||
InitializeGame();
|
||||
_map.LoadMap();
|
||||
GameRepo.Resume();
|
||||
InGameUI.Show();
|
||||
InGameUI.PlayerInfoUI.Activate();
|
||||
_player.TeleportPlayer(_map.CurrentFloor.GetPlayerSpawnPoint());
|
||||
_player.Activate();
|
||||
}
|
||||
|
||||
private void FloorClearMenu_SaveAndExit()
|
||||
{
|
||||
//SaveFile.Save();
|
||||
Player.Deactivate();
|
||||
_player.Deactivate();
|
||||
GameState.Input(new GameState.Input.ReturnToMainMenu());
|
||||
InGameUI.Hide();
|
||||
MainMenu.FadeIn();
|
||||
}
|
||||
|
||||
private void FloorClearMenu_GoToNextFloor() => GameState.Input(new GameState.Input.LoadNextFloor());
|
||||
@@ -276,8 +272,8 @@ public partial class Game : Node3D, IGame
|
||||
|
||||
public void InitializeGame()
|
||||
{
|
||||
Map.InitializeMapData();
|
||||
Player.InitializePlayerState();
|
||||
_map.InitializeMapData();
|
||||
_player.InitializePlayerState();
|
||||
}
|
||||
|
||||
public void FloorExitReached()
|
||||
@@ -327,11 +323,11 @@ public partial class Game : Node3D, IGame
|
||||
thrown.Throw(_effectService);
|
||||
}
|
||||
|
||||
public IDungeonFloor CurrentFloor => Map.CurrentFloor;
|
||||
public IDungeonFloor CurrentFloor => _map.CurrentFloor;
|
||||
|
||||
public void EnemyDefeated(Vector3 defeatedLocation, EnemyStatResource resource)
|
||||
{
|
||||
Player.GainExp(resource.ExpFromDefeat * GameRepo.ExpRate);
|
||||
_player.GainExp(resource.ExpFromDefeat * GameRepo.ExpRate);
|
||||
DropRestorative(defeatedLocation);
|
||||
}
|
||||
|
||||
@@ -339,10 +335,10 @@ public partial class Game : Node3D, IGame
|
||||
{
|
||||
var itemDb = new ItemDatabase();
|
||||
|
||||
var currentIndex = Player.Inventory.Items.IndexOf(itemToReroll);
|
||||
var currentIndex = _player.Inventory.Items.IndexOf(itemToReroll);
|
||||
|
||||
if (insertIntoInventory)
|
||||
Player.Inventory.Remove(itemToReroll);
|
||||
_player.Inventory.Remove(itemToReroll);
|
||||
|
||||
InventoryItem rolledItem = null;
|
||||
|
||||
@@ -360,7 +356,7 @@ public partial class Game : Node3D, IGame
|
||||
rolledItem = itemDb.PickItem(consumableItem);
|
||||
|
||||
if (insertIntoInventory)
|
||||
Player.Inventory.TryInsert(rolledItem, currentIndex);
|
||||
_player.Inventory.TryInsert(rolledItem, currentIndex);
|
||||
|
||||
return rolledItem;
|
||||
}
|
||||
@@ -398,7 +394,7 @@ public partial class Game : Node3D, IGame
|
||||
GameRepo.Resume();
|
||||
}
|
||||
|
||||
private void GameEventDepot_RestorativePickedUp(IHealthPack obj) => Player.Stats.SetCurrentVT(Player.Stats.CurrentVT.Value + (int)obj.RestoreAmount);
|
||||
private void GameEventDepot_RestorativePickedUp(IHealthPack obj) => _player.Stats.SetCurrentVT(_player.Stats.CurrentVT.Value + (int)obj.RestoreAmount);
|
||||
|
||||
private void IsPaused_Sync(bool isPaused) => GetTree().Paused = isPaused;
|
||||
|
||||
@@ -408,15 +404,15 @@ public partial class Game : Node3D, IGame
|
||||
|
||||
private void EnactConsumableItemEffects(ConsumableItem consumableItem)
|
||||
{
|
||||
if (Player.Stats.CurrentHP == Player.Stats.MaximumHP && consumableItem.RaiseHPAmount > 0)
|
||||
Player.RaiseHP(consumableItem.RaiseHPAmount);
|
||||
if (Player.Stats.CurrentVT == Player.Stats.MaximumVT && consumableItem.RaiseVTAmount > 0)
|
||||
Player.RaiseVT(consumableItem.RaiseVTAmount);
|
||||
if (_player.Stats.CurrentHP == _player.Stats.MaximumHP && consumableItem.RaiseHPAmount > 0)
|
||||
_player.RaiseHP(consumableItem.RaiseHPAmount);
|
||||
if (_player.Stats.CurrentVT == _player.Stats.MaximumVT && consumableItem.RaiseVTAmount > 0)
|
||||
_player.RaiseVT(consumableItem.RaiseVTAmount);
|
||||
|
||||
if (consumableItem.HealHPAmount > 0 && Player.Stats.CurrentHP != Player.Stats.MaximumHP)
|
||||
Player.HealHP(consumableItem.HealHPAmount);
|
||||
if (consumableItem.HealVTAmount > 0 && Player.Stats.CurrentVT != Player.Stats.MaximumVT)
|
||||
Player.HealVT(consumableItem.HealVTAmount);
|
||||
if (consumableItem.HealHPAmount > 0 && _player.Stats.CurrentHP != _player.Stats.MaximumHP)
|
||||
_player.HealHP(consumableItem.HealHPAmount);
|
||||
if (consumableItem.HealVTAmount > 0 && _player.Stats.CurrentVT != _player.Stats.MaximumVT)
|
||||
_player.HealVT(consumableItem.HealVTAmount);
|
||||
}
|
||||
|
||||
private void EnactEffectItemEffects(EffectItem effectItem)
|
||||
@@ -475,22 +471,22 @@ public partial class Game : Node3D, IGame
|
||||
GameRepo.CloseInventory();
|
||||
break;
|
||||
case ThrowableItemTag.TeleportToRandomLocation:
|
||||
_effectService.TeleportToRandomRoom(Player);
|
||||
_effectService.TeleportToRandomRoom(_player);
|
||||
GameRepo.CloseInventory();
|
||||
break;
|
||||
case ThrowableItemTag.CanChangeAffinity:
|
||||
_effectService.ChangeAffinity(throwableItem);
|
||||
break;
|
||||
case ThrowableItemTag.WarpToExitIfFound:
|
||||
_effectService.WarpToExit(Player);
|
||||
_effectService.WarpToExit(_player);
|
||||
GameRepo.CloseInventory();
|
||||
break;
|
||||
}
|
||||
|
||||
if (throwableItem.HealHPAmount > 0)
|
||||
Player.HealHP(throwableItem.HealHPAmount);
|
||||
_player.HealHP(throwableItem.HealHPAmount);
|
||||
if (throwableItem.HealVTAmount > 0)
|
||||
Player.HealVT(throwableItem.HealVTAmount);
|
||||
_player.HealVT(throwableItem.HealVTAmount);
|
||||
}
|
||||
|
||||
private void RemoveItemOrSubtractFromItemCount(InventoryItem item)
|
||||
@@ -504,7 +500,6 @@ public partial class Game : Node3D, IGame
|
||||
private void OnNewGame()
|
||||
{
|
||||
GameState.Input(new GameState.Input.NewGame());
|
||||
MainMenu.Hide();
|
||||
}
|
||||
|
||||
private void OnContinueGame()
|
||||
@@ -515,11 +510,9 @@ public partial class Game : Node3D, IGame
|
||||
private void OnLoadGame()
|
||||
{
|
||||
GameState.Input(new GameState.Input.LoadGame());
|
||||
MainMenu.Hide();
|
||||
}
|
||||
|
||||
private void OnQuit()
|
||||
{
|
||||
MainMenu.Hide();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user