Fix enemy attacks

This commit is contained in:
2024-09-16 22:14:46 -07:00
parent dac4e26662
commit 993ec2bf1b
8 changed files with 111 additions and 25 deletions

View File

@@ -61,6 +61,8 @@ public partial class Enemy : CharacterBody3D, IEnemy, IProvide<IEnemyLogic>
[Node] public AnimationTree AnimationTree { get; set; } = default!;
[Node] public IHitbox Hitbox { get; set; } = default!;
private const string IDLE_FORWARD = "idle_front_walk";
private const string IDLE_LEFT = "idle_left_walk";
private const string IDLE_BACK = "idle_back_walk";
@@ -73,9 +75,16 @@ public partial class Enemy : CharacterBody3D, IEnemy, IProvide<IEnemyLogic>
EnemyLogic.Set(EnemyStatResource);
EnemyLogic.Set(this as IEnemy);
EnemyLogic.Set(GameRepo);
AnimationTree.AnimationFinished += AnimationTree_AnimationFinished;
AnimationTree.Get("parameters/playback").As<AnimationNodeStateMachinePlayback>().Start(IDLE_FORWARD);
}
private void AnimationTree_AnimationFinished(StringName animName)
{
if (animName == "attack")
AnimationTree.Get("parameters/playback").As<AnimationNodeStateMachinePlayback>().Travel(IDLE_FORWARD);
}
private void DeathAnimationPlayer_AnimationFinished(StringName animName)
{
GameEventDepot.OnEnemyDefeated(GlobalPosition, EnemyStatResource);
@@ -88,6 +97,27 @@ public partial class Enemy : CharacterBody3D, IEnemy, IProvide<IEnemyLogic>
CollisionDetector = CollisionDetectorScene.Instantiate<Area3D>();
CollisionDetector.AreaEntered += OnPlayerHitboxEntered;
AddChild(CollisionDetector);
Hitbox.AreaEntered += Hitbox_AreaEntered;
}
private void Hitbox_AreaEntered(Area3D area)
{
if (area.GetParent().GetParent() is IPlayer player)
{
var isCriticalHit = false;
var rng = new RandomNumberGenerator();
rng.Randomize();
var roll = rng.Randf();
if (roll <= EnemyStatResource.Luck)
isCriticalHit = true;
var damage = DamageCalculator.CalculateEnemyDamage(
GameRepo.PlayerData.CurrentDefense.Value + GameRepo.PlayerData.BonusDefense,
EnemyStatResource,
GameRepo.PlayerData.Inventory.EquippedArmor.Value.ArmorStats,
isCriticalHit);
GameRepo.PlayerData.SetCurrentHP(GameRepo.PlayerData.CurrentHP.Value - Mathf.RoundToInt(damage));
GD.Print($"Player hit for {Mathf.Abs(damage)} damage.");
}
}
public void OnResolved()
@@ -190,7 +220,9 @@ public partial class Enemy : CharacterBody3D, IEnemy, IProvide<IEnemyLogic>
var rng = new RandomNumberGenerator();
rng.Randomize();
EnemyLogic.Input(new EnemyLogic.Input.AttackTimer());
AttackTimer.WaitTime = rng.RandfRange(2f, 3.0f);
AttackTimer.Stop();
AttackTimer.WaitTime = rng.RandfRange(3f, 7.0f);
AttackTimer.Start();
}
private void LineOfSight_BodyEntered(Node3D body)

View File

