Grahpics overhaul and predicting bullets

This commit is contained in:
MaddoScientisto 2025-05-04 22:01:59 +02:00
commit 946e7df71e
14 changed files with 144 additions and 26 deletions

View file

@ -4,6 +4,7 @@ using System.Linq;
using Cirno.Scripts.Actors;
using Cirno.Scripts.Components;
using Cirno.Scripts.Resources;
using Cirno.Scripts.Utils;
using Godot;
using Godot.Collections;
using Array = System.Array;
@ -36,6 +37,7 @@ public partial class SpiralPattern : AttackPattern
// [ExportGroup("Modifiers")] [Export] private BulletCreationModifier _modifier;
// [ExportGroup("Modifiers")] [Export] private Array<TimeModifier> _timeModifiers;
[ExportCategory("Other")] [Export] public bool _targetPlayer = false;
[ExportCategory("Other")] [Export] public bool _predictPlayer = false;
[ExportCategory("Overrides")]
[Export] public bool OverrideOwner { get; private set; } = false;
@ -224,7 +226,20 @@ public partial class SpiralPattern : AttackPattern
if (pattern._targetPlayer && GameManager.Instance.PlayerPosition.HasValue)
{
direction = (GameManager.Instance.PlayerPosition.Value - Parent.GlobalPosition).Normalized();
if (pattern._predictPlayer && GameManager.Instance.PlayerVelocity.HasValue)
{
var predictedDirection = MathFunctions.PredictInterceptPosition(Parent.GlobalPosition,
GameManager.Instance.PlayerPosition.Value, GameManager.Instance.PlayerVelocity.Value,
pattern.BulletResource.BulletSpeed);
if (predictedDirection.HasValue)
{
direction = (predictedDirection.Value - Parent.GlobalPosition).Normalized();
}
}
else
{
direction = (GameManager.Instance.PlayerPosition.Value - Parent.GlobalPosition).Normalized();
}
}
var bullet = pattern.MakeBullet(Parent.GlobalPosition, pattern.bulletCount,

View file

@ -16,6 +16,7 @@ public partial class PlayerDetectionModule : Area2D
public uint ObstaclesCollisionMask { get; private set; }
public Vector2? LastKnownPlayerPosition { get; set; }
public Vector2? LastKnowPlayerVelocity { get; set; }
//public bool PlayerInActiveArea { get; private set; }
private CollisionShape2D _collisionShape2D;
@ -77,6 +78,7 @@ public partial class PlayerDetectionModule : Area2D
if (found)
{
LastKnownPlayerPosition = GameManager.Instance.PlayerPosition;
LastKnowPlayerVelocity = GameManager.Instance.PlayerVelocity;
}
return found;
}

View file

@ -1,5 +1,6 @@
using Cirno.Scripts.Components.Actors;
using Cirno.Scripts.Enums;
using Cirno.Scripts.Utils;
using Godot;
namespace Cirno.Scripts.Components.FSM.Enemy;
@ -150,13 +151,27 @@ public partial class Shooting : EnemyStateBase
return null; // No valid strafe position found
}
private Vector2 GetShootDirection()
{
if (StorageModule.EnemyData.PredictPlayer && PlayerDetection.LastKnowPlayerVelocity.HasValue)
{
var predictedDirection = MathFunctions.PredictInterceptPosition(MainObject.GlobalPosition,
PlayerDetection.LastKnownPlayerPosition.Value, PlayerDetection.LastKnowPlayerVelocity.Value,
EquippedWeapon.WeaponData.BulletData.BulletSpeed);
if (predictedDirection.HasValue) return (predictedDirection.Value - MainObject.GlobalPosition).Normalized();
}
return ( PlayerDetection.LastKnownPlayerPosition.Value - MainObject.GlobalPosition).Normalized();
}
private void Shoot()
{
if (EquippedWeapon == null) return;
if (!PlayerDetection.LastKnownPlayerPosition.HasValue) return;
var direction = ( PlayerDetection.LastKnownPlayerPosition.Value - MainObject.GlobalPosition).Normalized();
var direction = GetShootDirection();
// Shoot at the player's last known position
EquippedWeapon.ShootDirection = direction;

View file

@ -25,6 +25,7 @@ public partial class GameManager : Node2D
private Node2D _cameraTarget;
public Vector2? PlayerPosition => _player?.GlobalPosition ?? null;
public Vector2? PlayerVelocity => _player?.Velocity ?? null;
[Export] public MapResource MapResource { get; private set; }

View file

@ -18,6 +18,7 @@ public partial class EnemyResource : Resource
[Export] public Array<LootDrop> LootDrops { get; private set; } = [];
[Export] public float MotivationReward { get; private set; } = 4f;
[Export] public bool PredictPlayer { get; private set; } = false;
[ExportCategory("AI")] [Export] public float PlayerDetectionRange { get; private set; } = 90f;
[Export] public float ViewRange { get; private set; } = 120f;
[Export] public float AlarmReactRange { get; private set; }

View file

@ -0,0 +1,32 @@
using Godot;
namespace Cirno.Scripts.Utils;
public static class MathFunctions
{
public static Vector2? PredictInterceptPosition(Vector2 shooterPos, Vector2 targetPos, Vector2 targetVel, float projectileSpeed)
{
Vector2 displacement = targetPos - shooterPos;
float a = targetVel.LengthSquared() - projectileSpeed * projectileSpeed;
float b = 2 * displacement.Dot(targetVel);
float c = displacement.LengthSquared();
float discriminant = b * b - 4 * a * c;
if (discriminant < 0 || Mathf.Abs(a) < 0.001f)
return null; // No solution or projectile too slow
float sqrtDisc = Mathf.Sqrt(discriminant);
float t1 = (-b - sqrtDisc) / (2 * a);
float t2 = (-b + sqrtDisc) / (2 * a);
float t = Mathf.Min(t1, t2);
if (t < 0)
t = Mathf.Max(t1, t2);
if (t < 0)
return null; // No valid positive time
return targetPos + targetVel * t;
}
}