Added reaction time to enemies

This commit is contained in:
Marco 2025-03-25 15:15:04 +01:00
commit 543f5f0d6b
11 changed files with 63 additions and 31 deletions

View file

@ -52,4 +52,7 @@ PlayerDetectionRange = 90.0
ViewRange = 120.0
AlarmReactRange = 300.0
PlayerDisengageRange = 200.0
StrafeSpeed = 25.0
MaxStrafeDistance = 16.0
MinStrafeDistance = 8.0
metadata/_custom_type_script = "uid://cd5o0ceb50jki"

View file

@ -52,4 +52,7 @@ PlayerDetectionRange = 90.0
ViewRange = 120.0
AlarmReactRange = 200.0
PlayerDisengageRange = 500.0
StrafeSpeed = 25.0
MaxStrafeDistance = 32.0
MinStrafeDistance = 16.0
metadata/_custom_type_script = "uid://cd5o0ceb50jki"

View file

@ -11,7 +11,12 @@ PrefabPath = &"res://Scenes/Actors/RoamingSusan.tscn"
MaxHealth = 6.0
MovementSpeed = 20.0
Weapon = ExtResource("1_bpaea")
LootDrops = Array[Object]([])
LootDrops = []
PlayerDetectionRange = 90.0
ViewRange = 120.0
AlarmReactRange = 200.0
PlayerDisengageRange = 500.0
StrafeSpeed = 25.0
MaxStrafeDistance = 16.0
MinStrafeDistance = 8.0
metadata/_custom_type_script = "uid://cd5o0ceb50jki"

View file

@ -1,6 +1,5 @@
[gd_resource type="Resource" script_class="EnemyResource" load_steps=4 format=3 uid="uid://cfdvg162u65sr"]
[gd_resource type="Resource" script_class="EnemyResource" load_steps=3 format=3 uid="uid://cfdvg162u65sr"]
[ext_resource type="Script" uid="uid://cq65aed620ijo" path="res://Scripts/Resources/Loot/LootDrop.cs" id="1_f3huq"]
[ext_resource type="Resource" uid="uid://cdfmedtgp2rcn" path="res://Resources/Weapons/EnemyWeapon.tres" id="7_filx8"]
[ext_resource type="Script" uid="uid://cd5o0ceb50jki" path="res://Scripts/Resources/EnemyResource.cs" id="8_x8scf"]
@ -12,9 +11,12 @@ PrefabPath = &"res://Scenes/Actors/Thermathron.tscn"
MaxHealth = 8.0
MovementSpeed = 38.0
Weapon = ExtResource("7_filx8")
LootDrops = Array[ExtResource("1_f3huq")]([])
LootDrops = []
PlayerDetectionRange = 90.0
ViewRange = 120.0
AlarmReactRange = 200.0
PlayerDisengageRange = 500.0
StrafeSpeed = 25.0
MaxStrafeDistance = 0.0
MinStrafeDistance = 0.0
metadata/_custom_type_script = "uid://cd5o0ceb50jki"

View file

@ -1,6 +1,5 @@
[gd_resource type="Resource" script_class="EnemyResource" load_steps=4 format=3 uid="uid://ysd6wl2gmdhn"]
[gd_resource type="Resource" script_class="EnemyResource" load_steps=3 format=3 uid="uid://ysd6wl2gmdhn"]
[ext_resource type="Script" uid="uid://cq65aed620ijo" path="res://Scripts/Resources/Loot/LootDrop.cs" id="1_sma76"]
[ext_resource type="Resource" uid="uid://dyaxm3st70sh6" path="res://Resources/Weapons/Enemy/TurretWeapon.tres" id="1_yap8t"]
[ext_resource type="Script" uid="uid://cd5o0ceb50jki" path="res://Scripts/Resources/EnemyResource.cs" id="2_sma76"]
@ -12,9 +11,12 @@ PrefabPath = &"res://Scenes/Actors/Turret360.tscn"
MaxHealth = 14.0
MovementSpeed = 0.0
Weapon = ExtResource("1_yap8t")
LootDrops = Array[ExtResource("1_sma76")]([])
LootDrops = []
PlayerDetectionRange = 90.0
ViewRange = 120.0
AlarmReactRange = 200.0
PlayerDisengageRange = 500.0
StrafeSpeed = 0.0
MaxStrafeDistance = 0.0
MinStrafeDistance = 0.0
metadata/_custom_type_script = "uid://cd5o0ceb50jki"

View file

@ -652,7 +652,7 @@ IsEnabled = true
Target = NodePath("../DebugTeleporter")
[node name="DebugTeleporter" parent="Parallax2D/Factory Tilemaps/LevelProps" instance=ExtResource("30_8fdby")]
position = Vector2(-2002, -817)
position = Vector2(-429, 105)
Invisible = true
metadata/_edit_group_ = true

View file

@ -97,7 +97,7 @@ public partial class Alert : EnemyStateBase
MoveTowardsPosition(PlayerDetection.LastKnownPlayerPosition.Value);
}
NavigationModule.Move();
NavigationModule.Move(StorageModule.EnemyData.MovementSpeed);
StorageModule.FacingDirection = MainObject.Velocity.SnapToCardinal().Normalized();
}

View file

