601 lines
17 KiB
C#
601 lines
17 KiB
C#
using Chickensoft.AutoInject;
|
|
using Chickensoft.Collections;
|
|
using Chickensoft.GodotNodeInterfaces;
|
|
using Chickensoft.Introspection;
|
|
using Chickensoft.SaveFileBuilder;
|
|
using Godot;
|
|
using Godot.Collections;
|
|
using System;
|
|
using System.Linq;
|
|
using Zennysoft.Game.Ma.Implementation;
|
|
|
|
namespace Zennysoft.Game.Ma;
|
|
|
|
[Meta(typeof(IAutoNode))]
|
|
public partial class Player : CharacterBody3D, IPlayer
|
|
{
|
|
#region Dependency Injection
|
|
public override void _Notification(int what) => this.Notify(what);
|
|
|
|
private PlayerLogic.IBinding PlayerBinding { get; set; } = default!;
|
|
#endregion
|
|
|
|
#region Save
|
|
public ISaveChunk<PlayerData> PlayerChunk { get; set; } = default!;
|
|
#endregion
|
|
|
|
public double CurrentHP => Stats.CurrentHP.Value;
|
|
|
|
public Vector3 CurrentPosition => GlobalPosition;
|
|
|
|
public Basis CurrentBasis => Transform.Basis;
|
|
public PlayerStatController Stats { get; set; } = default!;
|
|
|
|
public Inventory Inventory { get; private set; } = default!;
|
|
|
|
public IAutoProp<Weapon> EquippedWeapon => _equippedWeapon;
|
|
private AutoProp<Weapon> _equippedWeapon { get; set; } = new AutoProp<Weapon>(new Weapon());
|
|
|
|
public IAutoProp<Armor> EquippedArmor => _equippedArmor;
|
|
private AutoProp<Armor> _equippedArmor { get; set; } = new AutoProp<Armor>(new Armor());
|
|
|
|
public IAutoProp<Accessory> EquippedAccessory => _equippedAccessory;
|
|
private AutoProp<Accessory> _equippedAccessory { get; set; } = new AutoProp<Accessory>(new Accessory());
|
|
|
|
private PlayerLogic.Settings Settings { get; set; } = default!;
|
|
|
|
private PlayerLogic PlayerLogic { get; set; } = default!;
|
|
|
|
|
|
#region Dependencies
|
|
[Dependency]
|
|
public IGame Game => this.DependOn<IGame>();
|
|
|
|
[Dependency]
|
|
public ISaveChunk<GameData> GameChunk => this.DependOn<ISaveChunk<GameData>>();
|
|
|
|
ISaveChunk<PlayerData> IProvide<ISaveChunk<PlayerData>>.Value() => PlayerChunk;
|
|
#endregion
|
|
|
|
#region Event Signals
|
|
[Signal]
|
|
public delegate void InventoryButtonPressedEventHandler();
|
|
[Signal]
|
|
public delegate void MinimapButtonHeldEventHandler();
|
|
#endregion
|
|
|
|
#region Exports
|
|
[Export]
|
|
public PlayerStatResource PlayerStatResource { get; set; } = default!;
|
|
|
|
[Export]
|
|
private WeaponStats _defaultWeapon { get; set; } = default!;
|
|
[Export]
|
|
private ArmorStats _defaultArmor { get; set; } = default!;
|
|
#endregion
|
|
|
|
#region Node Dependencies
|
|
[Node] private IAnimationPlayer AnimationPlayer { get; set; } = default!;
|
|
|
|
[Node] private AnimatedSprite2D SwordSlashAnimation { get; set; } = default!;
|
|
|
|
[Node] private IHitbox Hitbox { get; set; } = default!;
|
|
|
|
[Node] private Area3D CollisionDetector { get; set; } = default!;
|
|
|
|
[Node] private Timer HealthTimer { get; set; } = default!;
|
|
#endregion
|
|
|
|
|
|
private bool flipAttack = false;
|
|
|
|
private float _healthTimerWaitTime = 3.0f;
|
|
|
|
private bool reduceOnTick = true;
|
|
|
|
private float _knockbackStrength = 0.0f;
|
|
private Vector3 _knockbackDirection = Vector3.Zero;
|
|
|
|
private Dictionary<int, int> _expToNextLevel;
|
|
|
|
#region Initialization
|
|
public void Initialize()
|
|
{
|
|
AnimationPlayer.AnimationFinished += OnAnimationFinished;
|
|
_expToNextLevel = new Dictionary<int, int>
|
|
{
|
|
{ 2, 12 },
|
|
{ 3, 39 },
|
|
{ 4, 87 },
|
|
{ 5, 162 },
|
|
{ 6, 270 },
|
|
{ 7, 417 },
|
|
{ 8, 609 }
|
|
};
|
|
}
|
|
|
|
public void Setup()
|
|
{
|
|
Settings = new PlayerLogic.Settings() { RotationSpeed = PlayerStatResource.RotationSpeed, MoveSpeed = PlayerStatResource.MoveSpeed, Acceleration = PlayerStatResource.Acceleration };
|
|
Stats = new PlayerStatController();
|
|
Stats.Init(
|
|
new PlayerStats
|
|
{
|
|
CurrentHP = PlayerStatResource.CurrentHP,
|
|
MaximumHP = PlayerStatResource.MaximumHP,
|
|
CurrentVT = PlayerStatResource.CurrentVT,
|
|
MaximumVT = PlayerStatResource.MaximumVT,
|
|
CurrentAttack = PlayerStatResource.CurrentAttack,
|
|
BonusAttack = PlayerStatResource.BonusAttack,
|
|
MaxAttack = PlayerStatResource.MaxAttack,
|
|
CurrentDefense = PlayerStatResource.CurrentDefense,
|
|
BonusDefense = PlayerStatResource.BonusDefense,
|
|
MaxDefense = PlayerStatResource.MaxDefense,
|
|
CurrentExp = PlayerStatResource.CurrentExp,
|
|
CurrentLevel = PlayerStatResource.CurrentLevel,
|
|
ExpToNextLevel = PlayerStatResource.ExpToNextLevel,
|
|
Luck = PlayerStatResource.Luck
|
|
});
|
|
|
|
Inventory = new Inventory();
|
|
|
|
PlayerLogic = new PlayerLogic();
|
|
PlayerLogic.Set(this as IPlayer);
|
|
PlayerLogic.Set(Settings);
|
|
PlayerLogic.Set(Stats);
|
|
|
|
var defaultWeapon = new Weapon();
|
|
defaultWeapon.ItemStats = _defaultWeapon;
|
|
var defaultArmor = new Armor();
|
|
defaultArmor.ItemStats = _defaultArmor;
|
|
Inventory.TryAdd(defaultWeapon);
|
|
Inventory.TryAdd(defaultArmor);
|
|
|
|
EquippedWeapon.Sync += EquippedWeapon_Sync;
|
|
EquippedArmor.Sync += EquippedArmor_Sync;
|
|
EquippedAccessory.Sync += EquippedAccessory_Sync;
|
|
Stats.CurrentHP.Sync += CurrentHP_Sync;
|
|
Stats.CurrentExp.Sync += CurrentEXP_Sync;
|
|
|
|
Equip(defaultWeapon);
|
|
Equip(defaultArmor);
|
|
|
|
HealthTimer.WaitTime = _healthTimerWaitTime;
|
|
HealthTimer.Timeout += OnHealthTimerTimeout;
|
|
Hitbox.AreaEntered += Hitbox_AreaEntered;
|
|
CollisionDetector.AreaEntered += CollisionDetector_AreaEntered;
|
|
}
|
|
|
|
public void OnResolved()
|
|
{
|
|
PlayerChunk = new SaveChunk<PlayerData>(
|
|
onSave: (chunk) => new PlayerData()
|
|
{
|
|
PlayerStats = new PlayerStats()
|
|
{
|
|
CurrentHP = Stats.CurrentHP.Value,
|
|
MaximumHP = Stats.MaximumHP.Value,
|
|
CurrentVT = Stats.CurrentVT.Value,
|
|
MaximumVT = Stats.MaximumVT.Value,
|
|
CurrentAttack = Stats.CurrentAttack.Value,
|
|
BonusAttack = Stats.BonusAttack.Value,
|
|
MaxAttack = Stats.MaxAttack.Value,
|
|
CurrentDefense = Stats.CurrentDefense.Value,
|
|
BonusDefense = Stats.BonusDefense.Value,
|
|
MaxDefense = Stats.MaxDefense.Value,
|
|
CurrentExp = Stats.CurrentExp.Value,
|
|
CurrentLevel = Stats.CurrentLevel.Value,
|
|
ExpToNextLevel = Stats.ExpToNextLevel.Value,
|
|
Luck = Stats.Luck.Value
|
|
},
|
|
Inventory = Inventory
|
|
},
|
|
onLoad: (chunk, data) =>
|
|
{
|
|
Stats.Init(data.PlayerStats);
|
|
Inventory = data.Inventory;
|
|
}
|
|
);
|
|
|
|
PlayerBinding = PlayerLogic.Bind();
|
|
|
|
PlayerBinding
|
|
.Handle((in PlayerLogic.Output.Animations.Attack output) =>
|
|
{
|
|
var attackSpeed = EquippedWeapon.Value.AttackSpeed;
|
|
AnimationPlayer.SetSpeedScale((float)attackSpeed);
|
|
|
|
AnimationPlayer.Play("attack");
|
|
})
|
|
.Handle((in PlayerLogic.Output.ThrowItem output) =>
|
|
{
|
|
});
|
|
|
|
GameChunk.AddChunk(PlayerChunk);
|
|
|
|
PlayerLogic.Start();
|
|
this.Provide();
|
|
}
|
|
|
|
private void CollisionDetector_AreaEntered(Area3D area)
|
|
{
|
|
if (area.GetParent() is InventoryItem inventoryItem)
|
|
{
|
|
var isAdded = Inventory.TryAdd(inventoryItem);
|
|
if (isAdded)
|
|
{
|
|
Game.AnnounceMessageOnMainScreen($"{inventoryItem.ItemName} picked up.");
|
|
inventoryItem.QueueFree();
|
|
}
|
|
else
|
|
Game.AnnounceMessageOnMainScreen($"Could not pick up {inventoryItem.ItemName}.");
|
|
}
|
|
if (area.GetParent() is DroppedItem droppedItem)
|
|
{
|
|
var isAdded = Inventory.TryAdd(droppedItem.Item);
|
|
if (isAdded)
|
|
{
|
|
Game.AnnounceMessageOnMainScreen($"{droppedItem.Item.ItemName} picked up.");
|
|
droppedItem.QueueFree();
|
|
}
|
|
else
|
|
Game.AnnounceMessageOnMainScreen($"Could not pick up {droppedItem.Item.ItemName}.");
|
|
}
|
|
}
|
|
|
|
public void OnReady()
|
|
{
|
|
SetPhysicsProcess(true);
|
|
SwordSlashAnimation.Position = GetViewport().GetVisibleRect().Size / 2;
|
|
}
|
|
#endregion
|
|
|
|
public void Attack()
|
|
{
|
|
PlayerLogic.Input(new PlayerLogic.Input.Attack());
|
|
}
|
|
|
|
public void ToggleInventory()
|
|
{
|
|
Game.ToggleInventory();
|
|
}
|
|
|
|
public void ToggleMinimap()
|
|
{
|
|
Game.ToggleMinimap();
|
|
}
|
|
|
|
public void PlayerPause()
|
|
{
|
|
Game.TogglePause();
|
|
}
|
|
|
|
public IDungeonRoom GetCurrentRoom()
|
|
{
|
|
var rooms = Game.CurrentFloor.Rooms;
|
|
var playersRoom = rooms.SingleOrDefault(x => x.IsPlayerInRoom);
|
|
return playersRoom;
|
|
}
|
|
|
|
public void RaiseHP(int amountToRaise)
|
|
{
|
|
Stats.SetMaximumHP(Stats.MaximumHP.Value + amountToRaise);
|
|
Stats.SetCurrentHP(Stats.MaximumHP.Value);
|
|
Game.AnnounceMessageOnInventoryScreen($"{amountToRaise}MAXHP Up.");
|
|
}
|
|
|
|
public void HealHP(int amountToRestore)
|
|
{
|
|
Stats.SetCurrentHP(Stats.CurrentHP.Value + amountToRestore);
|
|
var raiseString = amountToRestore == 1000 ? "MAX" : $"{amountToRestore}";
|
|
Game.AnnounceMessageOnInventoryScreen($"{raiseString}HP Restored.");
|
|
}
|
|
|
|
public void RaiseVT(int amountToRaise)
|
|
{
|
|
if (Stats.CurrentVT == Stats.MaximumVT)
|
|
{
|
|
Stats.SetMaximumVT(Stats.MaximumVT.Value + amountToRaise);
|
|
Stats.SetCurrentVT(Stats.MaximumVT.Value);
|
|
Game.AnnounceMessageOnInventoryScreen($"{amountToRaise}MAXVT Up.");
|
|
}
|
|
}
|
|
|
|
public void HealVT(int amountToRestore)
|
|
{
|
|
Stats.SetCurrentVT(Stats.CurrentVT.Value + amountToRestore);
|
|
var raiseString = amountToRestore == 1000 ? "MAX" : $"{amountToRestore}";
|
|
Game.AnnounceMessageOnInventoryScreen($"{raiseString}VT Restored.");
|
|
}
|
|
|
|
public void ModifyBonusAttack(int amount)
|
|
{
|
|
Stats.SetBonusAttack(Stats.BonusAttack.Value + amount);
|
|
}
|
|
|
|
public void ModifyBonusDefense(int amount)
|
|
{
|
|
Stats.SetBonusDefense(Stats.BonusDefense.Value + amount);
|
|
}
|
|
|
|
public void ModifyMaximumHP(int amount)
|
|
{
|
|
Stats.SetMaximumHP(Stats.MaximumHP.Value + amount);
|
|
}
|
|
|
|
public void ModifyMaximumVT(int amount)
|
|
{
|
|
Stats.SetMaximumVT(Stats.MaximumVT.Value + amount);
|
|
}
|
|
|
|
public void ModifyBonusLuck(double amount)
|
|
{
|
|
Stats.SetLuck(Stats.Luck.Value + amount);
|
|
}
|
|
|
|
public void Move(float delta)
|
|
{
|
|
var rawInput = GlobalInputVector;
|
|
var strafeLeftInput = LeftStrafeInputVector;
|
|
var strafeRightInput = RightStrafeInputVector;
|
|
|
|
var transform = Transform;
|
|
transform.Basis = new Basis(Vector3.Up, Settings.RotationSpeed * -rawInput.X * delta) * transform.Basis;
|
|
var moveDirection = new Vector3(strafeRightInput - strafeLeftInput, 0, rawInput.Z).Normalized();
|
|
var velocity = Basis * moveDirection * Settings.MoveSpeed * Settings.Acceleration;
|
|
_knockbackStrength = _knockbackStrength * 0.9f;
|
|
Transform = Transform with { Basis = transform.Basis };
|
|
Velocity = velocity + (_knockbackDirection * _knockbackStrength);
|
|
MoveAndSlide();
|
|
}
|
|
|
|
public void TeleportPlayer(Transform3D newTransform)
|
|
{
|
|
Transform = newTransform;
|
|
}
|
|
|
|
public void TakeDamage(double damage, ElementType elementType, bool isCriticalHit = false)
|
|
{
|
|
if (Stats.CurrentHP.Value > 0)
|
|
{
|
|
damage = CalculateDefenseResistance(damage);
|
|
if (isCriticalHit)
|
|
damage *= 2;
|
|
Stats.SetCurrentHP(Stats.CurrentHP.Value - (int)damage);
|
|
}
|
|
}
|
|
|
|
public void Knockback(float impulse)
|
|
{
|
|
_knockbackStrength = impulse;
|
|
_knockbackDirection = GlobalBasis.Z.Normalized();
|
|
}
|
|
|
|
public void GainExp(int expGained)
|
|
{
|
|
Stats.SetCurrentExp(Stats.CurrentExp.Value + expGained);
|
|
}
|
|
|
|
public void LevelUp()
|
|
{
|
|
var nextLevel = Stats.CurrentLevel.Value + 1;
|
|
var expToNextLevel = _expToNextLevel[nextLevel];
|
|
var newCurrentExp = Mathf.Max(Stats.CurrentExp.Value - Stats.ExpToNextLevel.Value, 0);
|
|
Stats.SetCurrentLevel(nextLevel);
|
|
Stats.SetExpToNextLevel(expToNextLevel);
|
|
Stats.SetCurrentExp(newCurrentExp);
|
|
}
|
|
|
|
public void Die() => PlayerLogic.Input(new PlayerLogic.Input.Killed());
|
|
|
|
public override void _UnhandledInput(InputEvent @event)
|
|
{
|
|
if (@event.IsActionPressed(GameInputs.Inventory))
|
|
ToggleInventory();
|
|
|
|
if (@event.IsActionPressed(GameInputs.MiniMap))
|
|
ToggleMinimap();
|
|
|
|
if (@event.IsActionPressed(GameInputs.Pause))
|
|
PlayerPause();
|
|
|
|
if (@event.IsActionPressed(GameInputs.Attack))
|
|
Attack();
|
|
}
|
|
|
|
public void OnPhysicsProcess(double delta)
|
|
{
|
|
PlayerLogic.Input(new PlayerLogic.Input.PhysicsTick(delta));
|
|
PlayerLogic.Input(new PlayerLogic.Input.Moved(GlobalPosition, GlobalTransform));
|
|
}
|
|
|
|
public void Equip(EquipableItem equipable)
|
|
{
|
|
if (equipable is Weapon weapon)
|
|
{
|
|
Unequip(_equippedWeapon.Value);
|
|
weapon.IsEquipped = true;
|
|
_equippedWeapon.OnNext(weapon);
|
|
}
|
|
else if (equipable is Armor armor)
|
|
{
|
|
Unequip(_equippedArmor.Value);
|
|
armor.IsEquipped = true;
|
|
_equippedArmor.OnNext(armor);
|
|
}
|
|
else if (equipable is Accessory accessory)
|
|
{
|
|
Unequip(_equippedAccessory.Value);
|
|
accessory.IsEquipped = true;
|
|
_equippedAccessory.OnNext(accessory);
|
|
}
|
|
else
|
|
throw new NotImplementedException("Item type is not supported.");
|
|
}
|
|
|
|
public void Unequip(EquipableItem equipable)
|
|
{
|
|
if (equipable is Weapon weapon)
|
|
{
|
|
weapon.IsEquipped = false;
|
|
ModifyBonusAttack(-weapon.Damage);
|
|
_equippedWeapon.OnNext(new Weapon());
|
|
}
|
|
else if (equipable is Armor armor)
|
|
{
|
|
armor.IsEquipped = false;
|
|
ModifyBonusDefense(-armor.Defense);
|
|
_equippedArmor.OnNext(new Armor());
|
|
}
|
|
else if (equipable is Accessory accessory)
|
|
{
|
|
accessory.IsEquipped = false;
|
|
ModifyMaximumHP(-accessory.MaxHPUp);
|
|
ModifyMaximumVT(-accessory.MaxVTUp);
|
|
ModifyBonusAttack(-accessory.ATKUp);
|
|
ModifyBonusDefense(-accessory.DEFUp);
|
|
ModifyBonusLuck(-accessory.LuckUp);
|
|
_equippedAccessory.OnNext(new Accessory());
|
|
}
|
|
else
|
|
throw new NotImplementedException("Item type is not supported.");
|
|
|
|
if (equipable.ItemTag == ItemTag.BreaksOnChange)
|
|
Inventory.Remove(equipable);
|
|
}
|
|
|
|
private static Vector3 GlobalInputVector
|
|
{
|
|
get
|
|
{
|
|
var rawInput = Input.GetVector(GameInputs.MoveLeft, GameInputs.MoveRight, GameInputs.MoveUp, GameInputs.MoveDown);
|
|
var input = new Vector3
|
|
{
|
|
X = rawInput.X,
|
|
Z = rawInput.Y
|
|
};
|
|
return input with { Y = 0f };
|
|
}
|
|
}
|
|
|
|
private static float LeftStrafeInputVector
|
|
{
|
|
get
|
|
{
|
|
return Input.GetActionStrength(GameInputs.StrafeLeft);
|
|
}
|
|
}
|
|
|
|
private static float RightStrafeInputVector
|
|
{
|
|
get
|
|
{
|
|
return Input.GetActionStrength(GameInputs.StrafeRight);
|
|
}
|
|
}
|
|
|
|
private void ThrowItem()
|
|
{
|
|
var itemScene = GD.Load<PackedScene>("res://src/items/throwable/ThrowableItem.tscn");
|
|
var throwItem = itemScene.Instantiate<ThrowableItem>();
|
|
GetTree().Root.AddChildEx(throwItem);
|
|
throwItem.GlobalPosition = CurrentPosition + new Vector3(0, 3.5f, 0);
|
|
throwItem.GlobalRotation = GlobalRotation;
|
|
}
|
|
|
|
private void OnAnimationFinished(StringName animation)
|
|
{
|
|
GD.Print("Attack finished");
|
|
PlayerLogic.Input(new PlayerLogic.Input.AttackAnimationFinished());
|
|
}
|
|
|
|
private void OnExitTree()
|
|
{
|
|
PlayerLogic.Stop();
|
|
PlayerBinding.Dispose();
|
|
AnimationPlayer.AnimationFinished -= OnAnimationFinished;
|
|
}
|
|
|
|
private void OnPlayerPositionUpdated(Vector3 globalPosition) => GlobalPosition = globalPosition;
|
|
|
|
private void OnHealthTimerTimeout()
|
|
{
|
|
if (Stats.CurrentHP.Value <= 0)
|
|
return;
|
|
|
|
if (Stats.CurrentVT.Value > 0)
|
|
{
|
|
if (EquippedAccessory.Value.AccessoryTag == AccessoryTag.HalfVTConsumption)
|
|
{
|
|
reduceOnTick = !reduceOnTick;
|
|
}
|
|
Stats.SetCurrentHP(Stats.CurrentHP.Value + 1);
|
|
if (reduceOnTick)
|
|
Stats.SetCurrentVT(Stats.CurrentVT.Value - 1);
|
|
}
|
|
else
|
|
Stats.SetCurrentHP(Stats.CurrentHP.Value - 1);
|
|
}
|
|
|
|
private void EquippedWeapon_Sync(Weapon obj)
|
|
{
|
|
ModifyBonusAttack(obj.Damage);
|
|
}
|
|
|
|
private void EquippedArmor_Sync(Armor obj)
|
|
{
|
|
ModifyBonusDefense(obj.Defense);
|
|
}
|
|
|
|
private void EquippedAccessory_Sync(Accessory accessory)
|
|
{
|
|
ModifyMaximumHP(accessory.MaxHPUp);
|
|
ModifyMaximumVT(accessory.MaxVTUp);
|
|
ModifyBonusAttack(accessory.ATKUp);
|
|
ModifyBonusDefense(accessory.DEFUp);
|
|
ModifyBonusLuck(accessory.LuckUp);
|
|
}
|
|
|
|
private void CurrentHP_Sync(int newHealth)
|
|
{
|
|
if (newHealth <= 0)
|
|
Die();
|
|
}
|
|
|
|
private void CurrentEXP_Sync(int newExp)
|
|
{
|
|
if (Stats.CurrentExp.Value >= Stats.ExpToNextLevel.Value)
|
|
LevelUp();
|
|
}
|
|
|
|
private double CalculateDefenseResistance(double incomingDamage)
|
|
{
|
|
return Mathf.Max(incomingDamage - Stats.CurrentDefense.Value - Stats.BonusDefense.Value, 0.0);
|
|
}
|
|
|
|
private void Hitbox_AreaEntered(Area3D area)
|
|
{
|
|
var target = area.GetOwner();
|
|
if (target is IEnemy enemy)
|
|
HitEnemy(enemy);
|
|
}
|
|
|
|
private void HitEnemy(IEnemy enemy)
|
|
{
|
|
var attackValue = Stats.CurrentAttack.Value + Stats.BonusAttack.Value;
|
|
var ignoreElementalResistance = EquippedWeapon.Value.WeaponTag == WeaponTag.IgnoreAffinity;
|
|
var isCriticalHit = BattleExtensions.IsCriticalHit(Stats.Luck.Value);
|
|
var element = EquippedWeapon.Value.WeaponElement;
|
|
|
|
enemy.TakeDamage(
|
|
attackValue * EquippedWeapon.Value.ElementalDamageBonus,
|
|
element,
|
|
isCriticalHit,
|
|
false,
|
|
ignoreElementalResistance);
|
|
|
|
if (EquippedWeapon.Value.WeaponTag == WeaponTag.Knockback)
|
|
enemy.Knockback(0.3f, -CurrentBasis.Z.Normalized());
|
|
}
|
|
}
|