Rework game over logic and game initialization

This commit is contained in:
2025-10-27 15:04:01 -07:00
parent 720696aed0
commit 7e6dca1c29
46 changed files with 653 additions and 610 deletions

View File

@@ -1,23 +1,23 @@
using Chickensoft.AutoInject;
using Chickensoft.GodotNodeInterfaces;
using Chickensoft.Introspection;
using Chickensoft.SaveFileBuilder;
using Godot;
using SimpleInjector;
using System;
using Zennysoft.Ma.Adapter;
using Zennysoft.Ma.Adapter.Entity;
namespace Zennysoft.Game.Ma;
[Meta(typeof(IAutoNode))]
public partial class Player : CharacterBody3D, IPlayer
public partial class Player : CharacterBody3D, IPlayer, IProvide<IPlayer>
{
#region Dependency Injection
public override void _Notification(int what) => this.Notify(what);
private PlayerLogic.IBinding PlayerBinding { get; set; } = default!;
IPlayer IProvide<IPlayer>.Value() => this;
[Dependency] private IGameRepo _gameRepo => this.DependOn<IGameRepo>(() => new GameRepo());
private PlayerLogic.IBinding PlayerBinding { get; set; } = default!;
#endregion
public IHealthComponent HealthComponent { get; private set; }
@@ -40,16 +40,12 @@ public partial class Player : CharacterBody3D, IPlayer
public IInventory Inventory { get; private set; } = default!;
public event Action PlayerDied;
private PlayerLogic.Settings Settings { get; set; } = default!;
private IPlayerLogic PlayerLogic { get; set; } = default!;
#region Dependencies
[Dependency]
public IGame Game => this.DependOn<IGame>();
#endregion
#region Exports
[ExportGroup("Movement")]
[Export(PropertyHint.Range, "0, 100, 0.1")]
@@ -99,9 +95,17 @@ public partial class Player : CharacterBody3D, IPlayer
private float _knockbackStrength = 0.0f;
private Vector3 _knockbackDirection = Vector3.Zero;
#region Initialization
public Player()
private ItemReroller _itemReroller;
public void Initialize()
{
var container = new SimpleInjector.Container();
container.Register<IPlayerLogic, PlayerLogic>(Lifestyle.Singleton);
PlayerLogic = container.GetInstance<IPlayerLogic>();
PlayerLogic.Set(this as IPlayer);
PlayerLogic.Set(Settings);
Inventory = new Inventory();
HealthComponent = new HealthComponent(InitialHP);
VTComponent = new VTComponent(InitialVT);
@@ -110,21 +114,9 @@ public partial class Player : CharacterBody3D, IPlayer
ExperiencePointsComponent = new ExperiencePointsComponent();
LuckComponent = new LuckComponent(InitialLuck);
EquipmentComponent = new EquipmentComponent();
}
public void Setup()
{
var container = new SimpleInjector.Container();
container.Register<IPlayerLogic, PlayerLogic>(Lifestyle.Singleton);
_itemReroller = new ItemReroller(ItemDatabase.Instance);
PlayerLogic = container.GetInstance<IPlayerLogic>();
PlayerLogic.Set(this as IPlayer);
PlayerLogic.Set(Settings);
PlayerLogic.Set(_gameRepo);
}
public void OnResolved()
{
Settings = new PlayerLogic.Settings() { RotationSpeed = RotationSpeed, MoveSpeed = MoveSpeed, Acceleration = Acceleration };
PlayerBinding = PlayerLogic.Bind();
@@ -142,6 +134,23 @@ public partial class Player : CharacterBody3D, IPlayer
this.Provide();
}
public void ResetPlayerData()
{
foreach (var item in Inventory.Items)
Inventory.Remove(item);
HealthComponent.Reset();
VTComponent.Reset();
AttackComponent.Reset();
DefenseComponent.Reset();
ExperiencePointsComponent.Reset();
LuckComponent.Reset();
EquipmentComponent.Reset();
HealthTimer.Timeout += OnHealthTimerTimeout;
}
#region Initialization
public void OnReady()
{
Hitbox.AreaEntered += Hitbox_AreaEntered;
@@ -149,7 +158,6 @@ public partial class Player : CharacterBody3D, IPlayer
SwordSlashAnimation.Position = GetViewport().GetVisibleRect().Size / 2;
HealthComponent.HealthReachedZero += Die;
HealthTimer.WaitTime = _healthTimerWaitTime;
HealthTimer.Timeout += OnHealthTimerTimeout;
SetProcessInput(false);
SetPhysicsProcess(false);
}
@@ -210,7 +218,7 @@ public partial class Player : CharacterBody3D, IPlayer
SwordSlashAnimation.Stop();
SetProcessInput(false);
SetPhysicsProcess(false);
Game.GameOver();
PlayerDied?.Invoke();
}
public override void _Input(InputEvent @event)
@@ -233,7 +241,7 @@ public partial class Player : CharacterBody3D, IPlayer
{
if (equipable.ItemTag == ItemTag.MysteryItem)
{
var rerolledItem = Game.RerollItem(equipable) as EquipableItem;
var rerolledItem = _itemReroller.RerollItem(equipable, Inventory);
Equip(rerolledItem);
return;
}
@@ -269,7 +277,6 @@ public partial class Player : CharacterBody3D, IPlayer
if (PlayerIsHittingGeometry())
{
AnimationPlayer.Play("hit_wall");
_gameRepo.OnPlayerAttackedWall();
}
else
{
@@ -291,7 +298,6 @@ public partial class Player : CharacterBody3D, IPlayer
var attackSpeed = ((Weapon)EquipmentComponent.EquippedWeapon.Value).AttackSpeed;
AnimationPlayer.SetSpeedScale((float)attackSpeed);
AnimationPlayer.Play("attack");
_gameRepo.OnPlayerAttack();
}
private void OnExitTree()
@@ -370,40 +376,25 @@ public partial class Player : CharacterBody3D, IPlayer
{
if (area.GetParent() is InventoryItem inventoryItem)
{
var isAdded = Inventory.TryAdd(inventoryItem);
var isAdded = Inventory.PickUpItem(inventoryItem);
if (isAdded)
{
_gameRepo.AnnounceMessageOnMainScreen($"{inventoryItem.ItemName} picked up.");
inventoryItem.QueueFree();
}
else
_gameRepo.AnnounceMessageOnMainScreen($"Could not pick up {inventoryItem.ItemName}.");
}
if (area.GetParent() is DroppedItem droppedItem)
{
var isAdded = Inventory.TryAdd(droppedItem.Item);
var isAdded = Inventory.PickUpItem(droppedItem.Item);
if (isAdded)
{
_gameRepo.AnnounceMessageOnMainScreen($"{droppedItem.Item.ItemName} picked up.");
droppedItem.QueueFree();
}
else
_gameRepo.AnnounceMessageOnMainScreen($"Could not pick up {droppedItem.Item.ItemName}.");
}
if (area.GetParent() is ThrownItem thrownItem)
{
var isAdded = Inventory.TryAdd(thrownItem.ItemThatIsThrown);
var isAdded = Inventory.PickUpItem(thrownItem.ItemThatIsThrown);
if (isAdded)
{
_gameRepo.AnnounceMessageOnMainScreen($"{thrownItem.ItemThatIsThrown.ItemName} picked up.");
thrownItem.QueueFree();
}
else
_gameRepo.AnnounceMessageOnMainScreen($"Could not pick up {thrownItem.ItemThatIsThrown.ItemName}.");
}
if (area.GetParent() is Restorative restorative)
{
_gameRepo.OnRestorativePickedUp(restorative);
//_gameRepo.OnRestorativePickedUp(restorative);
restorative.QueueFree();
}
}