Files
AppleHillsProduction/Assets/Scripts/Minigames/FortFight/Projectiles/CeilingFanProjectile.cs
Michal Pikulski 0b8d2a279f Working MVP
2025-12-04 02:16:47 +01:00

184 lines
5.8 KiB
C#

using System.Collections;
using Core;
using UnityEngine;
namespace Minigames.FortFight.Projectiles
{
/// <summary>
/// Ceiling Fan projectile - drops straight down when player taps screen.
/// Implements ITouchInputConsumer to capture tap input mid-flight.
/// </summary>
public class CeilingFanProjectile : ProjectileBase, ITouchInputConsumer
{
[Header("Ceiling Fan Specific")]
[Tooltip("Visual indicator showing drop is available (arrow down)")]
[SerializeField] private GameObject indicator;
private bool isDropping = false;
private bool inputEnabled = false;
public override void Launch(Vector2 direction, float force)
{
base.Launch(direction, force);
// Hide indicator initially
if (indicator != null)
{
indicator.SetActive(false);
}
// Start activation delay coroutine
StartCoroutine(ActivationDelayCoroutine());
}
private IEnumerator ActivationDelayCoroutine()
{
// Get activation delay from settings
var settings = GameManager.GetSettingsObject<AppleHills.Core.Settings.IFortFightSettings>();
float activationDelay = settings?.CeilingFanActivationDelay ?? 0.5f;
// Wait for delay
yield return new WaitForSeconds(activationDelay);
// Enable input and show indicator (if not already dropped)
if (!isDropping && !AbilityActivated)
{
inputEnabled = true;
if (indicator != null)
{
indicator.SetActive(true);
}
// Register with InputManager to capture tap-to-drop
if (Input.InputManager.Instance != null)
{
Input.InputManager.Instance.RegisterOverrideConsumer(this);
Logging.Debug("[CeilingFanProjectile] Tap-to-drop now available");
}
}
}
public override void ActivateAbility()
{
base.ActivateAbility();
if (AbilityActivated)
{
Logging.Debug("[CeilingFanProjectile] Ability activated - dropping straight down");
StartCoroutine(DropCoroutine());
}
}
private IEnumerator DropCoroutine()
{
isDropping = true;
// Stop all velocity
if (rb2D != null)
{
rb2D.linearVelocity = Vector2.zero;
rb2D.angularVelocity = 0f;
}
// Get drop configuration from settings
var settings = GameManager.GetSettingsObject<AppleHills.Core.Settings.IFortFightSettings>();
float dropDelay = settings?.CeilingFanDropDelay ?? 0.2f;
float dropSpeed = settings?.CeilingFanDropSpeed ?? 20f;
// Wait brief moment
yield return new WaitForSeconds(dropDelay);
// Drop straight down
if (rb2D != null)
{
rb2D.linearVelocity = Vector2.down * dropSpeed;
Logging.Debug($"[CeilingFanProjectile] Dropping with velocity: {rb2D.linearVelocity}");
}
}
protected override void OnHit(Collision2D collision)
{
// Spawn impact effect only if dropped (not on normal arc hit)
if (isDropping)
{
SpawnImpactEffect(collision.contacts[0].point);
}
// Deal damage to blocks
var block = collision.gameObject.GetComponent<Fort.FortBlock>();
if (block != null)
{
block.TakeDamage(Damage);
Logging.Debug($"[CeilingFanProjectile] Dealt {Damage} damage to {block.gameObject.name}");
}
// Destroy projectile
DestroyProjectile();
}
#region ITouchInputConsumer Implementation
public void OnTap(Vector2 worldPosition)
{
// Only respond if input is enabled
if (inputEnabled && !AbilityActivated && !isDropping)
{
Logging.Debug("[CeilingFanProjectile] Tap detected - activating drop");
// Hide indicator
if (indicator != null)
{
indicator.SetActive(false);
}
ActivateAbility();
// Unregister immediately after tap
UnregisterFromInput();
}
}
public void OnHoldStart(Vector2 worldPosition)
{
// Not used for ceiling fan
}
public void OnHoldMove(Vector2 worldPosition)
{
// Not used for ceiling fan
}
public void OnHoldEnd(Vector2 worldPosition)
{
// Not used for ceiling fan
}
#endregion
private void UnregisterFromInput()
{
inputEnabled = false;
if (indicator != null)
{
indicator.SetActive(false);
}
if (Input.InputManager.Instance != null)
{
Input.InputManager.Instance.UnregisterOverrideConsumer(this);
Logging.Debug("[CeilingFanProjectile] Unregistered from input");
}
}
protected override void DestroyProjectile()
{
// Make sure we unregister when destroyed
UnregisterFromInput();
base.DestroyProjectile();
}
}
}