MVP of the plane throwing game (#77)
Co-authored-by: Michal Pikulski <michal@foolhardyhorizons.com> Co-authored-by: Michal Pikulski <michal.a.pikulski@gmail.com> Reviewed-on: #77
This commit is contained in:
3
Assets/Scripts/Minigames/Airplane/Abilities.meta
Normal file
3
Assets/Scripts/Minigames/Airplane/Abilities.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 71a8e50c7218456c96ceb54cd1140918
|
||||
timeCreated: 1764975940
|
||||
@@ -0,0 +1,69 @@
|
||||
using AppleHills.Core.Settings;
|
||||
using Core;
|
||||
using Minigames.Airplane.Data;
|
||||
|
||||
namespace Minigames.Airplane.Abilities
|
||||
{
|
||||
/// <summary>
|
||||
/// Factory for creating airplane abilities from settings configuration.
|
||||
/// </summary>
|
||||
public static class AbilityFactory
|
||||
{
|
||||
/// <summary>
|
||||
/// Create an ability instance based on type and settings.
|
||||
/// </summary>
|
||||
public static BaseAirplaneAbility CreateAbility(AirplaneAbilityType type, IAirplaneSettings settings)
|
||||
{
|
||||
if (settings == null)
|
||||
{
|
||||
Logging.Error("[AbilityFactory] Settings is null!");
|
||||
return null;
|
||||
}
|
||||
|
||||
return type switch
|
||||
{
|
||||
AirplaneAbilityType.Jet => CreateJetAbility(settings),
|
||||
AirplaneAbilityType.Bobbing => CreateBobbingAbility(settings),
|
||||
AirplaneAbilityType.Drop => CreateDropAbility(settings),
|
||||
_ => null
|
||||
};
|
||||
}
|
||||
|
||||
private static JetAbility CreateJetAbility(IAirplaneSettings settings)
|
||||
{
|
||||
var config = settings.JetAbilityConfig;
|
||||
return new JetAbility(
|
||||
config.abilityName,
|
||||
config.abilityIcon,
|
||||
config.cooldownDuration,
|
||||
config.jetSpeed,
|
||||
config.jetAngle
|
||||
);
|
||||
}
|
||||
|
||||
private static BobbingAbility CreateBobbingAbility(IAirplaneSettings settings)
|
||||
{
|
||||
var config = settings.BobbingAbilityConfig;
|
||||
return new BobbingAbility(
|
||||
config.abilityName,
|
||||
config.abilityIcon,
|
||||
config.cooldownDuration,
|
||||
config.bobForce
|
||||
);
|
||||
}
|
||||
|
||||
private static DropAbility CreateDropAbility(IAirplaneSettings settings)
|
||||
{
|
||||
var config = settings.DropAbilityConfig;
|
||||
return new DropAbility(
|
||||
config.abilityName,
|
||||
config.abilityIcon,
|
||||
config.cooldownDuration,
|
||||
config.dropForce,
|
||||
config.dropDistance,
|
||||
config.zeroHorizontalVelocity
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6668ddc48c30428f98d780700e93cab5
|
||||
timeCreated: 1764977809
|
||||
@@ -0,0 +1,232 @@
|
||||
using System;
|
||||
using Core;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Minigames.Airplane.Abilities
|
||||
{
|
||||
/// <summary>
|
||||
/// Abstract base class for airplane special abilities.
|
||||
/// Each ability defines its own execution logic, input handling, and cooldown.
|
||||
/// Subclasses override Execute() to implement specific ability behavior.
|
||||
/// Created from settings configuration at runtime.
|
||||
/// </summary>
|
||||
[System.Serializable]
|
||||
public abstract class BaseAirplaneAbility
|
||||
{
|
||||
#region Configuration
|
||||
|
||||
protected readonly string abilityName;
|
||||
protected readonly Sprite abilityIcon;
|
||||
protected readonly float cooldownDuration;
|
||||
protected readonly bool canReuse;
|
||||
protected bool showDebugLogs;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
|
||||
/// <summary>
|
||||
/// Base constructor for abilities. Called by subclasses.
|
||||
/// </summary>
|
||||
protected BaseAirplaneAbility(string name, Sprite icon, float cooldown, bool reusable = true)
|
||||
{
|
||||
abilityName = name;
|
||||
abilityIcon = icon;
|
||||
cooldownDuration = cooldown;
|
||||
canReuse = reusable;
|
||||
showDebugLogs = false;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
|
||||
public string AbilityName => abilityName;
|
||||
public Sprite AbilityIcon => abilityIcon;
|
||||
public float CooldownDuration => cooldownDuration;
|
||||
public bool CanReuse => canReuse;
|
||||
|
||||
#endregion
|
||||
|
||||
#region State (Runtime)
|
||||
|
||||
protected Core.AirplaneController currentAirplane;
|
||||
protected bool isActive;
|
||||
protected bool isOnCooldown;
|
||||
protected float cooldownTimer;
|
||||
|
||||
public bool IsActive => isActive;
|
||||
public bool IsOnCooldown => isOnCooldown;
|
||||
public float CooldownRemaining => cooldownTimer;
|
||||
public bool CanActivate => !isOnCooldown && !isActive && currentAirplane != null && currentAirplane.IsFlying;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Events
|
||||
|
||||
public event Action<BaseAirplaneAbility> OnAbilityActivated;
|
||||
public event Action<BaseAirplaneAbility> OnAbilityDeactivated;
|
||||
public event Action<float, float> OnCooldownChanged; // (remaining, total)
|
||||
|
||||
#endregion
|
||||
|
||||
#region Lifecycle
|
||||
|
||||
/// <summary>
|
||||
/// Initialize ability with airplane reference.
|
||||
/// Called when airplane is spawned.
|
||||
/// </summary>
|
||||
public virtual void Initialize(Core.AirplaneController airplane)
|
||||
{
|
||||
currentAirplane = airplane;
|
||||
isActive = false;
|
||||
isOnCooldown = false;
|
||||
cooldownTimer = 0f;
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[{abilityName}] Initialized with airplane");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update cooldown timer. Called every frame by ability manager.
|
||||
/// </summary>
|
||||
public virtual void UpdateCooldown(float deltaTime)
|
||||
{
|
||||
if (isOnCooldown)
|
||||
{
|
||||
cooldownTimer -= deltaTime;
|
||||
|
||||
if (cooldownTimer <= 0f)
|
||||
{
|
||||
cooldownTimer = 0f;
|
||||
isOnCooldown = false;
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[{abilityName}] Cooldown complete");
|
||||
}
|
||||
}
|
||||
|
||||
OnCooldownChanged?.Invoke(cooldownTimer, cooldownDuration);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cleanup when airplane is destroyed or flight ends.
|
||||
/// </summary>
|
||||
public virtual void Cleanup()
|
||||
{
|
||||
if (isActive)
|
||||
{
|
||||
Deactivate();
|
||||
}
|
||||
|
||||
currentAirplane = null;
|
||||
isActive = false;
|
||||
isOnCooldown = false;
|
||||
cooldownTimer = 0f;
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[{abilityName}] Cleaned up");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Abstract Methods (Must Override)
|
||||
|
||||
/// <summary>
|
||||
/// Execute the ability effect.
|
||||
/// Override to implement specific ability behavior.
|
||||
/// </summary>
|
||||
public abstract void Execute();
|
||||
|
||||
/// <summary>
|
||||
/// Stop the ability effect (for sustained abilities).
|
||||
/// Override if ability can be deactivated.
|
||||
/// </summary>
|
||||
public virtual void Deactivate()
|
||||
{
|
||||
if (!isActive) return;
|
||||
|
||||
isActive = false;
|
||||
OnAbilityDeactivated?.Invoke(this);
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[{abilityName}] Deactivated");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Protected Helpers
|
||||
|
||||
/// <summary>
|
||||
/// Start ability activation (called by subclasses).
|
||||
/// </summary>
|
||||
protected virtual void StartActivation()
|
||||
{
|
||||
if (!CanActivate)
|
||||
{
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Warning($"[{abilityName}] Cannot activate - IsOnCooldown: {isOnCooldown}, IsActive: {isActive}, CanFly: {currentAirplane?.IsFlying}");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
isActive = true;
|
||||
OnAbilityActivated?.Invoke(this);
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[{abilityName}] Activated");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Start cooldown timer (called by subclasses after execution).
|
||||
/// </summary>
|
||||
protected virtual void StartCooldown()
|
||||
{
|
||||
isOnCooldown = true;
|
||||
cooldownTimer = cooldownDuration;
|
||||
OnCooldownChanged?.Invoke(cooldownTimer, cooldownDuration);
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[{abilityName}] Cooldown started: {cooldownDuration}s");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if airplane reference is valid.
|
||||
/// </summary>
|
||||
protected bool ValidateAirplane()
|
||||
{
|
||||
if (currentAirplane == null)
|
||||
{
|
||||
Logging.Warning($"[{abilityName}] Cannot execute - airplane reference is null!");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!currentAirplane.IsFlying)
|
||||
{
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[{abilityName}] Cannot execute - airplane is not flying!");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9b5ef9d7a9ce48ddb98de1e974e1d496
|
||||
timeCreated: 1764975940
|
||||
@@ -0,0 +1,63 @@
|
||||
using Core;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Minigames.Airplane.Abilities
|
||||
{
|
||||
/// <summary>
|
||||
/// Bobbing Plane Ability: Tap to jump upward and forward.
|
||||
/// Instant ability - activates once, then cooldown.
|
||||
/// Applies diagonal impulse (forward + upward) to maintain airborne momentum.
|
||||
/// Configuration loaded from settings at runtime.
|
||||
/// </summary>
|
||||
public class BobbingAbility : BaseAirplaneAbility
|
||||
{
|
||||
#region Configuration
|
||||
|
||||
private readonly Vector2 bobForce;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
|
||||
/// <summary>
|
||||
/// Create bobbing ability with configuration from settings.
|
||||
/// </summary>
|
||||
public BobbingAbility(string name, Sprite icon, float cooldown, Vector2 force)
|
||||
: base(name, icon, cooldown)
|
||||
{
|
||||
bobForce = force;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Override Methods
|
||||
|
||||
public override void Execute()
|
||||
{
|
||||
if (!ValidateAirplane()) return;
|
||||
if (!CanActivate) return;
|
||||
|
||||
StartActivation();
|
||||
|
||||
var rb = currentAirplane.GetComponent<Rigidbody2D>();
|
||||
if (rb != null)
|
||||
{
|
||||
// Apply configured forward and upward impulse
|
||||
// X = forward momentum, Y = upward lift
|
||||
rb.AddForce(bobForce, ForceMode2D.Impulse);
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[BobbingAbility] Executed - Force: {bobForce} (forward: {bobForce.x:F1}, upward: {bobForce.y:F1})");
|
||||
}
|
||||
}
|
||||
|
||||
// Instant ability - deactivate immediately and start cooldown
|
||||
base.Deactivate();
|
||||
StartCooldown();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cc60dfa311424a7a9f2fdfe19eda8639
|
||||
timeCreated: 1764975962
|
||||
140
Assets/Scripts/Minigames/Airplane/Abilities/DropAbility.cs
Normal file
140
Assets/Scripts/Minigames/Airplane/Abilities/DropAbility.cs
Normal file
@@ -0,0 +1,140 @@
|
||||
using System.Collections;
|
||||
using Core;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Minigames.Airplane.Abilities
|
||||
{
|
||||
/// <summary>
|
||||
/// Drop Plane Ability: Swipe down to drop straight down.
|
||||
/// Sustained ability - drops for fixed duration/distance.
|
||||
/// Good for precision strikes on targets.
|
||||
/// Configuration loaded from settings at runtime.
|
||||
/// </summary>
|
||||
public class DropAbility : BaseAirplaneAbility
|
||||
{
|
||||
#region Configuration
|
||||
|
||||
private readonly float dropForce;
|
||||
private readonly float dropDistance;
|
||||
private readonly bool zeroHorizontalVelocity;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
|
||||
/// <summary>
|
||||
/// Create drop ability with configuration from settings.
|
||||
/// </summary>
|
||||
public DropAbility(string name, Sprite icon, float cooldown, float force, float distance, bool zeroHorizontal = true)
|
||||
: base(name, icon, cooldown)
|
||||
{
|
||||
dropForce = force;
|
||||
dropDistance = distance;
|
||||
zeroHorizontalVelocity = zeroHorizontal;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region State
|
||||
|
||||
private float originalXVelocity;
|
||||
private Vector3 dropStartPosition;
|
||||
private Coroutine dropCoroutine;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Override Methods
|
||||
|
||||
public override void Execute()
|
||||
{
|
||||
if (!ValidateAirplane()) return;
|
||||
if (!CanActivate) return;
|
||||
|
||||
StartActivation();
|
||||
|
||||
var rb = currentAirplane.GetComponent<Rigidbody2D>();
|
||||
if (rb != null)
|
||||
{
|
||||
// Store original velocity
|
||||
originalXVelocity = rb.linearVelocity.x;
|
||||
|
||||
// Zero horizontal velocity if configured
|
||||
if (zeroHorizontalVelocity)
|
||||
{
|
||||
rb.linearVelocity = new Vector2(0f, rb.linearVelocity.y);
|
||||
}
|
||||
|
||||
// Apply strong downward force
|
||||
rb.AddForce(Vector2.down * dropForce, ForceMode2D.Impulse);
|
||||
|
||||
// Track drop distance
|
||||
dropStartPosition = currentAirplane.transform.position;
|
||||
|
||||
// Start monitoring drop distance
|
||||
dropCoroutine = currentAirplane.StartCoroutine(MonitorDropDistance());
|
||||
}
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[DropAbility] Activated - Force: {dropForce}, Distance: {dropDistance}");
|
||||
}
|
||||
}
|
||||
|
||||
public override void Deactivate()
|
||||
{
|
||||
if (!isActive) return;
|
||||
|
||||
// Stop monitoring
|
||||
if (dropCoroutine != null && currentAirplane != null)
|
||||
{
|
||||
currentAirplane.StopCoroutine(dropCoroutine);
|
||||
dropCoroutine = null;
|
||||
}
|
||||
|
||||
// Restore horizontal velocity (optional)
|
||||
if (currentAirplane != null)
|
||||
{
|
||||
var rb = currentAirplane.GetComponent<Rigidbody2D>();
|
||||
if (rb != null && zeroHorizontalVelocity)
|
||||
{
|
||||
Vector2 currentVel = rb.linearVelocity;
|
||||
rb.linearVelocity = new Vector2(originalXVelocity * 0.5f, currentVel.y); // Resume at reduced speed
|
||||
}
|
||||
}
|
||||
|
||||
base.Deactivate();
|
||||
|
||||
// Start cooldown
|
||||
StartCooldown();
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug("[DropAbility] Deactivated, cooldown started");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Drop Monitoring
|
||||
|
||||
private IEnumerator MonitorDropDistance()
|
||||
{
|
||||
while (isActive && currentAirplane != null)
|
||||
{
|
||||
float distanceDropped = Mathf.Abs(dropStartPosition.y - currentAirplane.transform.position.y);
|
||||
|
||||
if (distanceDropped >= dropDistance)
|
||||
{
|
||||
// Drop distance reached - deactivate
|
||||
Deactivate();
|
||||
yield break;
|
||||
}
|
||||
|
||||
yield return null;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3b79dff7e24b4167af7631351242d500
|
||||
timeCreated: 1764975977
|
||||
104
Assets/Scripts/Minigames/Airplane/Abilities/JetAbility.cs
Normal file
104
Assets/Scripts/Minigames/Airplane/Abilities/JetAbility.cs
Normal file
@@ -0,0 +1,104 @@
|
||||
using Core;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Minigames.Airplane.Abilities
|
||||
{
|
||||
/// <summary>
|
||||
/// Jet Plane Ability: Hold to fly straight without gravity.
|
||||
/// Sustained ability - active while button held, deactivates on release.
|
||||
/// </summary>
|
||||
public class JetAbility : BaseAirplaneAbility
|
||||
{
|
||||
#region Configuration
|
||||
|
||||
private readonly float jetSpeed;
|
||||
private readonly float jetAngle;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
|
||||
/// <summary>
|
||||
/// Create jet ability with configuration from settings.
|
||||
/// </summary>
|
||||
public JetAbility(string name, Sprite icon, float cooldown, float speed, float angle)
|
||||
: base(name, icon, cooldown)
|
||||
{
|
||||
jetSpeed = speed;
|
||||
jetAngle = angle;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region State
|
||||
|
||||
private float originalGravityScale;
|
||||
private bool originalRotateToVelocity;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Override Methods
|
||||
|
||||
public override void Execute()
|
||||
{
|
||||
if (!ValidateAirplane()) return;
|
||||
if (!CanActivate) return;
|
||||
|
||||
StartActivation();
|
||||
|
||||
// Store original physics values
|
||||
var rb = currentAirplane.GetComponent<Rigidbody2D>();
|
||||
if (rb != null)
|
||||
{
|
||||
originalGravityScale = rb.gravityScale;
|
||||
|
||||
// Disable gravity
|
||||
rb.gravityScale = 0f;
|
||||
|
||||
// Set constant velocity in forward direction
|
||||
Vector2 direction = Quaternion.Euler(0, 0, jetAngle) * Vector2.right;
|
||||
rb.linearVelocity = direction.normalized * jetSpeed;
|
||||
}
|
||||
|
||||
// Disable rotation to velocity (maintain straight angle)
|
||||
originalRotateToVelocity = currentAirplane.RotateToVelocity;
|
||||
currentAirplane.RotateToVelocity = false;
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[JetAbility] Activated - Speed: {jetSpeed}, Angle: {jetAngle}");
|
||||
}
|
||||
}
|
||||
|
||||
public override void Deactivate()
|
||||
{
|
||||
if (!isActive) return;
|
||||
|
||||
// Restore original physics
|
||||
if (currentAirplane != null)
|
||||
{
|
||||
var rb = currentAirplane.GetComponent<Rigidbody2D>();
|
||||
if (rb != null)
|
||||
{
|
||||
rb.gravityScale = originalGravityScale;
|
||||
}
|
||||
|
||||
// Restore rotation behavior
|
||||
currentAirplane.RotateToVelocity = originalRotateToVelocity;
|
||||
}
|
||||
|
||||
base.Deactivate();
|
||||
|
||||
// Start cooldown after deactivation
|
||||
StartCooldown();
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug("[JetAbility] Deactivated, cooldown started");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1175e6da9b23482c8ca74e18b35a82e4
|
||||
timeCreated: 1764975953
|
||||
3
Assets/Scripts/Minigames/Airplane/Core.meta
Normal file
3
Assets/Scripts/Minigames/Airplane/Core.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 48e6932cbd9645bfac8add678e705033
|
||||
timeCreated: 1764851249
|
||||
120
Assets/Scripts/Minigames/Airplane/Core/AirplaneCameraManager.cs
Normal file
120
Assets/Scripts/Minigames/Airplane/Core/AirplaneCameraManager.cs
Normal file
@@ -0,0 +1,120 @@
|
||||
using Common.Camera;
|
||||
using Core;
|
||||
using Minigames.Airplane.Data;
|
||||
using Unity.Cinemachine;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Minigames.Airplane.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Manages camera states for the airplane minigame.
|
||||
/// Handles transitions between Intro, NextPerson, Aiming, and Flight cameras.
|
||||
/// Flight camera includes follow functionality for tracking airplanes.
|
||||
/// </summary>
|
||||
public class AirplaneCameraManager : CameraStateManager<AirplaneCameraState>
|
||||
{
|
||||
#region Singleton
|
||||
|
||||
private static AirplaneCameraManager _instance;
|
||||
public static AirplaneCameraManager Instance => _instance;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region Lifecycle
|
||||
|
||||
internal override void OnManagedAwake()
|
||||
{
|
||||
// Base class handles InitializeCameraMap() and ValidateCameras()
|
||||
base.OnManagedAwake();
|
||||
|
||||
// Set singleton
|
||||
if (_instance != null && _instance != this)
|
||||
{
|
||||
Logging.Warning("[AirplaneCameraManager] Multiple instances detected! Destroying duplicate.");
|
||||
Destroy(gameObject);
|
||||
return;
|
||||
}
|
||||
_instance = this;
|
||||
}
|
||||
|
||||
internal override void OnManagedDestroy()
|
||||
{
|
||||
base.OnManagedDestroy();
|
||||
|
||||
if (_instance == this)
|
||||
{
|
||||
_instance = null;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Validation
|
||||
|
||||
protected override void ValidateCameras()
|
||||
{
|
||||
// Base class validates all enum states have cameras assigned
|
||||
base.ValidateCameras();
|
||||
|
||||
// Additional validation: Check if flight camera has follow component
|
||||
var flightCamera = GetCamera(AirplaneCameraState.Flight);
|
||||
if (flightCamera != null)
|
||||
{
|
||||
var followComponent = flightCamera.GetComponent<CinemachineFollow>();
|
||||
if (followComponent == null)
|
||||
{
|
||||
Logging.Warning("[AirplaneCameraManager] Flight camera missing CinemachineFollow component!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Flight Camera Follow
|
||||
|
||||
/// <summary>
|
||||
/// Start following an airplane with the flight camera
|
||||
/// </summary>
|
||||
public void StartFollowingAirplane(Transform airplaneTransform)
|
||||
{
|
||||
var flightCamera = GetCamera(AirplaneCameraState.Flight);
|
||||
if (flightCamera == null)
|
||||
{
|
||||
Logging.Warning("[AirplaneCameraManager] Cannot follow airplane - flight camera not assigned!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (airplaneTransform == null)
|
||||
{
|
||||
Logging.Warning("[AirplaneCameraManager] Cannot follow null airplane transform!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Set the follow target on the flight camera
|
||||
flightCamera.Target.TrackingTarget = airplaneTransform;
|
||||
|
||||
// Switch to flight camera
|
||||
SwitchToState(AirplaneCameraState.Flight);
|
||||
|
||||
if (showDebugLogs) Logging.Debug($"[AirplaneCameraManager] Now following airplane: {airplaneTransform.gameObject.name}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stop following the airplane and clear the target
|
||||
/// </summary>
|
||||
public void StopFollowingAirplane()
|
||||
{
|
||||
var flightCamera = GetCamera(AirplaneCameraState.Flight);
|
||||
if (flightCamera == null) return;
|
||||
|
||||
// Clear the follow target
|
||||
flightCamera.Target.TrackingTarget = null;
|
||||
|
||||
if (showDebugLogs) Logging.Debug("[AirplaneCameraManager] Stopped following airplane");
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 34b856742e12475793b85a0a3019d67b
|
||||
timeCreated: 1764851249
|
||||
385
Assets/Scripts/Minigames/Airplane/Core/AirplaneController.cs
Normal file
385
Assets/Scripts/Minigames/Airplane/Core/AirplaneController.cs
Normal file
@@ -0,0 +1,385 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using Core;
|
||||
using Core.Lifecycle;
|
||||
using Minigames.Airplane.Abilities;
|
||||
using Minigames.Airplane.Data;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Minigames.Airplane.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Controls airplane movement using physics-based flight.
|
||||
/// Uses dynamic Rigidbody2D with impulse force for smooth, natural motion.
|
||||
/// Follows an arc trajectory based on launch parameters and gravity.
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(Rigidbody2D), typeof(Collider2D))]
|
||||
public class AirplaneController : ManagedBehaviour
|
||||
{
|
||||
#region Events
|
||||
|
||||
/// <summary>
|
||||
/// Fired when airplane is launched. Parameters: (AirplaneController airplane)
|
||||
/// </summary>
|
||||
public event Action<AirplaneController> OnLaunched;
|
||||
|
||||
/// <summary>
|
||||
/// Fired when airplane lands/stops. Parameters: (AirplaneController airplane)
|
||||
/// </summary>
|
||||
public event Action<AirplaneController> OnLanded;
|
||||
|
||||
/// <summary>
|
||||
/// Fired when airplane hits a target. Parameters: (AirplaneController airplane, string targetName)
|
||||
/// </summary>
|
||||
public event Action<AirplaneController, string> OnTargetHit;
|
||||
|
||||
/// <summary>
|
||||
/// Fired when airplane times out. Parameters: (AirplaneController airplane)
|
||||
/// </summary>
|
||||
public event Action<AirplaneController> OnTimeout;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Inspector Properties
|
||||
|
||||
[Header("Flight Settings")]
|
||||
[Tooltip("Gravity multiplier for arc calculation")]
|
||||
[SerializeField] private float gravity = 9.81f;
|
||||
|
||||
[Header("Debug")]
|
||||
[SerializeField] private bool showDebugLogs = false;
|
||||
|
||||
#endregion
|
||||
|
||||
#region State
|
||||
|
||||
private Rigidbody2D rb2D;
|
||||
private Collider2D airplaneCollider;
|
||||
private bool isFlying = false;
|
||||
private float flightTimer = 0f;
|
||||
private string lastHitTarget = null;
|
||||
|
||||
// Runtime values loaded from settings
|
||||
private float mass;
|
||||
private float maxFlightTime;
|
||||
|
||||
// Ability system
|
||||
private BaseAirplaneAbility currentAbility;
|
||||
|
||||
public bool IsFlying => isFlying;
|
||||
public Vector2 CurrentVelocity => rb2D != null ? rb2D.linearVelocity : Vector2.zero;
|
||||
public string LastHitTarget => lastHitTarget;
|
||||
public BaseAirplaneAbility CurrentAbility => currentAbility;
|
||||
public bool RotateToVelocity { get; set; } = true; // Made public for ability access
|
||||
|
||||
#endregion
|
||||
|
||||
#region Lifecycle
|
||||
|
||||
internal override void OnManagedAwake()
|
||||
{
|
||||
base.OnManagedAwake();
|
||||
|
||||
// Load settings
|
||||
var settings = GameManager.GetSettingsObject<AppleHills.Core.Settings.IAirplaneSettings>();
|
||||
if (settings != null)
|
||||
{
|
||||
mass = settings.AirplaneMass;
|
||||
maxFlightTime = settings.MaxFlightTime;
|
||||
}
|
||||
else
|
||||
{
|
||||
Logging.Warning("[AirplaneController] AirplaneSettings not found, using defaults!");
|
||||
mass = 1f;
|
||||
maxFlightTime = 10f;
|
||||
}
|
||||
|
||||
// Cache components
|
||||
rb2D = GetComponent<Rigidbody2D>();
|
||||
airplaneCollider = GetComponent<Collider2D>();
|
||||
|
||||
// Configure Rigidbody2D for physics-based movement
|
||||
if (rb2D != null)
|
||||
{
|
||||
rb2D.bodyType = RigidbodyType2D.Dynamic;
|
||||
rb2D.mass = mass;
|
||||
rb2D.gravityScale = 1f; // Use Unity's gravity
|
||||
rb2D.collisionDetectionMode = CollisionDetectionMode2D.Continuous;
|
||||
}
|
||||
|
||||
// Configure Collider2D as trigger
|
||||
if (airplaneCollider != null)
|
||||
{
|
||||
airplaneCollider.isTrigger = true;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Launch
|
||||
|
||||
/// <summary>
|
||||
/// Launch the airplane with physics impulse force
|
||||
/// </summary>
|
||||
public void Launch(Vector2 direction, float force)
|
||||
{
|
||||
if (isFlying)
|
||||
{
|
||||
Logging.Warning($"[AirplaneController] {gameObject.name} already flying!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (rb2D == null)
|
||||
{
|
||||
Logging.Error("[AirplaneController] Cannot launch - Rigidbody2D is null!");
|
||||
return;
|
||||
}
|
||||
|
||||
isFlying = true;
|
||||
flightTimer = 0f;
|
||||
lastHitTarget = null;
|
||||
|
||||
// Apply impulse force - Unity physics handles the rest
|
||||
Vector2 impulse = direction.normalized * force;
|
||||
rb2D.AddForce(impulse, ForceMode2D.Impulse);
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
float expectedSpeed = force / mass;
|
||||
Logging.Debug($"[AirplaneController] Launched - Force: {force:F2}, Mass: {mass:F2}, " +
|
||||
$"Expected Speed: {expectedSpeed:F2}, Direction: {direction}");
|
||||
}
|
||||
|
||||
OnLaunched?.Invoke(this);
|
||||
|
||||
// Start flight monitoring (timeout and rotation)
|
||||
StartCoroutine(FlightMonitorCoroutine());
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Flight Monitoring
|
||||
|
||||
/// <summary>
|
||||
/// Monitor airplane flight for rotation and timeout.
|
||||
/// Physics movement is handled automatically by Unity.
|
||||
/// </summary>
|
||||
private IEnumerator FlightMonitorCoroutine()
|
||||
{
|
||||
while (isFlying)
|
||||
{
|
||||
// Rotate to face velocity direction (visual only)
|
||||
if (RotateToVelocity && rb2D != null && rb2D.linearVelocity.magnitude > 0.1f)
|
||||
{
|
||||
float angle = Mathf.Atan2(rb2D.linearVelocity.y, rb2D.linearVelocity.x) * Mathf.Rad2Deg;
|
||||
transform.rotation = Quaternion.Euler(0, 0, angle);
|
||||
}
|
||||
|
||||
// Update ability cooldown
|
||||
if (currentAbility != null)
|
||||
{
|
||||
currentAbility.UpdateCooldown(Time.deltaTime);
|
||||
}
|
||||
|
||||
// Update flight timer
|
||||
flightTimer += Time.deltaTime;
|
||||
|
||||
// Check for timeout
|
||||
if (flightTimer >= maxFlightTime)
|
||||
{
|
||||
if (showDebugLogs) Logging.Debug("[AirplaneController] Flight timeout reached");
|
||||
HandleTimeout();
|
||||
yield break;
|
||||
}
|
||||
|
||||
// Check if airplane has gone off screen
|
||||
if (transform.position.y < -10f)
|
||||
{
|
||||
if (showDebugLogs) Logging.Debug("[AirplaneController] Airplane went off screen");
|
||||
HandleLanding();
|
||||
yield break;
|
||||
}
|
||||
|
||||
yield return null; // Update every frame, not just fixed update
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Collision Detection
|
||||
|
||||
/// <summary>
|
||||
/// Detect trigger collisions with targets
|
||||
/// </summary>
|
||||
private void OnTriggerEnter2D(Collider2D other)
|
||||
{
|
||||
if (!isFlying) return;
|
||||
|
||||
// Check if it's a target
|
||||
var target = other.GetComponent<Minigames.Airplane.Targets.AirplaneTarget>();
|
||||
if (target != null)
|
||||
{
|
||||
lastHitTarget = target.TargetName;
|
||||
|
||||
if (showDebugLogs) Logging.Debug($"[AirplaneController] Hit target: {lastHitTarget}");
|
||||
|
||||
OnTargetHit?.Invoke(this, lastHitTarget);
|
||||
|
||||
// Land after hitting target
|
||||
HandleLanding();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Landing and Timeout
|
||||
|
||||
/// <summary>
|
||||
/// Handle airplane landing
|
||||
/// </summary>
|
||||
private void HandleLanding()
|
||||
{
|
||||
if (!isFlying) return;
|
||||
|
||||
isFlying = false;
|
||||
|
||||
if (rb2D != null)
|
||||
{
|
||||
rb2D.linearVelocity = Vector2.zero;
|
||||
rb2D.angularVelocity = 0f;
|
||||
}
|
||||
|
||||
if (showDebugLogs) Logging.Debug("[AirplaneController] Airplane landed");
|
||||
|
||||
OnLanded?.Invoke(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handle airplane timeout
|
||||
/// </summary>
|
||||
private void HandleTimeout()
|
||||
{
|
||||
if (!isFlying) return;
|
||||
|
||||
isFlying = false;
|
||||
|
||||
if (rb2D != null)
|
||||
{
|
||||
rb2D.linearVelocity = Vector2.zero;
|
||||
rb2D.angularVelocity = 0f;
|
||||
}
|
||||
|
||||
if (showDebugLogs) Logging.Debug("[AirplaneController] Airplane timed out");
|
||||
|
||||
OnTimeout?.Invoke(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Public method to force stop the airplane
|
||||
/// </summary>
|
||||
public void ForceStop()
|
||||
{
|
||||
HandleLanding();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Ability System
|
||||
|
||||
/// <summary>
|
||||
/// Initialize airplane with ability type from settings.
|
||||
/// Called before launch to setup airplane properties.
|
||||
/// </summary>
|
||||
public void Initialize(AirplaneAbilityType abilityType)
|
||||
{
|
||||
// Get settings
|
||||
var settings = GameManager.GetSettingsObject<AppleHills.Core.Settings.IAirplaneSettings>();
|
||||
if (settings == null)
|
||||
{
|
||||
Logging.Error("[AirplaneController] Cannot initialize - settings not found!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Get airplane config
|
||||
var config = settings.GetAirplaneConfig(abilityType);
|
||||
|
||||
// Create ability from settings
|
||||
currentAbility = Abilities.AbilityFactory.CreateAbility(abilityType, settings);
|
||||
if (currentAbility != null)
|
||||
{
|
||||
currentAbility.Initialize(this);
|
||||
}
|
||||
|
||||
// Apply physics overrides
|
||||
if (rb2D != null)
|
||||
{
|
||||
if (config.overrideMass)
|
||||
{
|
||||
rb2D.mass = config.mass;
|
||||
mass = config.mass;
|
||||
}
|
||||
|
||||
if (config.overrideGravityScale)
|
||||
rb2D.gravityScale = config.gravityScale;
|
||||
|
||||
if (config.overrideDrag)
|
||||
rb2D.linearDamping = config.drag;
|
||||
}
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[AirplaneController] Initialized with type: {config.displayName}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Activate the airplane's special ability.
|
||||
/// Called by UI button or input system.
|
||||
/// </summary>
|
||||
public void ActivateAbility()
|
||||
{
|
||||
if (currentAbility != null && currentAbility.CanActivate)
|
||||
{
|
||||
currentAbility.Execute();
|
||||
}
|
||||
else if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug("[AirplaneController] Cannot activate ability - not ready or no ability assigned");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deactivate the airplane's special ability (for sustained abilities).
|
||||
/// Called when releasing hold button.
|
||||
/// </summary>
|
||||
public void DeactivateAbility()
|
||||
{
|
||||
if (currentAbility != null && currentAbility.IsActive)
|
||||
{
|
||||
currentAbility.Deactivate();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cleanup
|
||||
|
||||
internal override void OnManagedDestroy()
|
||||
{
|
||||
base.OnManagedDestroy();
|
||||
|
||||
// Cleanup ability
|
||||
if (currentAbility != null)
|
||||
{
|
||||
currentAbility.Cleanup();
|
||||
currentAbility = null;
|
||||
}
|
||||
|
||||
// Stop any coroutines
|
||||
StopAllCoroutines();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0cdaac23e969495d8c0deeaf236c259e
|
||||
timeCreated: 1764851277
|
||||
757
Assets/Scripts/Minigames/Airplane/Core/AirplaneGameManager.cs
Normal file
757
Assets/Scripts/Minigames/Airplane/Core/AirplaneGameManager.cs
Normal file
@@ -0,0 +1,757 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using Core;
|
||||
using Core.Lifecycle;
|
||||
using Minigames.Airplane.Data;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Minigames.Airplane.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Main game manager for the airplane minigame.
|
||||
/// Orchestrates game flow through state machine with distinct phases:
|
||||
/// Intro -> NextPerson -> Aiming -> Flying -> Evaluating -> (repeat or GameOver)
|
||||
/// </summary>
|
||||
public class AirplaneGameManager : ManagedBehaviour
|
||||
{
|
||||
#region Singleton
|
||||
|
||||
private static AirplaneGameManager _instance;
|
||||
public static AirplaneGameManager Instance => _instance;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Inspector References
|
||||
|
||||
[Header("Core Systems")]
|
||||
[SerializeField] private PersonQueue personQueue;
|
||||
[SerializeField] private AirplaneCameraManager cameraManager;
|
||||
[SerializeField] private AirplaneLaunchController launchController;
|
||||
[SerializeField] private AirplaneTargetValidator targetValidator;
|
||||
[SerializeField] private AirplaneSpawnManager spawnManager;
|
||||
|
||||
[Header("Airplane Type Selection")]
|
||||
[SerializeField] private UI.AirplaneSelectionUI selectionUI;
|
||||
[SerializeField] private UI.AirplaneAbilityButton abilityButton;
|
||||
|
||||
[Header("Debug")]
|
||||
[SerializeField] private bool showDebugLogs = true;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Events
|
||||
|
||||
/// <summary>
|
||||
/// Fired when game state changes. Parameters: (AirplaneGameState oldState, AirplaneGameState newState)
|
||||
/// </summary>
|
||||
public event Action<AirplaneGameState, AirplaneGameState> OnStateChanged;
|
||||
|
||||
/// <summary>
|
||||
/// Fired when a person starts their turn. Parameters: (Person person)
|
||||
/// </summary>
|
||||
public event Action<Person> OnPersonStartTurn;
|
||||
|
||||
/// <summary>
|
||||
/// Fired when a person finishes their turn. Parameters: (Person person, bool success)
|
||||
/// </summary>
|
||||
public event Action<Person, bool> OnPersonFinishTurn;
|
||||
|
||||
/// <summary>
|
||||
/// Fired when game completes
|
||||
/// </summary>
|
||||
public event Action OnGameComplete;
|
||||
|
||||
#endregion
|
||||
|
||||
#region State
|
||||
|
||||
private AirplaneGameState _currentState = AirplaneGameState.AirplaneSelection;
|
||||
private Person _currentPerson;
|
||||
private Person _previousPerson;
|
||||
private AirplaneController _currentAirplane;
|
||||
private bool _lastShotHit;
|
||||
private int _successCount;
|
||||
private int _failCount;
|
||||
private int _totalTurns;
|
||||
private AirplaneAbilityType _selectedAirplaneType;
|
||||
|
||||
public AirplaneGameState CurrentState => _currentState;
|
||||
public Person CurrentPerson => _currentPerson;
|
||||
public int SuccessCount => _successCount;
|
||||
public int FailCount => _failCount;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Lifecycle
|
||||
|
||||
internal override void OnManagedAwake()
|
||||
{
|
||||
base.OnManagedAwake();
|
||||
|
||||
// Set singleton
|
||||
if (_instance != null && _instance != this)
|
||||
{
|
||||
Logging.Warning("[AirplaneGameManager] Multiple instances detected! Destroying duplicate.");
|
||||
Destroy(gameObject);
|
||||
return;
|
||||
}
|
||||
_instance = this;
|
||||
|
||||
// Validate references
|
||||
ValidateReferences();
|
||||
}
|
||||
|
||||
internal override void OnManagedStart()
|
||||
{
|
||||
base.OnManagedStart();
|
||||
|
||||
// Subscribe to events
|
||||
if (launchController != null)
|
||||
{
|
||||
launchController.OnAirplaneLaunched += HandleAirplaneLaunched;
|
||||
}
|
||||
|
||||
if (targetValidator != null)
|
||||
{
|
||||
targetValidator.OnCorrectTargetHit += HandleCorrectTargetHit;
|
||||
targetValidator.OnWrongTargetHit += HandleWrongTargetHit;
|
||||
targetValidator.OnMissedAllTargets += HandleMissedTargets;
|
||||
}
|
||||
|
||||
// Start the game
|
||||
StartGame();
|
||||
}
|
||||
|
||||
internal override void OnManagedDestroy()
|
||||
{
|
||||
base.OnManagedDestroy();
|
||||
|
||||
// Unsubscribe from events
|
||||
if (launchController != null)
|
||||
{
|
||||
launchController.OnAirplaneLaunched -= HandleAirplaneLaunched;
|
||||
}
|
||||
|
||||
if (targetValidator != null)
|
||||
{
|
||||
targetValidator.OnCorrectTargetHit -= HandleCorrectTargetHit;
|
||||
targetValidator.OnWrongTargetHit -= HandleWrongTargetHit;
|
||||
targetValidator.OnMissedAllTargets -= HandleMissedTargets;
|
||||
}
|
||||
|
||||
if (_instance == this)
|
||||
{
|
||||
_instance = null;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Validation
|
||||
|
||||
private void ValidateReferences()
|
||||
{
|
||||
if (personQueue == null)
|
||||
{
|
||||
Logging.Error("[AirplaneGameManager] PersonQueue not assigned!");
|
||||
}
|
||||
|
||||
if (cameraManager == null)
|
||||
{
|
||||
Logging.Error("[AirplaneGameManager] AirplaneCameraManager not assigned!");
|
||||
}
|
||||
|
||||
if (launchController == null)
|
||||
{
|
||||
Logging.Error("[AirplaneGameManager] AirplaneLaunchController not assigned!");
|
||||
}
|
||||
|
||||
if (targetValidator == null)
|
||||
{
|
||||
Logging.Error("[AirplaneGameManager] AirplaneTargetValidator not assigned!");
|
||||
}
|
||||
|
||||
if (spawnManager == null)
|
||||
{
|
||||
Logging.Error("[AirplaneGameManager] AirplaneSpawnManager not assigned!");
|
||||
}
|
||||
|
||||
// Validate airplane selection system
|
||||
if (selectionUI == null)
|
||||
{
|
||||
Logging.Warning("[AirplaneGameManager] ⚠️ SelectionUI not assigned! Player will not be able to choose airplane type.");
|
||||
Logging.Warning("[AirplaneGameManager] → Assign AirplaneSelectionUI GameObject in Inspector under 'Airplane Type Selection'");
|
||||
}
|
||||
|
||||
if (abilityButton == null)
|
||||
{
|
||||
Logging.Warning("[AirplaneGameManager] ⚠️ AbilityButton not assigned! Player will not be able to use abilities.");
|
||||
Logging.Warning("[AirplaneGameManager] → Assign AirplaneAbilityButton GameObject in Inspector under 'Airplane Type Selection'");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region State Management
|
||||
|
||||
private void ChangeState(AirplaneGameState newState)
|
||||
{
|
||||
AirplaneGameState oldState = _currentState;
|
||||
_currentState = newState;
|
||||
|
||||
if (showDebugLogs) Logging.Debug($"[AirplaneGameManager] State: {oldState} -> {newState}");
|
||||
|
||||
OnStateChanged?.Invoke(oldState, newState);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Game Flow
|
||||
|
||||
/// <summary>
|
||||
/// Start the game - begins with intro sequence
|
||||
/// </summary>
|
||||
public void StartGame()
|
||||
{
|
||||
if (showDebugLogs) Logging.Debug("[AirplaneGameManager] ===== GAME STARTING =====");
|
||||
|
||||
// Start with intro camera blend, THEN show selection UI
|
||||
StartCoroutine(IntroSequence());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Airplane selection sequence: show selection UI, wait for player choice
|
||||
/// Called AFTER intro camera blend
|
||||
/// </summary>
|
||||
private IEnumerator AirplaneSelectionSequence()
|
||||
{
|
||||
ChangeState(AirplaneGameState.AirplaneSelection);
|
||||
|
||||
if (showDebugLogs) Logging.Debug("[AirplaneGameManager] === AIRPLANE SELECTION STARTING ===");
|
||||
|
||||
// Show selection UI
|
||||
if (selectionUI != null)
|
||||
{
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[AirplaneGameManager] SelectionUI found! GameObject: {selectionUI.gameObject.name}, Active: {selectionUI.gameObject.activeSelf}");
|
||||
}
|
||||
|
||||
selectionUI.Show();
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[AirplaneGameManager] Called selectionUI.Show(). GameObject now active: {selectionUI.gameObject.activeSelf}");
|
||||
}
|
||||
|
||||
// Wait for player to select and confirm
|
||||
yield return new WaitUntil(() => selectionUI.HasSelectedType);
|
||||
|
||||
_selectedAirplaneType = selectionUI.GetSelectedType();
|
||||
selectionUI.Hide();
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[AirplaneGameManager] Selected airplane: {_selectedAirplaneType}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Logging.Warning("[AirplaneGameManager] ⚠️ selectionUI is NULL! Cannot show selection UI. Check Inspector.");
|
||||
Logging.Warning("[AirplaneGameManager] Using default airplane type from settings as fallback.");
|
||||
|
||||
// Fallback: use default type from settings
|
||||
var settings = GameManager.GetSettingsObject<AppleHills.Core.Settings.IAirplaneSettings>();
|
||||
if (settings != null)
|
||||
{
|
||||
_selectedAirplaneType = settings.DefaultAirplaneType;
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[AirplaneGameManager] No selection UI, using default: {_selectedAirplaneType}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_selectedAirplaneType = AirplaneAbilityType.Jet; // Ultimate fallback
|
||||
}
|
||||
}
|
||||
|
||||
// Continue with hellos after selection
|
||||
yield return StartCoroutine(IntroHellosSequence());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Intro sequence: blend to intro camera, THEN show airplane selection
|
||||
/// </summary>
|
||||
private IEnumerator IntroSequence()
|
||||
{
|
||||
ChangeState(AirplaneGameState.Intro);
|
||||
|
||||
if (showDebugLogs) Logging.Debug("[AirplaneGameManager] Playing intro sequence...");
|
||||
|
||||
// 1. Blend to intro camera
|
||||
if (cameraManager != null)
|
||||
{
|
||||
cameraManager.SwitchToState(AirplaneCameraState.Intro);
|
||||
yield return new WaitForSeconds(0.5f); // Camera blend time
|
||||
}
|
||||
|
||||
if (showDebugLogs) Logging.Debug("[AirplaneGameManager] Intro camera ready. Now showing airplane selection...");
|
||||
|
||||
// 2. Show airplane selection UI and wait for player choice
|
||||
yield return StartCoroutine(AirplaneSelectionSequence());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Hellos sequence: all people greet, then blend to aiming camera
|
||||
/// Called AFTER airplane selection is complete
|
||||
/// </summary>
|
||||
private IEnumerator IntroHellosSequence()
|
||||
{
|
||||
if (showDebugLogs) Logging.Debug("[AirplaneGameManager] Starting hellos sequence...");
|
||||
|
||||
// 1. Iterate over each person and allow them to say their hellos
|
||||
if (personQueue != null && personQueue.HasMorePeople())
|
||||
{
|
||||
if (showDebugLogs) Logging.Debug("[AirplaneGameManager] Introducing all people...");
|
||||
|
||||
// Get all people from queue without removing them
|
||||
var allPeople = personQueue.GetAllPeople();
|
||||
foreach (var person in allPeople)
|
||||
{
|
||||
if (person != null)
|
||||
{
|
||||
// Wait for each person's greeting to complete
|
||||
yield return StartCoroutine(person.OnHello());
|
||||
}
|
||||
}
|
||||
|
||||
if (showDebugLogs) Logging.Debug("[AirplaneGameManager] All introductions complete");
|
||||
}
|
||||
|
||||
// 2. Blend to aiming camera (first person's turn will start)
|
||||
if (cameraManager != null)
|
||||
{
|
||||
cameraManager.SwitchToState(AirplaneCameraState.Aiming);
|
||||
yield return new WaitForSeconds(0.5f); // Camera blend time
|
||||
}
|
||||
|
||||
if (showDebugLogs) Logging.Debug("[AirplaneGameManager] Intro complete");
|
||||
|
||||
// Move to first person's turn
|
||||
StartCoroutine(SetupNextPerson());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Setup the next person's turn
|
||||
/// </summary>
|
||||
private IEnumerator SetupNextPerson()
|
||||
{
|
||||
// Check if there are more people
|
||||
if (personQueue == null || !personQueue.HasMorePeople())
|
||||
{
|
||||
if (showDebugLogs) Logging.Debug("[AirplaneGameManager] No more people, ending game");
|
||||
StartCoroutine(GameOver());
|
||||
yield break;
|
||||
}
|
||||
|
||||
ChangeState(AirplaneGameState.NextPerson);
|
||||
|
||||
// If this is NOT the first turn, handle post-shot reaction
|
||||
if (_currentPerson != null)
|
||||
{
|
||||
// Switch to next person camera for reaction/transition
|
||||
if (cameraManager != null)
|
||||
{
|
||||
cameraManager.SwitchToState(AirplaneCameraState.NextPerson);
|
||||
|
||||
// Wait for camera blend to complete before cleanup and reaction
|
||||
yield return new WaitForSeconds(0.5f); // Camera blend time
|
||||
}
|
||||
|
||||
// NOW cleanup spawned objects after camera has blended (camera shows scene before cleanup)
|
||||
if (spawnManager != null)
|
||||
{
|
||||
if (_lastShotHit)
|
||||
{
|
||||
// Success: Full cleanup - destroy all spawned objects and target
|
||||
spawnManager.CleanupSpawnedObjects();
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug("[AirplaneGameManager] Cleaned up spawned objects after successful shot");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Failure: Keep spawned objects for retry, just reset tracking state
|
||||
spawnManager.ResetForRetry();
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug("[AirplaneGameManager] Kept spawned objects for retry after miss");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Handle the previous person's reaction (celebrate/disappointment), removal (if hit), and shuffle
|
||||
yield return StartCoroutine(personQueue.HandlePostShotReaction(_lastShotHit));
|
||||
}
|
||||
|
||||
// Get the next person (now at front of queue after potential removal)
|
||||
_previousPerson = _currentPerson;
|
||||
_currentPerson = personQueue.PopNextPerson();
|
||||
_totalTurns++;
|
||||
|
||||
if (_currentPerson == null)
|
||||
{
|
||||
Logging.Error("[AirplaneGameManager] Failed to get next person!");
|
||||
yield break;
|
||||
}
|
||||
|
||||
// Check if this is a NEW person (different from previous) or a retry (same person)
|
||||
bool isNewPerson = _previousPerson != _currentPerson;
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
string turnType = isNewPerson ? "NEW PERSON" : "RETRY";
|
||||
Logging.Debug($"[AirplaneGameManager] === Turn {_totalTurns}: {_currentPerson.PersonName} ({turnType}) ===" +
|
||||
$"\n Target: {_currentPerson.TargetName}");
|
||||
}
|
||||
|
||||
OnPersonStartTurn?.Invoke(_currentPerson);
|
||||
|
||||
// Only introduce if this is a NEW person
|
||||
if (isNewPerson && _previousPerson != null)
|
||||
{
|
||||
// Switching to a new person (after success) - they say hello
|
||||
yield return StartCoroutine(personQueue.IntroduceNextPerson());
|
||||
}
|
||||
else if (_previousPerson == null)
|
||||
{
|
||||
// First turn - they already said hello during intro, just brief camera pause
|
||||
if (cameraManager != null)
|
||||
{
|
||||
cameraManager.SwitchToState(AirplaneCameraState.NextPerson);
|
||||
yield return new WaitForSeconds(0.5f);
|
||||
}
|
||||
}
|
||||
// else: Same person retry (after failure) - skip introduction, go straight to aiming
|
||||
|
||||
// Initialize spawn manager for this person's target
|
||||
if (spawnManager != null)
|
||||
{
|
||||
// Pass retry flag: true if same person, false if new person
|
||||
bool isRetry = !isNewPerson;
|
||||
spawnManager.InitializeForGame(_currentPerson.TargetName, isRetry);
|
||||
}
|
||||
|
||||
// Queue done - continue game flow
|
||||
if (targetValidator != null)
|
||||
{
|
||||
targetValidator.SetExpectedTarget(_currentPerson.TargetName);
|
||||
}
|
||||
|
||||
// Enter aiming state
|
||||
EnterAimingState();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enter aiming state - player can aim and launch
|
||||
/// </summary>
|
||||
private void EnterAimingState()
|
||||
{
|
||||
ChangeState(AirplaneGameState.Aiming);
|
||||
|
||||
if (showDebugLogs) Logging.Debug("[AirplaneGameManager] Ready to aim and launch!");
|
||||
|
||||
// Switch to aiming camera
|
||||
if (cameraManager != null)
|
||||
{
|
||||
cameraManager.SwitchToState(AirplaneCameraState.Aiming);
|
||||
}
|
||||
|
||||
// Spawn airplane at slingshot with selected type
|
||||
if (launchController != null && _selectedAirplaneType != AirplaneAbilityType.None)
|
||||
{
|
||||
launchController.SetAirplaneType(_selectedAirplaneType);
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[AirplaneGameManager] Spawned airplane at slingshot: {_selectedAirplaneType}");
|
||||
}
|
||||
}
|
||||
|
||||
// Show target UI
|
||||
if (spawnManager != null)
|
||||
{
|
||||
spawnManager.ShowTargetUI();
|
||||
}
|
||||
|
||||
// Enable launch controller
|
||||
if (launchController != null)
|
||||
{
|
||||
launchController.Enable();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Event Handlers
|
||||
|
||||
/// <summary>
|
||||
/// Handle airplane launched event
|
||||
/// </summary>
|
||||
private void HandleAirplaneLaunched(AirplaneController airplane)
|
||||
{
|
||||
if (showDebugLogs) Logging.Debug("[AirplaneGameManager] Airplane launched!");
|
||||
|
||||
_currentAirplane = airplane;
|
||||
|
||||
// Disable launch controller
|
||||
if (launchController != null)
|
||||
{
|
||||
launchController.Disable();
|
||||
}
|
||||
|
||||
ChangeState(AirplaneGameState.Flying);
|
||||
|
||||
// Show ability button if airplane has an ability
|
||||
if (abilityButton != null && airplane.CurrentAbility != null)
|
||||
{
|
||||
abilityButton.Setup(airplane, airplane.CurrentAbility);
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[AirplaneGameManager] Ability button shown: {airplane.CurrentAbility.AbilityName}");
|
||||
}
|
||||
}
|
||||
|
||||
// Start following airplane with camera
|
||||
if (cameraManager != null)
|
||||
{
|
||||
cameraManager.StartFollowingAirplane(airplane.transform);
|
||||
}
|
||||
|
||||
// Start spawn manager tracking
|
||||
if (spawnManager != null)
|
||||
{
|
||||
spawnManager.StartTracking(airplane.transform);
|
||||
}
|
||||
|
||||
// Subscribe to airplane events
|
||||
airplane.OnTargetHit += HandleAirplaneHitTarget;
|
||||
airplane.OnLanded += HandleAirplaneLanded;
|
||||
airplane.OnTimeout += HandleAirplaneTimeout;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handle airplane hitting a target
|
||||
/// </summary>
|
||||
private void HandleAirplaneHitTarget(AirplaneController airplane, string targetName)
|
||||
{
|
||||
if (showDebugLogs) Logging.Debug($"[AirplaneGameManager] Airplane hit target: {targetName}");
|
||||
|
||||
// Validate the hit
|
||||
if (targetValidator != null)
|
||||
{
|
||||
targetValidator.ValidateHit(targetName);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handle airplane landing
|
||||
/// </summary>
|
||||
private void HandleAirplaneLanded(AirplaneController airplane)
|
||||
{
|
||||
if (showDebugLogs) Logging.Debug("[AirplaneGameManager] Airplane landed");
|
||||
|
||||
// If no target was hit, count as miss
|
||||
if (targetValidator != null && !targetValidator.HasValidated)
|
||||
{
|
||||
targetValidator.HandleMiss();
|
||||
}
|
||||
|
||||
// Evaluate result
|
||||
StartCoroutine(EvaluateResult());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handle airplane timeout
|
||||
/// </summary>
|
||||
private void HandleAirplaneTimeout(AirplaneController airplane)
|
||||
{
|
||||
if (showDebugLogs) Logging.Debug("[AirplaneGameManager] Airplane timed out");
|
||||
|
||||
// Count as miss
|
||||
if (targetValidator != null && !targetValidator.HasValidated)
|
||||
{
|
||||
targetValidator.HandleMiss();
|
||||
}
|
||||
|
||||
// Evaluate result
|
||||
StartCoroutine(EvaluateResult());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handle correct target hit
|
||||
/// </summary>
|
||||
private void HandleCorrectTargetHit(string targetName)
|
||||
{
|
||||
_lastShotHit = true;
|
||||
_successCount++;
|
||||
|
||||
// Hide target UI immediately on successful hit
|
||||
if (spawnManager != null)
|
||||
{
|
||||
spawnManager.HideTargetUI();
|
||||
}
|
||||
|
||||
if (showDebugLogs) Logging.Debug($"[AirplaneGameManager] ✓ SUCCESS! Hit correct target: {targetName}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handle wrong target hit
|
||||
/// </summary>
|
||||
private void HandleWrongTargetHit(string expectedTarget, string actualTarget)
|
||||
{
|
||||
_lastShotHit = false;
|
||||
_failCount++;
|
||||
if (showDebugLogs) Logging.Debug($"[AirplaneGameManager] ✗ FAIL! Expected: {expectedTarget}, Hit: {actualTarget}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handle missed all targets
|
||||
/// </summary>
|
||||
private void HandleMissedTargets()
|
||||
{
|
||||
_lastShotHit = false;
|
||||
_failCount++;
|
||||
if (showDebugLogs) Logging.Debug("[AirplaneGameManager] ✗ MISS! Didn't hit any target");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Evaluation and Cleanup
|
||||
|
||||
/// <summary>
|
||||
/// Evaluate the result of the turn
|
||||
/// </summary>
|
||||
private IEnumerator EvaluateResult()
|
||||
{
|
||||
ChangeState(AirplaneGameState.Evaluating);
|
||||
|
||||
// Hide ability button
|
||||
if (abilityButton != null)
|
||||
{
|
||||
abilityButton.Hide();
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug("[AirplaneGameManager] Ability button hidden");
|
||||
}
|
||||
}
|
||||
|
||||
// Stop following airplane
|
||||
if (cameraManager != null)
|
||||
{
|
||||
cameraManager.StopFollowingAirplane();
|
||||
}
|
||||
|
||||
// Stop spawn manager tracking
|
||||
if (spawnManager != null)
|
||||
{
|
||||
spawnManager.StopTracking();
|
||||
}
|
||||
|
||||
// Determine success/failure
|
||||
bool success = targetValidator != null &&
|
||||
targetValidator.HasValidated &&
|
||||
_currentAirplane != null &&
|
||||
!string.IsNullOrEmpty(_currentAirplane.LastHitTarget) &&
|
||||
targetValidator.IsExpectedTarget(_currentAirplane.LastHitTarget);
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[AirplaneGameManager] Turn result: {(success ? "SUCCESS" : "FAILURE")}" +
|
||||
$"\n Score: {_successCount} / {_totalTurns}");
|
||||
}
|
||||
|
||||
OnPersonFinishTurn?.Invoke(_currentPerson, success);
|
||||
|
||||
// Store success state for later use
|
||||
_lastShotHit = success;
|
||||
|
||||
// Wait for evaluation display (stub)
|
||||
yield return new WaitForSeconds(1f);
|
||||
|
||||
// Clean up airplane
|
||||
if (_currentAirplane != null)
|
||||
{
|
||||
Destroy(_currentAirplane.gameObject);
|
||||
_currentAirplane = null;
|
||||
}
|
||||
|
||||
// Clear launch controller reference
|
||||
if (launchController != null)
|
||||
{
|
||||
launchController.ClearActiveAirplane();
|
||||
}
|
||||
|
||||
// NOTE: Spawned objects cleanup moved to SetupNextPerson() to happen AFTER camera blend
|
||||
// This ensures camera shows the scene before cleanup and person reaction
|
||||
|
||||
// Move to next person
|
||||
StartCoroutine(SetupNextPerson());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Game over - no more people
|
||||
/// </summary>
|
||||
private IEnumerator GameOver()
|
||||
{
|
||||
ChangeState(AirplaneGameState.GameOver);
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[AirplaneGameManager] ===== GAME OVER =====" +
|
||||
$"\n Total Turns: {_totalTurns}" +
|
||||
$"\n Success: {_successCount}" +
|
||||
$"\n Failures: {_failCount}" +
|
||||
$"\n Success Rate: {(_totalTurns > 0 ? (_successCount * 100f / _totalTurns) : 0):F1}%");
|
||||
}
|
||||
|
||||
OnGameComplete?.Invoke();
|
||||
|
||||
// Stub: Show game over UI
|
||||
yield return new WaitForSeconds(2f);
|
||||
|
||||
if (showDebugLogs) Logging.Debug("[AirplaneGameManager] Game complete");
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Query Methods
|
||||
|
||||
/// <summary>
|
||||
/// Get current game statistics
|
||||
/// </summary>
|
||||
public (int total, int success, int fail) GetStatistics()
|
||||
{
|
||||
return (_totalTurns, _successCount, _failCount);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if game is active
|
||||
/// </summary>
|
||||
public bool IsGameActive()
|
||||
{
|
||||
return _currentState != AirplaneGameState.Intro && _currentState != AirplaneGameState.GameOver;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fd2c6d27dee546479b16d0dfd8c3b2ee
|
||||
timeCreated: 1764851399
|
||||
@@ -0,0 +1,244 @@
|
||||
using System;
|
||||
using AppleHills.Core.Settings;
|
||||
using Common.Input;
|
||||
using Core;
|
||||
using Minigames.Airplane.Data;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Minigames.Airplane.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Launch controller for the airplane minigame.
|
||||
/// Extends DragLaunchController with airplane-specific behavior.
|
||||
/// Spawns and launches airplanes on release.
|
||||
/// </summary>
|
||||
public class AirplaneLaunchController : DragLaunchController
|
||||
{
|
||||
#region Events
|
||||
|
||||
/// <summary>
|
||||
/// Fired when airplane is launched. Parameters: (AirplaneController airplane)
|
||||
/// </summary>
|
||||
public event Action<AirplaneController> OnAirplaneLaunched;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Settings
|
||||
|
||||
protected override SlingshotConfig GetSlingshotConfig()
|
||||
{
|
||||
return GameManager.GetSettingsObject<IAirplaneSettings>()?.SlingshotSettings;
|
||||
}
|
||||
|
||||
protected override GameObject GetProjectilePrefab()
|
||||
{
|
||||
return airplanePrefab;
|
||||
}
|
||||
|
||||
protected override float GetProjectileMass()
|
||||
{
|
||||
var settings = GameManager.GetSettingsObject<IAirplaneSettings>();
|
||||
if (settings == null)
|
||||
{
|
||||
if (showDebugLogs)
|
||||
Logging.Warning("[AirplaneLaunchController] AirplaneSettings not found!");
|
||||
return 1f; // Default fallback
|
||||
}
|
||||
|
||||
// Read from AirplaneSettings - same mass that AirplaneController uses
|
||||
return settings.AirplaneMass;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Inspector Properties
|
||||
|
||||
[Header("Airplane Setup")]
|
||||
[Tooltip("Airplane prefab to spawn")]
|
||||
[SerializeField] private GameObject airplanePrefab;
|
||||
|
||||
// Note: Trajectory preview is handled by base DragLaunchController class
|
||||
// It will auto-find TrajectoryPreview component on this GameObject
|
||||
|
||||
#endregion
|
||||
|
||||
#region State
|
||||
|
||||
private AirplaneController _activeAirplane;
|
||||
private AirplaneAbilityType _selectedAirplaneType;
|
||||
|
||||
public AirplaneController ActiveAirplane => _activeAirplane;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Lifecycle
|
||||
|
||||
internal override void OnManagedAwake()
|
||||
{
|
||||
base.OnManagedAwake();
|
||||
|
||||
// Validate airplane prefab
|
||||
if (airplanePrefab == null)
|
||||
{
|
||||
Logging.Error("[AirplaneLaunchController] Airplane prefab not assigned!");
|
||||
}
|
||||
else
|
||||
{
|
||||
// Verify airplane has AirplaneController
|
||||
if (airplanePrefab.GetComponent<AirplaneController>() == null)
|
||||
{
|
||||
Logging.Error("[AirplaneLaunchController] Airplane prefab missing AirplaneController component!");
|
||||
}
|
||||
}
|
||||
|
||||
// Base class handles trajectory preview setup
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Override Methods
|
||||
|
||||
public override void Enable()
|
||||
{
|
||||
// Clear any trajectory from previous turn
|
||||
if (trajectoryPreview != null)
|
||||
{
|
||||
trajectoryPreview.ForceHide();
|
||||
}
|
||||
|
||||
base.Enable();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Visual Feedback
|
||||
|
||||
// Base class handles trajectory preview via TrajectoryPreview component
|
||||
// No custom visual feedback needed for airplane - using default implementation
|
||||
|
||||
#endregion
|
||||
|
||||
#region Airplane Type System
|
||||
|
||||
/// <summary>
|
||||
/// Set the airplane type and spawn it at slingshot (before aiming).
|
||||
/// </summary>
|
||||
public void SetAirplaneType(Data.AirplaneAbilityType abilityType)
|
||||
{
|
||||
_selectedAirplaneType = abilityType;
|
||||
SpawnAirplaneAtSlingshot();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Spawn airplane at slingshot anchor (pre-launch).
|
||||
/// </summary>
|
||||
private void SpawnAirplaneAtSlingshot()
|
||||
{
|
||||
// Clear existing
|
||||
if (_activeAirplane != null)
|
||||
{
|
||||
Destroy(_activeAirplane.gameObject);
|
||||
_activeAirplane = null;
|
||||
}
|
||||
|
||||
// Get settings and airplane config
|
||||
var settings = GameManager.GetSettingsObject<AppleHills.Core.Settings.IAirplaneSettings>();
|
||||
if (settings == null)
|
||||
{
|
||||
Logging.Error("[AirplaneLaunchController] Cannot spawn - settings not found!");
|
||||
return;
|
||||
}
|
||||
|
||||
var config = settings.GetAirplaneConfig(_selectedAirplaneType);
|
||||
GameObject prefab = config.prefab ?? airplanePrefab;
|
||||
|
||||
if (prefab == null)
|
||||
{
|
||||
Logging.Error("[AirplaneLaunchController] No airplane prefab available!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Instantiate at launch anchor
|
||||
GameObject airplaneObj = Instantiate(prefab, launchAnchor.position, Quaternion.identity);
|
||||
_activeAirplane = airplaneObj.GetComponent<AirplaneController>();
|
||||
|
||||
if (_activeAirplane == null)
|
||||
{
|
||||
Logging.Error("[AirplaneLaunchController] Spawned airplane missing AirplaneController!");
|
||||
Destroy(airplaneObj);
|
||||
return;
|
||||
}
|
||||
|
||||
// Initialize with ability type
|
||||
_activeAirplane.Initialize(_selectedAirplaneType);
|
||||
|
||||
// Set kinematic until launch
|
||||
var rb = _activeAirplane.GetComponent<Rigidbody2D>();
|
||||
if (rb != null)
|
||||
{
|
||||
rb.bodyType = RigidbodyType2D.Kinematic;
|
||||
}
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[AirplaneLaunchController] Spawned airplane at slingshot: {config.displayName}");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Launch
|
||||
|
||||
protected override void PerformLaunch(Vector2 direction, float force)
|
||||
{
|
||||
if (_activeAirplane == null)
|
||||
{
|
||||
Logging.Error("[AirplaneLaunchController] No airplane to launch! Call SetAirplaneType first.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Set dynamic before launch
|
||||
var rb = _activeAirplane.GetComponent<Rigidbody2D>();
|
||||
if (rb != null)
|
||||
{
|
||||
rb.bodyType = RigidbodyType2D.Dynamic;
|
||||
}
|
||||
|
||||
// Launch the airplane
|
||||
_activeAirplane.Launch(direction, force);
|
||||
|
||||
// Trajectory preview is automatically hidden by base class
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[AirplaneLaunchController] Launched airplane with force {force:F2}, direction {direction}");
|
||||
}
|
||||
|
||||
// Fire event
|
||||
OnAirplaneLaunched?.Invoke(_activeAirplane);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Methods
|
||||
|
||||
/// <summary>
|
||||
/// Get reference to the currently active airplane (if any)
|
||||
/// </summary>
|
||||
public AirplaneController GetActiveAirplane()
|
||||
{
|
||||
return _activeAirplane;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clear reference to active airplane (called after airplane is destroyed)
|
||||
/// </summary>
|
||||
public void ClearActiveAirplane()
|
||||
{
|
||||
_activeAirplane = null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a819923cb68240d494bdcf6d5ecf6b9b
|
||||
timeCreated: 1764851349
|
||||
843
Assets/Scripts/Minigames/Airplane/Core/AirplaneSpawnManager.cs
Normal file
843
Assets/Scripts/Minigames/Airplane/Core/AirplaneSpawnManager.cs
Normal file
@@ -0,0 +1,843 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using AppleHills.Core.Settings;
|
||||
using Core;
|
||||
using Core.Lifecycle;
|
||||
using Minigames.Airplane.UI;
|
||||
using UnityEngine;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
namespace Minigames.Airplane.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Manages dynamic spawning of targets, positive/negative objects, and ground tiles
|
||||
/// as the airplane moves through the level.
|
||||
/// </summary>
|
||||
public class AirplaneSpawnManager : ManagedBehaviour
|
||||
{
|
||||
#region Serialized Data Classes
|
||||
|
||||
[Serializable]
|
||||
public class TargetPrefabEntry
|
||||
{
|
||||
[Tooltip("Unique key to identify this target")]
|
||||
public string targetKey;
|
||||
|
||||
[Tooltip("Prefab to spawn for this target")]
|
||||
public GameObject prefab;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Inspector References
|
||||
|
||||
[Header("Prefab References")]
|
||||
[Tooltip("Dictionary of target prefabs (key = target name)")]
|
||||
[SerializeField] private TargetPrefabEntry[] targetPrefabs;
|
||||
|
||||
[Tooltip("Array of positive object prefabs")]
|
||||
[SerializeField] private GameObject[] positiveObjectPrefabs;
|
||||
|
||||
[Tooltip("Array of negative object prefabs")]
|
||||
[SerializeField] private GameObject[] negativeObjectPrefabs;
|
||||
|
||||
[Tooltip("Array of ground tile prefabs")]
|
||||
[SerializeField] private GameObject[] groundTilePrefabs;
|
||||
|
||||
[Header("UI")]
|
||||
[Tooltip("Target display UI component")]
|
||||
[SerializeField] private TargetDisplayUI targetDisplayUI;
|
||||
|
||||
[Header("Launch Reference")]
|
||||
[Tooltip("Launch controller (provides launch anchor position for distance calculation)")]
|
||||
[SerializeField] private AirplaneLaunchController launchController;
|
||||
|
||||
[Header("Spawn Parents")]
|
||||
[Tooltip("Parent transform for spawned objects (optional, for organization)")]
|
||||
[SerializeField] private Transform spawnedObjectsParent;
|
||||
|
||||
[Tooltip("Parent transform for ground tiles (optional)")]
|
||||
[SerializeField] private Transform groundTilesParent;
|
||||
|
||||
[Header("Ground Settings")]
|
||||
[Tooltip("Y position at which to spawn ground tiles")]
|
||||
[SerializeField] private float groundSpawnY = -18f;
|
||||
|
||||
[Header("Debug")]
|
||||
[SerializeField] private bool showDebugLogs;
|
||||
|
||||
#endregion
|
||||
|
||||
#region State
|
||||
|
||||
// Target info
|
||||
private string _currentTargetKey;
|
||||
private float _targetDistance;
|
||||
private Vector3 _targetSpawnPosition;
|
||||
private Sprite _targetIconSprite;
|
||||
private GameObject _spawnedTarget;
|
||||
private GameObject _targetPrefabToSpawn;
|
||||
private bool _hasSpawnedTarget;
|
||||
|
||||
// Plane tracking
|
||||
private Transform _planeTransform;
|
||||
private bool _isSpawningActive;
|
||||
private bool _hasPassedThreshold;
|
||||
|
||||
// Spawning timers
|
||||
private float _nextObjectSpawnTime;
|
||||
private float _nextGroundSpawnX;
|
||||
|
||||
// Spawn statistics (for weighted ratio adjustment)
|
||||
private int _positiveSpawnCount;
|
||||
private int _negativeSpawnCount;
|
||||
|
||||
// Adaptive spawn distance (persistent across retries)
|
||||
private float _furthestReachedX;
|
||||
private bool _isRetryAttempt;
|
||||
|
||||
// Cached dictionaries
|
||||
private Dictionary<string, GameObject> _targetPrefabDict;
|
||||
private IAirplaneSettings _settings;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Lifecycle
|
||||
|
||||
internal override void OnManagedAwake()
|
||||
{
|
||||
base.OnManagedAwake();
|
||||
|
||||
// Build target dictionary
|
||||
BuildTargetDictionary();
|
||||
|
||||
// Get settings
|
||||
_settings = GameManager.GetSettingsObject<IAirplaneSettings>();
|
||||
|
||||
// Validate references
|
||||
ValidateReferences();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
|
||||
if (!_isSpawningActive || _planeTransform == null) return;
|
||||
|
||||
float planeX = _planeTransform.position.x;
|
||||
|
||||
// Track furthest X position reached
|
||||
if (planeX > _furthestReachedX)
|
||||
{
|
||||
_furthestReachedX = planeX;
|
||||
}
|
||||
|
||||
// Check if target should be spawned (when plane gets within spawn distance)
|
||||
if (!_hasSpawnedTarget && _targetPrefabToSpawn != null)
|
||||
{
|
||||
float distanceToTarget = _targetSpawnPosition.x - planeX;
|
||||
if (distanceToTarget <= _settings.SpawnDistanceAhead)
|
||||
{
|
||||
SpawnTarget();
|
||||
_hasSpawnedTarget = true;
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[SpawnManager] Target spawned at distance {distanceToTarget:F2} from plane");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check if plane has crossed threshold
|
||||
if (!_hasPassedThreshold && planeX >= _settings.DynamicSpawnThreshold)
|
||||
{
|
||||
_hasPassedThreshold = true;
|
||||
InitializeDynamicSpawning();
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[SpawnManager] Plane crossed threshold at X={planeX:F2}");
|
||||
}
|
||||
}
|
||||
|
||||
// If past threshold, handle spawning (only if we're going further than before)
|
||||
if (_hasPassedThreshold)
|
||||
{
|
||||
// Only spawn new content if plane is beyond previous furthest point (for retries)
|
||||
bool shouldSpawnNewContent = !_isRetryAttempt || planeX > (_furthestReachedX - _settings.SpawnDistanceAhead);
|
||||
|
||||
if (shouldSpawnNewContent)
|
||||
{
|
||||
// Spawn objects at intervals
|
||||
if (Time.time >= _nextObjectSpawnTime)
|
||||
{
|
||||
SpawnRandomObject();
|
||||
ScheduleNextObjectSpawn();
|
||||
}
|
||||
|
||||
// Spawn ground tiles ahead of plane
|
||||
float groundSpawnTargetX = planeX + GetGroundSpawnAheadDistance();
|
||||
while (_nextGroundSpawnX < groundSpawnTargetX)
|
||||
{
|
||||
SpawnGroundTile();
|
||||
_nextGroundSpawnX += _settings.GroundSpawnInterval;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public API
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the spawn system for a new game.
|
||||
/// Determines target spawn position and sets up UI, but doesn't spawn target yet.
|
||||
/// Target will spawn when plane gets within spawn distance.
|
||||
/// </summary>
|
||||
/// <param name="targetKey">Key of the target to spawn</param>
|
||||
/// <param name="isRetry">True if this is a retry attempt (keeps existing spawned objects and target position)</param>
|
||||
public void InitializeForGame(string targetKey, bool isRetry = false)
|
||||
{
|
||||
_currentTargetKey = targetKey;
|
||||
_isSpawningActive = false;
|
||||
_hasPassedThreshold = false;
|
||||
_isRetryAttempt = isRetry;
|
||||
|
||||
// Only reset target and spawn state if NOT a retry
|
||||
if (!isRetry)
|
||||
{
|
||||
_hasSpawnedTarget = false;
|
||||
_positiveSpawnCount = 0;
|
||||
_negativeSpawnCount = 0;
|
||||
_furthestReachedX = 0f;
|
||||
|
||||
// Determine NEW target spawn distance
|
||||
_targetDistance = Random.Range((float)_settings.TargetMinDistance, (float)_settings.TargetMaxDistance);
|
||||
_targetSpawnPosition = new Vector3(_targetDistance, 0f, 0f);
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[SpawnManager] Initialized NEW turn for target '{targetKey}' at distance {_targetDistance:F2}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Retry: Keep existing target position and spawned objects
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[SpawnManager] Initialized RETRY for target '{targetKey}' at distance {_targetDistance:F2}, furthest reached: {_furthestReachedX:F2}");
|
||||
}
|
||||
}
|
||||
|
||||
// Find target prefab and extract icon WITHOUT spawning
|
||||
if (!_targetPrefabDict.TryGetValue(_currentTargetKey, out _targetPrefabToSpawn))
|
||||
{
|
||||
Logging.Error($"[SpawnManager] Target prefab not found for key '{_currentTargetKey}'!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Extract icon from prefab (doesn't need to be instantiated)
|
||||
ExtractTargetIconFromPrefab(_targetPrefabToSpawn);
|
||||
|
||||
// Setup target display UI (but don't show yet - will show when entering aiming state)
|
||||
SetupTargetUI();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Start tracking the airplane and enable spawning.
|
||||
/// </summary>
|
||||
/// <param name="planeTransform">Transform of the airplane</param>
|
||||
public void StartTracking(Transform planeTransform)
|
||||
{
|
||||
_planeTransform = planeTransform;
|
||||
_isSpawningActive = true;
|
||||
|
||||
// Initialize ground spawning position ahead of plane
|
||||
_nextGroundSpawnX = _planeTransform.position.x + GetGroundSpawnAheadDistance();
|
||||
|
||||
// Start UI tracking with calculated target position
|
||||
if (targetDisplayUI != null)
|
||||
{
|
||||
targetDisplayUI.StartTracking(planeTransform);
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug("[SpawnManager] UI tracking started, distance updates will show");
|
||||
}
|
||||
}
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug("[SpawnManager] Started tracking airplane");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stop spawning and tracking.
|
||||
/// </summary>
|
||||
public void StopTracking()
|
||||
{
|
||||
_isSpawningActive = false;
|
||||
_planeTransform = null;
|
||||
|
||||
// Stop UI tracking
|
||||
if (targetDisplayUI != null)
|
||||
{
|
||||
targetDisplayUI.StopTracking();
|
||||
}
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug("[SpawnManager] Stopped tracking");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Show the target display UI.
|
||||
/// Call when entering aiming state.
|
||||
/// </summary>
|
||||
public void ShowTargetUI()
|
||||
{
|
||||
if (targetDisplayUI != null)
|
||||
{
|
||||
// Update distance and show UI (refreshes distance from launch point if plane not launched yet)
|
||||
targetDisplayUI.UpdateAndShow();
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug("[SpawnManager] Target UI shown with updated distance");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Hide the target display UI.
|
||||
/// Call when target is successfully hit.
|
||||
/// </summary>
|
||||
public void HideTargetUI()
|
||||
{
|
||||
if (targetDisplayUI != null)
|
||||
{
|
||||
targetDisplayUI.Hide();
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug("[SpawnManager] Target UI hidden");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clean up all spawned objects (call on successful shot or game restart).
|
||||
/// Destroys all spawned content including target, objects, and ground tiles.
|
||||
/// </summary>
|
||||
public void CleanupSpawnedObjects()
|
||||
{
|
||||
if (spawnedObjectsParent != null)
|
||||
{
|
||||
foreach (Transform child in spawnedObjectsParent)
|
||||
{
|
||||
Destroy(child.gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
if (groundTilesParent != null)
|
||||
{
|
||||
foreach (Transform child in groundTilesParent)
|
||||
{
|
||||
Destroy(child.gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
if (_spawnedTarget != null)
|
||||
{
|
||||
Destroy(_spawnedTarget);
|
||||
_spawnedTarget = null;
|
||||
}
|
||||
|
||||
// Reset all spawn state
|
||||
_hasSpawnedTarget = false;
|
||||
_hasPassedThreshold = false;
|
||||
_furthestReachedX = 0f;
|
||||
_positiveSpawnCount = 0;
|
||||
_negativeSpawnCount = 0;
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug("[SpawnManager] Full cleanup completed (success or game restart)");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reset tracking state for retry attempt (keeps spawned objects).
|
||||
/// Call this when player fails and will retry the same shot.
|
||||
/// </summary>
|
||||
public void ResetForRetry()
|
||||
{
|
||||
// Don't destroy anything - keep all spawned objects and target
|
||||
// Just reset the tracking state so spawning can continue if plane goes further
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[SpawnManager] Reset for retry (keeping spawned objects, furthest reached: {_furthestReachedX:F2})");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get target information for external use.
|
||||
/// </summary>
|
||||
public (Vector3 position, float distance, Sprite icon) GetTargetInfo()
|
||||
{
|
||||
return (_targetSpawnPosition, _targetDistance, _targetIconSprite);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Initialization
|
||||
|
||||
/// <summary>
|
||||
/// Build dictionary from serialized target prefab array.
|
||||
/// </summary>
|
||||
private void BuildTargetDictionary()
|
||||
{
|
||||
_targetPrefabDict = new Dictionary<string, GameObject>();
|
||||
|
||||
if (targetPrefabs == null || targetPrefabs.Length == 0)
|
||||
{
|
||||
Logging.Warning("[SpawnManager] No target prefabs assigned!");
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var entry in targetPrefabs)
|
||||
{
|
||||
if (string.IsNullOrEmpty(entry.targetKey))
|
||||
{
|
||||
Logging.Warning("[SpawnManager] Target entry has empty key!");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (entry.prefab == null)
|
||||
{
|
||||
Logging.Warning($"[SpawnManager] Target entry '{entry.targetKey}' has no prefab assigned!");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (_targetPrefabDict.ContainsKey(entry.targetKey))
|
||||
{
|
||||
Logging.Warning($"[SpawnManager] Duplicate target key '{entry.targetKey}'!");
|
||||
continue;
|
||||
}
|
||||
|
||||
_targetPrefabDict[entry.targetKey] = entry.prefab;
|
||||
}
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[SpawnManager] Built target dictionary with {_targetPrefabDict.Count} entries");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validate all required references.
|
||||
/// </summary>
|
||||
private void ValidateReferences()
|
||||
{
|
||||
if (_settings == null)
|
||||
{
|
||||
Logging.Error("[SpawnManager] Could not load IAirplaneSettings!");
|
||||
}
|
||||
|
||||
if (positiveObjectPrefabs == null || positiveObjectPrefabs.Length == 0)
|
||||
{
|
||||
Logging.Warning("[SpawnManager] No positive object prefabs assigned!");
|
||||
}
|
||||
|
||||
if (negativeObjectPrefabs == null || negativeObjectPrefabs.Length == 0)
|
||||
{
|
||||
Logging.Warning("[SpawnManager] No negative object prefabs assigned!");
|
||||
}
|
||||
|
||||
if (groundTilePrefabs == null || groundTilePrefabs.Length == 0)
|
||||
{
|
||||
Logging.Warning("[SpawnManager] No ground tile prefabs assigned!");
|
||||
}
|
||||
|
||||
if (targetDisplayUI == null)
|
||||
{
|
||||
Logging.Warning("[SpawnManager] Target display UI not assigned!");
|
||||
}
|
||||
|
||||
if (launchController == null)
|
||||
{
|
||||
Logging.Warning("[SpawnManager] Launch controller not assigned! Distance calculation will use world origin.");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Target Spawning
|
||||
|
||||
/// <summary>
|
||||
/// Spawn the target at the predetermined position.
|
||||
/// </summary>
|
||||
private void SpawnTarget()
|
||||
{
|
||||
if (!_targetPrefabDict.TryGetValue(_currentTargetKey, out GameObject targetPrefab))
|
||||
{
|
||||
Logging.Error($"[SpawnManager] Target prefab not found for key '{_currentTargetKey}'!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Spawn target at initial position
|
||||
_spawnedTarget = Instantiate(targetPrefab, _targetSpawnPosition, Quaternion.identity);
|
||||
|
||||
if (spawnedObjectsParent != null)
|
||||
{
|
||||
_spawnedTarget.transform.SetParent(spawnedObjectsParent);
|
||||
}
|
||||
|
||||
// Snap target to ground
|
||||
SnapObjectToGround(_spawnedTarget, _targetSpawnPosition.x);
|
||||
|
||||
// Update target spawn position to actual snapped position
|
||||
_targetSpawnPosition = _spawnedTarget.transform.position;
|
||||
|
||||
// Extract sprite for UI icon
|
||||
ExtractTargetIcon(_spawnedTarget);
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[SpawnManager] Spawned target '{_currentTargetKey}' at {_targetSpawnPosition}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extract sprite from target prefab for UI display (without instantiation).
|
||||
/// Finds first SpriteRenderer in prefab or children.
|
||||
/// </summary>
|
||||
private void ExtractTargetIconFromPrefab(GameObject prefab)
|
||||
{
|
||||
// Try to find SpriteRenderer in prefab or children
|
||||
SpriteRenderer spriteRenderer = prefab.GetComponentInChildren<SpriteRenderer>();
|
||||
|
||||
if (spriteRenderer != null && spriteRenderer.sprite != null)
|
||||
{
|
||||
_targetIconSprite = spriteRenderer.sprite;
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[SpawnManager] Extracted target icon from prefab: {_targetIconSprite.name}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Logging.Warning($"[SpawnManager] Could not find SpriteRenderer in target prefab '{_currentTargetKey}'!");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extract sprite from target for UI display.
|
||||
/// Finds first SpriteRenderer in target or children.
|
||||
/// </summary>
|
||||
private void ExtractTargetIcon(GameObject targetObject)
|
||||
{
|
||||
// Try to find SpriteRenderer in target or children
|
||||
SpriteRenderer spriteRenderer = targetObject.GetComponentInChildren<SpriteRenderer>();
|
||||
|
||||
if (spriteRenderer != null && spriteRenderer.sprite != null)
|
||||
{
|
||||
_targetIconSprite = spriteRenderer.sprite;
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[SpawnManager] Extracted target icon: {_targetIconSprite.name}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Logging.Warning($"[SpawnManager] Could not find SpriteRenderer in target '{_currentTargetKey}'!");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Setup the target display UI with icon and position.
|
||||
/// </summary>
|
||||
private void SetupTargetUI()
|
||||
{
|
||||
if (targetDisplayUI != null && _targetIconSprite != null)
|
||||
{
|
||||
// Get launch anchor from launch controller
|
||||
Transform launchPoint = GetLaunchAnchor();
|
||||
targetDisplayUI.Setup(_targetIconSprite, _targetSpawnPosition, launchPoint);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get launch anchor transform from the launch controller.
|
||||
/// </summary>
|
||||
private Transform GetLaunchAnchor()
|
||||
{
|
||||
return launchController != null ?
|
||||
// Access the public launchAnchor field from DragLaunchController
|
||||
launchController.GetLaunchAnchorTransform() : null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Dynamic Spawning
|
||||
|
||||
/// <summary>
|
||||
/// Initialize dynamic spawning when threshold is crossed.
|
||||
/// </summary>
|
||||
private void InitializeDynamicSpawning()
|
||||
{
|
||||
ScheduleNextObjectSpawn();
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug("[SpawnManager] Dynamic spawning initialized");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the distance ahead to spawn ground (2x object spawn distance).
|
||||
/// </summary>
|
||||
private float GetGroundSpawnAheadDistance()
|
||||
{
|
||||
return _settings.SpawnDistanceAhead * 2f;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Schedule the next object spawn based on random interval.
|
||||
/// </summary>
|
||||
private void ScheduleNextObjectSpawn()
|
||||
{
|
||||
float interval = Random.Range((float)_settings.ObjectSpawnMinInterval, (float)_settings.ObjectSpawnMaxInterval);
|
||||
_nextObjectSpawnTime = Time.time + interval;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Spawn a random positive or negative object.
|
||||
/// Uses weighted randomness to maintain target ratio.
|
||||
/// Avoids spawning near target position to prevent obscuring it.
|
||||
/// </summary>
|
||||
private void SpawnRandomObject()
|
||||
{
|
||||
if (_planeTransform == null) return;
|
||||
|
||||
// Calculate spawn X position ahead of plane
|
||||
float spawnX = _planeTransform.position.x + _settings.SpawnDistanceAhead;
|
||||
|
||||
// Check if spawn position is too close to target (avoid obscuring it)
|
||||
float distanceToTarget = Mathf.Abs(spawnX - _targetSpawnPosition.x);
|
||||
float targetClearanceZone = 10f; // Don't spawn within 10 units of target
|
||||
|
||||
if (distanceToTarget < targetClearanceZone)
|
||||
{
|
||||
// Too close to target, skip this spawn
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[SpawnManager] Skipped object spawn at X={spawnX:F2} (too close to target at X={_targetSpawnPosition.x:F2})");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Determine if spawning positive or negative based on weighted ratio
|
||||
bool spawnPositive = ShouldSpawnPositive();
|
||||
|
||||
GameObject prefabToSpawn = null;
|
||||
|
||||
if (spawnPositive)
|
||||
{
|
||||
if (positiveObjectPrefabs != null && positiveObjectPrefabs.Length > 0)
|
||||
{
|
||||
prefabToSpawn = positiveObjectPrefabs[UnityEngine.Random.Range(0, positiveObjectPrefabs.Length)];
|
||||
_positiveSpawnCount++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (negativeObjectPrefabs != null && negativeObjectPrefabs.Length > 0)
|
||||
{
|
||||
prefabToSpawn = negativeObjectPrefabs[UnityEngine.Random.Range(0, negativeObjectPrefabs.Length)];
|
||||
_negativeSpawnCount++;
|
||||
}
|
||||
}
|
||||
|
||||
if (prefabToSpawn == null) return;
|
||||
|
||||
|
||||
// Spawn object at temporary position
|
||||
Vector3 tempPosition = new Vector3(spawnX, 0f, 0f);
|
||||
GameObject spawnedObject = Instantiate(prefabToSpawn, tempPosition, Quaternion.identity);
|
||||
|
||||
if (spawnedObjectsParent != null)
|
||||
{
|
||||
spawnedObject.transform.SetParent(spawnedObjectsParent);
|
||||
}
|
||||
|
||||
// Snap to ground
|
||||
SnapObjectToGround(spawnedObject, spawnX);
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[SpawnManager] Spawned {(spawnPositive ? "positive" : "negative")} object at {spawnedObject.transform.position}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determine if next spawn should be positive based on weighted ratio.
|
||||
/// Adjusts to maintain target positive/negative ratio.
|
||||
/// </summary>
|
||||
private bool ShouldSpawnPositive()
|
||||
{
|
||||
int totalSpawned = _positiveSpawnCount + _negativeSpawnCount;
|
||||
|
||||
// First few spawns - use pure random based on ratio
|
||||
if (totalSpawned < 5)
|
||||
{
|
||||
return UnityEngine.Random.value <= _settings.PositiveNegativeRatio;
|
||||
}
|
||||
|
||||
// Calculate current ratio
|
||||
float currentRatio = totalSpawned > 0 ? (float)_positiveSpawnCount / totalSpawned : 0.5f;
|
||||
float targetRatio = _settings.PositiveNegativeRatio;
|
||||
|
||||
// If we're below target ratio, heavily favor positive
|
||||
// If we're above target ratio, heavily favor negative
|
||||
float adjustedProbability;
|
||||
if (currentRatio < targetRatio)
|
||||
{
|
||||
// Need more positive - increase probability
|
||||
adjustedProbability = Mathf.Lerp(targetRatio, 1f, (targetRatio - currentRatio) * 2f);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Need more negative - decrease probability
|
||||
adjustedProbability = Mathf.Lerp(0f, targetRatio, 1f - (currentRatio - targetRatio) * 2f);
|
||||
}
|
||||
|
||||
return UnityEngine.Random.value <= adjustedProbability;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Spawn a ground tile at the next ground spawn position.
|
||||
/// </summary>
|
||||
private void SpawnGroundTile()
|
||||
{
|
||||
if (groundTilePrefabs == null || groundTilePrefabs.Length == 0) return;
|
||||
|
||||
// Pick random ground tile
|
||||
GameObject tilePrefab = groundTilePrefabs[Random.Range(0, groundTilePrefabs.Length)];
|
||||
|
||||
// Calculate spawn position using configured Y
|
||||
Vector3 spawnPosition = new Vector3(_nextGroundSpawnX, groundSpawnY, 0f);
|
||||
|
||||
// Spawn tile
|
||||
GameObject spawnedTile = Instantiate(tilePrefab, spawnPosition, Quaternion.identity);
|
||||
|
||||
if (groundTilesParent != null)
|
||||
{
|
||||
spawnedTile.transform.SetParent(groundTilesParent);
|
||||
}
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[SpawnManager] Spawned ground tile at ({_nextGroundSpawnX:F2}, {groundSpawnY:F2})");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Ground Snapping
|
||||
|
||||
/// <summary>
|
||||
/// Snap an object to the ground using raycast.
|
||||
/// Positions object so its bottom bounds touch the ground.
|
||||
/// </summary>
|
||||
/// <param name="obj">Object to snap to ground</param>
|
||||
/// <param name="xPosition">X position to raycast from</param>
|
||||
private void SnapObjectToGround(GameObject obj, float xPosition)
|
||||
{
|
||||
if (obj == null) return;
|
||||
|
||||
// Start raycast from high Y position
|
||||
Vector2 rayOrigin = new Vector2(xPosition, 0.0f);
|
||||
|
||||
// Raycast downward to find ground (convert layer to layer mask)
|
||||
int layerMask = 1 << _settings.GroundLayer;
|
||||
RaycastHit2D hit = Physics2D.Raycast(
|
||||
rayOrigin,
|
||||
Vector2.down,
|
||||
_settings.MaxGroundRaycastDistance,
|
||||
layerMask
|
||||
);
|
||||
|
||||
float targetY;
|
||||
|
||||
if (hit.collider != null)
|
||||
{
|
||||
// Found ground - calculate Y position
|
||||
float groundY = hit.point.y;
|
||||
|
||||
// Get object bounds
|
||||
Bounds bounds = GetObjectBounds(obj);
|
||||
float boundsBottomOffset = bounds.extents.y; // Half height
|
||||
|
||||
// Position object so bottom touches ground
|
||||
targetY = groundY + boundsBottomOffset;
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[SpawnManager] Snapped object to ground at Y={targetY:F2} (ground at {groundY:F2})");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// No ground found - use default offset
|
||||
targetY = _settings.DefaultObjectYOffset;
|
||||
|
||||
Logging.Warning($"[SpawnManager] No ground found at X={xPosition}, using default Y={targetY}");
|
||||
}
|
||||
|
||||
// Apply position
|
||||
Vector3 newPosition = obj.transform.position;
|
||||
newPosition.y = targetY;
|
||||
obj.transform.position = newPosition;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the bounds of an object from its Renderer or Collider.
|
||||
/// </summary>
|
||||
private Bounds GetObjectBounds(GameObject obj)
|
||||
{
|
||||
// Try Renderer first
|
||||
Renderer objRenderer = obj.GetComponentInChildren<Renderer>();
|
||||
if (objRenderer != null)
|
||||
{
|
||||
return objRenderer.bounds;
|
||||
}
|
||||
|
||||
// Try Collider2D
|
||||
Collider2D objCollider2D = obj.GetComponentInChildren<Collider2D>();
|
||||
if (objCollider2D != null)
|
||||
{
|
||||
return objCollider2D.bounds;
|
||||
}
|
||||
|
||||
// Try Collider3D
|
||||
Collider objCollider3D = obj.GetComponentInChildren<Collider>();
|
||||
if (objCollider3D != null)
|
||||
{
|
||||
return objCollider3D.bounds;
|
||||
}
|
||||
|
||||
// Fallback - create minimal bounds
|
||||
Logging.Warning($"[SpawnManager] No Renderer or Collider found on {obj.name}, using default bounds");
|
||||
return new Bounds(obj.transform.position, Vector3.one);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 70f14ee4b04b46b793ec2652fd2ca7b9
|
||||
timeCreated: 1764943526
|
||||
@@ -0,0 +1,186 @@
|
||||
using System;
|
||||
using Core;
|
||||
using Core.Lifecycle;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Minigames.Airplane.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Validates whether the airplane hit the correct target.
|
||||
/// Singleton for easy access throughout the minigame.
|
||||
/// </summary>
|
||||
public class AirplaneTargetValidator : ManagedBehaviour
|
||||
{
|
||||
#region Singleton
|
||||
|
||||
private static AirplaneTargetValidator _instance;
|
||||
public static AirplaneTargetValidator Instance => _instance;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Events
|
||||
|
||||
/// <summary>
|
||||
/// Fired when correct target is hit. Parameters: (string targetName)
|
||||
/// </summary>
|
||||
public event Action<string> OnCorrectTargetHit;
|
||||
|
||||
/// <summary>
|
||||
/// Fired when wrong target is hit. Parameters: (string expectedTarget, string actualTarget)
|
||||
/// </summary>
|
||||
public event Action<string, string> OnWrongTargetHit;
|
||||
|
||||
/// <summary>
|
||||
/// Fired when no target is hit
|
||||
/// </summary>
|
||||
public event Action OnMissedAllTargets;
|
||||
|
||||
#endregion
|
||||
|
||||
#region State
|
||||
|
||||
private string _expectedTargetName = null;
|
||||
private bool _hasValidatedCurrentShot = false;
|
||||
|
||||
public string ExpectedTargetName => _expectedTargetName;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Configuration
|
||||
|
||||
[Header("Debug")]
|
||||
[SerializeField] private bool showDebugLogs = false;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Lifecycle
|
||||
|
||||
internal override void OnManagedAwake()
|
||||
{
|
||||
base.OnManagedAwake();
|
||||
|
||||
// Set singleton
|
||||
if (_instance != null && _instance != this)
|
||||
{
|
||||
Logging.Warning("[AirplaneTargetValidator] Multiple instances detected! Destroying duplicate.");
|
||||
Destroy(gameObject);
|
||||
return;
|
||||
}
|
||||
_instance = this;
|
||||
}
|
||||
|
||||
internal override void OnManagedDestroy()
|
||||
{
|
||||
base.OnManagedDestroy();
|
||||
|
||||
if (_instance == this)
|
||||
{
|
||||
_instance = null;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Target Setting
|
||||
|
||||
/// <summary>
|
||||
/// Set the expected target for the current shot
|
||||
/// </summary>
|
||||
public void SetExpectedTarget(string targetName)
|
||||
{
|
||||
_expectedTargetName = targetName;
|
||||
_hasValidatedCurrentShot = false;
|
||||
|
||||
if (showDebugLogs) Logging.Debug($"[AirplaneTargetValidator] Expected target set to: {targetName}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clear the expected target
|
||||
/// </summary>
|
||||
public void ClearExpectedTarget()
|
||||
{
|
||||
_expectedTargetName = null;
|
||||
_hasValidatedCurrentShot = false;
|
||||
|
||||
if (showDebugLogs) Logging.Debug("[AirplaneTargetValidator] Expected target cleared");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Validation
|
||||
|
||||
/// <summary>
|
||||
/// Validate if the hit target matches the expected target
|
||||
/// </summary>
|
||||
public bool ValidateHit(string hitTargetName)
|
||||
{
|
||||
// Prevent multiple validations for the same shot
|
||||
if (_hasValidatedCurrentShot)
|
||||
{
|
||||
if (showDebugLogs) Logging.Debug("[AirplaneTargetValidator] Already validated this shot");
|
||||
return false;
|
||||
}
|
||||
|
||||
_hasValidatedCurrentShot = true;
|
||||
|
||||
if (string.IsNullOrEmpty(_expectedTargetName))
|
||||
{
|
||||
Logging.Warning("[AirplaneTargetValidator] No expected target set!");
|
||||
return false;
|
||||
}
|
||||
|
||||
bool isCorrect = string.Equals(hitTargetName, _expectedTargetName, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
if (isCorrect)
|
||||
{
|
||||
if (showDebugLogs) Logging.Debug($"[AirplaneTargetValidator] ✓ Correct! Hit target: {hitTargetName}");
|
||||
OnCorrectTargetHit?.Invoke(hitTargetName);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (showDebugLogs) Logging.Debug($"[AirplaneTargetValidator] ✗ Wrong! Expected: {_expectedTargetName}, Hit: {hitTargetName}");
|
||||
OnWrongTargetHit?.Invoke(_expectedTargetName, hitTargetName);
|
||||
}
|
||||
|
||||
return isCorrect;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handle case where airplane didn't hit any target
|
||||
/// </summary>
|
||||
public void HandleMiss()
|
||||
{
|
||||
// Prevent multiple validations for the same shot
|
||||
if (_hasValidatedCurrentShot)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_hasValidatedCurrentShot = true;
|
||||
|
||||
if (showDebugLogs) Logging.Debug($"[AirplaneTargetValidator] ✗ Missed! Expected target: {_expectedTargetName}");
|
||||
|
||||
OnMissedAllTargets?.Invoke();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Query Methods
|
||||
|
||||
/// <summary>
|
||||
/// Check if a target name matches the expected target
|
||||
/// </summary>
|
||||
public bool IsExpectedTarget(string targetName)
|
||||
{
|
||||
return string.Equals(targetName, _expectedTargetName, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if validation has been done for current shot
|
||||
/// </summary>
|
||||
public bool HasValidated => _hasValidatedCurrentShot;
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3cf7815b220240e090fb5cba4fc7414f
|
||||
timeCreated: 1764851309
|
||||
184
Assets/Scripts/Minigames/Airplane/Core/Person.cs
Normal file
184
Assets/Scripts/Minigames/Airplane/Core/Person.cs
Normal file
@@ -0,0 +1,184 @@
|
||||
using System.Collections;
|
||||
using Core;
|
||||
using Core.Lifecycle;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Minigames.Airplane.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a person participating in the airplane minigame.
|
||||
/// Holds data (name, target) and provides awaitable callbacks for game events.
|
||||
/// </summary>
|
||||
public class Person : ManagedBehaviour
|
||||
{
|
||||
#region Inspector Data
|
||||
|
||||
[Header("Person Data")]
|
||||
[Tooltip("Name of this person")]
|
||||
[SerializeField] private string personName = "Unknown";
|
||||
|
||||
[Tooltip("Target name they need to hit")]
|
||||
[SerializeField] private string targetName = "Unknown";
|
||||
|
||||
[Header("Visual (Placeholder)")]
|
||||
[Tooltip("TextMeshPro text for debug/placeholder animations")]
|
||||
[SerializeField] private TextMeshPro debugText;
|
||||
|
||||
[Header("Debug")]
|
||||
[SerializeField] private bool showDebugLogs = false;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
|
||||
public string PersonName => personName;
|
||||
public string TargetName => targetName;
|
||||
public int TurnNumber { get; set; }
|
||||
public Transform PersonTransform => transform;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Internal
|
||||
|
||||
// Tracks the currently running debug hide coroutine so we can cancel overlaps.
|
||||
private Coroutine _activeDebugCoroutine;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Event Callbacks (Awaitable via Coroutines)
|
||||
|
||||
/// <summary>
|
||||
/// Called when this person is first shown (game start or their turn).
|
||||
/// Awaitable - game flow waits for this to complete.
|
||||
/// </summary>
|
||||
public IEnumerator OnHello()
|
||||
{
|
||||
if (showDebugLogs)
|
||||
Logging.Debug($"[Person] {personName}: Hello! I need to hit {targetName}!");
|
||||
|
||||
// Show debug text with hello message
|
||||
yield return PrintDebugText($"Hello! I need to hit {targetName}!");
|
||||
|
||||
if (showDebugLogs)
|
||||
Logging.Debug($"[Person] {personName}: Ready to aim!");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when this person successfully hit their target.
|
||||
/// Awaitable - game flow waits for celebration to complete.
|
||||
/// </summary>
|
||||
public IEnumerator OnTargetHit()
|
||||
{
|
||||
if (showDebugLogs)
|
||||
Logging.Debug($"[Person] {personName}: Yes! I hit {targetName}!");
|
||||
|
||||
// Show debug text with hit message
|
||||
yield return PrintDebugText($"Yay — hit {targetName}!");
|
||||
|
||||
if (showDebugLogs)
|
||||
Logging.Debug($"[Person] {personName}: That was awesome!");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when this person missed their target.
|
||||
/// Awaitable - game flow waits for reaction to complete.
|
||||
/// </summary>
|
||||
public IEnumerator OnTargetMissed()
|
||||
{
|
||||
if (showDebugLogs)
|
||||
Logging.Debug($"[Person] {personName}: Oh no, I missed {targetName}...");
|
||||
|
||||
// Show debug text with miss message
|
||||
yield return PrintDebugText($"Missed {targetName}...");
|
||||
|
||||
if (showDebugLogs)
|
||||
Logging.Debug($"[Person] {personName}: I'll try better next time.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shows debug text, waits, then hides it. Cancels any previous debug display.
|
||||
/// Awaitable so callers can yield return this coroutine.
|
||||
/// </summary>
|
||||
public IEnumerator PrintDebugText(string inputText, float duration = 0.5f)
|
||||
{
|
||||
if (debugText != null)
|
||||
{
|
||||
// Cancel any active hide coroutine to avoid overlap
|
||||
if (_activeDebugCoroutine != null)
|
||||
{
|
||||
StopCoroutine(_activeDebugCoroutine);
|
||||
_activeDebugCoroutine = null;
|
||||
}
|
||||
|
||||
debugText.text = inputText;
|
||||
debugText.gameObject.SetActive(true);
|
||||
|
||||
// Start a coroutine to hide after delay and yield it so this method is awaitable
|
||||
_activeDebugCoroutine = StartCoroutine(HideAfterDelay(duration));
|
||||
yield return _activeDebugCoroutine;
|
||||
|
||||
_activeDebugCoroutine = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
// No visual, still allow callers to wait the same duration
|
||||
yield return new WaitForSeconds(duration);
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator HideAfterDelay(float duration)
|
||||
{
|
||||
yield return new WaitForSeconds(duration);
|
||||
|
||||
if (debugText != null)
|
||||
{
|
||||
debugText.gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Validation
|
||||
|
||||
internal override void OnManagedAwake()
|
||||
{
|
||||
base.OnManagedAwake();
|
||||
|
||||
// Hide debug text on start
|
||||
if (debugText != null)
|
||||
{
|
||||
debugText.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(personName))
|
||||
{
|
||||
Logging.Warning($"[Person] Person on {gameObject.name} has no name assigned!");
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(targetName))
|
||||
{
|
||||
Logging.Warning($"[Person] Person '{personName}' has no target assigned!");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Editor Helpers
|
||||
|
||||
#if UNITY_EDITOR
|
||||
private void OnValidate()
|
||||
{
|
||||
// Auto-set person name from GameObject name if empty
|
||||
if (string.IsNullOrEmpty(personName) && gameObject != null)
|
||||
{
|
||||
personName = gameObject.name;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
3
Assets/Scripts/Minigames/Airplane/Core/Person.cs.meta
Normal file
3
Assets/Scripts/Minigames/Airplane/Core/Person.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: dcd6c4e7afe141399878a768cf6bfa24
|
||||
timeCreated: 1764938205
|
||||
352
Assets/Scripts/Minigames/Airplane/Core/PersonQueue.cs
Normal file
352
Assets/Scripts/Minigames/Airplane/Core/PersonQueue.cs
Normal file
@@ -0,0 +1,352 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Core;
|
||||
using Core.Lifecycle;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Minigames.Airplane.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Manages the queue of people waiting to launch airplanes.
|
||||
/// Controls person transitions, reactions, and shuffle animations.
|
||||
/// Provides awaitable coroutines for game flow integration.
|
||||
/// </summary>
|
||||
public class PersonQueue : ManagedBehaviour
|
||||
{
|
||||
#region Inspector Properties
|
||||
|
||||
[Header("Person Setup")]
|
||||
[Tooltip("List of people in the queue (order matters - index 0 goes first)")]
|
||||
[SerializeField] private List<Person> peopleInQueue = new List<Person>();
|
||||
|
||||
[Header("Shuffle Settings")]
|
||||
[Tooltip("Duration of shuffle transition between people")]
|
||||
[SerializeField] private float shuffleDuration = 0.5f;
|
||||
|
||||
[Tooltip("Distance to move people during shuffle")]
|
||||
[SerializeField] private float shuffleDistance = 2f;
|
||||
|
||||
[Header("Debug")]
|
||||
[SerializeField] private bool showDebugLogs = false;
|
||||
|
||||
#endregion
|
||||
|
||||
#region State
|
||||
|
||||
private int _currentTurnNumber = 1;
|
||||
private int _initialPeopleCount;
|
||||
|
||||
public int TotalPeople => _initialPeopleCount;
|
||||
public int RemainingPeople => peopleInQueue.Count;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Lifecycle
|
||||
|
||||
internal override void OnManagedAwake()
|
||||
{
|
||||
base.OnManagedAwake();
|
||||
|
||||
_initialPeopleCount = peopleInQueue.Count;
|
||||
ValidateQueue();
|
||||
}
|
||||
|
||||
internal override void OnManagedStart()
|
||||
{
|
||||
base.OnManagedStart();
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[PersonQueue] Initialized with {TotalPeople} people");
|
||||
foreach (var person in peopleInQueue)
|
||||
{
|
||||
Logging.Debug($" - {person.PersonName} -> Target: {person.TargetName}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Validation
|
||||
|
||||
/// <summary>
|
||||
/// Validate the queue setup
|
||||
/// </summary>
|
||||
private void ValidateQueue()
|
||||
{
|
||||
if (peopleInQueue.Count == 0)
|
||||
{
|
||||
Logging.Warning("[PersonQueue] No people in queue! Add Person components in the inspector.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Check for null references and validate data
|
||||
for (int i = 0; i < peopleInQueue.Count; i++)
|
||||
{
|
||||
var person = peopleInQueue[i];
|
||||
|
||||
if (person == null)
|
||||
{
|
||||
Logging.Error($"[PersonQueue] Person at index {i} is null!");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(person.PersonName))
|
||||
{
|
||||
Logging.Warning($"[PersonQueue] Person at index {i} has no name!");
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(person.TargetName))
|
||||
{
|
||||
Logging.Warning($"[PersonQueue] Person '{person.PersonName}' at index {i} has no target assigned!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Queue Management
|
||||
|
||||
/// <summary>
|
||||
/// Check if there are more people in the queue
|
||||
/// </summary>
|
||||
public bool HasMorePeople()
|
||||
{
|
||||
return peopleInQueue.Count > 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the next person without removing them from the queue
|
||||
/// </summary>
|
||||
public Person PeekNextPerson()
|
||||
{
|
||||
if (peopleInQueue.Count == 0)
|
||||
{
|
||||
if (showDebugLogs) Logging.Debug("[PersonQueue] Queue is empty!");
|
||||
return null;
|
||||
}
|
||||
|
||||
return peopleInQueue[0];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Pop the next person from the queue (does not remove until after their turn)
|
||||
/// </summary>
|
||||
public Person PopNextPerson()
|
||||
{
|
||||
if (peopleInQueue.Count == 0)
|
||||
{
|
||||
if (showDebugLogs) Logging.Debug("[PersonQueue] Queue is empty!");
|
||||
return null;
|
||||
}
|
||||
|
||||
// Get first person (don't remove yet - happens after their turn)
|
||||
Person nextPerson = peopleInQueue[0];
|
||||
|
||||
// Assign turn number
|
||||
nextPerson.TurnNumber = _currentTurnNumber;
|
||||
_currentTurnNumber++;
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[PersonQueue] Next person: {nextPerson.PersonName} (Turn {nextPerson.TurnNumber}), " +
|
||||
$"Remaining: {RemainingPeople}");
|
||||
}
|
||||
|
||||
return nextPerson;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get all people in the queue (for intro sequence). Returns a copy of the list.
|
||||
/// </summary>
|
||||
public List<Person> GetAllPeople()
|
||||
{
|
||||
return new List<Person>(peopleInQueue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove the current person from the queue after their turn.
|
||||
/// Destroys the person's GameObject.
|
||||
/// </summary>
|
||||
public void RemoveCurrentPerson()
|
||||
{
|
||||
if (peopleInQueue.Count == 0) return;
|
||||
|
||||
Person removedPerson = peopleInQueue[0];
|
||||
peopleInQueue.RemoveAt(0);
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[PersonQueue] Removed {removedPerson.PersonName} from queue. " +
|
||||
$"Remaining: {RemainingPeople}");
|
||||
}
|
||||
|
||||
// Destroy the person's GameObject
|
||||
if (removedPerson != null && removedPerson.gameObject != null)
|
||||
{
|
||||
Destroy(removedPerson.gameObject);
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[PersonQueue] Destroyed GameObject for {removedPerson.PersonName}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Transition Control
|
||||
|
||||
/// <summary>
|
||||
/// Show first person at game start.
|
||||
/// Awaitable - game flow waits for introduction to complete.
|
||||
/// </summary>
|
||||
public IEnumerator ShowFirstPerson(Person person)
|
||||
{
|
||||
if (showDebugLogs) Logging.Debug($"[PersonQueue] Showing first person: {person.PersonName}");
|
||||
|
||||
// Call person's hello sequence
|
||||
yield return person.OnHello();
|
||||
|
||||
if (showDebugLogs) Logging.Debug("[PersonQueue] First person introduction complete");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handle post-shot reaction for current person (who just shot).
|
||||
/// If successful: celebrate, remove from queue, shuffle remaining people.
|
||||
/// If failed: show disappointment, stay in queue.
|
||||
/// Awaitable - game flow waits for reactions and animations to complete.
|
||||
/// </summary>
|
||||
public IEnumerator HandlePostShotReaction(bool targetHit)
|
||||
{
|
||||
if (peopleInQueue.Count == 0)
|
||||
{
|
||||
Logging.Warning("[PersonQueue] HandlePostShotReaction called but queue is empty!");
|
||||
yield break;
|
||||
}
|
||||
|
||||
// Person at index 0 is the one who just shot
|
||||
Person currentPerson = peopleInQueue[0];
|
||||
|
||||
if (showDebugLogs)
|
||||
Logging.Debug($"[PersonQueue] Post-shot reaction for {currentPerson.PersonName} (Hit: {targetHit})");
|
||||
|
||||
// Call person's reaction based on result
|
||||
if (targetHit)
|
||||
{
|
||||
// Success reaction
|
||||
yield return StartCoroutine(currentPerson.OnTargetHit());
|
||||
|
||||
if (showDebugLogs) Logging.Debug("[PersonQueue] Success! Removing person and shuffling queue...");
|
||||
|
||||
// Remember the first person's position BEFORE removing them
|
||||
Vector3 firstPersonPosition = currentPerson.PersonTransform.position;
|
||||
|
||||
// Remove successful person from queue (they're no longer in peopleInQueue)
|
||||
RemoveCurrentPerson();
|
||||
|
||||
// Shuffle remaining people forward to fill the first person's spot
|
||||
yield return StartCoroutine(ShuffleToPosition(firstPersonPosition));
|
||||
}
|
||||
else
|
||||
{
|
||||
// Failure reaction
|
||||
yield return StartCoroutine(currentPerson.OnTargetMissed());
|
||||
|
||||
if (showDebugLogs) Logging.Debug("[PersonQueue] Failed - person stays in queue");
|
||||
// On failure, don't remove or shuffle, person gets another turn
|
||||
}
|
||||
|
||||
if (showDebugLogs) Logging.Debug("[PersonQueue] Post-shot reaction complete");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Introduce the next person (at front of queue) for their turn.
|
||||
/// Awaitable - game flow waits for introduction to complete.
|
||||
/// </summary>
|
||||
public IEnumerator IntroduceNextPerson()
|
||||
{
|
||||
if (peopleInQueue.Count == 0)
|
||||
{
|
||||
Logging.Warning("[PersonQueue] IntroduceNextPerson called but queue is empty!");
|
||||
yield break;
|
||||
}
|
||||
|
||||
Person nextPerson = peopleInQueue[0];
|
||||
|
||||
if (showDebugLogs) Logging.Debug($"[PersonQueue] Introducing next person: {nextPerson.PersonName}");
|
||||
|
||||
// Call person's hello sequence
|
||||
yield return StartCoroutine(nextPerson.OnHello());
|
||||
|
||||
if (showDebugLogs) Logging.Debug("[PersonQueue] Introduction complete");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shuffle remaining people forward to fill the first person's spot.
|
||||
/// The next person (now at index 0) moves to the first person's position.
|
||||
/// All other people move forward proportionally.
|
||||
/// Only tweens X position to prevent characters with foot pivots from floating.
|
||||
/// </summary>
|
||||
private IEnumerator ShuffleToPosition(Vector3 firstPersonPosition)
|
||||
{
|
||||
if (peopleInQueue.Count == 0)
|
||||
{
|
||||
yield break; // No one left to shuffle
|
||||
}
|
||||
|
||||
if (showDebugLogs) Logging.Debug($"[PersonQueue] Shuffling {peopleInQueue.Count} people forward to first position");
|
||||
|
||||
// Store starting X positions and calculate target X positions
|
||||
List<float> startXPositions = new List<float>();
|
||||
List<float> targetXPositions = new List<float>();
|
||||
|
||||
for (int i = 0; i < peopleInQueue.Count; i++)
|
||||
{
|
||||
Person person = peopleInQueue[i];
|
||||
startXPositions.Add(person.PersonTransform.position.x);
|
||||
|
||||
if (i == 0)
|
||||
{
|
||||
// Next person (index 0) moves to first person's X position
|
||||
targetXPositions.Add(firstPersonPosition.x);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Everyone else moves to the X position of the person ahead of them
|
||||
targetXPositions.Add(peopleInQueue[i - 1].PersonTransform.position.x);
|
||||
}
|
||||
}
|
||||
|
||||
// Animate shuffle (only X axis)
|
||||
float elapsed = 0f;
|
||||
while (elapsed < shuffleDuration)
|
||||
{
|
||||
elapsed += Time.deltaTime;
|
||||
float t = elapsed / shuffleDuration;
|
||||
|
||||
// Smoothly lerp each person's X position only (preserve Y and Z)
|
||||
for (int i = 0; i < peopleInQueue.Count; i++)
|
||||
{
|
||||
float newX = Mathf.Lerp(startXPositions[i], targetXPositions[i], t);
|
||||
Vector3 currentPos = peopleInQueue[i].PersonTransform.position;
|
||||
peopleInQueue[i].PersonTransform.position = new Vector3(newX, currentPos.y, currentPos.z);
|
||||
}
|
||||
|
||||
yield return null;
|
||||
}
|
||||
|
||||
// Ensure final X positions are exact (preserve Y and Z)
|
||||
for (int i = 0; i < peopleInQueue.Count; i++)
|
||||
{
|
||||
Vector3 currentPos = peopleInQueue[i].PersonTransform.position;
|
||||
peopleInQueue[i].PersonTransform.position = new Vector3(targetXPositions[i], currentPos.y, currentPos.z);
|
||||
}
|
||||
|
||||
if (showDebugLogs) Logging.Debug("[PersonQueue] Shuffle complete");
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 77964ec3bd5848a6b947ed4ac9b0ee3f
|
||||
timeCreated: 1764851326
|
||||
3
Assets/Scripts/Minigames/Airplane/Data.meta
Normal file
3
Assets/Scripts/Minigames/Airplane/Data.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4d58653664484f58be14ab8089e22ce3
|
||||
timeCreated: 1764851234
|
||||
108
Assets/Scripts/Minigames/Airplane/Data/AirplaneAbilityConfig.cs
Normal file
108
Assets/Scripts/Minigames/Airplane/Data/AirplaneAbilityConfig.cs
Normal file
@@ -0,0 +1,108 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Minigames.Airplane.Data
|
||||
{
|
||||
/// <summary>
|
||||
/// Configuration for Jet Plane ability.
|
||||
/// </summary>
|
||||
[System.Serializable]
|
||||
public class JetAbilityConfig
|
||||
{
|
||||
[Header("Jet Ability")]
|
||||
[Tooltip("Display name")]
|
||||
public string abilityName = "Jet Boost";
|
||||
|
||||
[Tooltip("Icon for ability button")]
|
||||
public Sprite abilityIcon;
|
||||
|
||||
[Tooltip("Cooldown duration in seconds")]
|
||||
public float cooldownDuration = 5f;
|
||||
|
||||
[Tooltip("Speed while ability is active")]
|
||||
public float jetSpeed = 15f;
|
||||
|
||||
[Tooltip("Direction angle (0 = right, 90 = up)")]
|
||||
public float jetAngle = 0f;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configuration for Bobbing Plane ability.
|
||||
/// </summary>
|
||||
[System.Serializable]
|
||||
public class BobbingAbilityConfig
|
||||
{
|
||||
[Header("Bobbing Ability")]
|
||||
[Tooltip("Display name")]
|
||||
public string abilityName = "Air Hop";
|
||||
|
||||
[Tooltip("Icon for ability button")]
|
||||
public Sprite abilityIcon;
|
||||
|
||||
[Tooltip("Cooldown duration in seconds")]
|
||||
public float cooldownDuration = 3f;
|
||||
|
||||
[Tooltip("Force applied on activation (X = forward, Y = upward)")]
|
||||
public Vector2 bobForce = new Vector2(7f, 10f);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configuration for Drop Plane ability.
|
||||
/// </summary>
|
||||
[System.Serializable]
|
||||
public class DropAbilityConfig
|
||||
{
|
||||
[Header("Drop Ability")]
|
||||
[Tooltip("Display name")]
|
||||
public string abilityName = "Dive Bomb";
|
||||
|
||||
[Tooltip("Icon for ability button")]
|
||||
public Sprite abilityIcon;
|
||||
|
||||
[Tooltip("Cooldown duration in seconds")]
|
||||
public float cooldownDuration = 4f;
|
||||
|
||||
[Tooltip("Downward force applied")]
|
||||
public float dropForce = 20f;
|
||||
|
||||
[Tooltip("Distance to drop before returning to normal flight")]
|
||||
public float dropDistance = 5f;
|
||||
|
||||
[Tooltip("Should horizontal velocity be zeroed during drop?")]
|
||||
public bool zeroHorizontalVelocity = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configuration for an airplane type with visual and physics properties.
|
||||
/// </summary>
|
||||
[System.Serializable]
|
||||
public class AirplaneTypeConfig
|
||||
{
|
||||
[Header("Identity")]
|
||||
[Tooltip("Display name for UI")]
|
||||
public string displayName = "Airplane";
|
||||
|
||||
[Tooltip("Airplane prefab")]
|
||||
public GameObject prefab;
|
||||
|
||||
[Tooltip("Preview sprite for selection UI")]
|
||||
public Sprite previewSprite;
|
||||
|
||||
[Header("Ability")]
|
||||
[Tooltip("Which ability this airplane uses")]
|
||||
public AirplaneAbilityType abilityType = AirplaneAbilityType.Jet;
|
||||
|
||||
[Header("Physics Overrides (Optional)")]
|
||||
[Tooltip("Override default mass")]
|
||||
public bool overrideMass;
|
||||
public float mass = 1f;
|
||||
|
||||
[Tooltip("Override default gravity scale")]
|
||||
public bool overrideGravityScale;
|
||||
public float gravityScale = 1f;
|
||||
|
||||
[Tooltip("Override default drag")]
|
||||
public bool overrideDrag;
|
||||
public float drag = 0f;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 57bed242caa44ef5bbcd33348d5c908f
|
||||
timeCreated: 1764977731
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace Minigames.Airplane.Data
|
||||
{
|
||||
/// <summary>
|
||||
/// Types of special abilities available for airplanes.
|
||||
/// </summary>
|
||||
public enum AirplaneAbilityType
|
||||
{
|
||||
None,
|
||||
Jet, // Hold to fly straight without gravity
|
||||
Bobbing, // Tap to jump upward, reduces speed
|
||||
Drop // Swipe down to drop straight down
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 944943da845c403b8b43aeab3c9ef696
|
||||
timeCreated: 1764975919
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace Minigames.Airplane.Data
|
||||
{
|
||||
/// <summary>
|
||||
/// Camera states for the airplane minigame
|
||||
/// </summary>
|
||||
public enum AirplaneCameraState
|
||||
{
|
||||
Intro, // Intro sequence camera
|
||||
NextPerson, // Camera focusing on the next person
|
||||
Aiming, // Camera for aiming the airplane
|
||||
Flight // Camera following the airplane in flight
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f5b6a3623e7040be9dfeac6ee8e195cf
|
||||
timeCreated: 1764851235
|
||||
17
Assets/Scripts/Minigames/Airplane/Data/AirplaneGameState.cs
Normal file
17
Assets/Scripts/Minigames/Airplane/Data/AirplaneGameState.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
namespace Minigames.Airplane.Data
|
||||
{
|
||||
/// <summary>
|
||||
/// Game states for the airplane minigame
|
||||
/// </summary>
|
||||
public enum AirplaneGameState
|
||||
{
|
||||
AirplaneSelection, // Player selecting airplane type
|
||||
Intro, // Intro sequence
|
||||
NextPerson, // Introducing the next person
|
||||
Aiming, // Player is aiming the airplane
|
||||
Flying, // Airplane is in flight
|
||||
Evaluating, // Evaluating the result of the flight
|
||||
GameOver // All people have had their turn
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 59636bd1dbca4575b431820510da201f
|
||||
timeCreated: 1764851235
|
||||
52
Assets/Scripts/Minigames/Airplane/Data/PersonData.cs
Normal file
52
Assets/Scripts/Minigames/Airplane/Data/PersonData.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Minigames.Airplane.Data
|
||||
{
|
||||
/// <summary>
|
||||
/// Data for a person participating in the airplane minigame.
|
||||
/// Contains their name, target assignment, and scene reference.
|
||||
/// </summary>
|
||||
[System.Serializable]
|
||||
public class PersonData
|
||||
{
|
||||
[Tooltip("Name of the person")]
|
||||
public string personName;
|
||||
|
||||
[Tooltip("Target name they need to hit")]
|
||||
public string targetName;
|
||||
|
||||
[Tooltip("Transform reference to the person in the scene")]
|
||||
public Transform personTransform;
|
||||
|
||||
[Tooltip("Turn number (assigned at runtime)")]
|
||||
public int turnNumber;
|
||||
|
||||
/// <summary>
|
||||
/// Constructor for creating person data
|
||||
/// </summary>
|
||||
public PersonData(string name, string target, Transform transform, int turn = 0)
|
||||
{
|
||||
personName = name;
|
||||
targetName = target;
|
||||
personTransform = transform;
|
||||
turnNumber = turn;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Default constructor for serialization
|
||||
/// </summary>
|
||||
public PersonData()
|
||||
{
|
||||
personName = "Unknown";
|
||||
targetName = "Unknown";
|
||||
personTransform = null;
|
||||
turnNumber = 0;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"Person: {personName}, Target: {targetName}, Turn: {turnNumber}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b9a03de5cfa64dadaf6c53b8f3935d3e
|
||||
timeCreated: 1764851235
|
||||
3
Assets/Scripts/Minigames/Airplane/Interactive.meta
Normal file
3
Assets/Scripts/Minigames/Airplane/Interactive.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e00ca01a87d64f7c83128cb731225039
|
||||
timeCreated: 1765135354
|
||||
@@ -0,0 +1,123 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Minigames.Airplane.Interactive
|
||||
{
|
||||
/// <summary>
|
||||
/// Bounces airplanes on collision with configurable bounce multiplier.
|
||||
/// Can be used for trampolines, bounce pads, or elastic surfaces.
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(Collider2D))]
|
||||
public class AirplaneBouncySurface : MonoBehaviour
|
||||
{
|
||||
[Header("Bounce Configuration")]
|
||||
[SerializeField] private float bounceMultiplier = 1.5f;
|
||||
[SerializeField] private Vector2 bounceDirection = Vector2.up;
|
||||
[SerializeField] private bool useReflection = true;
|
||||
|
||||
[Header("Optional Modifiers")]
|
||||
[SerializeField] private float minBounceVelocity = 2f;
|
||||
[SerializeField] private float maxBounceVelocity = 20f;
|
||||
|
||||
[Header("Visual Feedback (Optional)")]
|
||||
[SerializeField] private Animator bounceAnimator;
|
||||
[SerializeField] private string bounceTrigger = "Bounce";
|
||||
[SerializeField] private AudioSource bounceSound;
|
||||
|
||||
[Header("Debug")]
|
||||
[SerializeField] private bool showDebugLogs;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
// Ensure collider is trigger
|
||||
var collider = GetComponent<Collider2D>();
|
||||
if (collider != null)
|
||||
{
|
||||
collider.isTrigger = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTriggerEnter2D(Collider2D other)
|
||||
{
|
||||
// Check if it's an airplane
|
||||
var airplane = other.GetComponent<Core.AirplaneController>();
|
||||
if (airplane == null || !airplane.IsFlying) return;
|
||||
|
||||
var rb = other.GetComponent<Rigidbody2D>();
|
||||
if (rb == null) return;
|
||||
|
||||
// Calculate bounce velocity
|
||||
Vector2 newVelocity;
|
||||
|
||||
if (useReflection)
|
||||
{
|
||||
// Reflect velocity around bounce direction (normal)
|
||||
Vector2 normal = bounceDirection.normalized;
|
||||
newVelocity = Vector2.Reflect(rb.linearVelocity, normal) * bounceMultiplier;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Direct bounce in specified direction
|
||||
float speed = rb.linearVelocity.magnitude * bounceMultiplier;
|
||||
newVelocity = bounceDirection.normalized * speed;
|
||||
}
|
||||
|
||||
// Clamp velocity
|
||||
float magnitude = newVelocity.magnitude;
|
||||
if (magnitude < minBounceVelocity)
|
||||
{
|
||||
newVelocity = newVelocity.normalized * minBounceVelocity;
|
||||
}
|
||||
else if (magnitude > maxBounceVelocity)
|
||||
{
|
||||
newVelocity = newVelocity.normalized * maxBounceVelocity;
|
||||
}
|
||||
|
||||
// Apply bounce
|
||||
rb.linearVelocity = newVelocity;
|
||||
|
||||
// Visual/audio feedback
|
||||
PlayBounceEffects();
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Debug.Log($"[AirplaneBouncySurface] Bounced {other.name}: velocity={newVelocity}");
|
||||
}
|
||||
}
|
||||
|
||||
private void PlayBounceEffects()
|
||||
{
|
||||
// Trigger animation
|
||||
if (bounceAnimator != null && !string.IsNullOrEmpty(bounceTrigger))
|
||||
{
|
||||
bounceAnimator.SetTrigger(bounceTrigger);
|
||||
}
|
||||
|
||||
// Play sound
|
||||
if (bounceSound != null)
|
||||
{
|
||||
bounceSound.Play();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDrawGizmos()
|
||||
{
|
||||
// Visualize bounce direction in editor
|
||||
Gizmos.color = Color.cyan;
|
||||
|
||||
var collider = GetComponent<Collider2D>();
|
||||
if (collider != null)
|
||||
{
|
||||
Vector3 center = collider.bounds.center;
|
||||
Vector3 direction = bounceDirection.normalized;
|
||||
|
||||
// Draw arrow showing bounce direction
|
||||
Gizmos.DrawRay(center, direction * 2f);
|
||||
Gizmos.DrawWireSphere(center + direction * 2f, 0.3f);
|
||||
|
||||
// Draw surface bounds
|
||||
Gizmos.DrawWireCube(collider.bounds.center, collider.bounds.size);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6f1ff69bae8e49188f439a8e5cdb7dfc
|
||||
timeCreated: 1765135371
|
||||
@@ -0,0 +1,148 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Minigames.Airplane.Interactive
|
||||
{
|
||||
/// <summary>
|
||||
/// Gravity well that pulls airplanes toward its center.
|
||||
/// Creates challenging "danger zones" that players must avoid or escape from.
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(Collider2D))]
|
||||
public class AirplaneGravityWell : MonoBehaviour
|
||||
{
|
||||
[Header("Gravity Configuration")]
|
||||
[SerializeField] private float pullStrength = 5f;
|
||||
[SerializeField] private bool useInverseSquare = false;
|
||||
[SerializeField] private float minPullDistance = 0.5f;
|
||||
|
||||
[Header("Optional Modifiers")]
|
||||
[SerializeField] private float maxPullForce = 15f;
|
||||
[SerializeField] private AnimationCurve pullFalloff = AnimationCurve.Linear(0, 1, 1, 0);
|
||||
|
||||
[Header("Visual Feedback (Optional)")]
|
||||
[SerializeField] private ParticleSystem gravityParticles;
|
||||
[SerializeField] private SpriteRenderer centerSprite;
|
||||
[SerializeField] private float rotationSpeed = 90f;
|
||||
|
||||
[Header("Debug")]
|
||||
[SerializeField] private bool showDebugLogs;
|
||||
[SerializeField] private bool drawDebugLines = true;
|
||||
|
||||
private Vector2 centerPosition;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
// Ensure collider is trigger
|
||||
var collider = GetComponent<Collider2D>();
|
||||
if (collider != null)
|
||||
{
|
||||
collider.isTrigger = true;
|
||||
}
|
||||
|
||||
centerPosition = transform.position;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
// Rotate center visual if present
|
||||
if (centerSprite != null)
|
||||
{
|
||||
centerSprite.transform.Rotate(0, 0, rotationSpeed * Time.deltaTime);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTriggerStay2D(Collider2D other)
|
||||
{
|
||||
// Check if it's an airplane
|
||||
var airplane = other.GetComponent<Core.AirplaneController>();
|
||||
if (airplane == null || !airplane.IsFlying) return;
|
||||
|
||||
var rb = other.GetComponent<Rigidbody2D>();
|
||||
if (rb == null) return;
|
||||
|
||||
// Calculate direction and distance to center
|
||||
Vector2 airplanePos = rb.position;
|
||||
Vector2 toCenter = centerPosition - airplanePos;
|
||||
float distance = toCenter.magnitude;
|
||||
|
||||
// Prevent division by zero
|
||||
if (distance < minPullDistance)
|
||||
{
|
||||
distance = minPullDistance;
|
||||
}
|
||||
|
||||
// Calculate pull force
|
||||
float forceMagnitude;
|
||||
|
||||
if (useInverseSquare)
|
||||
{
|
||||
// Realistic gravity-like force (inverse square law)
|
||||
forceMagnitude = pullStrength / (distance * distance);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Linear falloff based on distance
|
||||
var collider = GetComponent<Collider2D>();
|
||||
float maxDistance = collider != null ? collider.bounds.extents.magnitude : 5f;
|
||||
float normalizedDistance = Mathf.Clamp01(distance / maxDistance);
|
||||
float falloff = pullFalloff.Evaluate(1f - normalizedDistance);
|
||||
forceMagnitude = pullStrength * falloff;
|
||||
}
|
||||
|
||||
// Clamp force
|
||||
forceMagnitude = Mathf.Min(forceMagnitude, maxPullForce);
|
||||
|
||||
// Apply force toward center
|
||||
Vector2 pullForce = toCenter.normalized * forceMagnitude;
|
||||
rb.AddForce(pullForce, ForceMode2D.Force);
|
||||
|
||||
if (showDebugLogs && Time.frameCount % 30 == 0) // Log every 30 frames
|
||||
{
|
||||
Debug.Log($"[AirplaneGravityWell] Pulling {other.name}: force={forceMagnitude:F2}, distance={distance:F2}");
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDrawGizmos()
|
||||
{
|
||||
// Visualize gravity well in editor
|
||||
Gizmos.color = new Color(1f, 0f, 1f, 0.3f); // Magenta transparent
|
||||
|
||||
var collider = GetComponent<Collider2D>();
|
||||
if (collider != null)
|
||||
{
|
||||
// Draw zone bounds
|
||||
Gizmos.DrawWireSphere(collider.bounds.center, collider.bounds.extents.magnitude);
|
||||
|
||||
// Draw center point
|
||||
Gizmos.color = Color.magenta;
|
||||
Gizmos.DrawWireSphere(transform.position, 0.5f);
|
||||
|
||||
// Draw pull strength indicator
|
||||
Gizmos.DrawRay(transform.position, Vector3.up * pullStrength * 0.2f);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDrawGizmosSelected()
|
||||
{
|
||||
if (!drawDebugLines) return;
|
||||
|
||||
// Draw pull force visualization at multiple points
|
||||
var collider = GetComponent<Collider2D>();
|
||||
if (collider == null) return;
|
||||
|
||||
float radius = collider.bounds.extents.magnitude;
|
||||
int samples = 8;
|
||||
|
||||
for (int i = 0; i < samples; i++)
|
||||
{
|
||||
float angle = (i / (float)samples) * 360f * Mathf.Deg2Rad;
|
||||
Vector2 offset = new Vector2(Mathf.Cos(angle), Mathf.Sin(angle)) * radius;
|
||||
Vector3 samplePoint = transform.position + (Vector3)offset;
|
||||
Vector3 direction = (transform.position - samplePoint).normalized;
|
||||
|
||||
Gizmos.color = Color.yellow;
|
||||
Gizmos.DrawLine(samplePoint, samplePoint + direction * 2f);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f6c5008f2782416095c5b3f5092843a9
|
||||
timeCreated: 1765135424
|
||||
@@ -0,0 +1,122 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Minigames.Airplane.Interactive
|
||||
{
|
||||
/// <summary>
|
||||
/// Speed boost ring that increases airplane velocity when passed through.
|
||||
/// Can be used as collectible power-ups or checkpoint rings.
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(Collider2D))]
|
||||
public class AirplaneSpeedRing : MonoBehaviour
|
||||
{
|
||||
[Header("Boost Configuration")]
|
||||
[SerializeField] private float velocityMultiplier = 1.5f;
|
||||
[SerializeField] private float boostDuration = 1f;
|
||||
[SerializeField] private bool oneTimeUse = true;
|
||||
|
||||
[Header("Optional Constraints")]
|
||||
[SerializeField] private float minResultSpeed = 5f;
|
||||
[SerializeField] private float maxResultSpeed = 25f;
|
||||
|
||||
[Header("Visual Feedback")]
|
||||
[SerializeField] private GameObject ringVisual;
|
||||
[SerializeField] private ParticleSystem collectEffect;
|
||||
[SerializeField] private AudioSource collectSound;
|
||||
|
||||
[Header("Debug")]
|
||||
[SerializeField] private bool showDebugLogs;
|
||||
|
||||
private bool hasBeenUsed;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
// Ensure collider is trigger
|
||||
var collider = GetComponent<Collider2D>();
|
||||
if (collider != null)
|
||||
{
|
||||
collider.isTrigger = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTriggerEnter2D(Collider2D other)
|
||||
{
|
||||
// Check if already used
|
||||
if (oneTimeUse && hasBeenUsed) return;
|
||||
|
||||
// Check if it's an airplane
|
||||
var airplane = other.GetComponent<Core.AirplaneController>();
|
||||
if (airplane == null || !airplane.IsFlying) return;
|
||||
|
||||
var rb = other.GetComponent<Rigidbody2D>();
|
||||
if (rb == null) return;
|
||||
|
||||
// Apply speed boost
|
||||
Vector2 boostedVelocity = rb.linearVelocity * velocityMultiplier;
|
||||
|
||||
// Clamp to constraints
|
||||
float speed = boostedVelocity.magnitude;
|
||||
if (speed < minResultSpeed)
|
||||
{
|
||||
boostedVelocity = boostedVelocity.normalized * minResultSpeed;
|
||||
}
|
||||
else if (speed > maxResultSpeed)
|
||||
{
|
||||
boostedVelocity = boostedVelocity.normalized * maxResultSpeed;
|
||||
}
|
||||
|
||||
rb.linearVelocity = boostedVelocity;
|
||||
|
||||
// Mark as used
|
||||
hasBeenUsed = true;
|
||||
|
||||
// Trigger effects
|
||||
PlayCollectEffects();
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Debug.Log($"[AirplaneSpeedRing] Boosted {other.name}: velocity={boostedVelocity.magnitude:F1}");
|
||||
}
|
||||
|
||||
// Hide or destroy ring
|
||||
if (oneTimeUse)
|
||||
{
|
||||
if (ringVisual != null)
|
||||
{
|
||||
ringVisual.SetActive(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
Destroy(gameObject, collectEffect != null ? collectEffect.main.duration : 0.5f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void PlayCollectEffects()
|
||||
{
|
||||
// Play particle effect
|
||||
if (collectEffect != null)
|
||||
{
|
||||
collectEffect.Play();
|
||||
}
|
||||
|
||||
// Play sound
|
||||
if (collectSound != null)
|
||||
{
|
||||
collectSound.Play();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDrawGizmos()
|
||||
{
|
||||
// Visualize ring in editor
|
||||
Gizmos.color = hasBeenUsed ? Color.gray : Color.yellow;
|
||||
|
||||
var collider = GetComponent<Collider2D>();
|
||||
if (collider != null)
|
||||
{
|
||||
Gizmos.DrawWireSphere(collider.bounds.center, collider.bounds.extents.magnitude);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 30fadeee96664cf28e3e2e562c99db26
|
||||
timeCreated: 1765135387
|
||||
@@ -0,0 +1,107 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Minigames.Airplane.Interactive
|
||||
{
|
||||
/// <summary>
|
||||
/// Turbulence zone that applies random chaotic forces to airplanes.
|
||||
/// Creates unpredictable movement, adding challenge to navigation.
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(Collider2D))]
|
||||
public class AirplaneTurbulenceZone : MonoBehaviour
|
||||
{
|
||||
[Header("Turbulence Configuration")]
|
||||
[SerializeField] private float turbulenceStrength = 3f;
|
||||
[SerializeField] private float changeFrequency = 0.1f;
|
||||
|
||||
[Header("Optional Modifiers")]
|
||||
[SerializeField] private bool preventUpwardForce = false;
|
||||
[SerializeField] private float maxTotalForce = 10f;
|
||||
|
||||
[Header("Visual Feedback (Optional)")]
|
||||
[SerializeField] private ParticleSystem turbulenceParticles;
|
||||
|
||||
[Header("Debug")]
|
||||
[SerializeField] private bool showDebugLogs;
|
||||
|
||||
private float nextChangeTime;
|
||||
private Vector2 currentTurbulenceDirection;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
// Ensure collider is trigger
|
||||
var collider = GetComponent<Collider2D>();
|
||||
if (collider != null)
|
||||
{
|
||||
collider.isTrigger = true;
|
||||
}
|
||||
|
||||
// Initialize random direction
|
||||
UpdateTurbulenceDirection();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
// Change turbulence direction periodically
|
||||
if (Time.time >= nextChangeTime)
|
||||
{
|
||||
UpdateTurbulenceDirection();
|
||||
nextChangeTime = Time.time + changeFrequency;
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateTurbulenceDirection()
|
||||
{
|
||||
currentTurbulenceDirection = Random.insideUnitCircle.normalized;
|
||||
|
||||
// Prevent upward force if configured
|
||||
if (preventUpwardForce && currentTurbulenceDirection.y > 0)
|
||||
{
|
||||
currentTurbulenceDirection.y = -currentTurbulenceDirection.y;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTriggerStay2D(Collider2D other)
|
||||
{
|
||||
// Check if it's an airplane
|
||||
var airplane = other.GetComponent<Core.AirplaneController>();
|
||||
if (airplane == null || !airplane.IsFlying) return;
|
||||
|
||||
var rb = other.GetComponent<Rigidbody2D>();
|
||||
if (rb == null) return;
|
||||
|
||||
// Apply turbulence force
|
||||
Vector2 turbulenceForce = currentTurbulenceDirection * turbulenceStrength;
|
||||
|
||||
// Clamp total force
|
||||
if (turbulenceForce.magnitude > maxTotalForce)
|
||||
{
|
||||
turbulenceForce = turbulenceForce.normalized * maxTotalForce;
|
||||
}
|
||||
|
||||
rb.AddForce(turbulenceForce, ForceMode2D.Force);
|
||||
|
||||
if (showDebugLogs && Time.frameCount % 30 == 0) // Log every 30 frames to avoid spam
|
||||
{
|
||||
Debug.Log($"[AirplaneTurbulenceZone] Applied turbulence: {turbulenceForce} to {other.name}");
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDrawGizmos()
|
||||
{
|
||||
// Visualize turbulence zone in editor
|
||||
Gizmos.color = new Color(1f, 0.5f, 0f, 0.3f); // Orange transparent
|
||||
|
||||
var collider = GetComponent<Collider2D>();
|
||||
if (collider != null)
|
||||
{
|
||||
Gizmos.DrawCube(collider.bounds.center, collider.bounds.size);
|
||||
|
||||
// Draw current direction
|
||||
Gizmos.color = Color.red;
|
||||
Vector3 center = collider.bounds.center;
|
||||
Gizmos.DrawRay(center, (Vector3)currentTurbulenceDirection * 2f);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ae20630c0dc74174ae4d851d97d101c0
|
||||
timeCreated: 1765135403
|
||||
@@ -0,0 +1,70 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Minigames.Airplane.Interactive
|
||||
{
|
||||
/// <summary>
|
||||
/// Applies a constant force to airplanes passing through this zone.
|
||||
/// Can be used for updrafts, downdrafts, or crosswinds.
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(Collider2D))]
|
||||
public class AirplaneWindZone : MonoBehaviour
|
||||
{
|
||||
[Header("Wind Configuration")]
|
||||
[SerializeField] private Vector2 windForce = new Vector2(0, 5f);
|
||||
[SerializeField] private bool isWorldSpace = true;
|
||||
|
||||
[Header("Visual Feedback (Optional)")]
|
||||
[SerializeField] private ParticleSystem windParticles;
|
||||
|
||||
[Header("Debug")]
|
||||
[SerializeField] private bool showDebugLogs;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
// Ensure collider is trigger
|
||||
var collider = GetComponent<Collider2D>();
|
||||
if (collider != null)
|
||||
{
|
||||
collider.isTrigger = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTriggerStay2D(Collider2D other)
|
||||
{
|
||||
// Check if it's an airplane
|
||||
var airplane = other.GetComponent<Core.AirplaneController>();
|
||||
if (airplane == null || !airplane.IsFlying) return;
|
||||
|
||||
// Apply wind force
|
||||
var rb = other.GetComponent<Rigidbody2D>();
|
||||
if (rb != null)
|
||||
{
|
||||
Vector2 force = isWorldSpace ? windForce : transform.TransformDirection(windForce);
|
||||
rb.AddForce(force * Time.fixedDeltaTime, ForceMode2D.Force);
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Debug.Log($"[AirplaneWindZone] Applied force: {force} to {other.name}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDrawGizmos()
|
||||
{
|
||||
// Visualize wind direction in editor
|
||||
Gizmos.color = windForce.y > 0 ? Color.green : Color.red;
|
||||
|
||||
var collider = GetComponent<Collider2D>();
|
||||
if (collider != null)
|
||||
{
|
||||
Vector3 center = collider.bounds.center;
|
||||
Vector2 direction = isWorldSpace ? windForce : transform.TransformDirection(windForce);
|
||||
|
||||
// Draw arrow showing wind direction
|
||||
Gizmos.DrawRay(center, direction.normalized * 2f);
|
||||
Gizmos.DrawWireSphere(center + (Vector3)(direction.normalized * 2f), 0.3f);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: dc3242fd3fe042919496d71933a760a5
|
||||
timeCreated: 1765135354
|
||||
3
Assets/Scripts/Minigames/Airplane/Settings.meta
Normal file
3
Assets/Scripts/Minigames/Airplane/Settings.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 81b8f6aeeaf946cea5f5338a9127ae74
|
||||
timeCreated: 1764851415
|
||||
157
Assets/Scripts/Minigames/Airplane/Settings/AirplaneSettings.cs
Normal file
157
Assets/Scripts/Minigames/Airplane/Settings/AirplaneSettings.cs
Normal file
@@ -0,0 +1,157 @@
|
||||
using AppleHills.Core.Settings;
|
||||
using Common.Input;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Minigames.Airplane.Settings
|
||||
{
|
||||
/// <summary>
|
||||
/// Settings for the airplane minigame.
|
||||
/// Create via Assets > Create > AppleHills > Settings > Airplane
|
||||
/// </summary>
|
||||
[CreateAssetMenu(fileName = "AirplaneSettings", menuName = "AppleHills/Settings/Airplane", order = 9)]
|
||||
public class AirplaneSettings : BaseSettings, IAirplaneSettings
|
||||
{
|
||||
[Header("=== AIRPLANE TYPES ===")]
|
||||
|
||||
[Header("Jet Plane")]
|
||||
[SerializeField] private Data.AirplaneTypeConfig jetPlaneConfig = new Data.AirplaneTypeConfig
|
||||
{
|
||||
displayName = "Jet Plane",
|
||||
abilityType = Data.AirplaneAbilityType.Jet
|
||||
};
|
||||
|
||||
[SerializeField] private Data.JetAbilityConfig jetAbilityConfig = new Data.JetAbilityConfig();
|
||||
|
||||
[Header("Bobbing Plane")]
|
||||
[SerializeField] private Data.AirplaneTypeConfig bobbingPlaneConfig = new Data.AirplaneTypeConfig
|
||||
{
|
||||
displayName = "Bobbing Plane",
|
||||
abilityType = Data.AirplaneAbilityType.Bobbing
|
||||
};
|
||||
|
||||
[SerializeField] private Data.BobbingAbilityConfig bobbingAbilityConfig = new Data.BobbingAbilityConfig();
|
||||
|
||||
[Header("Drop Plane")]
|
||||
[SerializeField] private Data.AirplaneTypeConfig dropPlaneConfig = new Data.AirplaneTypeConfig
|
||||
{
|
||||
displayName = "Drop Plane",
|
||||
abilityType = Data.AirplaneAbilityType.Drop
|
||||
};
|
||||
|
||||
[SerializeField] private Data.DropAbilityConfig dropAbilityConfig = new Data.DropAbilityConfig();
|
||||
|
||||
[Header("Default Selection")]
|
||||
[Tooltip("Which airplane type is selected by default")]
|
||||
[SerializeField] private Data.AirplaneAbilityType defaultAirplaneType = Data.AirplaneAbilityType.Jet;
|
||||
|
||||
[Header("=== SLINGSHOT ===")]
|
||||
[SerializeField] private SlingshotConfig slingshotSettings = new SlingshotConfig
|
||||
{
|
||||
maxDragDistance = 5f,
|
||||
baseLaunchForce = 20f,
|
||||
minForceMultiplier = 0.1f,
|
||||
maxForceMultiplier = 1f,
|
||||
trajectoryPoints = 20,
|
||||
trajectoryTimeStep = 0.1f,
|
||||
trajectoryLockDuration = 0f, // No locking for airplane
|
||||
autoRegisterInput = true // Direct registration
|
||||
};
|
||||
|
||||
[Header("Flight Settings")]
|
||||
[Tooltip("Mass of the airplane")]
|
||||
[SerializeField] private float airplaneMass = 1f;
|
||||
|
||||
[Tooltip("Maximum flight time before timeout (seconds)")]
|
||||
[SerializeField] private float maxFlightTime = 10f;
|
||||
|
||||
[Header("Timing")]
|
||||
[Tooltip("Duration of intro sequence (seconds)")]
|
||||
[SerializeField] private float introDuration = 1f;
|
||||
|
||||
[Tooltip("Duration of person introduction (seconds)")]
|
||||
[SerializeField] private float personIntroDuration = 1f;
|
||||
|
||||
[Tooltip("Duration of result evaluation (seconds)")]
|
||||
[SerializeField] private float evaluationDuration = 1f;
|
||||
|
||||
[Header("Spawn System")]
|
||||
[Tooltip("X position where dynamic spawning begins")]
|
||||
[SerializeField] private float dynamicSpawnThreshold = 10f;
|
||||
|
||||
[Tooltip("Minimum random distance for target spawn")]
|
||||
[SerializeField] private float targetMinDistance = 30f;
|
||||
|
||||
[Tooltip("Maximum random distance for target spawn")]
|
||||
[SerializeField] private float targetMaxDistance = 50f;
|
||||
|
||||
[Tooltip("Minimum time interval between object spawns (seconds)")]
|
||||
[SerializeField] private float objectSpawnMinInterval = 1f;
|
||||
|
||||
[Tooltip("Maximum time interval between object spawns (seconds)")]
|
||||
[SerializeField] private float objectSpawnMaxInterval = 3f;
|
||||
|
||||
[Tooltip("Ratio of positive to negative objects (0 = all negative, 1 = all positive)")]
|
||||
[Range(0f, 1f)]
|
||||
[SerializeField] private float positiveNegativeRatio = 0.5f;
|
||||
|
||||
[Tooltip("Distance ahead of plane to spawn objects")]
|
||||
[SerializeField] private float spawnDistanceAhead = 15f;
|
||||
|
||||
[Tooltip("Distance interval for ground tile spawning")]
|
||||
[SerializeField] private float groundSpawnInterval = 5f;
|
||||
|
||||
[Header("Ground Snapping")]
|
||||
[Tooltip("Layer for ground detection (objects will snap to this)")]
|
||||
[Layer]
|
||||
[SerializeField] private int groundLayer = 0; // Default layer
|
||||
|
||||
[Tooltip("Maximum distance to raycast for ground")]
|
||||
[SerializeField] private float maxGroundRaycastDistance = 50f;
|
||||
|
||||
[Tooltip("Default Y offset for objects if no ground found")]
|
||||
[SerializeField] private float defaultObjectYOffset = 0f;
|
||||
|
||||
[Header("Debug")]
|
||||
[Tooltip("Show debug logs in console")]
|
||||
[SerializeField] private bool showDebugLogs;
|
||||
|
||||
#region IAirplaneSettings Implementation
|
||||
|
||||
public Data.AirplaneTypeConfig GetAirplaneConfig(Data.AirplaneAbilityType type)
|
||||
{
|
||||
return type switch
|
||||
{
|
||||
Data.AirplaneAbilityType.Jet => jetPlaneConfig,
|
||||
Data.AirplaneAbilityType.Bobbing => bobbingPlaneConfig,
|
||||
Data.AirplaneAbilityType.Drop => dropPlaneConfig,
|
||||
_ => jetPlaneConfig // Fallback to jet
|
||||
};
|
||||
}
|
||||
|
||||
public Data.JetAbilityConfig JetAbilityConfig => jetAbilityConfig;
|
||||
public Data.BobbingAbilityConfig BobbingAbilityConfig => bobbingAbilityConfig;
|
||||
public Data.DropAbilityConfig DropAbilityConfig => dropAbilityConfig;
|
||||
public Data.AirplaneAbilityType DefaultAirplaneType => defaultAirplaneType;
|
||||
public SlingshotConfig SlingshotSettings => slingshotSettings;
|
||||
public float AirplaneMass => airplaneMass;
|
||||
public float MaxFlightTime => maxFlightTime;
|
||||
public float IntroDuration => introDuration;
|
||||
public float PersonIntroDuration => personIntroDuration;
|
||||
public float EvaluationDuration => evaluationDuration;
|
||||
public float DynamicSpawnThreshold => dynamicSpawnThreshold;
|
||||
public float TargetMinDistance => targetMinDistance;
|
||||
public float TargetMaxDistance => targetMaxDistance;
|
||||
public float ObjectSpawnMinInterval => objectSpawnMinInterval;
|
||||
public float ObjectSpawnMaxInterval => objectSpawnMaxInterval;
|
||||
public float PositiveNegativeRatio => positiveNegativeRatio;
|
||||
public float SpawnDistanceAhead => spawnDistanceAhead;
|
||||
public float GroundSpawnInterval => groundSpawnInterval;
|
||||
public int GroundLayer => groundLayer;
|
||||
public float MaxGroundRaycastDistance => maxGroundRaycastDistance;
|
||||
public float DefaultObjectYOffset => defaultObjectYOffset;
|
||||
public bool ShowDebugLogs => showDebugLogs;
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1c277e2fec3d42e2b3b0bed1b8a33beb
|
||||
timeCreated: 1764851415
|
||||
3
Assets/Scripts/Minigames/Airplane/Targets.meta
Normal file
3
Assets/Scripts/Minigames/Airplane/Targets.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bef822469ac14cedad520c7d8f01562a
|
||||
timeCreated: 1764851291
|
||||
115
Assets/Scripts/Minigames/Airplane/Targets/AirplaneTarget.cs
Normal file
115
Assets/Scripts/Minigames/Airplane/Targets/AirplaneTarget.cs
Normal file
@@ -0,0 +1,115 @@
|
||||
using System;
|
||||
using Core;
|
||||
using Core.Lifecycle;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Minigames.Airplane.Targets
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a target in the airplane minigame.
|
||||
/// Detects airplane collisions.
|
||||
///
|
||||
/// NOTE: Active/inactive highlighting is deprecated - targets now spawn dynamically per person,
|
||||
/// so only one target exists in the scene at a time (no need for highlighting multiple targets).
|
||||
/// The SetAsActiveTarget() method is kept for backward compatibility but is no longer used.
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(Collider2D))]
|
||||
public class AirplaneTarget : ManagedBehaviour
|
||||
{
|
||||
#region Events
|
||||
|
||||
/// <summary>
|
||||
/// Fired when this target is hit. Parameters: (AirplaneTarget target, GameObject airplane)
|
||||
/// </summary>
|
||||
public event Action<AirplaneTarget, GameObject> OnTargetHit;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Inspector Properties
|
||||
|
||||
[Header("Target Configuration")]
|
||||
[Tooltip("Name of this target (for validation)")]
|
||||
[SerializeField] private string targetName = "Target";
|
||||
|
||||
[Header("Visual Feedback")]
|
||||
[Tooltip("Sprite renderer for visual feedback (optional)")]
|
||||
[SerializeField] private SpriteRenderer spriteRenderer;
|
||||
|
||||
[Tooltip("Color when target is active")]
|
||||
[SerializeField] private Color activeColor = Color.yellow;
|
||||
|
||||
[Tooltip("Color when target is inactive")]
|
||||
[SerializeField] private Color inactiveColor = Color.white;
|
||||
|
||||
[Header("Debug")]
|
||||
[SerializeField] private bool showDebugLogs = false;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
|
||||
public string TargetName => targetName;
|
||||
|
||||
private bool _isActive = false;
|
||||
public bool IsActive => _isActive;
|
||||
|
||||
#endregion
|
||||
|
||||
#region State
|
||||
|
||||
private Collider2D _targetCollider;
|
||||
private Color _originalColor;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Lifecycle
|
||||
|
||||
internal override void OnManagedAwake()
|
||||
{
|
||||
base.OnManagedAwake();
|
||||
|
||||
// Cache components
|
||||
_targetCollider = GetComponent<Collider2D>();
|
||||
|
||||
// Configure collider as trigger
|
||||
if (_targetCollider != null)
|
||||
{
|
||||
_targetCollider.isTrigger = true;
|
||||
}
|
||||
|
||||
// Cache sprite renderer if not assigned
|
||||
if (spriteRenderer == null)
|
||||
{
|
||||
spriteRenderer = GetComponent<SpriteRenderer>();
|
||||
}
|
||||
|
||||
// Store original color
|
||||
if (spriteRenderer != null)
|
||||
{
|
||||
_originalColor = spriteRenderer.color;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Collision Detection
|
||||
|
||||
/// <summary>
|
||||
/// Detect when airplane enters trigger
|
||||
/// </summary>
|
||||
private void OnTriggerEnter2D(Collider2D other)
|
||||
{
|
||||
// Check if it's an airplane
|
||||
var airplane = other.GetComponent<Core.AirplaneController>();
|
||||
if (airplane != null)
|
||||
{
|
||||
if (showDebugLogs) Logging.Debug($"[AirplaneTarget] {targetName} hit by airplane: {other.gameObject.name}");
|
||||
|
||||
OnTargetHit?.Invoke(this, other.gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 53e3dae13bb14c109a038bb5a84bd941
|
||||
timeCreated: 1764851291
|
||||
3
Assets/Scripts/Minigames/Airplane/UI.meta
Normal file
3
Assets/Scripts/Minigames/Airplane/UI.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a675ac5f4ade4a0c935da4fd378935f2
|
||||
timeCreated: 1764943474
|
||||
300
Assets/Scripts/Minigames/Airplane/UI/AirplaneAbilityButton.cs
Normal file
300
Assets/Scripts/Minigames/Airplane/UI/AirplaneAbilityButton.cs
Normal file
@@ -0,0 +1,300 @@
|
||||
using Core;
|
||||
using Input;
|
||||
using Minigames.Airplane.Abilities;
|
||||
using Minigames.Airplane.Core;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Minigames.Airplane.UI
|
||||
{
|
||||
/// <summary>
|
||||
/// UI button for activating airplane special abilities.
|
||||
/// Handles input, visual feedback, and cooldown display.
|
||||
/// Implements ITouchInputConsumer to properly handle hold/release for Jet ability.
|
||||
/// </summary>
|
||||
public class AirplaneAbilityButton : MonoBehaviour, ITouchInputConsumer
|
||||
{
|
||||
#region Inspector References
|
||||
|
||||
[Header("UI Components")]
|
||||
[SerializeField] private Button button;
|
||||
[SerializeField] private Image abilityIcon;
|
||||
[SerializeField] private Image cooldownFill;
|
||||
[SerializeField] private TextMeshProUGUI cooldownText;
|
||||
|
||||
[Header("Debug")]
|
||||
[SerializeField] private bool showDebugLogs;
|
||||
|
||||
#endregion
|
||||
|
||||
#region State
|
||||
|
||||
private BaseAirplaneAbility currentAbility;
|
||||
private AirplaneController currentAirplane;
|
||||
private bool isHoldAbility; // Jet plane needs hold mechanic
|
||||
private bool isHolding; // Track if button is currently being held
|
||||
|
||||
#endregion
|
||||
|
||||
#region Lifecycle
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (button != null)
|
||||
{
|
||||
button.onClick.AddListener(OnButtonClick);
|
||||
}
|
||||
|
||||
// Hide by default
|
||||
gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (currentAbility == null) return;
|
||||
|
||||
// Update cooldown display
|
||||
if (currentAbility.IsOnCooldown)
|
||||
{
|
||||
// Fill starts at 1 and reduces to 0 over cooldown duration
|
||||
float fillAmount = currentAbility.CooldownRemaining / currentAbility.CooldownDuration;
|
||||
if (cooldownFill != null)
|
||||
{
|
||||
cooldownFill.fillAmount = fillAmount;
|
||||
}
|
||||
|
||||
// Show timer text
|
||||
if (cooldownText != null)
|
||||
{
|
||||
cooldownText.text = $"{currentAbility.CooldownRemaining:F1}s";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Cooldown complete - fill at 0, no text
|
||||
if (cooldownFill != null)
|
||||
cooldownFill.fillAmount = 0f;
|
||||
|
||||
if (cooldownText != null)
|
||||
cooldownText.text = "";
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
// Unsubscribe from events
|
||||
if (currentAbility != null)
|
||||
{
|
||||
currentAbility.OnAbilityActivated -= HandleAbilityActivated;
|
||||
currentAbility.OnAbilityDeactivated -= HandleAbilityDeactivated;
|
||||
currentAbility.OnCooldownChanged -= HandleCooldownChanged;
|
||||
}
|
||||
|
||||
// Unregister from input system
|
||||
if (InputManager.Instance != null)
|
||||
{
|
||||
InputManager.Instance.UnregisterOverrideConsumer(this);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public API
|
||||
|
||||
/// <summary>
|
||||
/// Setup button with airplane and ability reference.
|
||||
/// </summary>
|
||||
public void Setup(AirplaneController airplane, BaseAirplaneAbility ability)
|
||||
{
|
||||
currentAirplane = airplane;
|
||||
currentAbility = ability;
|
||||
isHolding = false;
|
||||
|
||||
// Set icon and show immediately
|
||||
if (abilityIcon != null && ability != null)
|
||||
{
|
||||
abilityIcon.sprite = ability.AbilityIcon;
|
||||
abilityIcon.enabled = true;
|
||||
}
|
||||
|
||||
// Initialize cooldown display
|
||||
if (cooldownFill != null)
|
||||
{
|
||||
cooldownFill.fillAmount = 0f;
|
||||
}
|
||||
|
||||
if (cooldownText != null)
|
||||
{
|
||||
cooldownText.text = "";
|
||||
}
|
||||
|
||||
// Check if this is a hold ability (Jet)
|
||||
isHoldAbility = ability is JetAbility;
|
||||
|
||||
// Subscribe to ability events
|
||||
if (ability != null)
|
||||
{
|
||||
ability.OnAbilityActivated += HandleAbilityActivated;
|
||||
ability.OnAbilityDeactivated += HandleAbilityDeactivated;
|
||||
ability.OnCooldownChanged += HandleCooldownChanged;
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[AirplaneAbilityButton] Subscribed to ability events for: {ability.AbilityName}");
|
||||
}
|
||||
}
|
||||
|
||||
// Show UI
|
||||
gameObject.SetActive(true);
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[AirplaneAbilityButton] Setup complete with ability: {ability?.AbilityName ?? "None"}, Hold: {isHoldAbility}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Hide and cleanup button.
|
||||
/// </summary>
|
||||
public void Hide()
|
||||
{
|
||||
if (currentAbility != null)
|
||||
{
|
||||
currentAbility.OnAbilityActivated -= HandleAbilityActivated;
|
||||
currentAbility.OnAbilityDeactivated -= HandleAbilityDeactivated;
|
||||
currentAbility.OnCooldownChanged -= HandleCooldownChanged;
|
||||
}
|
||||
|
||||
// Unregister from input system
|
||||
if (InputManager.Instance != null)
|
||||
{
|
||||
InputManager.Instance.UnregisterOverrideConsumer(this);
|
||||
}
|
||||
|
||||
currentAbility = null;
|
||||
currentAirplane = null;
|
||||
isHolding = false;
|
||||
gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Input Handling
|
||||
|
||||
private void OnButtonClick()
|
||||
{
|
||||
if (currentAirplane == null || currentAbility == null) return;
|
||||
if (!currentAbility.CanActivate) return;
|
||||
|
||||
// Activate ability
|
||||
currentAirplane.ActivateAbility();
|
||||
|
||||
// For hold abilities (Jet), mark as holding and register for input
|
||||
if (isHoldAbility)
|
||||
{
|
||||
isHolding = true;
|
||||
|
||||
// Register as override consumer to receive hold/release events
|
||||
if (InputManager.Instance != null)
|
||||
{
|
||||
InputManager.Instance.RegisterOverrideConsumer(this);
|
||||
}
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug("[AirplaneAbilityButton] Started holding ability, registered for input");
|
||||
}
|
||||
}
|
||||
|
||||
// For non-hold abilities (Bobbing, Drop), this is all we need
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Event Handlers
|
||||
|
||||
private void HandleAbilityActivated(BaseAirplaneAbility ability)
|
||||
{
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[AirplaneAbilityButton] Ability activated: {ability.AbilityName}");
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleAbilityDeactivated(BaseAirplaneAbility ability)
|
||||
{
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[AirplaneAbilityButton] Ability deactivated: {ability.AbilityName}");
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleCooldownChanged(float remaining, float total)
|
||||
{
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[AirplaneAbilityButton] OnCooldownChanged: remaining={remaining:F2}, total={total:F2}");
|
||||
}
|
||||
|
||||
// When cooldown starts (remaining == total), set fill to 1
|
||||
if (remaining >= total - 0.01f && cooldownFill != null)
|
||||
{
|
||||
cooldownFill.fillAmount = 1f;
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[AirplaneAbilityButton] Cooldown started: {total}s, fill set to 1");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region ITouchInputConsumer Implementation
|
||||
|
||||
public void OnTap(Vector2 position)
|
||||
{
|
||||
// If Jet ability is active (holding), next tap anywhere deactivates it
|
||||
if (isHoldAbility && isHolding)
|
||||
{
|
||||
isHolding = false;
|
||||
currentAirplane?.DeactivateAbility();
|
||||
|
||||
// Unregister from input system after tap
|
||||
if (InputManager.Instance != null)
|
||||
{
|
||||
InputManager.Instance.UnregisterOverrideConsumer(this);
|
||||
}
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug("[AirplaneAbilityButton] Tap detected - deactivated Jet ability, unregistered");
|
||||
}
|
||||
}
|
||||
// Handle as button click for non-hold abilities
|
||||
else if (!isHoldAbility)
|
||||
{
|
||||
OnButtonClick();
|
||||
}
|
||||
}
|
||||
|
||||
public void OnHoldStart(Vector2 position)
|
||||
{
|
||||
// Not used - button click handles activation, tap handles deactivation
|
||||
}
|
||||
|
||||
public void OnHoldMove(Vector2 position)
|
||||
{
|
||||
// Not used
|
||||
}
|
||||
|
||||
public void OnHoldEnd(Vector2 position)
|
||||
{
|
||||
// Not used - tap handles deactivation for Jet ability
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 44f826b6d40c47c0b5a9985f7f793278
|
||||
timeCreated: 1764976132
|
||||
@@ -0,0 +1,38 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Minigames.Airplane.UI
|
||||
{
|
||||
/// <summary>
|
||||
/// Component for individual airplane selection buttons.
|
||||
/// Handles visual highlight feedback via show/hide of a highlight image.
|
||||
/// </summary>
|
||||
public class AirplaneSelectionButton : MonoBehaviour
|
||||
{
|
||||
[Header("Highlight Visual")]
|
||||
[SerializeField] private Image highlightImage;
|
||||
|
||||
/// <summary>
|
||||
/// Show the highlight visual.
|
||||
/// </summary>
|
||||
public void HighlightStart()
|
||||
{
|
||||
if (highlightImage != null)
|
||||
{
|
||||
highlightImage.gameObject.SetActive(true);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Hide the highlight visual.
|
||||
/// </summary>
|
||||
public void HighlightEnd()
|
||||
{
|
||||
if (highlightImage != null)
|
||||
{
|
||||
highlightImage.gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4ccf530e55324aec8dc6e09eb827f123
|
||||
timeCreated: 1765132601
|
||||
297
Assets/Scripts/Minigames/Airplane/UI/AirplaneSelectionUI.cs
Normal file
297
Assets/Scripts/Minigames/Airplane/UI/AirplaneSelectionUI.cs
Normal file
@@ -0,0 +1,297 @@
|
||||
using System;
|
||||
using Core;
|
||||
using Minigames.Airplane.Data;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Minigames.Airplane.UI
|
||||
{
|
||||
/// <summary>
|
||||
/// UI for selecting airplane type before game starts.
|
||||
/// Displays buttons for each available airplane type.
|
||||
/// </summary>
|
||||
public class AirplaneSelectionUI : MonoBehaviour
|
||||
{
|
||||
#region Inspector References
|
||||
|
||||
[Header("UI References")]
|
||||
[SerializeField] private Button jetPlaneButton;
|
||||
[SerializeField] private Button bobbingPlaneButton;
|
||||
[SerializeField] private Button dropPlaneButton;
|
||||
[SerializeField] private Button confirmButton;
|
||||
|
||||
[Header("Debug")]
|
||||
[SerializeField] private bool showDebugLogs;
|
||||
|
||||
#endregion
|
||||
|
||||
#region State
|
||||
|
||||
private AirplaneAbilityType selectedType;
|
||||
private AirplaneSelectionButton selectedButtonComponent;
|
||||
private bool hasConfirmed;
|
||||
|
||||
public bool HasSelectedType => hasConfirmed;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Events
|
||||
|
||||
public event Action<AirplaneAbilityType> OnTypeSelected;
|
||||
public event Action<AirplaneAbilityType> OnConfirmed;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Lifecycle
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
// Setup button listeners
|
||||
if (jetPlaneButton != null)
|
||||
jetPlaneButton.onClick.AddListener(() => SelectType(AirplaneAbilityType.Jet, jetPlaneButton));
|
||||
|
||||
if (bobbingPlaneButton != null)
|
||||
bobbingPlaneButton.onClick.AddListener(() => SelectType(AirplaneAbilityType.Bobbing, bobbingPlaneButton));
|
||||
|
||||
if (dropPlaneButton != null)
|
||||
dropPlaneButton.onClick.AddListener(() => SelectType(AirplaneAbilityType.Drop, dropPlaneButton));
|
||||
|
||||
if (confirmButton != null)
|
||||
{
|
||||
confirmButton.onClick.AddListener(ConfirmSelection);
|
||||
confirmButton.interactable = false; // Disabled until selection made
|
||||
}
|
||||
|
||||
// Hide by default (deactivate container child, not root)
|
||||
if (transform.childCount > 0)
|
||||
{
|
||||
Transform container = transform.GetChild(0);
|
||||
container.gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public API
|
||||
|
||||
/// <summary>
|
||||
/// Show the selection UI.
|
||||
/// Activates the immediate child container.
|
||||
/// Script should be on Root, with UI elements under a Container child.
|
||||
/// </summary>
|
||||
public void Show()
|
||||
{
|
||||
selectedType = AirplaneAbilityType.None;
|
||||
selectedButtonComponent = null;
|
||||
hasConfirmed = false;
|
||||
|
||||
if (confirmButton != null)
|
||||
confirmButton.interactable = false;
|
||||
|
||||
// Reset all button highlights
|
||||
ResetButtonHighlights();
|
||||
|
||||
// Populate icons from settings
|
||||
PopulateButtonIcons();
|
||||
|
||||
// Activate the container (immediate child)
|
||||
if (transform.childCount > 0)
|
||||
{
|
||||
Transform container = transform.GetChild(0);
|
||||
container.gameObject.SetActive(true);
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[AirplaneSelectionUI] Shown. Activated container: {container.name}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Logging.Error("[AirplaneSelectionUI] No child container found! Expected structure: Root(script)->Container->UI Elements");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Hide the selection UI.
|
||||
/// Deactivates the immediate child container.
|
||||
/// </summary>
|
||||
public void Hide()
|
||||
{
|
||||
// Deactivate the container (immediate child)
|
||||
if (transform.childCount > 0)
|
||||
{
|
||||
Transform container = transform.GetChild(0);
|
||||
container.gameObject.SetActive(false);
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[AirplaneSelectionUI] Hidden. Deactivated container: {container.name}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the selected airplane type.
|
||||
/// </summary>
|
||||
public AirplaneAbilityType GetSelectedType()
|
||||
{
|
||||
return selectedType;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
|
||||
private void SelectType(AirplaneAbilityType type, Button button)
|
||||
{
|
||||
if (type == AirplaneAbilityType.None)
|
||||
{
|
||||
Logging.Warning("[AirplaneSelectionUI] Attempted to select None type!");
|
||||
return;
|
||||
}
|
||||
|
||||
selectedType = type;
|
||||
|
||||
// Get the AirplaneSelectionButton component (on same GameObject as Button)
|
||||
var buttonComponent = button.GetComponent<AirplaneSelectionButton>();
|
||||
if (buttonComponent == null)
|
||||
{
|
||||
Logging.Warning($"[AirplaneSelectionUI] Button {button.name} is missing AirplaneSelectionButton component!");
|
||||
return;
|
||||
}
|
||||
|
||||
selectedButtonComponent = buttonComponent;
|
||||
|
||||
// Update visual feedback
|
||||
ResetButtonHighlights();
|
||||
HighlightButton(buttonComponent);
|
||||
|
||||
// Enable confirm button
|
||||
if (confirmButton != null)
|
||||
confirmButton.interactable = true;
|
||||
|
||||
// Fire event
|
||||
OnTypeSelected?.Invoke(type);
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[AirplaneSelectionUI] Selected type: {type}");
|
||||
}
|
||||
}
|
||||
|
||||
private void ConfirmSelection()
|
||||
{
|
||||
if (selectedType == AirplaneAbilityType.None)
|
||||
{
|
||||
Logging.Warning("[AirplaneSelectionUI] Cannot confirm - no type selected!");
|
||||
return;
|
||||
}
|
||||
|
||||
hasConfirmed = true;
|
||||
|
||||
// Fire event
|
||||
OnConfirmed?.Invoke(selectedType);
|
||||
|
||||
// Hide UI
|
||||
Hide();
|
||||
}
|
||||
|
||||
private void ResetButtonHighlights()
|
||||
{
|
||||
// End highlight on all buttons
|
||||
if (jetPlaneButton != null)
|
||||
{
|
||||
var component = jetPlaneButton.GetComponent<AirplaneSelectionButton>();
|
||||
if (component != null) component.HighlightEnd();
|
||||
}
|
||||
|
||||
if (bobbingPlaneButton != null)
|
||||
{
|
||||
var component = bobbingPlaneButton.GetComponent<AirplaneSelectionButton>();
|
||||
if (component != null) component.HighlightEnd();
|
||||
}
|
||||
|
||||
if (dropPlaneButton != null)
|
||||
{
|
||||
var component = dropPlaneButton.GetComponent<AirplaneSelectionButton>();
|
||||
if (component != null) component.HighlightEnd();
|
||||
}
|
||||
}
|
||||
|
||||
private void HighlightButton(AirplaneSelectionButton buttonComponent)
|
||||
{
|
||||
if (buttonComponent != null)
|
||||
{
|
||||
buttonComponent.HighlightStart();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Populate button icons from airplane settings.
|
||||
/// Assumes Image component is on the same GameObject as the Button.
|
||||
/// </summary>
|
||||
private void PopulateButtonIcons()
|
||||
{
|
||||
// Get airplane settings
|
||||
var settings = GameManager.GetSettingsObject<AppleHills.Core.Settings.IAirplaneSettings>();
|
||||
if (settings == null)
|
||||
{
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Warning("[AirplaneSelectionUI] Could not load airplane settings for icons");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Populate Jet button icon
|
||||
if (jetPlaneButton != null)
|
||||
{
|
||||
var jetConfig = settings.GetAirplaneConfig(AirplaneAbilityType.Jet);
|
||||
if (jetConfig != null && jetConfig.previewSprite != null)
|
||||
{
|
||||
var image = jetPlaneButton.GetComponent<Image>();
|
||||
if (image != null)
|
||||
{
|
||||
image.sprite = jetConfig.previewSprite;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Populate Bobbing button icon
|
||||
if (bobbingPlaneButton != null)
|
||||
{
|
||||
var bobbingConfig = settings.GetAirplaneConfig(AirplaneAbilityType.Bobbing);
|
||||
if (bobbingConfig != null && bobbingConfig.previewSprite != null)
|
||||
{
|
||||
var image = bobbingPlaneButton.GetComponent<Image>();
|
||||
if (image != null)
|
||||
{
|
||||
image.sprite = bobbingConfig.previewSprite;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Populate Drop button icon
|
||||
if (dropPlaneButton != null)
|
||||
{
|
||||
var dropConfig = settings.GetAirplaneConfig(AirplaneAbilityType.Drop);
|
||||
if (dropConfig != null && dropConfig.previewSprite != null)
|
||||
{
|
||||
var image = dropPlaneButton.GetComponent<Image>();
|
||||
if (image != null)
|
||||
{
|
||||
image.sprite = dropConfig.previewSprite;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug("[AirplaneSelectionUI] Populated airplane icons from settings");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6463ce42d43142878816170f53a0f5bd
|
||||
timeCreated: 1764976150
|
||||
230
Assets/Scripts/Minigames/Airplane/UI/TargetDisplayUI.cs
Normal file
230
Assets/Scripts/Minigames/Airplane/UI/TargetDisplayUI.cs
Normal file
@@ -0,0 +1,230 @@
|
||||
using Core;
|
||||
using Core.Lifecycle;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Minigames.Airplane.UI
|
||||
{
|
||||
/// <summary>
|
||||
/// Displays target information: icon and distance remaining to target.
|
||||
/// Updates in real-time as the airplane moves.
|
||||
/// </summary>
|
||||
public class TargetDisplayUI : ManagedBehaviour
|
||||
{
|
||||
#region Inspector References
|
||||
|
||||
[Header("UI Elements")]
|
||||
[Tooltip("Image to display target icon")]
|
||||
[SerializeField] private Image targetIcon;
|
||||
|
||||
[Tooltip("Text to display distance remaining")]
|
||||
[SerializeField] private TextMeshProUGUI distanceText;
|
||||
|
||||
[Header("Display Settings")]
|
||||
[Tooltip("Format string for distance display (e.g., '{0:F1}m')")]
|
||||
[SerializeField] private string distanceFormat = "{0:F1}m";
|
||||
|
||||
[Tooltip("Update distance every N frames (0 = every frame)")]
|
||||
[SerializeField] private int updateInterval = 5;
|
||||
|
||||
[Header("Debug")]
|
||||
[SerializeField] private bool showDebugLogs;
|
||||
|
||||
#endregion
|
||||
|
||||
#region State
|
||||
|
||||
private Transform _planeTransform;
|
||||
private Transform _launchPointTransform;
|
||||
private Vector3 _targetPosition;
|
||||
private bool _isActive;
|
||||
private int _frameCounter;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Lifecycle
|
||||
|
||||
internal override void OnManagedAwake()
|
||||
{
|
||||
base.OnManagedAwake();
|
||||
|
||||
// Hide by default
|
||||
Hide();
|
||||
|
||||
// Validate references
|
||||
if (targetIcon == null)
|
||||
{
|
||||
Logging.Warning("[TargetDisplayUI] Target icon image not assigned!");
|
||||
}
|
||||
|
||||
if (distanceText == null)
|
||||
{
|
||||
Logging.Warning("[TargetDisplayUI] Distance text not assigned!");
|
||||
}
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
// Only update if active and we have at least one transform to calculate from
|
||||
if (!_isActive) return;
|
||||
if (_planeTransform == null && _launchPointTransform == null) return;
|
||||
|
||||
// Update distance at specified interval
|
||||
_frameCounter++;
|
||||
if (updateInterval == 0 || _frameCounter >= updateInterval)
|
||||
{
|
||||
_frameCounter = 0;
|
||||
UpdateDistance();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public API
|
||||
|
||||
/// <summary>
|
||||
/// Setup the target display with icon and target position.
|
||||
/// Activates tracking using launch point for distance calculation.
|
||||
/// </summary>
|
||||
/// <param name="targetSprite">Sprite to display as target icon</param>
|
||||
/// <param name="targetPosition">World position of the target</param>
|
||||
/// <param name="launchPoint">Launch point transform (used for distance when plane not available)</param>
|
||||
public void Setup(Sprite targetSprite, Vector3 targetPosition, Transform launchPoint)
|
||||
{
|
||||
_targetPosition = targetPosition;
|
||||
_launchPointTransform = launchPoint;
|
||||
|
||||
// Set icon
|
||||
if (targetIcon != null && targetSprite != null)
|
||||
{
|
||||
targetIcon.sprite = targetSprite;
|
||||
targetIcon.enabled = true;
|
||||
}
|
||||
|
||||
// Activate tracking so distance updates even before plane spawns
|
||||
_isActive = true;
|
||||
_frameCounter = 0;
|
||||
|
||||
// Update distance immediately using launch point
|
||||
UpdateDistance();
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[TargetDisplayUI] Setup with target at {targetPosition}, launch point at {launchPoint?.position ?? Vector3.zero}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Start tracking the airplane and updating distance.
|
||||
/// Switches distance calculation from launch point to airplane position.
|
||||
/// Note: Does not automatically show UI - call Show() separately.
|
||||
/// </summary>
|
||||
/// <param name="planeTransform">Transform of the airplane to track</param>
|
||||
public void StartTracking(Transform planeTransform)
|
||||
{
|
||||
_planeTransform = planeTransform;
|
||||
_isActive = true;
|
||||
_frameCounter = 0;
|
||||
|
||||
// Update distance immediately if visible (now using plane position)
|
||||
if (gameObject.activeSelf)
|
||||
{
|
||||
UpdateDistance();
|
||||
}
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug("[TargetDisplayUI] Started tracking airplane");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stop tracking the airplane.
|
||||
/// Reverts to using launch point for distance calculation if available.
|
||||
/// Note: Does not automatically hide UI - call Hide() separately.
|
||||
/// </summary>
|
||||
public void StopTracking()
|
||||
{
|
||||
_planeTransform = null;
|
||||
// Keep _isActive true so we can show distance from launch point
|
||||
// Will be set false when Hide() is called
|
||||
|
||||
// Update immediately to show launch point distance again
|
||||
if (_launchPointTransform != null && gameObject.activeSelf)
|
||||
{
|
||||
UpdateDistance();
|
||||
}
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug("[TargetDisplayUI] Stopped tracking airplane, reverted to launch point");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Show the UI.
|
||||
/// </summary>
|
||||
public void Show()
|
||||
{
|
||||
gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Hide the UI and deactivate tracking.
|
||||
/// </summary>
|
||||
public void Hide()
|
||||
{
|
||||
_isActive = false;
|
||||
gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Internal
|
||||
|
||||
/// <summary>
|
||||
/// Update the distance text based on current plane position.
|
||||
/// Uses launch point if plane isn't available yet.
|
||||
/// </summary>
|
||||
private void UpdateDistance()
|
||||
{
|
||||
if (distanceText == null) return;
|
||||
|
||||
// Use plane position if available, otherwise use launch point
|
||||
Vector3 currentPosition;
|
||||
if (_planeTransform != null)
|
||||
{
|
||||
currentPosition = _planeTransform.position;
|
||||
}
|
||||
else if (_launchPointTransform != null)
|
||||
{
|
||||
currentPosition = _launchPointTransform.position;
|
||||
}
|
||||
else
|
||||
{
|
||||
// No reference available
|
||||
return;
|
||||
}
|
||||
|
||||
// Calculate horizontal distance (X-axis only for side-scroller)
|
||||
float distance = Mathf.Abs(_targetPosition.x - currentPosition.x);
|
||||
|
||||
// Update text
|
||||
distanceText.text = string.Format(distanceFormat, distance);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update distance and ensure UI is shown.
|
||||
/// Call when showing UI to refresh distance display.
|
||||
/// </summary>
|
||||
public void UpdateAndShow()
|
||||
{
|
||||
UpdateDistance();
|
||||
Show();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6aadeed064b648a78ec13b9a76d2853b
|
||||
timeCreated: 1764943474
|
||||
Reference in New Issue
Block a user