cirnogodot/Scripts/GameManager.cs

427 lines
12 KiB
C#
Raw Normal View History

2025-01-22 11:35:37 +01:00
using Godot;
using System;
2025-02-11 11:50:45 +01:00
using System.Linq;
using System.Threading.Tasks;
2025-01-22 11:35:37 +01:00
using Cirno.Scripts;
2025-03-02 11:58:30 +01:00
using Cirno.Scripts.Components.FSM;
2025-04-30 16:11:25 +02:00
using Cirno.Scripts.Enums;
using Cirno.Scripts.Misc;
2025-02-11 11:50:45 +01:00
using Cirno.Scripts.Resources;
using Godot.Collections;
2025-02-27 08:37:55 +01:00
using Cirno.Scripts.Utils;
2025-04-28 14:58:41 +02:00
using Environment = Godot.Environment;
2025-01-22 11:35:37 +01:00
public partial class GameManager : Node2D
{
2025-02-28 11:17:28 +01:00
public static GameManager Instance { get; private set; }
2025-02-15 17:51:06 +01:00
private Hud _hud;
2025-03-05 12:27:15 +01:00
private PlayerFSMProxy _player;
2025-02-15 17:51:06 +01:00
public GameState GameState { get; private set; }
2025-03-05 12:27:15 +01:00
public PlayerStateMachine Player => _player.PlayerFSM;
2025-02-15 17:51:06 +01:00
private Node2D _cameraTarget;
public Vector2? PlayerPosition => _player?.GlobalPosition ?? null;
public Vector2? PlayerVelocity => _player?.Velocity ?? null;
2025-02-15 17:51:06 +01:00
2025-04-02 17:42:55 +02:00
[Export] public MapResource MapResource { get; private set; }
2025-02-15 17:51:06 +01:00
[Export] public PackedScene PlayerTemplate { get; set; }
2025-02-21 11:39:22 +01:00
[Export] public Dictionary<int, NodePath> SpawnMarkers { get; private set; } = new();
2025-02-28 11:17:28 +01:00
2025-02-21 11:39:22 +01:00
//[Export] public Marker2D PlayerSpawnMarker { get; set; }
2025-02-15 17:51:06 +01:00
private InventoryManager _inventoryManager { get; set; }
//private AlarmManager _alarmManager { get; set; }
//public InventoryManager Inventory => _inventoryManager;
//public AlarmManager AlarmManager => _alarmManager;
2025-03-01 20:50:47 +01:00
[Export]
2025-03-13 14:20:30 +01:00
public StringName PauseActionName { get; private set; } = "pause";
2025-03-01 20:50:47 +01:00
2025-03-25 13:51:35 +01:00
[Export]
public TilemapAvoidance NavigationTilemap { get; private set; }
2025-02-15 17:51:06 +01:00
private Node2D _bulletsContainer;
public Node2D BulletsContainer => _bulletsContainer;
[Signal]
public delegate void GameStateChangeEventHandler(GameState state);
2025-02-24 13:54:02 +01:00
[Signal]
public delegate void PlayerRespawnedEventHandler();
2025-03-05 12:27:15 +01:00
2025-03-01 20:50:47 +01:00
public Vector2 LastCheckpointPosition { get; set; }
private AudioManager _audioManager;
2025-03-12 18:46:17 +01:00
[Export]
public Node2D PlayerParentNode { get; set; }
2025-02-28 11:17:28 +01:00
2025-04-07 15:58:43 +02:00
[Export]
public int EggStartIndex = 0;
2025-04-09 11:43:44 +02:00
[Export] public NavigationRegion2D NavigationRegion { get; private set; }
2025-04-10 19:04:06 +02:00
[Signal] public delegate void ManagerReadyEventHandler();
2025-02-15 17:51:06 +01:00
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
2025-02-28 11:17:28 +01:00
Instance = this;
2025-04-28 14:58:41 +02:00
RenderingServer.SetDefaultClearColor(Colors.Black);
2025-04-30 16:11:25 +02:00
if (GlobalState.Instance.SessionSettings.GameMode is GameMode.Roguelite)
{
if (GlobalState.Instance.SessionSettings.LevelNumber < 0)
{
// TODO: Change based on which level we're going to
GlobalState.Instance.SessionSettings.LevelNumber = 0;
}
}
else
{
GlobalState.Session.LevelNumber = MapResource.LevelId;
}
2025-04-10 12:00:08 +02:00
2025-05-08 12:32:16 +02:00
GlobalState.Instance.ChangeCursor(false);
2025-04-04 09:53:29 +02:00
2025-04-02 18:39:37 +02:00
if (GlobalState.Instance.SessionSettings.AllowSaving)
{
GlobalState.Instance.SaveGame();
}
_audioManager = new AudioManager();
this.AddChild(_audioManager);
2025-02-15 17:51:06 +01:00
_hud = GetNodeOrNull<Hud>("HUD");
if (_hud == null) GD.Print("No HUD in scene.");
_cameraTarget = GetNodeOrNull<Node2D>("CameraTarget");
if (_cameraTarget == null) GD.Print("No camera target in scene.");
_inventoryManager = GetNodeOrNull<InventoryManager>("InventoryManager");
if (_inventoryManager == null) GD.Print("No inventory manager in scene.");
//_alarmManager = GetNode<AlarmManager>("AlarmManager");
SpawnBulletsContainer();
2025-02-19 15:45:21 +01:00
if (_hud != null)
{
this.GameStateChange += _hud.OnGameStateChanged;
}
2025-02-28 11:17:28 +01:00
2025-02-15 17:51:06 +01:00
if (_inventoryManager != null && _hud != null)
{
2025-03-05 18:55:30 +01:00
//_inventoryManager.ItemAdded += (item, currentAmount) => _hud.AddInventoryItem(item, currentAmount);
//_inventoryManager.ItemRemoved += (item, currentAmount) => _hud.RemoveInventoryItem(item, currentAmount);
2025-05-08 11:07:23 +02:00
_inventoryManager.WeaponUpdate += key => _hud.EquipWeapon(key);
2025-02-15 17:51:06 +01:00
}
2025-02-24 13:54:02 +01:00
PlayerRespawned += OnPlayerRespawned;
2025-02-28 11:17:28 +01:00
2025-02-15 17:51:06 +01:00
GameState = GameState.Playing;
2025-02-19 13:34:15 +01:00
//_ = DelayPlayerSpawn();
2025-02-20 21:26:51 +01:00
CallDeferred(MethodName.DelayPlayerSpawn);
2025-04-10 19:04:06 +02:00
CallDeferred(MethodName.OnFinished);
2025-02-15 17:51:06 +01:00
}
2025-04-10 19:04:06 +02:00
private void OnFinished()
{
EmitSignalManagerReady();
}
2025-02-24 13:54:02 +01:00
private void OnPlayerRespawned()
{
this.ClearBullets();
}
2025-02-21 11:39:22 +01:00
public void ApplyMapStartData(MapStartDataResource mapStartData)
{
2025-04-07 15:58:43 +02:00
EggStartIndex = mapStartData.EggIndex;
//MapStartData = mapStartData;
2025-02-21 11:39:22 +01:00
2025-04-07 15:58:43 +02:00
// StartingEquipment.AddRange(mapStartData.StartingEquipment);
2025-02-27 08:37:55 +01:00
}
2025-03-31 18:28:33 +02:00
public void AddMotivation(float motivation)
{
Player.AddMotivation(motivation);
}
2025-02-28 11:17:28 +01:00
public void ApplySessionState(SessionSettings settings)
2025-02-27 08:37:55 +01:00
{
2025-03-24 16:56:35 +01:00
//_inventoryManager.Load(settings.Items);
2025-02-21 11:39:22 +01:00
}
2025-03-05 12:27:15 +01:00
2025-02-15 17:51:06 +01:00
public override void _Process(double delta)
{
2025-03-01 20:50:47 +01:00
if (GameState is GameState.Paused && Input.IsActionJustPressed(PauseActionName))
2025-02-15 17:51:06 +01:00
{
2025-03-01 20:50:47 +01:00
Unpause();
2025-02-15 17:51:06 +01:00
}
}
2025-02-19 13:34:15 +01:00
private void DelayPlayerSpawn()
2025-02-15 17:51:06 +01:00
{
2025-02-21 11:39:22 +01:00
if (SpawnMarkers.Any())
2025-02-15 17:51:06 +01:00
{
SpawnPlayer();
}
else
{
GD.Print("No player spawn marker in scene.");
return;
}
if (_player != null && _hud != null)
{
2025-03-02 11:58:30 +01:00
// _player.HealthChanged += (newHealth, maxHealth) => _hud.UpdateHealth(newHealth, maxHealth);
//
// _player.ShieldChanged += (newShield, maxShield) => _hud.UpdateShield(newShield, maxShield);
//
// _player.InteractableAreaEntered += (interactable) => _hud.UpdateInteractable(interactable);
//
// _player.Death += () =>
// {
// // Show Game Over
// _hud.ShowGameOver();
// };
2025-02-15 17:51:06 +01:00
}
else
{
GD.Print("No player and hud in scene");
return;
}
2025-02-28 11:17:28 +01:00
// Wait before the player is fully initialized before spawning weapons on it
CallDeferred(MethodName.SpawnWeapons);
2025-02-15 17:51:06 +01:00
}
public void SpawnPlayer()
{
if (_player != null) return;
2025-02-28 11:17:28 +01:00
2025-02-15 17:51:06 +01:00
//_player = this.CreateChild<PlayerMovement>(PlayerTemplate, PlayerSpawnMarker.Position );
2025-03-05 12:27:15 +01:00
_player = PlayerTemplate.Instantiate<PlayerFSMProxy>();
2025-02-15 17:51:06 +01:00
2025-03-06 11:42:45 +01:00
//this.CallDeferred("add_child", _player);
2025-03-12 18:46:17 +01:00
if (PlayerParentNode is not null)
{
PlayerParentNode.AddChild(_player);
_player.Owner = PlayerParentNode;
}
else
{
this.AddChild(_player);
_player.Owner = this;
}
2025-02-15 17:51:06 +01:00
_player.Transform = this.GlobalTransform;
2025-03-06 11:42:45 +01:00
2025-02-21 11:39:22 +01:00
_player.GlobalPosition = GetStartPosition();
2025-02-28 11:17:28 +01:00
2025-03-03 09:44:38 +01:00
LastCheckpointPosition = _player.GlobalPosition;
2025-02-21 11:39:22 +01:00
//_player.GlobalPosition = PlayerSpawnMarker.Position;
2025-02-15 17:51:06 +01:00
CameraTargetPlayer();
2025-02-22 14:45:46 +01:00
2025-02-15 17:51:06 +01:00
//
// if (_cameraTarget != null)
// {
// _cameraTarget.Reparent(_player, true);
// _cameraTarget.GlobalPosition = _player.Position;
// }
}
2025-02-21 11:39:22 +01:00
private Vector2 GetStartPosition()
{
2025-04-07 15:58:43 +02:00
if (MapResource != null)
2025-02-21 11:39:22 +01:00
{
2025-04-07 15:58:43 +02:00
if (SpawnMarkers.TryGetValue(EggStartIndex, out var spawnMarker))
2025-02-21 11:39:22 +01:00
{
var marker = GetNode<Node2D>(spawnMarker);
2025-02-28 11:17:28 +01:00
2025-02-21 11:39:22 +01:00
return marker.Position; // Why position and not globalposition? I have no idea
}
}
2025-02-28 11:17:28 +01:00
2025-02-21 11:39:22 +01:00
var m = GetNode<Node2D>(SpawnMarkers.First().Value);
return m.GlobalPosition;
}
2025-02-15 17:51:06 +01:00
public void CameraTargetPlayer()
{
if (_player is null) return;
CameraTargetObject(_player);
}
2025-02-21 18:57:00 +01:00
public void CameraTargetObject(Node2D target, Vector2? offset = null)
2025-02-15 17:51:06 +01:00
{
if (_cameraTarget is null) return;
_cameraTarget.Reparent(target, true);
_cameraTarget.GlobalPosition = target.GlobalPosition;
2025-02-21 18:57:00 +01:00
if (offset.HasValue)
{
_cameraTarget.Position += offset.Value;
}
2025-02-28 11:17:28 +01:00
2025-02-15 17:51:06 +01:00
}
private void SpawnWeapons()
{
2025-04-07 15:58:43 +02:00
foreach (var startingItem in MapResource.StartData.StartingEquipment)
2025-02-15 17:51:06 +01:00
{
2025-04-07 15:58:43 +02:00
_inventoryManager.AddItem(startingItem);
2025-02-15 17:51:06 +01:00
}
2025-04-07 15:58:43 +02:00
foreach (var item in MapResource.StartData.RemoveEquipment)
2025-02-15 17:51:06 +01:00
{
2025-04-07 15:58:43 +02:00
_inventoryManager.RemoveItem(item);
2025-02-15 17:51:06 +01:00
}
}
private void SpawnBulletsContainer()
{
_bulletsContainer = new Node2D();
_bulletsContainer.Name = "BulletsContainer";
2025-02-24 10:58:00 +01:00
_bulletsContainer.ProcessMode = ProcessModeEnum.Pausable;
2025-02-15 17:51:06 +01:00
AddChild(_bulletsContainer);
}
2025-03-01 20:50:47 +01:00
// public void TogglePause()
// {
// if (GameState == GameState.Paused)
// {
// Unpause();
// }
// else if (GameState == GameState.Playing)
// {
// Pause();
// }
// }
2025-02-15 17:51:06 +01:00
public void Pause()
{
if (GameState == GameState.Playing)
{
ChangeState(GameState.Paused);
}
}
public void Unpause()
{
if (GameState == GameState.Paused)
{
2025-03-01 20:50:47 +01:00
CallDeferred(MethodName.ChangeState, (int)GameState.Playing);
//ChangeState(GameState.Playing);
2025-02-15 17:51:06 +01:00
}
}
2025-02-28 11:17:28 +01:00
public GameState ToggleControlMode()
{
if (GameState is GameState.Playing)
2025-02-23 22:38:33 +01:00
{
ChangeState(GameState.Controlling);
}
2025-02-28 11:17:28 +01:00
else if (GameState is GameState.Controlling)
2025-02-23 22:38:33 +01:00
{
ChangeState(GameState.Playing);
}
return GameState;
}
2025-02-15 17:51:06 +01:00
public void ChangeState(GameState state)
{
if (state == GameState) return;
GameState = state;
2025-03-01 20:50:47 +01:00
EmitSignal(SignalName.GameStateChange, (int)state);
2025-02-15 17:51:06 +01:00
GD.Print($"Game state changed to {state}");
2025-02-24 10:50:14 +01:00
switch (state)
{
case GameState.Paused:
case GameState.Dialogue:
2025-04-25 11:29:21 +02:00
case GameState.Shop:
2025-03-01 20:50:47 +01:00
case GameState.Inventory:
2025-05-08 12:32:16 +02:00
GlobalState.Instance.ChangeCursor(true);
2025-02-24 10:50:14 +01:00
GetTree().SetPause(true);
2025-03-19 01:27:20 +01:00
//Input.MouseMode = Input.MouseModeEnum.Visible;
2025-02-24 10:50:14 +01:00
break;
case GameState.Playing:
case GameState.Controlling:
2025-03-19 01:27:20 +01:00
//Input.MouseMode = Input.MouseModeEnum.Confined;
2025-05-08 12:32:16 +02:00
GlobalState.Instance.ChangeCursor(false);
2025-03-22 00:33:24 +01:00
DelayedUnpause();
//CallDeferred(MethodName.DelayedUnpause);
//GetTree().SetPause(false);
2025-03-15 00:06:47 +01:00
break;
2025-03-01 20:50:47 +01:00
case GameState.Menu:
2025-05-08 12:32:16 +02:00
GlobalState.Instance.ChangeCursor(true);
2025-03-19 01:27:20 +01:00
//Input.MouseMode = Input.MouseModeEnum.Visible;
2025-03-22 00:33:24 +01:00
DelayedUnpause();
//CallDeferred(MethodName.DelayedUnpause);
//GetTree().SetPause(false);
2025-02-24 10:50:14 +01:00
break;
}
2025-02-15 17:51:06 +01:00
}
2025-02-24 13:54:02 +01:00
2025-03-22 00:33:24 +01:00
private void DelayedUnpause()
{
GetTree().SetPause(false);
}
2025-02-24 13:54:02 +01:00
public void ClearBullets()
{
if (_bulletsContainer is null) return;
foreach (var node in _bulletsContainer.GetChildren())
{
node.QueueFree();
}
}
2025-03-25 13:51:35 +01:00
public void RecalculateTilemap(Vector2 position)
{
2025-04-09 11:43:44 +02:00
//CallDeferred(MethodName.RecalculateTilemapDeferred, position);
2025-03-25 13:51:35 +01:00
}
private void RecalculateTilemapDeferred(Vector2 position)
{
2025-04-09 11:43:44 +02:00
//NavigationTilemap.Recalculate(position);
GD.Print("Rebaking tilemap");
NavigationRegion.BakeNavigationPolygon(true);
}
public void RebakeNavigation()
{
GD.Print("Rebaking tilemap");
//NavigationRegion.BakeNavigationPolygon(true);
2025-03-25 13:51:35 +01:00
}
2025-02-06 15:57:03 +01:00
}
public enum GameState
{
2025-02-15 17:51:06 +01:00
Menu,
Paused,
Playing,
2025-02-23 22:38:33 +01:00
Dialogue,
2025-03-01 20:50:47 +01:00
Controlling,
2025-04-25 11:29:21 +02:00
Inventory,
Shop
2025-02-15 17:51:06 +01:00
}