Stash work
This commit is contained in:
committed by
Michal Pikulski
parent
ab579e2d21
commit
7ce6d914e6
@@ -368,6 +368,11 @@ namespace Common.Input
|
||||
trajectoryPreview?.Hide();
|
||||
}
|
||||
|
||||
public Transform GetLaunchAnchorTransform()
|
||||
{
|
||||
return launchAnchor;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Abstract Methods - Physics Configuration
|
||||
|
||||
@@ -289,15 +289,26 @@ namespace AppleHills.Core.Settings
|
||||
float AirplaneMass { get; }
|
||||
float MaxFlightTime { get; }
|
||||
|
||||
// Camera Settings
|
||||
float CameraFollowSmoothing { get; }
|
||||
float FlightCameraZoom { get; }
|
||||
|
||||
// Timing
|
||||
float IntroDuration { get; }
|
||||
float PersonIntroDuration { get; }
|
||||
float EvaluationDuration { get; }
|
||||
|
||||
// Spawn System
|
||||
float DynamicSpawnThreshold { get; }
|
||||
float TargetMinDistance { get; }
|
||||
float TargetMaxDistance { get; }
|
||||
float ObjectSpawnMinInterval { get; }
|
||||
float ObjectSpawnMaxInterval { get; }
|
||||
float PositiveNegativeRatio { get; } // 0-1, where 1 = all positive, 0 = all negative
|
||||
float SpawnDistanceAhead { get; }
|
||||
float GroundSpawnInterval { get; }
|
||||
|
||||
// Ground Snapping
|
||||
int GroundLayer { get; }
|
||||
float MaxGroundRaycastDistance { get; }
|
||||
float DefaultObjectYOffset { get; }
|
||||
|
||||
// Debug
|
||||
bool ShowDebugLogs { get; }
|
||||
}
|
||||
|
||||
@@ -7,9 +7,9 @@ using UnityEngine;
|
||||
namespace Minigames.Airplane.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Controls airplane movement using calculated (non-physics-based) flight.
|
||||
/// Uses Rigidbody2D for velocity application but not for simulation.
|
||||
/// Follows an arc trajectory based on launch parameters.
|
||||
/// 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
|
||||
@@ -57,7 +57,6 @@ namespace Minigames.Airplane.Core
|
||||
|
||||
private Rigidbody2D rb2D;
|
||||
private Collider2D airplaneCollider;
|
||||
private Vector2 currentVelocity;
|
||||
private bool isFlying = false;
|
||||
private float flightTimer = 0f;
|
||||
private string lastHitTarget = null;
|
||||
@@ -67,7 +66,7 @@ namespace Minigames.Airplane.Core
|
||||
private float maxFlightTime;
|
||||
|
||||
public bool IsFlying => isFlying;
|
||||
public Vector2 CurrentVelocity => currentVelocity;
|
||||
public Vector2 CurrentVelocity => rb2D != null ? rb2D.linearVelocity : Vector2.zero;
|
||||
public string LastHitTarget => lastHitTarget;
|
||||
|
||||
#endregion
|
||||
@@ -96,10 +95,12 @@ namespace Minigames.Airplane.Core
|
||||
rb2D = GetComponent<Rigidbody2D>();
|
||||
airplaneCollider = GetComponent<Collider2D>();
|
||||
|
||||
// Configure Rigidbody2D
|
||||
// Configure Rigidbody2D for physics-based movement
|
||||
if (rb2D != null)
|
||||
{
|
||||
rb2D.isKinematic = true; // Not physics-simulated
|
||||
rb2D.bodyType = RigidbodyType2D.Dynamic;
|
||||
rb2D.mass = mass;
|
||||
rb2D.gravityScale = 1f; // Use Unity's gravity
|
||||
rb2D.collisionDetectionMode = CollisionDetectionMode2D.Continuous;
|
||||
}
|
||||
|
||||
@@ -115,7 +116,7 @@ namespace Minigames.Airplane.Core
|
||||
#region Launch
|
||||
|
||||
/// <summary>
|
||||
/// Launch the airplane with calculated velocity
|
||||
/// Launch the airplane with physics impulse force
|
||||
/// </summary>
|
||||
public void Launch(Vector2 direction, float force)
|
||||
{
|
||||
@@ -125,57 +126,54 @@ namespace Minigames.Airplane.Core
|
||||
return;
|
||||
}
|
||||
|
||||
// Calculate initial velocity from force and mass
|
||||
float initialSpeed = force / mass;
|
||||
currentVelocity = direction.normalized * initialSpeed;
|
||||
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}, " +
|
||||
$"Initial Speed: {initialSpeed:F2}, Direction: {direction}");
|
||||
$"Expected Speed: {expectedSpeed:F2}, Direction: {direction}");
|
||||
}
|
||||
|
||||
OnLaunched?.Invoke(this);
|
||||
|
||||
// Start flight update
|
||||
StartCoroutine(FlightUpdateCoroutine());
|
||||
// Start flight monitoring (timeout and rotation)
|
||||
StartCoroutine(FlightMonitorCoroutine());
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Flight Update
|
||||
#region Flight Monitoring
|
||||
|
||||
/// <summary>
|
||||
/// Update airplane flight physics each frame
|
||||
/// Monitor airplane flight for rotation and timeout.
|
||||
/// Physics movement is handled automatically by Unity.
|
||||
/// </summary>
|
||||
private IEnumerator FlightUpdateCoroutine()
|
||||
private IEnumerator FlightMonitorCoroutine()
|
||||
{
|
||||
while (isFlying)
|
||||
{
|
||||
float deltaTime = Time.fixedDeltaTime;
|
||||
|
||||
// Apply gravity to velocity
|
||||
currentVelocity.y -= gravity * deltaTime;
|
||||
|
||||
// Apply velocity to rigidbody
|
||||
if (rb2D != null)
|
||||
// Rotate to face velocity direction (visual only)
|
||||
if (rotateToVelocity && rb2D != null && rb2D.linearVelocity.magnitude > 0.1f)
|
||||
{
|
||||
rb2D.linearVelocity = currentVelocity;
|
||||
}
|
||||
|
||||
// Rotate to face velocity direction
|
||||
if (rotateToVelocity && currentVelocity.magnitude > 0.1f)
|
||||
{
|
||||
float angle = Mathf.Atan2(currentVelocity.y, currentVelocity.x) * Mathf.Rad2Deg;
|
||||
float angle = Mathf.Atan2(rb2D.linearVelocity.y, rb2D.linearVelocity.x) * Mathf.Rad2Deg;
|
||||
transform.rotation = Quaternion.Euler(0, 0, angle);
|
||||
}
|
||||
|
||||
// Update flight timer
|
||||
flightTimer += deltaTime;
|
||||
flightTimer += Time.deltaTime;
|
||||
|
||||
// Check for timeout
|
||||
if (flightTimer >= maxFlightTime)
|
||||
@@ -185,15 +183,15 @@ namespace Minigames.Airplane.Core
|
||||
yield break;
|
||||
}
|
||||
|
||||
// Check if airplane has landed (velocity near zero or hit ground)
|
||||
if (currentVelocity.y < -0.1f && transform.position.y < -10f) // Below screen
|
||||
// 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 new WaitForFixedUpdate();
|
||||
yield return null; // Update every frame, not just fixed update
|
||||
}
|
||||
}
|
||||
|
||||
@@ -235,11 +233,11 @@ namespace Minigames.Airplane.Core
|
||||
if (!isFlying) return;
|
||||
|
||||
isFlying = false;
|
||||
currentVelocity = Vector2.zero;
|
||||
|
||||
if (rb2D != null)
|
||||
{
|
||||
rb2D.linearVelocity = Vector2.zero;
|
||||
rb2D.angularVelocity = 0f;
|
||||
}
|
||||
|
||||
if (showDebugLogs) Logging.Debug("[AirplaneController] Airplane landed");
|
||||
@@ -255,11 +253,11 @@ namespace Minigames.Airplane.Core
|
||||
if (!isFlying) return;
|
||||
|
||||
isFlying = false;
|
||||
currentVelocity = Vector2.zero;
|
||||
|
||||
if (rb2D != null)
|
||||
{
|
||||
rb2D.linearVelocity = Vector2.zero;
|
||||
rb2D.angularVelocity = 0f;
|
||||
}
|
||||
|
||||
if (showDebugLogs) Logging.Debug("[AirplaneController] Airplane timed out");
|
||||
|
||||
@@ -28,6 +28,7 @@ namespace Minigames.Airplane.Core
|
||||
[SerializeField] private AirplaneCameraManager cameraManager;
|
||||
[SerializeField] private AirplaneLaunchController launchController;
|
||||
[SerializeField] private AirplaneTargetValidator targetValidator;
|
||||
[SerializeField] private AirplaneSpawnManager spawnManager;
|
||||
|
||||
[Header("Targets")]
|
||||
[Tooltip("All targets in the scene (for highlighting)")]
|
||||
@@ -46,14 +47,14 @@ namespace Minigames.Airplane.Core
|
||||
public event Action<AirplaneGameState, AirplaneGameState> OnStateChanged;
|
||||
|
||||
/// <summary>
|
||||
/// Fired when a person starts their turn. Parameters: (PersonData person)
|
||||
/// Fired when a person starts their turn. Parameters: (Person person)
|
||||
/// </summary>
|
||||
public event Action<PersonData> OnPersonStartTurn;
|
||||
public event Action<Person> OnPersonStartTurn;
|
||||
|
||||
/// <summary>
|
||||
/// Fired when a person finishes their turn. Parameters: (PersonData person, bool success)
|
||||
/// Fired when a person finishes their turn. Parameters: (Person person, bool success)
|
||||
/// </summary>
|
||||
public event Action<PersonData, bool> OnPersonFinishTurn;
|
||||
public event Action<Person, bool> OnPersonFinishTurn;
|
||||
|
||||
/// <summary>
|
||||
/// Fired when game completes
|
||||
@@ -65,14 +66,16 @@ namespace Minigames.Airplane.Core
|
||||
#region State
|
||||
|
||||
private AirplaneGameState _currentState = AirplaneGameState.Intro;
|
||||
private PersonData _currentPerson;
|
||||
private Person _currentPerson;
|
||||
private Person _previousPerson;
|
||||
private AirplaneController _currentAirplane;
|
||||
private bool _lastShotHit;
|
||||
private int _successCount;
|
||||
private int _failCount;
|
||||
private int _totalTurns;
|
||||
|
||||
public AirplaneGameState CurrentState => _currentState;
|
||||
public PersonData CurrentPerson => _currentPerson;
|
||||
public Person CurrentPerson => _currentPerson;
|
||||
public int SuccessCount => _successCount;
|
||||
public int FailCount => _failCount;
|
||||
|
||||
@@ -167,6 +170,11 @@ namespace Minigames.Airplane.Core
|
||||
Logging.Error("[AirplaneGameManager] AirplaneTargetValidator not assigned!");
|
||||
}
|
||||
|
||||
if (spawnManager == null)
|
||||
{
|
||||
Logging.Error("[AirplaneGameManager] AirplaneSpawnManager not assigned!");
|
||||
}
|
||||
|
||||
if (allTargets == null || allTargets.Length == 0)
|
||||
{
|
||||
Logging.Warning("[AirplaneGameManager] No targets assigned!");
|
||||
@@ -203,24 +211,48 @@ namespace Minigames.Airplane.Core
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Intro sequence (stub for MVP)
|
||||
/// Intro sequence: blend to intro camera, greet all people, blend to aiming camera
|
||||
/// </summary>
|
||||
private IEnumerator IntroSequence()
|
||||
{
|
||||
if (showDebugLogs) Logging.Debug("[AirplaneGameManager] Playing intro sequence...");
|
||||
|
||||
// Switch to intro camera
|
||||
// 1. Blend to intro camera
|
||||
if (cameraManager != null)
|
||||
{
|
||||
cameraManager.SwitchToState(AirplaneCameraState.Intro);
|
||||
yield return new WaitForSeconds(0.5f); // Camera blend time
|
||||
}
|
||||
|
||||
// Wait for intro duration (stub)
|
||||
yield return new WaitForSeconds(1f);
|
||||
// 2. 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");
|
||||
}
|
||||
|
||||
// 3. 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
|
||||
// Move to first person's turn
|
||||
StartCoroutine(SetupNextPerson());
|
||||
}
|
||||
|
||||
@@ -239,7 +271,21 @@ namespace Minigames.Airplane.Core
|
||||
|
||||
ChangeState(AirplaneGameState.NextPerson);
|
||||
|
||||
// Pop next person
|
||||
// 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);
|
||||
}
|
||||
|
||||
// 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++;
|
||||
|
||||
@@ -251,29 +297,42 @@ namespace Minigames.Airplane.Core
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[AirplaneGameManager] === Turn {_totalTurns}: {_currentPerson.personName} ===" +
|
||||
$"\n Target: {_currentPerson.targetName}");
|
||||
Logging.Debug($"[AirplaneGameManager] === Turn {_totalTurns}: {_currentPerson.PersonName} ===" +
|
||||
$"\n Target: {_currentPerson.TargetName}");
|
||||
}
|
||||
|
||||
OnPersonStartTurn?.Invoke(_currentPerson);
|
||||
|
||||
// Switch to next person camera
|
||||
if (cameraManager != null)
|
||||
// Introduce the new person (unless it's the first turn - they already greeted in intro)
|
||||
if (_previousPerson != null)
|
||||
{
|
||||
cameraManager.SwitchToState(AirplaneCameraState.NextPerson);
|
||||
// Subsequent turns - person says hello
|
||||
yield return StartCoroutine(personQueue.IntroduceNextPerson());
|
||||
}
|
||||
else
|
||||
{
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
// Wait for person introduction (stub)
|
||||
yield return new WaitForSeconds(1f);
|
||||
// Initialize spawn manager for this person's target
|
||||
if (spawnManager != null)
|
||||
{
|
||||
spawnManager.InitializeForGame(_currentPerson.TargetName);
|
||||
}
|
||||
|
||||
// Set expected target
|
||||
// Queue done - continue game flow
|
||||
if (targetValidator != null)
|
||||
{
|
||||
targetValidator.SetExpectedTarget(_currentPerson.targetName);
|
||||
targetValidator.SetExpectedTarget(_currentPerson.TargetName);
|
||||
}
|
||||
|
||||
// Highlight the target
|
||||
HighlightTarget(_currentPerson.targetName);
|
||||
HighlightTarget(_currentPerson.TargetName);
|
||||
|
||||
// Enter aiming state
|
||||
EnterAimingState();
|
||||
@@ -294,6 +353,12 @@ namespace Minigames.Airplane.Core
|
||||
cameraManager.SwitchToState(AirplaneCameraState.Aiming);
|
||||
}
|
||||
|
||||
// Show target UI
|
||||
if (spawnManager != null)
|
||||
{
|
||||
spawnManager.ShowTargetUI();
|
||||
}
|
||||
|
||||
// Enable launch controller
|
||||
if (launchController != null)
|
||||
{
|
||||
@@ -328,6 +393,12 @@ namespace Minigames.Airplane.Core
|
||||
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;
|
||||
@@ -387,7 +458,15 @@ namespace Minigames.Airplane.Core
|
||||
/// </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}");
|
||||
}
|
||||
|
||||
@@ -396,6 +475,7 @@ namespace Minigames.Airplane.Core
|
||||
/// </summary>
|
||||
private void HandleWrongTargetHit(string expectedTarget, string actualTarget)
|
||||
{
|
||||
_lastShotHit = false;
|
||||
_failCount++;
|
||||
if (showDebugLogs) Logging.Debug($"[AirplaneGameManager] ✗ FAIL! Expected: {expectedTarget}, Hit: {actualTarget}");
|
||||
}
|
||||
@@ -405,6 +485,7 @@ namespace Minigames.Airplane.Core
|
||||
/// </summary>
|
||||
private void HandleMissedTargets()
|
||||
{
|
||||
_lastShotHit = false;
|
||||
_failCount++;
|
||||
if (showDebugLogs) Logging.Debug("[AirplaneGameManager] ✗ MISS! Didn't hit any target");
|
||||
}
|
||||
@@ -426,6 +507,12 @@ namespace Minigames.Airplane.Core
|
||||
cameraManager.StopFollowingAirplane();
|
||||
}
|
||||
|
||||
// Stop spawn manager tracking
|
||||
if (spawnManager != null)
|
||||
{
|
||||
spawnManager.StopTracking();
|
||||
}
|
||||
|
||||
// Determine success/failure
|
||||
bool success = targetValidator != null &&
|
||||
targetValidator.HasValidated &&
|
||||
@@ -451,6 +538,12 @@ namespace Minigames.Airplane.Core
|
||||
_currentAirplane = null;
|
||||
}
|
||||
|
||||
// Clean up spawned objects
|
||||
if (spawnManager != null)
|
||||
{
|
||||
spawnManager.CleanupSpawnedObjects();
|
||||
}
|
||||
|
||||
// Clear launch controller reference
|
||||
if (launchController != null)
|
||||
{
|
||||
|
||||
@@ -2,8 +2,6 @@ using System;
|
||||
using AppleHills.Core.Settings;
|
||||
using Common.Input;
|
||||
using Core;
|
||||
using Core.Lifecycle;
|
||||
using Minigames.Airplane.Data;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Minigames.Airplane.Core
|
||||
@@ -96,6 +94,21 @@ namespace Minigames.Airplane.Core
|
||||
|
||||
#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
|
||||
|
||||
767
Assets/Scripts/Minigames/Airplane/Core/AirplaneSpawnManager.cs
Normal file
767
Assets/Scripts/Minigames/Airplane/Core/AirplaneSpawnManager.cs
Normal file
@@ -0,0 +1,767 @@
|
||||
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;
|
||||
|
||||
// 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;
|
||||
|
||||
// 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
|
||||
if (_hasPassedThreshold)
|
||||
{
|
||||
// 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>
|
||||
public void InitializeForGame(string targetKey)
|
||||
{
|
||||
_currentTargetKey = targetKey;
|
||||
_isSpawningActive = false;
|
||||
_hasPassedThreshold = false;
|
||||
_hasSpawnedTarget = false;
|
||||
_positiveSpawnCount = 0;
|
||||
_negativeSpawnCount = 0;
|
||||
|
||||
// Determine target spawn distance
|
||||
_targetDistance = Random.Range((float)_settings.TargetMinDistance, (float)_settings.TargetMaxDistance);
|
||||
_targetSpawnPosition = new Vector3(_targetDistance, 0f, 0f);
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[SpawnManager] Initialized for target '{targetKey}' at distance {_targetDistance: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 game restart/cleanup).
|
||||
/// </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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <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.
|
||||
/// </summary>
|
||||
private void SpawnRandomObject()
|
||||
{
|
||||
if (_planeTransform == null) 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;
|
||||
|
||||
// Calculate spawn X position ahead of plane
|
||||
float spawnX = _planeTransform.position.x + _settings.SpawnDistanceAhead;
|
||||
|
||||
// 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
|
||||
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 = 2.0f)
|
||||
{
|
||||
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
|
||||
@@ -1,22 +1,30 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Core;
|
||||
using Core.Lifecycle;
|
||||
using Minigames.Airplane.Data;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Minigames.Airplane.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Manages the queue of people waiting to launch airplanes.
|
||||
/// Provides methods to pop the next person and track remaining people.
|
||||
/// 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)")]
|
||||
[SerializeField] private List<PersonData> peopleInQueue = new List<PersonData>();
|
||||
[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;
|
||||
@@ -26,8 +34,9 @@ namespace Minigames.Airplane.Core
|
||||
#region State
|
||||
|
||||
private int _currentTurnNumber = 1;
|
||||
private int _initialPeopleCount;
|
||||
|
||||
public int TotalPeople => peopleInQueue.Count;
|
||||
public int TotalPeople => _initialPeopleCount;
|
||||
public int RemainingPeople => peopleInQueue.Count;
|
||||
|
||||
#endregion
|
||||
@@ -38,6 +47,7 @@ namespace Minigames.Airplane.Core
|
||||
{
|
||||
base.OnManagedAwake();
|
||||
|
||||
_initialPeopleCount = peopleInQueue.Count;
|
||||
ValidateQueue();
|
||||
}
|
||||
|
||||
@@ -50,7 +60,7 @@ namespace Minigames.Airplane.Core
|
||||
Logging.Debug($"[PersonQueue] Initialized with {TotalPeople} people");
|
||||
foreach (var person in peopleInQueue)
|
||||
{
|
||||
Logging.Debug($" - {person.personName} -> Target: {person.targetName}");
|
||||
Logging.Debug($" - {person.PersonName} -> Target: {person.TargetName}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -66,28 +76,29 @@ namespace Minigames.Airplane.Core
|
||||
{
|
||||
if (peopleInQueue.Count == 0)
|
||||
{
|
||||
Logging.Warning("[PersonQueue] No people in queue! Add people in the inspector.");
|
||||
Logging.Warning("[PersonQueue] No people in queue! Add Person components in the inspector.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Check for missing data
|
||||
// Check for null references and validate data
|
||||
for (int i = 0; i < peopleInQueue.Count; i++)
|
||||
{
|
||||
var person = peopleInQueue[i];
|
||||
|
||||
if (string.IsNullOrEmpty(person.personName))
|
||||
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))
|
||||
if (string.IsNullOrEmpty(person.TargetName))
|
||||
{
|
||||
Logging.Warning($"[PersonQueue] Person '{person.personName}' at index {i} has no target assigned!");
|
||||
}
|
||||
|
||||
if (person.personTransform == null)
|
||||
{
|
||||
Logging.Warning($"[PersonQueue] Person '{person.personName}' at index {i} has no transform reference!");
|
||||
Logging.Warning($"[PersonQueue] Person '{person.PersonName}' at index {i} has no target assigned!");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -107,7 +118,7 @@ namespace Minigames.Airplane.Core
|
||||
/// <summary>
|
||||
/// Get the next person without removing them from the queue
|
||||
/// </summary>
|
||||
public PersonData PeekNextPerson()
|
||||
public Person PeekNextPerson()
|
||||
{
|
||||
if (peopleInQueue.Count == 0)
|
||||
{
|
||||
@@ -119,9 +130,9 @@ namespace Minigames.Airplane.Core
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Pop the next person from the queue
|
||||
/// Pop the next person from the queue (does not remove until after their turn)
|
||||
/// </summary>
|
||||
public PersonData PopNextPerson()
|
||||
public Person PopNextPerson()
|
||||
{
|
||||
if (peopleInQueue.Count == 0)
|
||||
{
|
||||
@@ -129,19 +140,16 @@ namespace Minigames.Airplane.Core
|
||||
return null;
|
||||
}
|
||||
|
||||
// Get first person
|
||||
PersonData nextPerson = peopleInQueue[0];
|
||||
// Get first person (don't remove yet - happens after their turn)
|
||||
Person nextPerson = peopleInQueue[0];
|
||||
|
||||
// Assign turn number
|
||||
nextPerson.turnNumber = _currentTurnNumber;
|
||||
nextPerson.TurnNumber = _currentTurnNumber;
|
||||
_currentTurnNumber++;
|
||||
|
||||
// Remove from queue
|
||||
peopleInQueue.RemoveAt(0);
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[PersonQueue] Popped person: {nextPerson.personName} (Turn {nextPerson.turnNumber}), " +
|
||||
Logging.Debug($"[PersonQueue] Next person: {nextPerson.PersonName} (Turn {nextPerson.TurnNumber}), " +
|
||||
$"Remaining: {RemainingPeople}");
|
||||
}
|
||||
|
||||
@@ -149,46 +157,164 @@ namespace Minigames.Airplane.Core
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reset the queue (for testing or replay)
|
||||
/// Get all people in the queue (for intro sequence). Returns a copy of the list.
|
||||
/// </summary>
|
||||
public void ResetQueue(List<PersonData> newQueue)
|
||||
public List<Person> GetAllPeople()
|
||||
{
|
||||
peopleInQueue.Clear();
|
||||
peopleInQueue.AddRange(newQueue);
|
||||
_currentTurnNumber = 1;
|
||||
|
||||
if (showDebugLogs) Logging.Debug($"[PersonQueue] Reset queue with {TotalPeople} people");
|
||||
return new List<Person>(peopleInQueue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clear the queue
|
||||
/// Remove the current person from the queue after their turn
|
||||
/// </summary>
|
||||
public void Clear()
|
||||
public void RemoveCurrentPerson()
|
||||
{
|
||||
peopleInQueue.Clear();
|
||||
_currentTurnNumber = 1;
|
||||
if (peopleInQueue.Count == 0) return;
|
||||
|
||||
if (showDebugLogs) Logging.Debug("[PersonQueue] Queue cleared");
|
||||
Person removedPerson = peopleInQueue[0];
|
||||
peopleInQueue.RemoveAt(0);
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[PersonQueue] Removed {removedPerson.PersonName} from queue. " +
|
||||
$"Remaining: {RemainingPeople}");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Query Methods
|
||||
|
||||
#region Transition Control
|
||||
|
||||
/// <summary>
|
||||
/// Get count of people still in queue
|
||||
/// Show first person at game start.
|
||||
/// Awaitable - game flow waits for introduction to complete.
|
||||
/// </summary>
|
||||
public int GetRemainingCount()
|
||||
public IEnumerator ShowFirstPerson(Person person)
|
||||
{
|
||||
return peopleInQueue.Count;
|
||||
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>
|
||||
/// Get the current turn number
|
||||
/// 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 int GetCurrentTurnNumber()
|
||||
public IEnumerator HandlePostShotReaction(bool targetHit)
|
||||
{
|
||||
return _currentTurnNumber;
|
||||
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...");
|
||||
|
||||
// Store position before removal for shuffle animation
|
||||
Vector3 removedPosition = currentPerson.PersonTransform.position;
|
||||
|
||||
// Remove successful person from queue
|
||||
RemoveCurrentPerson();
|
||||
|
||||
// Shuffle remaining people toward the removed person's position
|
||||
yield return StartCoroutine(ShuffleTransition(removedPosition));
|
||||
}
|
||||
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 toward a target position (visual transition).
|
||||
/// </summary>
|
||||
private IEnumerator ShuffleTransition(Vector3 targetPosition)
|
||||
{
|
||||
if (peopleInQueue.Count == 0)
|
||||
{
|
||||
yield break; // No one left to shuffle
|
||||
}
|
||||
|
||||
if (showDebugLogs) Logging.Debug($"[PersonQueue] Shuffling {peopleInQueue.Count} people");
|
||||
|
||||
// Store starting positions
|
||||
List<Vector3> startPositions = new List<Vector3>();
|
||||
foreach (var person in peopleInQueue)
|
||||
{
|
||||
startPositions.Add(person.PersonTransform.position);
|
||||
}
|
||||
|
||||
// Animate shuffle
|
||||
float elapsed = 0f;
|
||||
while (elapsed < shuffleDuration)
|
||||
{
|
||||
elapsed += Time.deltaTime;
|
||||
float t = elapsed / shuffleDuration;
|
||||
|
||||
// Move each person toward the left (toward removed person's spot)
|
||||
for (int i = 0; i < peopleInQueue.Count; i++)
|
||||
{
|
||||
Vector3 start = startPositions[i];
|
||||
Vector3 end = start + Vector3.left * shuffleDistance;
|
||||
peopleInQueue[i].PersonTransform.position = Vector3.Lerp(start, end, t);
|
||||
}
|
||||
|
||||
yield return null;
|
||||
}
|
||||
|
||||
// Ensure final positions are exact
|
||||
for (int i = 0; i < peopleInQueue.Count; i++)
|
||||
{
|
||||
Vector3 finalPos = startPositions[i] + Vector3.left * shuffleDistance;
|
||||
peopleInQueue[i].PersonTransform.position = finalPos;
|
||||
}
|
||||
|
||||
if (showDebugLogs) Logging.Debug("[PersonQueue] Shuffle complete");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -31,13 +31,6 @@ namespace Minigames.Airplane.Settings
|
||||
[Tooltip("Maximum flight time before timeout (seconds)")]
|
||||
[SerializeField] private float maxFlightTime = 10f;
|
||||
|
||||
[Header("Camera Settings")]
|
||||
[Tooltip("Camera follow smoothness (higher = smoother but more lag)")]
|
||||
[SerializeField] private float cameraFollowSmoothing = 5f;
|
||||
|
||||
[Tooltip("Camera zoom level during flight")]
|
||||
[SerializeField] private float flightCameraZoom = 5f;
|
||||
|
||||
[Header("Timing")]
|
||||
[Tooltip("Duration of intro sequence (seconds)")]
|
||||
[SerializeField] private float introDuration = 1f;
|
||||
@@ -48,6 +41,43 @@ namespace Minigames.Airplane.Settings
|
||||
[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;
|
||||
@@ -57,11 +87,20 @@ namespace Minigames.Airplane.Settings
|
||||
public SlingshotConfig SlingshotSettings => slingshotSettings;
|
||||
public float AirplaneMass => airplaneMass;
|
||||
public float MaxFlightTime => maxFlightTime;
|
||||
public float CameraFollowSmoothing => cameraFollowSmoothing;
|
||||
public float FlightCameraZoom => flightCameraZoom;
|
||||
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
|
||||
|
||||
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
|
||||
213
Assets/Scripts/Minigames/Airplane/UI/TargetDisplayUI.cs
Normal file
213
Assets/Scripts/Minigames/Airplane/UI/TargetDisplayUI.cs
Normal file
@@ -0,0 +1,213 @@
|
||||
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()
|
||||
{
|
||||
if (!_isActive || _planeTransform == 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.
|
||||
/// </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;
|
||||
}
|
||||
|
||||
// Update distance immediately using launch point
|
||||
UpdateDistance();
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug($"[TargetDisplayUI] Setup with target at {targetPosition}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Start tracking the airplane and updating distance.
|
||||
/// 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
|
||||
if (gameObject.activeSelf)
|
||||
{
|
||||
UpdateDistance();
|
||||
}
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug("[TargetDisplayUI] Started tracking airplane");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stop tracking the airplane.
|
||||
/// Note: Does not automatically hide UI - call Hide() separately.
|
||||
/// </summary>
|
||||
public void StopTracking()
|
||||
{
|
||||
_isActive = false;
|
||||
_planeTransform = null;
|
||||
|
||||
if (showDebugLogs)
|
||||
{
|
||||
Logging.Debug("[TargetDisplayUI] Stopped tracking");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Show the UI.
|
||||
/// </summary>
|
||||
public void Show()
|
||||
{
|
||||
gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Hide the UI.
|
||||
/// </summary>
|
||||
public void Hide()
|
||||
{
|
||||
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
|
||||
@@ -236,7 +236,7 @@ namespace UI
|
||||
case "Quarry":
|
||||
currentUIMode = UIMode.Puzzle;
|
||||
break;
|
||||
case "DivingForPictures" or "CardQualityControl" or "BirdPoop" or "FortFight":
|
||||
case "DivingForPictures" or "CardQualityControl" or "BirdPoop" or "FortFight" or "ValentineNoteDelivery":
|
||||
currentUIMode = UIMode.Minigame;
|
||||
break;
|
||||
case "StatueDecoration":
|
||||
|
||||
Reference in New Issue
Block a user