cirnogodot/Scripts/GameManager.cs

367 lines
9.6 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-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-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;
[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
2025-02-21 11:39:22 +01:00
[Export] public Array<LootItem> StartingEquipment { get; private set; } = new();
2025-02-15 17:51:06 +01:00
private InventoryManager _inventoryManager { get; set; }
2025-02-21 11:39:22 +01:00
[Export]
public MapStartDataResource MapStartData { get; private set; }
2025-02-20 21:26:51 +01:00
2025-02-15 17:51:06 +01:00
//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-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; }
2025-03-12 18:46:17 +01:00
[Export]
public Node2D PlayerParentNode { get; set; }
2025-02-28 11:17:28 +01:00
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-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);
_inventoryManager.WeaponEquip += 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-02-15 17:51:06 +01:00
}
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)
{
MapStartData = mapStartData;
StartingEquipment.AddRange(mapStartData.StartingEquipment);
2025-02-27 08:37:55 +01:00
}
2025-02-28 11:17:28 +01:00
public void ApplySessionState(SessionSettings settings)
2025-02-27 08:37:55 +01:00
{
2025-02-28 11:17:28 +01:00
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-28 11:17:28 +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()
{
if (MapStartData != null)
{
if (SpawnMarkers.TryGetValue(MapStartData.EggIndex, out var spawnMarker))
{
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()
{
if (!StartingEquipment.Any())
{
GD.Print("No items to spawn on Player");
return;
}
foreach (var startingItem in StartingEquipment)
{
// Now automatically taken care of by the event
// switch (startingItem.Item)
// {
// case ItemTypes.Weapon:
// SpawnPlayerWeapon(startingItem);
//
// //_player.EquippedWeapon ??= weapon;
// break;
// }
_inventoryManager.AddItem(startingItem);
}
}
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-03-01 20:50:47 +01:00
case GameState.Inventory:
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-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-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-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,
Inventory
2025-02-15 17:51:06 +01:00
}