Damage calculation including elemental buff/resistance

This commit is contained in:
2024-09-04 01:24:37 -07:00
parent 9a24ebf058
commit d7a49ba974
23 changed files with 311 additions and 70 deletions

View File

@@ -1,7 +1,9 @@
using Chickensoft.AutoInject;
using Chickensoft.Collections;
using Chickensoft.GodotNodeInterfaces;
using Chickensoft.Introspection;
using Godot;
using System;
using System.Linq;
namespace GameJamDungeon;
@@ -10,9 +12,9 @@ public interface IEnemy : ICharacterBody3D
{
public IEnemyLogic EnemyLogic { get; }
public int CurrentHP { get; set; }
public AutoProp<double> CurrentHP { get; set; }
public Resource EnemyStats { get; set; }
public EnemyStatInfo EnemyStatInfo { get; set; }
public NavigationAgent3D NavAgent { get; set; }
}
@@ -31,28 +33,28 @@ public partial class Enemy : CharacterBody3D, IEnemy, IProvide<IEnemyLogic>
[Dependency] IGameRepo GameRepo => this.DependOn<IGameRepo>();
[Export]
public Resource EnemyStats { get; set; } = new();
public EnemyStatInfo EnemyStatInfo { get; set; } = new();
public static PackedScene CollisionDetectorScene => GD.Load<PackedScene>("res://src/enemy/CollisionDetector.tscn");
public static Area3D CollisionDetector { get; set; } = default!;
public int CurrentHP { get; set; }
public AutoProp<double> CurrentHP { get; set; }
[Node] public NavigationAgent3D NavAgent { get; set; } = default!;
public void Setup()
{
EnemyLogic = new EnemyLogic();
EnemyLogic.Set(EnemyStats);
EnemyLogic.Set(EnemyStatInfo);
EnemyLogic.Set(this as IEnemy);
EnemyLogic.Set(GameRepo);
}
public void Initialize()
{
var enemyResource = EnemyStats as EnemyStats;
CurrentHP = enemyResource.MaximumHP;
CurrentHP = new AutoProp<double>(EnemyStatInfo.MaximumHP);
CurrentHP.Sync += OnHPChanged;
}
public void OnResolved()
@@ -71,7 +73,6 @@ public partial class Enemy : CharacterBody3D, IEnemy, IProvide<IEnemyLogic>
})
.Handle((in EnemyLogic.Output.Die output) =>
{
CollisionDetector.Dispose();
QueueFree();
})
.Handle((in EnemyLogic.Output.HitByPlayer output) =>
@@ -112,14 +113,21 @@ public partial class Enemy : CharacterBody3D, IEnemy, IProvide<IEnemyLogic>
{
if (body is IHitbox hitBox)
{
if (CurrentHP > 0)
if (CurrentHP.Value > 0)
{
GD.Print("Enemy Hit");
EnemyLogic.Input(new EnemyLogic.Input.HitByPlayer(hitBox.Damage));
var damage = DamageCalculator.CalculatePlayerDamage(hitBox.Damage, hitBox.GetParent<IPlayer>().PlayerStatInfo, EnemyStatInfo);
GD.Print($"Enemy Hit for {damage} damage.");
EnemyLogic.Input(new EnemyLogic.Input.HitByPlayer(damage));
}
}
}
private void OnHPChanged(double newHP)
{
if (newHP <= 0)
EnemyLogic.Input(new EnemyLogic.Input.Killed());
}
public void OnReady()
{
SetPhysicsProcess(true);