@@ -77,10 +77,10 @@
script = ExtResource("2_x4pjh")
CurrentHP = 45.0
MaximumHP = 45.0
CurrentAttack = 3
CurrentDefense = 2
MaxAttack = 3
MaxDefense = 2
CurrentAttack = 15
CurrentDefense = 11
MaxAttack = 15
MaxDefense = 11
Luck = 0.05
TelluricResistance = 0.0
AeolicResistance = 0.0
@@ -95,11 +95,11 @@ FerrumDamageBonus = 0.0
DropsSoulGemChance = 0.75
[sub_resource type="CylinderShape3D" id="CylinderShape3D_jbgmx"]
height = 4.83757
height = 5.0
radius = 1.0
[sub_resource type="BoxShape3D" id="BoxShape3D_0yire"]
size = Vector3(1, 0.564941, 1.14453)
size = Vector3(1, 0.565, 2)
[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_0h5s2"]
radius = 1.0
@@ -358,6 +358,18 @@ tracks/1/keys = {
"update": 1,
"values": [&"idle_back_walk"]
}
tracks/2/type = "value"
tracks/2/imported = false
tracks/2/enabled = true
tracks/2/path = NodePath("Hitbox/CollisionShape3D:disabled")
tracks/2/interp = 1
tracks/2/loop_wrap = true
tracks/2/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [true]
}
[sub_resource type="Animation" id="Animation_0k3e8"]
resource_name = "attack"
@@ -387,6 +399,18 @@ tracks/1/keys = {
"update": 1,
"values": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
}
tracks/2/type = "value"
tracks/2/imported = false
tracks/2/enabled = true
tracks/2/path = NodePath("Hitbox/CollisionShape3D:disabled")
tracks/2/interp = 1
tracks/2/loop_wrap = true
tracks/2/keys = {
"times": PackedFloat32Array(0, 0.0333333, 0.3),
"transitions": PackedFloat32Array(1, 1, 1),
"update": 1,
"values": [true, false, true]
}
[sub_resource type="Animation" id="Animation_ppbeh"]
resource_name = "idle_back_walk"
@@ -542,7 +566,7 @@ states/End/position = Vector2(1464, 100)
states/attack/node = SubResource("AnimationNodeAnimation_erbrx")
states/attack/position = Vector2(1151, 86.9474)
states/idle_back_walk/node = SubResource("AnimationNodeAnimation_o0tmb")
states/idle_back_walk/position = Vector2(491, 92.9474)
states/idle_back_walk/position = Vector2(490, 92.9474)
states/idle_front_walk/node = SubResource("AnimationNodeAnimation_a6s5c")
states/idle_front_walk/position = Vector2(331, -12)
states/idle_left_walk/node = SubResource("AnimationNodeAnimation_dvj10")
@@ -580,20 +604,21 @@ wait_time = 1.8
autostart = true
[node name="Hitbox" type="Area3D" parent="."]
unique_name_in_owner = true
transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, -0.152949, 0, 0)
collision_layer = 64
collision_mask = 64
script = ExtResource("3_aiftp")
[node name="CollisionShape3D" type="CollisionShape3D" parent="Hitbox"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -0.189337, 0.217529, -0.78415)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -0.189337, 0.217529, -1.29986)
shape = SubResource("BoxShape3D_0yire")
disabled = true
[node name="Raycast" type="RayCast3D" parent="."]
unique_name_in_owner = true
transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, 0, 0, 0)
target_position = Vector3(0, 0, -3)
target_position = Vector3(0, 0, -5)
collision_mask = 3
[node name="CollisionShape3D" type="CollisionShape3D" parent="."]

View File

@@ -63,10 +63,10 @@
script = ExtResource("2_j3knd")
CurrentHP = 45.0
MaximumHP = 45.0
CurrentAttack = 3
CurrentDefense = 2
MaxAttack = 3
MaxDefense = 2
CurrentAttack = 20
CurrentDefense = 10
MaxAttack = 20
MaxDefense = 10
Luck = 0.05
TelluricResistance = 0.0
AeolicResistance = 0.0
@@ -84,11 +84,11 @@ DropsSoulGemChance = 0.9
radius = 1.0
[sub_resource type="CylinderShape3D" id="CylinderShape3D_jbgmx"]
height = 4.12783
height = 5.0
radius = 1.0
[sub_resource type="BoxShape3D" id="BoxShape3D_0yire"]
size = Vector3(1, 0.564941, 1.14453)
size = Vector3(1, 0.565, 2)
[sub_resource type="ViewportTexture" id="ViewportTexture_h1kaf"]
viewport_path = NodePath("Sprite3D/SubViewport")
@@ -308,6 +308,18 @@ tracks/1/keys = {
"update": 1,
"values": [&"idle_left_walk"]
}
tracks/2/type = "value"
tracks/2/imported = false
tracks/2/enabled = true
tracks/2/path = NodePath("Hitbox/CollisionShape3D:disabled")
tracks/2/interp = 1
tracks/2/loop_wrap = true
tracks/2/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [true]
}
[sub_resource type="Animation" id="Animation_ruc6s"]
resource_name = "attack"
@@ -337,6 +349,18 @@ tracks/1/keys = {
"update": 1,
"values": [&"attack"]
}
tracks/2/type = "value"
tracks/2/imported = false
tracks/2/enabled = true
tracks/2/path = NodePath("Hitbox/CollisionShape3D:disabled")
tracks/2/interp = 1
tracks/2/loop_wrap = true
tracks/2/keys = {
"times": PackedFloat32Array(0, 0.25, 0.666666),
"transitions": PackedFloat32Array(1, 1, 1),
"update": 1,
"values": [true, false, true]
}
[sub_resource type="Animation" id="Animation_1tda5"]
resource_name = "idle_back_walk"
@@ -526,7 +550,7 @@ collision_layer = 2
collision_mask = 2
[node name="CollisionShape3D" type="CollisionShape3D" parent="LineOfSight"]
transform = Transform3D(1, 0, 0, 0, 0.0745088, 0.99722, 0, -0.99722, 0.0745088, 1.13364e-08, 0, -1.59911)
transform = Transform3D(1, 0, 0, 0, -4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0, -2)
shape = SubResource("CylinderShape3D_jbgmx")
[node name="PatrolTimer" type="Timer" parent="."]
@@ -540,19 +564,20 @@ wait_time = 0.8
autostart = true
[node name="Hitbox" type="Area3D" parent="."]
unique_name_in_owner = true
transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, -0.152949, 0, 0)
collision_layer = 64
collision_mask = 64
script = ExtResource("3_usw2d")
[node name="CollisionShape3D" type="CollisionShape3D" parent="Hitbox"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -0.189337, 0.217529, -0.78415)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -0.189337, 0.217529, -1.45579)
shape = SubResource("BoxShape3D_0yire")
disabled = true
[node name="Raycast" type="RayCast3D" parent="."]
unique_name_in_owner = true
target_position = Vector3(0, 0, -3)
target_position = Vector3(0, 0, -5)
collision_mask = 3
[node name="Sprite3D" type="Sprite3D" parent="."]

