mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-11 18:45:53 +00:00
Player spawning and isometric in menu
This commit is contained in:
parent
a11f25e19d
commit
1229613def
10 changed files with 286 additions and 43 deletions
|
|
@ -1,24 +1,54 @@
|
|||
using Cirno.Scripts.Enums;
|
||||
using System.Linq;
|
||||
using Cirno.Scripts.Components.FSM._3DPlayer;
|
||||
using Cirno.Scripts.Enums;
|
||||
using Cirno.Scripts.Misc;
|
||||
using Cirno.Scripts.Resources;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
|
||||
namespace Cirno.Scripts;
|
||||
|
||||
public partial class GameController : Node
|
||||
{
|
||||
public static GameController Instance { get; private set; }
|
||||
|
||||
|
||||
private Hud _hud;
|
||||
private IsoPlayerFSMProxy _player;
|
||||
public IsoPlayerStateMachine Player => _player.PlayerFSM;
|
||||
|
||||
[Export] private Marker3D _cameraTarget;
|
||||
public Vector3? PlayerPosition => _player?.GlobalPosition ?? null;
|
||||
public Vector3? PlayerVelocity => _player?.Velocity ?? null;
|
||||
public GameState GameState { get; private set; }
|
||||
|
||||
[Signal] public delegate void GameStateChangeEventHandler(GameState state);
|
||||
|
||||
[Signal] public delegate void ManagerReadyEventHandler();
|
||||
|
||||
|
||||
[Signal]
|
||||
public delegate void GameStateChangeEventHandler(GameState state);
|
||||
|
||||
[Signal]
|
||||
public delegate void ManagerReadyEventHandler();
|
||||
|
||||
[Export] public StringName PauseActionName { get; private set; } = "pause";
|
||||
|
||||
[Export] public MapResource MapResource { get; private set; }
|
||||
|
||||
[Export] public PackedScene PlayerTemplate { get; set; }
|
||||
|
||||
[Export] public Dictionary<int, NodePath> SpawnMarkers { get; private set; } = new();
|
||||
|
||||
[Export]
|
||||
public int EggStartIndex = 0;
|
||||
|
||||
[Export] public Node3D PlayerParentNode { get; set; }
|
||||
private InventoryManager _inventoryManager { get; set; }
|
||||
|
||||
private Vector3 _lastCheckPointPosition;
|
||||
|
||||
public Vector3 LastCheckPointPosition
|
||||
{
|
||||
get => _lastCheckPointPosition;
|
||||
set => _lastCheckPointPosition = value;
|
||||
}
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
Instance = this;
|
||||
|
|
@ -35,28 +65,27 @@ public partial class GameController : Node
|
|||
{
|
||||
//GlobalState.Session.LevelNumber = MapResource.LevelId;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
GlobalState.Instance.ChangeCursor(false);
|
||||
|
||||
|
||||
if (GlobalState.Instance.SessionSettings.AllowSaving)
|
||||
{
|
||||
GlobalState.Instance.SaveGame();
|
||||
}
|
||||
|
||||
|
||||
//_audioManager = new AudioManager();
|
||||
// this.AddChild(_audioManager);
|
||||
|
||||
// this.AddChild(_audioManager);
|
||||
|
||||
_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.");
|
||||
_inventoryManager = GetNodeOrNull<InventoryManager>("InventoryManager");
|
||||
if (_inventoryManager == null) GD.Print("No inventory manager in scene.");
|
||||
|
||||
|
||||
//SpawnBulletsContainer();
|
||||
|
||||
if (_hud != null)
|
||||
|
|
@ -64,28 +93,26 @@ public partial class GameController : Node
|
|||
this.GameStateChange += _hud.OnGameStateChanged;
|
||||
}
|
||||
|
||||
// if (_inventoryManager != null && _hud != null)
|
||||
// {
|
||||
//
|
||||
// _inventoryManager.WeaponUpdate += key => _hud.EquipWeapon(key);
|
||||
// }
|
||||
if (_inventoryManager != null && _hud != null)
|
||||
{
|
||||
_inventoryManager.WeaponUpdate += key => _hud.EquipWeapon(key);
|
||||
}
|
||||
|
||||
//PlayerRespawned += OnPlayerRespawned;
|
||||
|
||||
GameState = GameState.Playing;
|
||||
|
||||
|
||||
|
||||
//CallDeferred(MethodName.DelayPlayerSpawn);
|
||||
CallDeferred(MethodName.DelayPlayerSpawn);
|
||||
|
||||
CallDeferred(MethodName.OnFinished);
|
||||
}
|
||||
|
||||
|
||||
private void OnFinished()
|
||||
{
|
||||
EmitSignalManagerReady();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
if (GameState is GameState.Paused && Input.IsActionJustPressed(PauseActionName))
|
||||
|
|
@ -93,7 +120,7 @@ public partial class GameController : Node
|
|||
Unpause();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void Pause()
|
||||
{
|
||||
if (GameState == GameState.Playing)
|
||||
|
|
@ -139,7 +166,7 @@ public partial class GameController : Node
|
|||
case GameState.Shop:
|
||||
case GameState.Inventory:
|
||||
GlobalState.Instance.ChangeCursor(true);
|
||||
|
||||
|
||||
GetTree().SetPause(true);
|
||||
//Input.MouseMode = Input.MouseModeEnum.Visible;
|
||||
break;
|
||||
|
|
@ -165,4 +192,123 @@ public partial class GameController : Node
|
|||
{
|
||||
GetTree().SetPause(false);
|
||||
}
|
||||
|
||||
private void DelayPlayerSpawn()
|
||||
{
|
||||
if (SpawnMarkers.Any())
|
||||
{
|
||||
SpawnPlayer();
|
||||
}
|
||||
else
|
||||
{
|
||||
GD.Print("No player spawn marker in scene.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (_player != null && _hud != null)
|
||||
{
|
||||
// _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();
|
||||
// };
|
||||
}
|
||||
else
|
||||
{
|
||||
GD.Print("No player and hud in scene");
|
||||
return;
|
||||
}
|
||||
|
||||
// Wait before the player is fully initialized before spawning weapons on it
|
||||
CallDeferred(MethodName.SpawnWeapons);
|
||||
}
|
||||
|
||||
private void SpawnWeapons()
|
||||
{
|
||||
foreach (var startingItem in MapResource.StartData.StartingEquipment)
|
||||
{
|
||||
_inventoryManager.AddItem(startingItem);
|
||||
}
|
||||
|
||||
foreach (var item in MapResource.StartData.RemoveEquipment)
|
||||
{
|
||||
_inventoryManager.RemoveItem(item);
|
||||
}
|
||||
}
|
||||
|
||||
public void SpawnPlayer()
|
||||
{
|
||||
if (_player != null) return;
|
||||
|
||||
//_player = this.CreateChild<PlayerMovement>(PlayerTemplate, PlayerSpawnMarker.Position );
|
||||
_player = PlayerTemplate.Instantiate<IsoPlayerFSMProxy>();
|
||||
|
||||
if (PlayerParentNode is not null)
|
||||
{
|
||||
PlayerParentNode.AddChild(_player);
|
||||
_player.Owner = PlayerParentNode;
|
||||
_player.Transform = PlayerParentNode.GlobalTransform;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.AddChild(_player);
|
||||
_player.Owner = this;
|
||||
}
|
||||
|
||||
_player.GlobalPosition = GetStartPosition();
|
||||
|
||||
LastCheckPointPosition = _player.GlobalPosition;
|
||||
|
||||
//_player.GlobalPosition = PlayerSpawnMarker.Position;
|
||||
|
||||
CameraTargetPlayer();
|
||||
|
||||
//
|
||||
// if (_cameraTarget != null)
|
||||
// {
|
||||
// _cameraTarget.Reparent(_player, true);
|
||||
// _cameraTarget.GlobalPosition = _player.Position;
|
||||
// }
|
||||
}
|
||||
|
||||
public void CameraTargetPlayer()
|
||||
{
|
||||
if (_player is null) return;
|
||||
CameraTargetObject(_player);
|
||||
}
|
||||
|
||||
public void CameraTargetObject(Node3D target, Vector3? offset = null)
|
||||
{
|
||||
if (_cameraTarget is null) return;
|
||||
_cameraTarget.Reparent(target, true);
|
||||
_cameraTarget.GlobalPosition = target.GlobalPosition;
|
||||
|
||||
if (offset.HasValue)
|
||||
{
|
||||
_cameraTarget.Position += offset.Value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Vector3 GetStartPosition()
|
||||
{
|
||||
if (MapResource != null)
|
||||
{
|
||||
if (SpawnMarkers.TryGetValue(EggStartIndex, out var spawnMarker))
|
||||
{
|
||||
var marker = GetNode<Node3D>(spawnMarker);
|
||||
|
||||
return marker.Position; // Why position and not globalposition? I have no idea
|
||||
}
|
||||
}
|
||||
|
||||
var m = GetNode<Node3D>(SpawnMarkers.First().Value);
|
||||
return m.GlobalPosition;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue