346 lines
14 KiB
C#
346 lines
14 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using GazeLab.Combat;
|
|
using GazeLab.Data;
|
|
using UnityEngine;
|
|
|
|
namespace GazeLab.Entities
|
|
{
|
|
public enum AsteroidSizeTier
|
|
{
|
|
Small = 0,
|
|
Mid = 1,
|
|
Big = 2,
|
|
Huge = 3
|
|
}
|
|
|
|
public sealed class AsteroidController : MonoBehaviour
|
|
{
|
|
public static readonly List<AsteroidController> ActiveAsteroids = new List<AsteroidController>();
|
|
private static readonly Queue<AsteroidController> asteroidPool = new Queue<AsteroidController>();
|
|
|
|
public AsteroidSizeTier sizeTier = AsteroidSizeTier.Mid;
|
|
public bool dropsResource = false;
|
|
public ResourceType resourceType = ResourceType.Scrap;
|
|
public int resourceDropAmount = 3;
|
|
public Vector2 velocity = new Vector2(0f, -1.1f);
|
|
public float rotationSpeedDeg = 35f;
|
|
|
|
private DamageableEntity damageable;
|
|
private SpriteRenderer spriteRenderer;
|
|
private WaveCatalog catalogRef;
|
|
private float baseScaleForTier = 0.35f;
|
|
private float currentWorldScale = 1f;
|
|
|
|
public static event Action<AsteroidController> OnAsteroidDestroyed;
|
|
|
|
public static AsteroidController Spawn(
|
|
WaveCatalog catalog,
|
|
AsteroidSizeTier size,
|
|
bool isResourceAsteroid,
|
|
ResourceType resType,
|
|
int dropVal,
|
|
Vector3 spawnPos,
|
|
Vector2 initialVel,
|
|
float worldScale = 1f,
|
|
float hpMultiplier = 1f)
|
|
{
|
|
AsteroidController ctrl = null;
|
|
while (asteroidPool.Count > 0 && ctrl == null)
|
|
{
|
|
ctrl = asteroidPool.Dequeue();
|
|
}
|
|
|
|
if (ctrl == null)
|
|
{
|
|
GameObject go = new GameObject($"Asteroid_{size}");
|
|
ctrl = go.AddComponent<AsteroidController>();
|
|
}
|
|
|
|
ctrl.transform.position = spawnPos;
|
|
ctrl.gameObject.SetActive(true);
|
|
ctrl.Initialize(catalog, size, isResourceAsteroid, resType, dropVal, initialVel, worldScale, hpMultiplier);
|
|
return ctrl;
|
|
}
|
|
|
|
public static void RecycleAllActive()
|
|
{
|
|
var arr = ActiveAsteroids.ToArray();
|
|
for (int i = 0; i < arr.Length; i++)
|
|
{
|
|
if (arr[i] != null)
|
|
{
|
|
arr[i].Recycle();
|
|
}
|
|
}
|
|
ActiveAsteroids.Clear();
|
|
}
|
|
|
|
public void Recycle()
|
|
{
|
|
if (!gameObject.activeSelf) return;
|
|
gameObject.SetActive(false);
|
|
asteroidPool.Enqueue(this);
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
if (spriteRenderer == null)
|
|
{
|
|
spriteRenderer = GetComponent<SpriteRenderer>();
|
|
if (spriteRenderer == null)
|
|
spriteRenderer = gameObject.AddComponent<SpriteRenderer>();
|
|
}
|
|
|
|
if (damageable == null)
|
|
{
|
|
damageable = GetComponent<DamageableEntity>();
|
|
if (damageable == null)
|
|
damageable = gameObject.AddComponent<DamageableEntity>();
|
|
}
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
if (!ActiveAsteroids.Contains(this))
|
|
ActiveAsteroids.Add(this);
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
ActiveAsteroids.Remove(this);
|
|
if (damageable != null)
|
|
damageable.OnKilled -= HandleKilled;
|
|
}
|
|
|
|
private void Initialize(
|
|
WaveCatalog catalog,
|
|
AsteroidSizeTier tier,
|
|
bool hasRes,
|
|
ResourceType resType,
|
|
int dropAmt,
|
|
Vector2 vel,
|
|
float worldScale,
|
|
float hpMult)
|
|
{
|
|
catalogRef = catalog;
|
|
sizeTier = tier;
|
|
dropsResource = hasRes;
|
|
resourceType = resType;
|
|
resourceDropAmount = Mathf.Max(1, dropAmt);
|
|
velocity = vel;
|
|
rotationSpeedDeg = UnityEngine.Random.Range(-65f, 65f);
|
|
|
|
if (spriteRenderer == null)
|
|
{
|
|
spriteRenderer = GetComponent<SpriteRenderer>();
|
|
if (spriteRenderer == null)
|
|
spriteRenderer = gameObject.AddComponent<SpriteRenderer>();
|
|
}
|
|
spriteRenderer.sprite = PickSprite(catalog, tier, hasRes, resType);
|
|
spriteRenderer.color = Color.white;
|
|
spriteRenderer.sortingOrder = 6;
|
|
|
|
float baseHp = 22f;
|
|
switch (tier)
|
|
{
|
|
case AsteroidSizeTier.Small:
|
|
baseScaleForTier = 0.22f;
|
|
baseHp = 16f;
|
|
break;
|
|
case AsteroidSizeTier.Mid:
|
|
baseScaleForTier = 0.32f;
|
|
baseHp = 32f;
|
|
break;
|
|
case AsteroidSizeTier.Big:
|
|
baseScaleForTier = 0.44f;
|
|
baseHp = 58f;
|
|
break;
|
|
case AsteroidSizeTier.Huge:
|
|
baseScaleForTier = 0.58f;
|
|
baseHp = 95f;
|
|
break;
|
|
}
|
|
|
|
if (hasRes) baseHp *= 1.2f;
|
|
|
|
if (damageable == null)
|
|
{
|
|
damageable = GetComponent<DamageableEntity>();
|
|
if (damageable == null)
|
|
damageable = gameObject.AddComponent<DamageableEntity>();
|
|
}
|
|
damageable.category = EntityCategory.Asteroid;
|
|
damageable.displayName = hasRes ? $"{resType} Asteroid ({tier})" : $"Asteroid ({tier})";
|
|
damageable.mainSpriteRenderer = spriteRenderer;
|
|
damageable.InitializeStats(baseHp * hpMult, 0f, 0f, false, 0f, 1, null);
|
|
damageable.OnKilled -= HandleKilled;
|
|
damageable.OnKilled += HandleKilled;
|
|
|
|
ApplyWorldScale(worldScale);
|
|
}
|
|
|
|
public void ApplyWorldScale(float worldScale)
|
|
{
|
|
currentWorldScale = Mathf.Clamp(worldScale, 0.25f, 1.5f);
|
|
float finalScale = baseScaleForTier * currentWorldScale;
|
|
transform.localScale = Vector3.one * finalScale;
|
|
if (damageable != null)
|
|
{
|
|
damageable.targetHitRadius = Mathf.Max(0.22f, finalScale * 1.45f);
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (damageable != null && !damageable.IsAlive) return;
|
|
|
|
float dt = Time.deltaTime;
|
|
float speedMult = damageable != null ? damageable.MovementSpeedMultiplier : 1f;
|
|
|
|
if (damageable != null && damageable.IsHarpoonImmobilized && SpaceStationController.Instance != null)
|
|
{
|
|
Vector3 targetPos = SpaceStationController.Instance.transform.position + Vector3.up * 1.2f;
|
|
transform.position = Vector3.MoveTowards(transform.position, targetPos, damageable.harpoonPullSpeed * dt);
|
|
}
|
|
else
|
|
{
|
|
transform.position += (Vector3)(velocity * (speedMult * dt));
|
|
}
|
|
|
|
transform.Rotate(0f, 0f, rotationSpeedDeg * dt);
|
|
|
|
if (SpaceStationController.Instance != null && SpaceStationController.Instance.StationDamageable != null)
|
|
{
|
|
Vector3 stPos = SpaceStationController.Instance.transform.position;
|
|
float hitDist = (damageable != null ? damageable.targetHitRadius : 0.35f) + 0.85f;
|
|
if ((transform.position - stPos).sqrMagnitude <= hitDist * hitDist)
|
|
{
|
|
float impactDmg = sizeTier == AsteroidSizeTier.Huge ? 28f : (sizeTier == AsteroidSizeTier.Big ? 18f : 10f);
|
|
SpaceStationController.Instance.ApplyCollisionDamage(impactDmg, transform.position);
|
|
ShockwaveExplosionController.Spawn(transform.position, null, null, 0.65f * currentWorldScale, new Color(1f, 0.65f, 0.3f));
|
|
OnAsteroidDestroyed?.Invoke(this);
|
|
Recycle();
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (transform.position.y < -6.2f)
|
|
{
|
|
OnAsteroidDestroyed?.Invoke(this);
|
|
Recycle();
|
|
}
|
|
}
|
|
|
|
private void HandleKilled(DamageableEntity ent, ProjectileDefinition killingProj)
|
|
{
|
|
Vector3 pos = transform.position;
|
|
ShockwaveExplosionController.Spawn(
|
|
pos,
|
|
killingProj != null ? killingProj.explosionSprite : null,
|
|
killingProj != null ? killingProj.explosionCentroidProfile : null,
|
|
(0.55f + (int)sizeTier * 0.18f) * currentWorldScale,
|
|
new Color(1f, 0.78f, 0.45f));
|
|
|
|
// 1. Spawn Resource Pickups
|
|
bool dropLoot = dropsResource || UnityEngine.Random.value < 0.45f;
|
|
if (dropLoot)
|
|
{
|
|
int chunks = Mathf.Clamp(sizeTier == AsteroidSizeTier.Huge ? 3 : (sizeTier == AsteroidSizeTier.Big ? 2 : 1), 1, 3);
|
|
int valPerChunk = Mathf.Max(1, Mathf.CeilToInt((float)resourceDropAmount / chunks));
|
|
Sprite lootSprite = PickLootSprite(catalogRef, resourceType);
|
|
Transform stTrans = SpaceStationController.Instance != null ? SpaceStationController.Instance.transform : null;
|
|
|
|
float minScat = ResourcePickupController.ActiveConfig != null ? ResourcePickupController.ActiveConfig.minScatterSpeed : 0.8f;
|
|
float maxScat = ResourcePickupController.ActiveConfig != null ? ResourcePickupController.ActiveConfig.maxScatterSpeed : 2.2f;
|
|
for (int i = 0; i < chunks; i++)
|
|
{
|
|
Vector2 scatterVel = UnityEngine.Random.insideUnitCircle.normalized * UnityEngine.Random.Range(minScat, maxScat);
|
|
ResourcePickupController.Spawn(
|
|
resourceType,
|
|
valPerChunk,
|
|
lootSprite,
|
|
pos + (Vector3)(scatterVel * 0.12f),
|
|
scatterVel,
|
|
stTrans,
|
|
currentWorldScale);
|
|
}
|
|
}
|
|
|
|
// 2. Roll Fragmentation Chance for Big / Huge Asteroids BEFORE recycling parent so ActiveAsteroids is never 0 prematurely
|
|
if (sizeTier == AsteroidSizeTier.Huge || sizeTier == AsteroidSizeTier.Big)
|
|
{
|
|
float baseChance = catalogRef != null ? catalogRef.baseBigAsteroidSplitChance : 0.28f;
|
|
float stationBonus = SpaceStationController.Instance != null ? SpaceStationController.Instance.BonusAsteroidSplitChance : 0f;
|
|
float projBonus = killingProj != null ? killingProj.bonusAsteroidSplitChance : 0f;
|
|
float totalSplitChance = Mathf.Clamp01(baseChance + stationBonus + projBonus);
|
|
|
|
if (UnityEngine.Random.value <= totalSplitChance)
|
|
{
|
|
AsteroidSizeTier childTier = sizeTier == AsteroidSizeTier.Huge ? AsteroidSizeTier.Mid : AsteroidSizeTier.Small;
|
|
int splitCount = UnityEngine.Random.Range(2, 4);
|
|
DamageableEntity.OnSpawnFloatingText?.Invoke(pos + Vector3.up * 0.3f, "SHATTERED!", new Color(1f, 0.88f, 0.4f), 0.9f);
|
|
|
|
for (int i = 0; i < splitCount; i++)
|
|
{
|
|
float angle = -90f + UnityEngine.Random.Range(-42f, 42f);
|
|
float rad = angle * Mathf.Deg2Rad;
|
|
Vector2 childVel = new Vector2(Mathf.Cos(rad), Mathf.Sin(rad)) * (velocity.magnitude * 1.15f);
|
|
Spawn(
|
|
catalogRef,
|
|
childTier,
|
|
dropsResource,
|
|
resourceType,
|
|
Mathf.Max(1, resourceDropAmount / 2),
|
|
pos + new Vector3(UnityEngine.Random.Range(-0.25f, 0.25f), UnityEngine.Random.Range(-0.15f, 0.15f), 0f),
|
|
childVel,
|
|
currentWorldScale,
|
|
0.75f);
|
|
}
|
|
}
|
|
}
|
|
|
|
OnAsteroidDestroyed?.Invoke(this);
|
|
Recycle();
|
|
}
|
|
|
|
private static Sprite PickSprite(WaveCatalog cat, AsteroidSizeTier tier, bool hasRes, ResourceType resType)
|
|
{
|
|
if (cat != null)
|
|
{
|
|
Sprite[] pool = cat.normalAsteroidSprites;
|
|
if (hasRes)
|
|
{
|
|
switch (resType)
|
|
{
|
|
case ResourceType.Scrap: pool = cat.scrapAsteroidSprites; break;
|
|
case ResourceType.Gold: pool = cat.goldAsteroidSprites; break;
|
|
case ResourceType.Uranium: pool = cat.uraniumAsteroidSprites; break;
|
|
}
|
|
}
|
|
if (pool != null && pool.Length > 0)
|
|
{
|
|
int idx = Mathf.Clamp((int)tier, 0, pool.Length - 1);
|
|
if (pool[idx] != null) return pool[idx];
|
|
return pool[UnityEngine.Random.Range(0, pool.Length)];
|
|
}
|
|
}
|
|
return ShockwaveExplosionController.GetFallbackGlowSprite();
|
|
}
|
|
|
|
public static Sprite PickLootSprite(WaveCatalog cat, ResourceType resType)
|
|
{
|
|
if (cat != null)
|
|
{
|
|
Sprite[] pool = cat.scrapLootSprites;
|
|
if (resType == ResourceType.Gold) pool = cat.goldLootSprites;
|
|
else if (resType == ResourceType.Uranium) pool = cat.uraniumLootSprites;
|
|
|
|
if (pool != null && pool.Length > 0)
|
|
return pool[UnityEngine.Random.Range(0, pool.Length)];
|
|
}
|
|
return ShockwaveExplosionController.GetFallbackGlowSprite();
|
|
}
|
|
}
|
|
}
|