View File

@@ -7,6 +7,7 @@ radius = 0.470016
[node name="Accessory" type="Node3D"]
script = ExtResource("1_ikyk2")
AccessoryStats = null
[node name="Pickup" type="Area3D" parent="."]
unique_name_in_owner = true

View File

@@ -7,6 +7,7 @@ size = Vector3(0.778381, 0.929947, 0.731567)
[node name="Armor" type="Node3D"]
script = ExtResource("1_cmjpq")
ArmorStats = null
[node name="Pickup" type="Area3D" parent="."]
unique_name_in_owner = true

View File

@@ -7,6 +7,7 @@ radius = 0.470016
[node name="Weapon" type="Node3D"]
script = ExtResource("1_7pkyf")
WeaponStats = null
[node name="Pickup" type="Area3D" parent="."]
unique_name_in_owner = true

View File

@@ -139,7 +139,7 @@ namespace GameJamDungeon
GlobalPosition = GameRepo.PlayerGlobalPosition.Value;
GameRepo.PlayerGlobalPosition.Sync += PlayerGlobalPosition_Sync;
HealthTimer.Timeout += OnHealthTimerTimeout;
CollisionDetector.AreaEntered += OnEnemyHitBoxEntered;
CollisionDetector.BodyEntered += CollisionDetector_BodyEntered;
PlayerData.Inventory.AccessoryUnequipped += Inventory_AccessoryUnequipped;
}
@@ -267,9 +267,9 @@ namespace GameJamDungeon
HealthTimer.WaitTime = _healthTimerWaitTime;
}
private void OnEnemyHitBoxEntered(Area3D area)
private void CollisionDetector_BodyEntered(Node3D body)
{
if (area is IHitbox hitBox)
if (body is IHitbox hitBox)
{
var enemy = hitBox.GetParent<IEnemy>();
var isCriticalHit = false;
@@ -284,6 +284,7 @@ namespace GameJamDungeon
}
}
private void CurrentHP_Sync(int newHealth)
{
if (newHealth <= 0)

View File

@@ -33,7 +33,7 @@ Luck = 0.05
radius = 1.0
[sub_resource type="BoxShape3D" id="BoxShape3D_hs4wf"]
size = Vector3(1.94531, 2.43945, 1.08447)
size = Vector3(1.94531, 2.43945, 2.35425)
[sub_resource type="Animation" id="Animation_hcjph"]
length = 0.001
@@ -366,10 +366,10 @@ shape = SubResource("CapsuleShape3D_dw45s")
unique_name_in_owner = true
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.937567, 0)
collision_layer = 196
collision_mask = 132
collision_mask = 196
[node name="CollisionShape3D" type="CollisionShape3D" parent="CollisionShape3D/CollisionDetector"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -0.0479561, 0.982638, -0.04021)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -0.0479561, 0.982638, -0.675098)
shape = SubResource("BoxShape3D_hs4wf")
[node name="Camera3D" type="Camera3D" parent="."]