Files

311 lines
12 KiB
C#

using System;
using System.Collections.Generic;
using GazeLab.Combat;
using UnityEngine;
namespace GazeLab.Entities
{
public enum ResourceType
{
Scrap = 0,
Gold = 1,
Uranium = 2
}
public sealed class ResourcePickupController : MonoBehaviour
{
public static readonly List<ResourcePickupController> ActivePickups = new List<ResourcePickupController>();
private static readonly Queue<ResourcePickupController> pickupPool = new Queue<ResourcePickupController>();
public static GazeLab.Data.ResourcePickupConfigSO ActiveConfig { get; set; }
public ResourceType resourceType = ResourceType.Scrap;
public int amount = 1;
public float collisionRadius = 0.24f;
public Vector2 velocity;
public float angularVelocity;
private SpriteRenderer spriteRenderer;
private Transform stationTarget;
private bool isBeingSucked;
private float suctionSpeed = 5.5f;
private float lifetime = 28f;
private float worldScaleMultiplier = 1f;
public bool IsBeingSucked => isBeingSucked;
public static event Action<ResourceType, int, Vector3> OnResourceCollected;
public static ResourcePickupController Spawn(
ResourceType type,
int value,
Sprite sprite,
Vector3 worldPos,
Vector2 initialVelocity,
Transform stationTransform,
float worldScale = 1f)
{
ResourcePickupController pickup = null;
while (pickupPool.Count > 0 && pickup == null)
{
pickup = pickupPool.Dequeue();
}
if (pickup == null)
{
GameObject go = new GameObject($"Loot_{type}_{value}");
pickup = go.AddComponent<ResourcePickupController>();
}
pickup.transform.position = worldPos;
pickup.gameObject.SetActive(true);
pickup.Initialize(type, value, sprite, initialVelocity, stationTransform, worldScale);
return pickup;
}
public static void RecycleAllActive()
{
var arr = ActivePickups.ToArray();
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] != null)
{
arr[i].Recycle();
}
}
ActivePickups.Clear();
}
public void Recycle()
{
if (!gameObject.activeSelf) return;
isBeingSucked = false;
stationTarget = null;
gameObject.SetActive(false);
pickupPool.Enqueue(this);
}
private void OnEnable()
{
if (!ActivePickups.Contains(this))
ActivePickups.Add(this);
}
private void OnDisable()
{
ActivePickups.Remove(this);
}
private void Initialize(
ResourceType type,
int val,
Sprite sprite,
Vector2 initVel,
Transform station,
float scaleMult)
{
resourceType = type;
amount = Mathf.Max(1, val);
velocity = initVel;
stationTarget = station;
worldScaleMultiplier = Mathf.Clamp(scaleMult, 0.3f, 1.5f);
angularVelocity = UnityEngine.Random.Range(-110f, 110f);
isBeingSucked = false;
suctionSpeed = ActiveConfig != null ? ActiveConfig.baseSuctionSpeed : 5.5f;
lifetime = ActiveConfig != null ? ActiveConfig.lifetime : 28f;
if (spriteRenderer == null)
{
spriteRenderer = gameObject.AddComponent<SpriteRenderer>();
}
spriteRenderer.sprite = sprite != null ? sprite : ShockwaveExplosionController.GetFallbackGlowSprite();
spriteRenderer.sortingOrder = 11;
float baseScale = ActiveConfig != null
? ActiveConfig.GetBaseScale(type, val)
: (type == ResourceType.Uranium ? 0.30f : (type == ResourceType.Gold ? 0.27f : 0.25f)) * (val >= 5 ? 1.25f : 1.0f);
float colRad = ActiveConfig != null ? ActiveConfig.collisionRadius : 0.25f;
transform.localScale = Vector3.one * (baseScale * worldScaleMultiplier);
collisionRadius = colRad * worldScaleMultiplier;
}
public void SetWorldScale(float scaleMult)
{
worldScaleMultiplier = Mathf.Clamp(scaleMult, 0.3f, 1.5f);
float baseScale = ActiveConfig != null
? ActiveConfig.GetBaseScale(resourceType, amount)
: (resourceType == ResourceType.Uranium ? 0.30f : (resourceType == ResourceType.Gold ? 0.27f : 0.25f)) * (amount >= 5 ? 1.25f : 1.0f);
float colRad = ActiveConfig != null ? ActiveConfig.collisionRadius : 0.25f;
transform.localScale = Vector3.one * (baseScale * worldScaleMultiplier);
collisionRadius = colRad * worldScaleMultiplier;
}
public void ActivateTractorSuction(Transform targetStation)
{
if (isBeingSucked) return;
isBeingSucked = true;
if (targetStation != null) stationTarget = targetStation;
suctionSpeed = 4.8f;
if (spriteRenderer != null)
{
spriteRenderer.sortingOrder = 20;
}
}
private void Update()
{
float dt = Time.deltaTime;
transform.Rotate(0f, 0f, angularVelocity * dt);
if (isBeingSucked)
{
// Physics disabled during suction: fly straight into the Space Station collector
Vector3 dest = stationTarget != null ? stationTarget.position : new Vector3(0f, -3.5f, 0f);
dest.z = 0f;
float maxSpeed = ActiveConfig != null ? ActiveConfig.maxSuctionSpeed : 22f;
float accel = ActiveConfig != null ? ActiveConfig.suctionAcceleration : 18f;
suctionSpeed = Mathf.Min(maxSpeed, suctionSpeed + accel * dt);
transform.position = Vector3.MoveTowards(transform.position, dest, suctionSpeed * dt);
if ((transform.position - dest).sqrMagnitude <= 0.16f)
{
CollectNow();
}
return;
}
lifetime -= dt;
if (lifetime <= 0f)
{
// Auto-suck before expiring so the player never loses hard-earned drops
ActivateTractorSuction(stationTarget);
return;
}
// 1. Check Passive Station Proximity Magnet
if (SpaceStationController.Instance != null)
{
stationTarget = SpaceStationController.Instance.transform;
float magRad = SpaceStationController.Instance.CurrentMagnetRadius;
Vector3 toStation = stationTarget.position - transform.position;
toStation.z = 0f;
if (toStation.sqrMagnitude <= magRad * magRad)
{
ActivateTractorSuction(stationTarget);
return;
}
}
// 2. Physical Drift & Inter-Object Collisions (nudge against other resources and props)
ApplyPhysicsInteractions(dt);
// 3. Soft Screen-Boundary Containment so resources stay inside the playable space
KeepWithinScreenBounds(dt);
// Apply gentle space drag + slight downward drift toward station plane
velocity = Vector2.Lerp(velocity, new Vector2(velocity.x * 0.4f, -0.28f), dt * 0.85f);
transform.position += new Vector3(velocity.x * dt, velocity.y * dt, 0f);
}
private void ApplyPhysicsInteractions(float dt)
{
Vector3 myPos = transform.position;
// Elastic collision with other floating resources
for (int i = 0; i < ActivePickups.Count; i++)
{
ResourcePickupController other = ActivePickups[i];
if (other == null || other == this || other.isBeingSucked) continue;
Vector2 diff = (Vector2)(myPos - other.transform.position);
float minDist = collisionRadius + other.collisionRadius;
float dSq = diff.sqrMagnitude;
if (dSq > 0.0001f && dSq < minDist * minDist)
{
float dist = Mathf.Sqrt(dSq);
Vector2 normal = diff / dist;
float penetration = minDist - dist;
transform.position += (Vector3)(normal * (penetration * 0.5f));
other.transform.position -= (Vector3)(normal * (penetration * 0.5f));
Vector2 relVel = velocity - other.velocity;
float velAlongNormal = Vector2.Dot(relVel, normal);
if (velAlongNormal < 0f)
{
Vector2 impulse = normal * (-0.65f * velAlongNormal);
velocity += impulse;
other.velocity -= impulse;
}
}
}
// Pushed around by regular props (Asteroids & Ships) without influencing their velocity
var entities = DamageableEntity.ActiveEntities;
for (int i = 0; i < entities.Count; i++)
{
DamageableEntity prop = entities[i];
if (prop == null || !prop.IsAlive || prop.IsPlayerStation) continue;
Vector2 diff = (Vector2)(myPos - prop.transform.position);
float minDist = collisionRadius + prop.targetHitRadius * 0.85f;
float dSq = diff.sqrMagnitude;
if (dSq > 0.0001f && dSq < minDist * minDist)
{
float dist = Mathf.Sqrt(dSq);
Vector2 pushNormal = diff / dist;
float pushStrength = (minDist - dist) * 6.5f;
velocity += pushNormal * pushStrength;
transform.position += (Vector3)(pushNormal * (minDist - dist));
}
}
}
private void KeepWithinScreenBounds(float dt)
{
if (Core.CameraViewportService.MainCamera == null) return;
float halfH = Core.CameraViewportService.HalfHeight;
float halfW = Core.CameraViewportService.HalfWidth;
Vector3 camPos = Core.CameraViewportService.CameraPosition;
float minX = camPos.x - halfW + 0.55f;
float maxX = camPos.x + halfW - 0.55f;
float minY = camPos.y - halfH + 1.15f;
float maxY = camPos.y + halfH - 0.55f;
Vector3 p = transform.position;
if (p.x < minX) { p.x = minX; velocity.x = Mathf.Abs(velocity.x) * 0.7f + 0.4f; }
else if (p.x > maxX) { p.x = maxX; velocity.x = -Mathf.Abs(velocity.x) * 0.7f - 0.4f; }
if (p.y < minY) { p.y = minY; velocity.y = Mathf.Abs(velocity.y) * 0.6f + 0.35f; }
else if (p.y > maxY) { p.y = maxY; velocity.y = -Mathf.Abs(velocity.y) * 0.6f - 0.35f; }
transform.position = p;
}
private void CollectNow()
{
int finalAmount = amount;
if (SpaceStationController.Instance != null && SpaceStationController.Instance.resourceDropBonusMultiplier > 0f)
{
finalAmount = Mathf.Max(1, Mathf.RoundToInt(amount * (1f + SpaceStationController.Instance.resourceDropBonusMultiplier)));
}
Color col = resourceType == ResourceType.Uranium
? new Color(0.35f, 1f, 0.55f)
: (resourceType == ResourceType.Gold ? new Color(1f, 0.85f, 0.25f) : new Color(0.78f, 0.86f, 0.95f));
DamageableEntity.OnSpawnFloatingText?.Invoke(
transform.position + Vector3.up * 0.3f,
$"+{finalAmount} {resourceType.ToString().ToUpper()}",
col,
0.9f);
OnResourceCollected?.Invoke(resourceType, finalAmount, transform.position);
Recycle();
}
}
}