puzzlestep_indicators (#14)

Co-authored-by: Michal Pikulski <michal.a.pikulski@gmail.com>
Reviewed-on: #14
This commit is contained in:
2025-10-02 05:42:17 +00:00
parent a6c63af911
commit e713a580a9
29 changed files with 899 additions and 10 deletions

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
@@ -14,6 +15,12 @@ namespace PuzzleS
private static PuzzleManager _instance;
private static bool _isQuitting;
[SerializeField] private float proximityCheckInterval = 0.02f;
// Reference to player transform for proximity calculations
private Transform _playerTransform;
private Coroutine _proximityCheckCoroutine;
/// <summary>
/// Singleton instance of the PuzzleManager.
/// </summary>
@@ -53,9 +60,19 @@ namespace PuzzleS
SceneManager.sceneLoaded += OnSceneLoaded;
}
void Start()
{
// Find player transform
_playerTransform = GameObject.FindGameObjectWithTag("Player")?.transform;
// Start proximity check coroutine
StartProximityChecks();
}
void OnDestroy()
{
SceneManager.sceneLoaded -= OnSceneLoaded;
StopProximityChecks();
}
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
@@ -66,6 +83,69 @@ namespace PuzzleS
_runtimeDependencies.Clear();
BuildRuntimeDependencies();
UnlockInitialSteps();
// Find player transform again in case it changed with scene load
_playerTransform = GameObject.FindGameObjectWithTag("Player")?.transform;
// Restart proximity checks
StartProximityChecks();
}
/// <summary>
/// Start the proximity check coroutine.
/// </summary>
private void StartProximityChecks()
{
StopProximityChecks();
_proximityCheckCoroutine = StartCoroutine(CheckProximityRoutine());
}
/// <summary>
/// Stop the proximity check coroutine.
/// </summary>
private void StopProximityChecks()
{
if (_proximityCheckCoroutine != null)
{
StopCoroutine(_proximityCheckCoroutine);
_proximityCheckCoroutine = null;
}
}
/// <summary>
/// Coroutine that periodically checks player proximity to all puzzle steps.
/// </summary>
private IEnumerator CheckProximityRoutine()
{
WaitForSeconds wait = new WaitForSeconds(proximityCheckInterval);
while (true)
{
if (_playerTransform != null)
{
// Get the proximity threshold from settings (half of the prompt range)
float proximityThreshold = GameManager.Instance.DefaultPuzzlePromptRange;
// Check distance to each step behavior
foreach (var kvp in _stepBehaviours)
{
if (kvp.Value == null) continue;
float distance = Vector3.Distance(_playerTransform.position, kvp.Value.transform.position);
// Determine the proximity state - only Close or Far now
ObjectiveStepBehaviour.ProximityState state =
(distance <= proximityThreshold)
? ObjectiveStepBehaviour.ProximityState.Close
: ObjectiveStepBehaviour.ProximityState.Far;
// Update the step's proximity state
kvp.Value.UpdateProximityState(state);
}
}
yield return wait;
}
}
/// <summary>