486 lines
18 KiB
C#
486 lines
18 KiB
C#
using System.Collections.Generic;
|
|
using GazeLab.Core;
|
|
using GazeLab.Data;
|
|
using UnityEngine;
|
|
|
|
namespace GazeLab.Combat
|
|
{
|
|
public sealed class ProjectileController : MonoBehaviour
|
|
{
|
|
public static readonly List<ProjectileController> ActiveProjectiles = new List<ProjectileController>();
|
|
|
|
public ProjectileDefinition definition;
|
|
public bool firedByPlayer = true;
|
|
public float damageMultiplier = 1f;
|
|
public float worldScaleMultiplier = 1f;
|
|
public DamageableEntity intendedTarget;
|
|
public Transform launcherOrigin;
|
|
public bool enableSoftTargetFollow = true;
|
|
public float softFollowTurnRateDeg = 140f;
|
|
|
|
private static readonly Queue<ProjectileController> projectilePool = new Queue<ProjectileController>();
|
|
|
|
private const float MAX_SOFT_FOLLOW_CONE_ANGLE_DEG = 18f;
|
|
private const float SOFT_FOLLOW_MAX_TURN_RATE = 42f;
|
|
|
|
// Harpoon payload parameters
|
|
public int harpoonChainsPayload = 1;
|
|
public float harpoonDuration = 4.5f;
|
|
public float harpoonPullDist = 2.1f;
|
|
public float harpoonPullSpeed = 4.2f;
|
|
public float harpoonFocusBonus = 1.25f;
|
|
|
|
private SpriteRenderer spriteRenderer;
|
|
private TrailRenderer trailRenderer;
|
|
private GameObject visualChild;
|
|
private Vector3 moveDirection = Vector3.up;
|
|
private Vector3 initialFiringDirection = Vector3.up;
|
|
private Vector3 baseLinearPosition;
|
|
private float elapsed;
|
|
private int remainingPierces;
|
|
private readonly HashSet<int> hitEntityIds = new HashSet<int>();
|
|
|
|
public static ProjectileController Spawn(
|
|
ProjectileDefinition def,
|
|
Vector3 spawnPos,
|
|
Vector3 direction,
|
|
bool isPlayer,
|
|
float dmgMultiplier = 1f,
|
|
float worldScale = 1f,
|
|
DamageableEntity lockTarget = null,
|
|
Transform origin = null)
|
|
{
|
|
if (def == null) return null;
|
|
|
|
ProjectileController ctrl = null;
|
|
while (projectilePool.Count > 0 && ctrl == null)
|
|
{
|
|
ctrl = projectilePool.Dequeue();
|
|
}
|
|
|
|
if (ctrl == null)
|
|
{
|
|
GameObject go = new GameObject($"Proj_{def.projectileId}");
|
|
ctrl = go.AddComponent<ProjectileController>();
|
|
}
|
|
|
|
ctrl.transform.position = spawnPos;
|
|
ctrl.gameObject.SetActive(true);
|
|
ctrl.Initialize(def, direction, isPlayer, dmgMultiplier, worldScale, lockTarget, origin);
|
|
return ctrl;
|
|
}
|
|
|
|
public static void RecycleAllActive()
|
|
{
|
|
var arr = ActiveProjectiles.ToArray();
|
|
for (int i = 0; i < arr.Length; i++)
|
|
{
|
|
if (arr[i] != null)
|
|
{
|
|
arr[i].Recycle();
|
|
}
|
|
}
|
|
ActiveProjectiles.Clear();
|
|
}
|
|
|
|
public void Recycle()
|
|
{
|
|
if (!gameObject.activeSelf) return;
|
|
|
|
if (trailRenderer != null)
|
|
{
|
|
trailRenderer.Clear();
|
|
trailRenderer.enabled = false;
|
|
}
|
|
hitEntityIds.Clear();
|
|
enableSoftTargetFollow = false;
|
|
intendedTarget = null;
|
|
launcherOrigin = null;
|
|
|
|
gameObject.SetActive(false);
|
|
projectilePool.Enqueue(this);
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
if (!ActiveProjectiles.Contains(this))
|
|
ActiveProjectiles.Add(this);
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
ActiveProjectiles.Remove(this);
|
|
}
|
|
|
|
public void Initialize(
|
|
ProjectileDefinition def,
|
|
Vector3 direction,
|
|
bool isPlayer,
|
|
float dmgMult,
|
|
float scaleMult,
|
|
DamageableEntity target,
|
|
Transform origin)
|
|
{
|
|
definition = def;
|
|
firedByPlayer = isPlayer;
|
|
damageMultiplier = dmgMult;
|
|
worldScaleMultiplier = Mathf.Clamp(scaleMult, 0.25f, 2.0f);
|
|
intendedTarget = target;
|
|
launcherOrigin = origin;
|
|
enableSoftTargetFollow = (target != null && target.IsAlive);
|
|
elapsed = 0f;
|
|
hitEntityIds.Clear();
|
|
|
|
direction.z = 0f;
|
|
moveDirection = direction.sqrMagnitude > 0.0001f ? direction.normalized : (isPlayer ? Vector3.up : Vector3.down);
|
|
initialFiringDirection = moveDirection;
|
|
baseLinearPosition = transform.position;
|
|
remainingPierces = Mathf.Max(0, def.pierceCount);
|
|
|
|
if (visualChild == null)
|
|
{
|
|
visualChild = new GameObject("SpriteVisual");
|
|
visualChild.transform.SetParent(transform, false);
|
|
spriteRenderer = visualChild.AddComponent<SpriteRenderer>();
|
|
}
|
|
|
|
visualChild.transform.localPosition = Vector3.zero;
|
|
visualChild.transform.localRotation = Quaternion.Euler(0f, 0f, def.spriteRotationOffsetDeg);
|
|
|
|
spriteRenderer.sprite = def.projectileSprite != null
|
|
? def.projectileSprite
|
|
: ShockwaveExplosionController.GetFallbackGlowSprite();
|
|
spriteRenderer.color = def.tintColor;
|
|
spriteRenderer.sortingOrder = 18;
|
|
|
|
transform.localScale = Vector3.one * (def.visualScale * worldScaleMultiplier);
|
|
AlignTransformToDirection(moveDirection);
|
|
|
|
if (def.enableTrail)
|
|
{
|
|
if (trailRenderer == null)
|
|
{
|
|
trailRenderer = gameObject.AddComponent<TrailRenderer>();
|
|
trailRenderer.sharedMaterial = ProceduralChainRenderer.GetSharedLightningMaterial();
|
|
trailRenderer.sortingOrder = 17;
|
|
}
|
|
trailRenderer.Clear();
|
|
trailRenderer.enabled = true;
|
|
trailRenderer.time = def.trailTime;
|
|
trailRenderer.startWidth = def.trailWidth * worldScaleMultiplier;
|
|
trailRenderer.endWidth = 0.0f;
|
|
trailRenderer.startColor = def.trailColor;
|
|
trailRenderer.endColor = new Color(def.trailColor.r, def.trailColor.g, def.trailColor.b, 0f);
|
|
}
|
|
else if (trailRenderer != null)
|
|
{
|
|
trailRenderer.Clear();
|
|
trailRenderer.enabled = false;
|
|
}
|
|
}
|
|
|
|
private void AlignTransformToDirection(Vector3 dir)
|
|
{
|
|
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
|
|
transform.rotation = Quaternion.Euler(0f, 0f, angle);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (definition == null)
|
|
{
|
|
Recycle();
|
|
return;
|
|
}
|
|
|
|
float dt = Time.deltaTime;
|
|
elapsed += dt;
|
|
float normLife = Mathf.Clamp01(elapsed / Mathf.Max(0.1f, definition.lifetime));
|
|
|
|
if (elapsed >= definition.lifetime)
|
|
{
|
|
if (definition.trajectoryType == TrajectoryType.ClusterBomb)
|
|
{
|
|
DetonateClusterBomb();
|
|
}
|
|
Recycle();
|
|
return;
|
|
}
|
|
|
|
float scaledSpeed = definition.speed * Mathf.Lerp(1f, worldScaleMultiplier, 0.45f);
|
|
|
|
if (definition.trajectoryType != TrajectoryType.HomingMissile && enableSoftTargetFollow)
|
|
{
|
|
UpdateSoftFollowSteering(dt, scaledSpeed);
|
|
}
|
|
|
|
switch (definition.trajectoryType)
|
|
{
|
|
case TrajectoryType.HomingMissile:
|
|
UpdateHomingSteering(dt);
|
|
transform.position += moveDirection * (scaledSpeed * dt);
|
|
break;
|
|
|
|
case TrajectoryType.ExpandingWave:
|
|
transform.position += moveDirection * (scaledSpeed * dt);
|
|
float waveScale = definition.waveScaleOverLifetime.Evaluate(normLife);
|
|
transform.localScale = Vector3.one * (definition.visualScale * worldScaleMultiplier * waveScale);
|
|
break;
|
|
|
|
case TrajectoryType.SineWaveSnake:
|
|
baseLinearPosition += moveDirection * (scaledSpeed * dt);
|
|
Vector3 perp = new Vector3(-moveDirection.y, moveDirection.x, 0f);
|
|
float offset = Mathf.Sin(elapsed * definition.sineFrequency) * definition.sineAmplitude * worldScaleMultiplier;
|
|
transform.position = baseLinearPosition + perp * offset;
|
|
break;
|
|
|
|
default:
|
|
transform.position += moveDirection * (scaledSpeed * dt);
|
|
break;
|
|
}
|
|
|
|
AlignTransformToDirection(moveDirection);
|
|
CheckCollisions(normLife);
|
|
}
|
|
|
|
private void UpdateSoftFollowSteering(float dt, float projSpeed)
|
|
{
|
|
if (intendedTarget == null || !intendedTarget.IsAlive)
|
|
{
|
|
enableSoftTargetFollow = false;
|
|
return;
|
|
}
|
|
|
|
Vector3 targetPos = intendedTarget.transform.position;
|
|
Vector3 targetVel = intendedTarget.EstimatedVelocity;
|
|
float dist = Vector3.Distance(transform.position, targetPos);
|
|
float leadTime = dist / Mathf.Max(1f, projSpeed);
|
|
Vector3 predictedPos = targetPos + targetVel * leadTime;
|
|
|
|
Vector3 toTarget = predictedPos - transform.position;
|
|
toTarget.z = 0f;
|
|
|
|
// Forward Progress Check: If projectile has already passed the target plane relative to launch direction,
|
|
// or if the target is behind the projectile, permanently disengage homing so it misses naturally.
|
|
if (Vector3.Dot(toTarget, initialFiringDirection) <= 0.05f)
|
|
{
|
|
enableSoftTargetFollow = false;
|
|
return;
|
|
}
|
|
|
|
// Also if target is outside a 45° angle from initial launch direction, disengage
|
|
float angleToTargetFromLaunch = Vector3.Angle(initialFiringDirection, toTarget);
|
|
if (angleToTargetFromLaunch > 45f)
|
|
{
|
|
enableSoftTargetFollow = false;
|
|
return;
|
|
}
|
|
|
|
if (toTarget.sqrMagnitude > 0.001f)
|
|
{
|
|
float currentAngle = Mathf.Atan2(moveDirection.y, moveDirection.x) * Mathf.Rad2Deg;
|
|
float targetAngle = Mathf.Atan2(toTarget.y, toTarget.x) * Mathf.Rad2Deg;
|
|
float nextAngle = Mathf.MoveTowardsAngle(currentAngle, targetAngle, SOFT_FOLLOW_MAX_TURN_RATE * dt);
|
|
|
|
// Clamp deflection from initial launch direction within +/- MAX_SOFT_FOLLOW_CONE_ANGLE_DEG (18°)
|
|
float launchAngle = Mathf.Atan2(initialFiringDirection.y, initialFiringDirection.x) * Mathf.Rad2Deg;
|
|
float deltaFromLaunch = Mathf.DeltaAngle(launchAngle, nextAngle);
|
|
float clampedDelta = Mathf.Clamp(deltaFromLaunch, -MAX_SOFT_FOLLOW_CONE_ANGLE_DEG, MAX_SOFT_FOLLOW_CONE_ANGLE_DEG);
|
|
float finalAngle = launchAngle + clampedDelta;
|
|
|
|
float rad = finalAngle * Mathf.Deg2Rad;
|
|
moveDirection = new Vector3(Mathf.Cos(rad), Mathf.Sin(rad), 0f);
|
|
}
|
|
}
|
|
|
|
private void UpdateHomingSteering(float dt)
|
|
{
|
|
if (intendedTarget == null || !intendedTarget.IsAlive)
|
|
{
|
|
intendedTarget = FindNearestEnemyTarget();
|
|
}
|
|
|
|
if (intendedTarget != null && intendedTarget.IsAlive)
|
|
{
|
|
Vector3 toTarget = (intendedTarget.transform.position - transform.position);
|
|
toTarget.z = 0f;
|
|
if (toTarget.sqrMagnitude > 0.001f)
|
|
{
|
|
float currentAngle = Mathf.Atan2(moveDirection.y, moveDirection.x) * Mathf.Rad2Deg;
|
|
float targetAngle = Mathf.Atan2(toTarget.y, toTarget.x) * Mathf.Rad2Deg;
|
|
float nextAngle = Mathf.MoveTowardsAngle(currentAngle, targetAngle, definition.homingTurnRateDeg * dt);
|
|
float rad = nextAngle * Mathf.Deg2Rad;
|
|
moveDirection = new Vector3(Mathf.Cos(rad), Mathf.Sin(rad), 0f);
|
|
}
|
|
}
|
|
}
|
|
|
|
private DamageableEntity FindNearestEnemyTarget()
|
|
{
|
|
var list = DamageableEntity.ActiveEntities;
|
|
DamageableEntity best = null;
|
|
float bestDistSq = float.MaxValue;
|
|
Vector3 myPos = transform.position;
|
|
|
|
for (int i = 0; i < list.Count; i++)
|
|
{
|
|
DamageableEntity ent = list[i];
|
|
if (ent == null || !ent.IsAlive) continue;
|
|
if (firedByPlayer && ent.IsPlayerStation) continue;
|
|
if (!firedByPlayer && !ent.IsPlayerStation) continue;
|
|
|
|
float dSq = (ent.transform.position - myPos).sqrMagnitude;
|
|
if (dSq < bestDistSq)
|
|
{
|
|
bestDistSq = dSq;
|
|
best = ent;
|
|
}
|
|
}
|
|
return best;
|
|
}
|
|
|
|
private void CheckCollisions(float normLife)
|
|
{
|
|
var entities = DamageableEntity.ActiveEntities;
|
|
Vector3 myPos = transform.position;
|
|
float projRadius = definition.hitRadius * worldScaleMultiplier;
|
|
if (definition.trajectoryType == TrajectoryType.ExpandingWave)
|
|
{
|
|
projRadius *= definition.waveScaleOverLifetime.Evaluate(normLife);
|
|
}
|
|
|
|
for (int i = entities.Count - 1; i >= 0; i--)
|
|
{
|
|
if (i >= entities.Count) continue;
|
|
DamageableEntity ent = entities[i];
|
|
if (ent == null || !ent.IsAlive) continue;
|
|
|
|
// Filter friendly fire
|
|
if (firedByPlayer && ent.IsPlayerStation) continue;
|
|
if (!firedByPlayer && !ent.IsPlayerStation) continue;
|
|
|
|
int id = ent.GetInstanceID();
|
|
if (hitEntityIds.Contains(id)) continue;
|
|
|
|
float combinedRadius = projRadius + ent.targetHitRadius;
|
|
Vector3 diff = ent.transform.position - myPos;
|
|
diff.z = 0f;
|
|
if (diff.sqrMagnitude <= combinedRadius * combinedRadius)
|
|
{
|
|
hitEntityIds.Add(id);
|
|
OnHitEntity(ent);
|
|
if (remainingPierces > 0 || definition.trajectoryType == TrajectoryType.ExpandingWave)
|
|
{
|
|
if (definition.trajectoryType != TrajectoryType.ExpandingWave)
|
|
{
|
|
remainingPierces--;
|
|
damageMultiplier *= definition.pierceDamageRetention;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Recycle();
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnHitEntity(DamageableEntity primaryTarget)
|
|
{
|
|
Vector3 hitPos = transform.position;
|
|
|
|
// Harpoon Grapple Chain Attachment
|
|
if (definition.trajectoryType == TrajectoryType.TetherHarpoon && launcherOrigin != null)
|
|
{
|
|
primaryTarget.AttachHarpoonChain(
|
|
launcherOrigin,
|
|
Vector3.up * 0.35f,
|
|
harpoonChainsPayload,
|
|
harpoonDuration,
|
|
harpoonPullDist,
|
|
harpoonPullSpeed,
|
|
harpoonFocusBonus);
|
|
}
|
|
|
|
// Direct Hit Damage
|
|
primaryTarget.ApplyProjectileHit(definition, hitPos, damageMultiplier);
|
|
|
|
// AoE Splash Damage
|
|
if (definition.aoeExplosionRadius > 0.05f)
|
|
{
|
|
float aoeRad = definition.aoeExplosionRadius * worldScaleMultiplier;
|
|
float aoeRadSq = aoeRad * aoeRad;
|
|
var list = DamageableEntity.ActiveEntities;
|
|
for (int i = list.Count - 1; i >= 0; i--)
|
|
{
|
|
if (i >= list.Count) continue;
|
|
DamageableEntity other = list[i];
|
|
if (other == null || other == primaryTarget || !other.IsAlive) continue;
|
|
if (firedByPlayer && other.IsPlayerStation) continue;
|
|
if (!firedByPlayer && !other.IsPlayerStation) continue;
|
|
|
|
float dSq = (other.transform.position - hitPos).sqrMagnitude;
|
|
if (dSq <= aoeRadSq)
|
|
{
|
|
other.ApplyProjectileHit(definition, other.transform.position, damageMultiplier * 0.65f);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (definition.trajectoryType == TrajectoryType.ClusterBomb)
|
|
{
|
|
DetonateClusterBomb();
|
|
}
|
|
|
|
// Play Projectile Impact SFX
|
|
if (definition.impactSfx != null)
|
|
{
|
|
SpaceAudioController.Instance?.PlaySfx(definition.impactSfx, 0.85f, 0.08f);
|
|
}
|
|
|
|
// Spawn Impact Shockwave / Explosion VFX
|
|
bool isVoid = definition.statusEffect == StatusEffectType.VoidGravityPull;
|
|
if (definition.explosionSprite != null || definition.explosionCentroidProfile != null || isVoid || definition.aoeExplosionRadius > 0.1f)
|
|
{
|
|
ShockwaveExplosionController.Spawn(
|
|
hitPos,
|
|
definition.explosionSprite,
|
|
definition.explosionCentroidProfile,
|
|
definition.explosionScale * worldScaleMultiplier,
|
|
definition.tintColor,
|
|
isVoid,
|
|
2.5f * worldScaleMultiplier);
|
|
}
|
|
}
|
|
|
|
private void DetonateClusterBomb()
|
|
{
|
|
if (definition.submunitionProjectile == null || definition.submunitionCount <= 0) return;
|
|
|
|
if (definition.impactSfx != null)
|
|
{
|
|
SpaceAudioController.Instance?.PlaySfx(definition.impactSfx, 0.95f, 0.08f);
|
|
}
|
|
|
|
int count = definition.submunitionCount;
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
float angle = (360f / count) * i + Random.Range(-10f, 10f);
|
|
float rad = angle * Mathf.Deg2Rad;
|
|
Vector3 dir = new Vector3(Mathf.Cos(rad), Mathf.Sin(rad), 0f);
|
|
Spawn(
|
|
definition.submunitionProjectile,
|
|
transform.position,
|
|
dir,
|
|
firedByPlayer,
|
|
damageMultiplier * 0.55f,
|
|
worldScaleMultiplier,
|
|
null,
|
|
launcherOrigin);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|