@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using Cirno.Scripts.Resources;
using Cirno.Scripts.Resources.Loot;
using Godot;
using Godot.Collections;
@ -10,6 +11,8 @@ public partial class EnemyStorageModule : Node2D
{
[Export]
public EnemyFSMProxy Root { get; private set; }
public EnemyResource EnemyData => Root.EnemyResource;
public Vector2 MovementDirection { get; set; }
public Vector2 FacingDirection { get; set; }

View file

@ -25,7 +25,7 @@ public partial class NavigationMovementModule : Node2D
_lastTargetPosition = target;
}
public void Move()
public void Move(float movementSpeed)
{
if (!_lastTargetPosition.HasValue)
{
@ -43,7 +43,7 @@ public partial class NavigationMovementModule : Node2D
var nextPathPosition = _navigationAgent.GetNextPathPosition();
var newVelocity = currentAgentPosition.DirectionTo(nextPathPosition) * StorageModule.MovementSpeed;
var newVelocity = currentAgentPosition.DirectionTo(nextPathPosition) * movementSpeed;
if (_navigationAgent.AvoidanceEnabled)
{

View file

@ -20,14 +20,18 @@ public partial class Shooting : EnemyStateBase
[Export]
public NavigationMovementModule NavigationModule { get; private set; }
[Export] public float MaxStrafeDistance { get; private set; } = 64f;
[Export] public float MinStrafeDistance { get; private set; } = 16f;
// [Export] public float MaxStrafeDistance { get; private set; } = 64f;
// [Export] public float MinStrafeDistance { get; private set; } = 16f;
[Export] public Weapon EquippedWeapon;
private bool _isPlayerInRange = false;
private Vector2? _currentStrafeTarget = null;
private float _strafeSpeed => StorageModule.EnemyData.StrafeSpeed;
private double _responseTimer = 0;
public override void EnterState()
{
@ -45,18 +49,8 @@ public partial class Shooting : EnemyStateBase
EquippedWeapon.WeaponData = StorageModule.Root.EnemyResource.Weapon;
_currentStrafeTarget = null;
// PlayerDetection.SetRange(StorageModule.Root.EnemyResource.PlayerDetectionRange);
//
// _isPlayerInRange = PlayerDetection.IsPlayerInRange(StorageModule.Root.EnemyResource.PlayerDetectionRange);
//
// PlayerDetection.PlayerInRange += PlayerDetectionOnPlayerInRange;
//
// PlayerDetection.PlayerOutOfRange += PlayerDetectionOnPlayerOutOfRange;
//
// DamageReceiver.ChangeState(true);
//
// DamageReceiver.HealthProvider.ResourceDepleted += HealthProviderOnResourceDepleted;
_responseTimer = 0;
}
private void HealthProviderOnResourceDepleted()
@ -93,10 +87,21 @@ public partial class Shooting : EnemyStateBase
// SHOOT
Shoot();
// Check if a strafe position is needed
if (!_currentStrafeTarget.HasValue || NavigationModule.IsNavigationFinished())
if (_strafeSpeed > 0)
{
_currentStrafeTarget = CalculateStrafePosition();
// Check if a strafe position is needed
if (!_currentStrafeTarget.HasValue || NavigationModule.IsNavigationFinished())
{
if (_responseTimer < StorageModule.EnemyData.ResponseTime)
{
_responseTimer += delta;
}
else
{
_currentStrafeTarget = CalculateStrafePosition();
_responseTimer = 0;
}
}
}
}
else
@ -108,7 +113,7 @@ public partial class Shooting : EnemyStateBase
if (_currentStrafeTarget.HasValue)
{
NavigationModule.SetTarget(_currentStrafeTarget.Value);
NavigationModule.Move();
NavigationModule.Move(_strafeSpeed);
}
}
@ -129,7 +134,7 @@ public partial class Shooting : EnemyStateBase
for (float factor = 1f; factor > 0.25f; factor *= 0.75f)
{
float strafeDistance = Mathf.Lerp(MinStrafeDistance, MaxStrafeDistance, factor);
float strafeDistance = Mathf.Lerp(StorageModule.EnemyData.MinStrafeDistance, StorageModule.EnemyData.MaxStrafeDistance, factor);
Vector2 newPos = enemyPos + (tryLeftFirst ? leftStrafe : rightStrafe) * strafeDistance;
if (NavigationModule.IsNavigable(newPos) && PlayerDetection.HasLineOfSight(newPos, playerPos))
{
@ -139,7 +144,7 @@ public partial class Shooting : EnemyStateBase
for (float factor = 1f; factor > 0.25f; factor *= 0.75f)
{
float strafeDistance = Mathf.Lerp(MinStrafeDistance, MaxStrafeDistance, factor);
float strafeDistance = Mathf.Lerp(StorageModule.EnemyData.MinStrafeDistance, StorageModule.EnemyData.MaxStrafeDistance, factor);
Vector2 newPos = enemyPos + (tryLeftFirst ? rightStrafe : leftStrafe) * strafeDistance;
if (NavigationModule.IsNavigable(newPos) && PlayerDetection.HasLineOfSight(newPos, playerPos))
{

View file

@ -21,4 +21,13 @@ public partial class EnemyResource : Resource
[Export] public float ViewRange { get; private set; } = 120f;
[Export] public float AlarmReactRange { get; private set; }
[Export] public float PlayerDisengageRange { get; private set; }
[Export] public float StrafeSpeed { get; private set; } = 25f;
[Export] public float MaxStrafeDistance { get; private set; } = 64f;
[Export] public float MinStrafeDistance { get; private set; } = 16f;
/// <summary>
/// Time it takes for the enemy to change strafing direction
/// </summary>
[Export] public float ResponseTime { get; private set; } = 0.5f;
}