Files
AppleHillsProduction/Assets/Scripts/Minigames/BirdPooper/ObstacleSpawner.cs

256 lines
9.7 KiB
C#
Raw Normal View History

using UnityEngine;
using Core;
using Core.Settings;
using Core.Lifecycle;
using AppleHillsCamera;
namespace Minigames.BirdPooper
{
/// <summary>
/// Spawns obstacles at regular intervals for Bird Pooper minigame.
/// Uses Transform references for spawn and despawn positions instead of hardcoded values.
/// All obstacles are spawned at Y = 0 (prefabs should be authored accordingly).
/// </summary>
public class ObstacleSpawner : ManagedBehaviour
{
[Header("Spawn Configuration")]
[Tooltip("Transform marking where obstacles spawn (off-screen right)")]
[SerializeField] private Transform spawnPoint;
[Tooltip("Transform marking where obstacles despawn (off-screen left)")]
[SerializeField] private Transform despawnPoint;
[Header("EdgeAnchor References")]
[Tooltip("ScreenReferenceMarker to pass to spawned obstacles")]
[SerializeField] private ScreenReferenceMarker referenceMarker;
[Tooltip("CameraScreenAdapter to pass to spawned obstacles")]
[SerializeField] private CameraScreenAdapter cameraAdapter;
[Header("Obstacle Prefabs")]
[Tooltip("Array of obstacle prefabs to spawn randomly")]
[SerializeField] private GameObject[] obstaclePrefabs;
2025-12-14 20:55:37 +01:00
[Header("Spawn Timing")]
[Tooltip("Minimum interval between spawns (seconds)")]
[SerializeField] private float minSpawnInterval = 1f;
[Tooltip("Maximum interval between spawns (seconds)")]
[SerializeField] private float maxSpawnInterval = 2f;
[Header("Difficulty")]
[Tooltip("Time in seconds for difficulty to ramp from 0 to 1")]
[SerializeField] private float difficultyRampDuration = 60f;
[Tooltip("Curve mapping normalized time (0..1) to difficulty (0..1). Default is linear.")]
[SerializeField] private AnimationCurve difficultyCurve = AnimationCurve.Linear(0f, 0f, 1f, 1f);
[Tooltip("Maximum jitter applied to computed interval (fractional). 0.1 = +/-10% jitter")]
[SerializeField] private float intervalJitter = 0.05f;
private IBirdPooperSettings settings;
private float spawnTimer;
private bool isSpawning;
2025-12-14 20:55:37 +01:00
private float _currentSpawnInterval = 1f;
// difficulty tracking
private float _elapsedTime = 0f;
internal override void OnManagedAwake()
{
base.OnManagedAwake();
// Load settings
settings = GameManager.GetSettingsObject<IBirdPooperSettings>();
if (settings == null)
{
Debug.LogError("[ObstacleSpawner] BirdPooperSettings not found!");
2025-12-14 20:55:37 +01:00
// continue — we now use min/max interval fields instead of relying on settings
}
// Validate references
if (spawnPoint == null)
{
Debug.LogError("[ObstacleSpawner] Spawn Point not assigned! Please assign a Transform in the Inspector.");
}
if (despawnPoint == null)
{
Debug.LogError("[ObstacleSpawner] Despawn Point not assigned! Please assign a Transform in the Inspector.");
}
if (obstaclePrefabs == null || obstaclePrefabs.Length == 0)
{
Debug.LogError("[ObstacleSpawner] No obstacle prefabs assigned! Please assign at least one prefab in the Inspector.");
}
if (referenceMarker == null)
{
Debug.LogError("[ObstacleSpawner] ScreenReferenceMarker not assigned! Obstacles need this for EdgeAnchor positioning.");
}
if (cameraAdapter == null)
{
Debug.LogWarning("[ObstacleSpawner] CameraScreenAdapter not assigned. EdgeAnchor will attempt to auto-find camera.");
}
2025-12-14 20:55:37 +01:00
// Validate interval range
if (minSpawnInterval < 0f) minSpawnInterval = 0f;
if (maxSpawnInterval < 0f) maxSpawnInterval = 0f;
if (minSpawnInterval > maxSpawnInterval)
{
float tmp = minSpawnInterval;
minSpawnInterval = maxSpawnInterval;
maxSpawnInterval = tmp;
Debug.LogWarning("[ObstacleSpawner] minSpawnInterval was greater than maxSpawnInterval. Values were swapped.");
}
// Clamp ramp duration
if (difficultyRampDuration < 0.01f) difficultyRampDuration = 0.01f;
Debug.Log("[ObstacleSpawner] Initialized successfully");
}
private void Update()
{
2025-12-14 20:55:37 +01:00
if (!isSpawning || spawnPoint == null) return;
spawnTimer += Time.deltaTime;
2025-12-14 20:55:37 +01:00
_elapsedTime += Time.deltaTime;
2025-12-14 20:55:37 +01:00
if (spawnTimer >= _currentSpawnInterval)
{
SpawnObstacle();
spawnTimer = 0f;
2025-12-14 20:55:37 +01:00
// pick next interval based on difficulty ramp
float t = Mathf.Clamp01(_elapsedTime / difficultyRampDuration);
float difficulty = difficultyCurve.Evaluate(t); // 0..1
// map difficulty to interval: difficulty 0 -> max interval (easy), 1 -> min interval (hard)
float baseInterval = Mathf.Lerp(maxSpawnInterval, minSpawnInterval, difficulty);
// apply small jitter
if (intervalJitter > 0f)
{
float jitter = Random.Range(-intervalJitter, intervalJitter);
_currentSpawnInterval = Mathf.Max(0f, baseInterval * (1f + jitter));
}
else
{
_currentSpawnInterval = baseInterval;
}
// Log the computed interval and difficulty for debugging
Debug.Log($"[ObstacleSpawner] Next spawn interval: {_currentSpawnInterval:F2}s (difficulty {difficulty:F2})");
}
}
/// <summary>
/// Spawn a random obstacle at the spawn point position (Y = 0).
/// </summary>
private void SpawnObstacle()
{
if (obstaclePrefabs == null || obstaclePrefabs.Length == 0)
{
Debug.LogWarning("[ObstacleSpawner] No obstacle prefabs to spawn!");
return;
}
if (despawnPoint == null)
{
Debug.LogWarning("[ObstacleSpawner] Cannot spawn obstacle without despawn point reference!");
return;
}
// Select random prefab
GameObject selectedPrefab = obstaclePrefabs[Random.Range(0, obstaclePrefabs.Length)];
// Spawn at spawn point position with Y = 0
Vector3 spawnPosition = new Vector3(spawnPoint.position.x, 0f, 0f);
GameObject obstacleObj = Instantiate(selectedPrefab, spawnPosition, Quaternion.identity);
// Initialize obstacle with despawn X position and EdgeAnchor references
Obstacle obstacle = obstacleObj.GetComponent<Obstacle>();
if (obstacle != null)
{
obstacle.Initialize(despawnPoint.position.x, referenceMarker, cameraAdapter);
}
else
{
Debug.LogError($"[ObstacleSpawner] Spawned prefab '{selectedPrefab.name}' does not have Obstacle component!");
Destroy(obstacleObj);
}
Debug.Log($"[ObstacleSpawner] Spawned obstacle '{selectedPrefab.name}' at position {spawnPosition}");
}
/// <summary>
/// Start spawning obstacles.
/// Spawns the first obstacle immediately, then continues with interval-based spawning.
/// </summary>
public void StartSpawning()
{
isSpawning = true;
spawnTimer = 0f;
2025-12-14 20:55:37 +01:00
_elapsedTime = 0f;
// choose initial interval based on difficulty (at time 0)
float initialDifficulty = difficultyCurve.Evaluate(0f);
float initialBase = Mathf.Lerp(maxSpawnInterval, minSpawnInterval, initialDifficulty);
if (intervalJitter > 0f)
{
float jitter = Random.Range(-intervalJitter, intervalJitter);
_currentSpawnInterval = Mathf.Max(0f, initialBase * (1f + jitter));
}
else
{
_currentSpawnInterval = initialBase;
}
// Log the initial computed interval
Debug.Log($"[ObstacleSpawner] Initial spawn interval: {_currentSpawnInterval:F2}s (difficulty {initialDifficulty:F2})");
// Spawn the first obstacle immediately
SpawnObstacle();
2025-12-14 20:55:37 +01:00
Debug.Log("[ObstacleSpawner] Started spawning (first obstacle spawned immediately)");
}
/// <summary>
/// Stop spawning obstacles.
/// </summary>
public void StopSpawning()
{
isSpawning = false;
Debug.Log("[ObstacleSpawner] Stopped spawning");
}
/// <summary>
/// Check if spawner is currently active.
/// </summary>
public bool IsSpawning => isSpawning;
#if UNITY_EDITOR
/// <summary>
/// Draw gizmos in editor to visualize spawn/despawn points.
/// </summary>
private void OnDrawGizmos()
{
if (spawnPoint != null)
{
Gizmos.color = Color.green;
Gizmos.DrawLine(spawnPoint.position + Vector3.up * 10f, spawnPoint.position + Vector3.down * 10f);
Gizmos.DrawWireSphere(spawnPoint.position, 0.5f);
}
if (despawnPoint != null)
{
Gizmos.color = Color.red;
Gizmos.DrawLine(despawnPoint.position + Vector3.up * 10f, despawnPoint.position + Vector3.down * 10f);
Gizmos.DrawWireSphere(despawnPoint.position, 0.5f);
}
}
#endif
}
}