121 lines
4.3 KiB
C#
121 lines
4.3 KiB
C#
using Core;
|
|
using UnityEngine;
|
|
|
|
namespace Minigames.FortFight.Projectiles
|
|
{
|
|
/// <summary>
|
|
/// Trash Bag projectile - splits into multiple smaller pieces on impact.
|
|
/// Deals AOE damage in a forward cone.
|
|
/// </summary>
|
|
public class TrashBagProjectile : ProjectileBase
|
|
{
|
|
[Header("Trash Bag Specific")]
|
|
[Tooltip("Number of trash pieces to spawn on impact")]
|
|
[SerializeField] private int trashPieceCount = 6;
|
|
|
|
[Tooltip("Prefab for individual trash pieces")]
|
|
[SerializeField] private GameObject trashPiecePrefab;
|
|
|
|
[Tooltip("Force applied to each trash piece")]
|
|
[SerializeField] private float pieceForce = 8f;
|
|
|
|
[Tooltip("Spread angle for trash pieces (degrees)")]
|
|
[SerializeField] private float spreadAngle = 30f;
|
|
|
|
[Tooltip("Damage dealt by each trash piece")]
|
|
[SerializeField] private float pieceDamage = 8f;
|
|
|
|
protected override void OnHit(Collider2D hit)
|
|
{
|
|
base.OnHit(hit);
|
|
|
|
Logging.Debug($"[TrashBagProjectile] Splitting into {trashPieceCount} pieces");
|
|
SpawnTrashPieces(hit.transform.position);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Spawn multiple trash pieces in a forward cone
|
|
/// </summary>
|
|
private void SpawnTrashPieces(Vector2 impactPoint)
|
|
{
|
|
if (trashPiecePrefab == null)
|
|
{
|
|
Logging.Warning("[TrashBagProjectile] No trash piece prefab assigned!");
|
|
return;
|
|
}
|
|
|
|
// Calculate forward direction from velocity
|
|
Vector2 forwardDirection = rb2D.linearVelocity.normalized;
|
|
if (forwardDirection == Vector2.zero)
|
|
{
|
|
forwardDirection = LaunchDirection;
|
|
}
|
|
|
|
// Spawn pieces in a cone
|
|
for (int i = 0; i < trashPieceCount; i++)
|
|
{
|
|
// Calculate angle for this piece
|
|
float angleOffset = Mathf.Lerp(-spreadAngle, spreadAngle, i / (float)(trashPieceCount - 1));
|
|
float angleRadians = Mathf.Atan2(forwardDirection.y, forwardDirection.x) + angleOffset * Mathf.Deg2Rad;
|
|
|
|
Vector2 pieceDirection = new Vector2(Mathf.Cos(angleRadians), Mathf.Sin(angleRadians));
|
|
|
|
// Spawn trash piece
|
|
GameObject piece = Instantiate(trashPiecePrefab, impactPoint, Quaternion.identity);
|
|
|
|
// Setup trash piece physics
|
|
Rigidbody2D pieceRb = piece.GetComponent<Rigidbody2D>();
|
|
if (pieceRb != null)
|
|
{
|
|
pieceRb.AddForce(pieceDirection * pieceForce, ForceMode2D.Impulse);
|
|
}
|
|
|
|
// Setup trash piece damage (if it has a component)
|
|
TrashPiece trashPieceComponent = piece.GetComponent<TrashPiece>();
|
|
if (trashPieceComponent != null)
|
|
{
|
|
trashPieceComponent.Initialize(pieceDamage);
|
|
}
|
|
|
|
// Auto-destroy after 3 seconds
|
|
Destroy(piece, 3f);
|
|
|
|
Logging.Debug($"[TrashBagProjectile] Spawned trash piece {i} in direction {pieceDirection}");
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Component for individual trash pieces spawned by TrashBagProjectile.
|
|
/// Deals damage on collision.
|
|
/// </summary>
|
|
public class TrashPiece : MonoBehaviour
|
|
{
|
|
private float damage;
|
|
private bool hasHit = false;
|
|
|
|
public void Initialize(float pieceDamage)
|
|
{
|
|
this.damage = pieceDamage;
|
|
}
|
|
|
|
private void OnCollisionEnter2D(Collision2D collision)
|
|
{
|
|
if (hasHit) return;
|
|
hasHit = true;
|
|
|
|
// Check if hit a fort block
|
|
var block = collision.gameObject.GetComponent<Fort.FortBlock>();
|
|
if (block != null)
|
|
{
|
|
block.TakeDamage(damage);
|
|
Logging.Debug($"[TrashPiece] Dealt {damage} damage to {block.gameObject.name}");
|
|
}
|
|
|
|
// Destroy this piece
|
|
Destroy(gameObject, 0.1f);
|
|
}
|
|
}
|
|
}
|
|
|