2025-09-11 17:03:56 +02:00
|
|
|
using UnityEngine;
|
2025-09-17 13:59:21 +02:00
|
|
|
using System.Collections;
|
2025-10-14 15:53:58 +02:00
|
|
|
using Core;
|
2025-09-16 18:14:00 +02:00
|
|
|
using Pathfinding;
|
2025-09-11 17:03:56 +02:00
|
|
|
|
2025-11-07 15:38:31 +00:00
|
|
|
// TODO: Remove movement based logic
|
2025-09-12 13:57:26 +02:00
|
|
|
public class AnneLiseBehaviour : MonoBehaviour
|
2025-09-11 17:03:56 +02:00
|
|
|
{
|
2025-09-16 15:52:30 +02:00
|
|
|
[SerializeField] public float moveSpeed;
|
2025-09-16 18:14:00 +02:00
|
|
|
private Animator animator;
|
|
|
|
|
private AIPath aiPath;
|
2025-09-16 23:52:28 +02:00
|
|
|
private bool hasArrived = false;
|
2025-09-17 13:59:21 +02:00
|
|
|
private LureSpot currentLureSpot;
|
2025-09-16 23:52:28 +02:00
|
|
|
private SpriteRenderer spriteRenderer; // Cached reference
|
|
|
|
|
private bool allowFacingByVelocity = true; // New flag
|
2025-09-17 13:59:21 +02:00
|
|
|
private Coroutine walkingCoroutine;
|
|
|
|
|
private bool annaLiseIsReady = false; // Flag to know if Anna Lise is ready to take the picture
|
2025-09-16 18:14:00 +02:00
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
|
{
|
|
|
|
|
animator = GetComponentInChildren<Animator>();
|
|
|
|
|
aiPath = GetComponent<AIPath>();
|
2025-09-16 23:52:28 +02:00
|
|
|
spriteRenderer = GetComponentInChildren<SpriteRenderer>(); // Cache the reference
|
2025-09-16 18:14:00 +02:00
|
|
|
if (aiPath != null)
|
|
|
|
|
{
|
|
|
|
|
aiPath.maxSpeed = moveSpeed;
|
2025-09-17 13:59:21 +02:00
|
|
|
aiPath.OnTargetReachedEvent += HandleArriveAtSpot;
|
2025-09-16 18:14:00 +02:00
|
|
|
}
|
|
|
|
|
}
|
2025-09-16 15:52:30 +02:00
|
|
|
|
2025-09-17 13:59:21 +02:00
|
|
|
private void OnDestroy()
|
|
|
|
|
{
|
|
|
|
|
if (aiPath != null)
|
|
|
|
|
{
|
|
|
|
|
aiPath.OnTargetReachedEvent -= HandleArriveAtSpot;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void TeleportJustOutOfView(Camera cam, float offset = 2f)
|
|
|
|
|
{
|
|
|
|
|
if (aiPath == null || cam == null || currentLureSpot == null || currentLureSpot.annaLiseSpot == null) return;
|
|
|
|
|
|
|
|
|
|
// Calculate direction from the target spot to Anna Lise's current position
|
|
|
|
|
Vector3 from = currentLureSpot.annaLiseSpot.transform.position;
|
|
|
|
|
Vector3 to = transform.position;
|
|
|
|
|
Vector3 direction = (to - from).normalized;
|
|
|
|
|
|
|
|
|
|
// Project the target spot to screen space
|
|
|
|
|
Vector3 targetScreen = cam.WorldToScreenPoint(from);
|
|
|
|
|
|
|
|
|
|
// Find the screen edge in the direction
|
|
|
|
|
Vector2 dir2D = new Vector2(direction.x, direction.y);
|
|
|
|
|
if (dir2D == Vector2.zero) dir2D = Vector2.right; // fallback
|
|
|
|
|
dir2D.Normalize();
|
|
|
|
|
|
|
|
|
|
// Calculate intersection with screen bounds
|
|
|
|
|
float tX = dir2D.x > 0 ? (Screen.width - targetScreen.x) / dir2D.x : (0 - targetScreen.x) / dir2D.x;
|
|
|
|
|
float tY = dir2D.y > 0 ? (Screen.height - targetScreen.y) / dir2D.y : (0 - targetScreen.y) / dir2D.y;
|
|
|
|
|
float t = Mathf.Min(Mathf.Abs(tX), Mathf.Abs(tY));
|
|
|
|
|
Vector2 edgeScreen = new Vector2(targetScreen.x, targetScreen.y) + dir2D * t;
|
|
|
|
|
edgeScreen += dir2D * offset; // Move outside the screen by offset
|
|
|
|
|
|
|
|
|
|
// Convert back to world position
|
|
|
|
|
Vector3 teleportWorld = cam.ScreenToWorldPoint(new Vector3(edgeScreen.x, edgeScreen.y, cam.WorldToScreenPoint(from).z));
|
|
|
|
|
teleportWorld.z = transform.position.z; // Keep original Z
|
|
|
|
|
|
|
|
|
|
aiPath.Teleport(teleportWorld, true);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-16 15:52:30 +02:00
|
|
|
public void GotoSpot(GameObject lurespot)
|
2025-09-11 17:03:56 +02:00
|
|
|
{
|
2025-09-17 13:59:21 +02:00
|
|
|
currentLureSpot = lurespot.GetComponent<LureSpot>();
|
|
|
|
|
// Teleport Anna Lise just out of view before moving
|
|
|
|
|
TeleportJustOutOfView(Camera.main, 2f);
|
|
|
|
|
|
2025-09-16 18:14:00 +02:00
|
|
|
if (aiPath == null) return;
|
2025-09-11 17:03:56 +02:00
|
|
|
|
2025-09-17 13:59:21 +02:00
|
|
|
aiPath.destination = currentLureSpot.annaLiseSpot.transform.position;
|
2025-09-16 18:14:00 +02:00
|
|
|
aiPath.canMove = true;
|
|
|
|
|
aiPath.SearchPath();
|
2025-09-16 23:52:28 +02:00
|
|
|
hasArrived = false;
|
2025-09-17 13:59:21 +02:00
|
|
|
allowFacingByVelocity = true;
|
|
|
|
|
if (walkingCoroutine != null)
|
|
|
|
|
{
|
|
|
|
|
StopCoroutine(walkingCoroutine);
|
|
|
|
|
}
|
|
|
|
|
walkingCoroutine = StartCoroutine(UpdateSpeedWhenWalking());
|
2025-09-16 15:52:30 +02:00
|
|
|
}
|
2025-09-16 18:14:00 +02:00
|
|
|
|
2025-09-17 13:59:21 +02:00
|
|
|
private IEnumerator UpdateSpeedWhenWalking()
|
2025-09-11 17:03:56 +02:00
|
|
|
{
|
2025-09-17 13:59:21 +02:00
|
|
|
while (!hasArrived && aiPath != null && animator != null)
|
2025-09-16 23:52:28 +02:00
|
|
|
{
|
2025-09-17 13:59:21 +02:00
|
|
|
float currentSpeed = aiPath.velocity.magnitude;
|
|
|
|
|
animator.SetFloat("speed", currentSpeed);
|
|
|
|
|
|
|
|
|
|
// Only allow facing by velocity if not arrived
|
|
|
|
|
if (allowFacingByVelocity && currentSpeed > 0.01f && spriteRenderer != null)
|
2025-09-16 23:52:28 +02:00
|
|
|
{
|
2025-09-17 13:59:21 +02:00
|
|
|
Vector3 velocity = aiPath.velocity;
|
|
|
|
|
if (velocity.x != 0)
|
|
|
|
|
{
|
|
|
|
|
Vector3 scale = spriteRenderer.transform.localScale;
|
|
|
|
|
scale.x = Mathf.Abs(scale.x) * (velocity.x > 0 ? 1 : -1);
|
|
|
|
|
spriteRenderer.transform.localScale = scale;
|
|
|
|
|
}
|
2025-09-16 23:52:28 +02:00
|
|
|
}
|
2025-09-17 13:59:21 +02:00
|
|
|
yield return null;
|
2025-09-16 15:52:30 +02:00
|
|
|
}
|
2025-09-16 18:14:00 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-17 13:59:21 +02:00
|
|
|
private void HandleArriveAtSpot()
|
2025-09-16 18:14:00 +02:00
|
|
|
{
|
2025-09-17 13:59:21 +02:00
|
|
|
if (hasArrived) return;
|
|
|
|
|
hasArrived = true;
|
|
|
|
|
allowFacingByVelocity = false; // Disable facing by velocity after arrival
|
|
|
|
|
aiPath.canMove = false;
|
|
|
|
|
if (walkingCoroutine != null)
|
|
|
|
|
{
|
|
|
|
|
StopCoroutine(walkingCoroutine);
|
|
|
|
|
walkingCoroutine = null;
|
|
|
|
|
}
|
|
|
|
|
// Face the "luredBird" of the current lurespot, if available
|
2025-09-16 23:52:28 +02:00
|
|
|
if (currentLureSpot != null)
|
|
|
|
|
{
|
2025-09-17 13:59:21 +02:00
|
|
|
if (currentLureSpot.luredBird != null)
|
2025-09-16 23:52:28 +02:00
|
|
|
{
|
2025-09-17 13:59:21 +02:00
|
|
|
FaceTarget(currentLureSpot.luredBird);
|
|
|
|
|
annaLiseIsReady = true; // Now Anna Lise is ready to take the picture
|
2025-09-16 23:52:28 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-17 13:59:21 +02:00
|
|
|
if (animator != null && currentLureSpot.name != "LureSpotB")// Horrible way to not take the photo if its Wolter
|
|
|
|
|
{
|
2025-09-16 18:14:00 +02:00
|
|
|
animator.SetTrigger("TakePhoto");
|
2025-09-17 13:59:21 +02:00
|
|
|
annaLiseIsReady = false; // Reset the flag after taking the photo
|
|
|
|
|
}
|
|
|
|
|
animator.SetFloat("speed", 0);
|
2025-09-11 17:03:56 +02:00
|
|
|
}
|
2025-09-16 23:52:28 +02:00
|
|
|
|
|
|
|
|
public void FaceTarget(GameObject target)
|
|
|
|
|
{
|
|
|
|
|
if (target == null || spriteRenderer == null) return;
|
|
|
|
|
|
|
|
|
|
// Compare X positions to determine facing direction
|
|
|
|
|
float direction = target.transform.position.x - transform.position.x;
|
|
|
|
|
if (Mathf.Abs(direction) > 0.01f) // Avoid flipping if almost aligned
|
|
|
|
|
{
|
|
|
|
|
Vector3 scale = spriteRenderer.transform.localScale;
|
|
|
|
|
scale.x = Mathf.Abs(scale.x) * (direction > 0 ? 1 : -1);
|
|
|
|
|
spriteRenderer.transform.localScale = scale;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-09-17 13:59:21 +02:00
|
|
|
public void TrafalgarTouchedAnnaLise()
|
|
|
|
|
{
|
|
|
|
|
if (annaLiseIsReady == true && currentLureSpot.name == "LureSpotB") // Only allow if Anna Lise is ready and it's the correct lure spot
|
|
|
|
|
{
|
|
|
|
|
// Trigger the photo taken animation
|
|
|
|
|
if (animator != null)
|
|
|
|
|
{
|
|
|
|
|
currentLureSpot.GetComponentInChildren<BirdEyesBehavior>().BirdReveal();
|
|
|
|
|
animator.SetTrigger("TakePhoto");
|
|
|
|
|
}
|
|
|
|
|
annaLiseIsReady = false; // Reset the flag after taking the photo
|
|
|
|
|
}
|
2025-10-14 15:53:58 +02:00
|
|
|
Logging.Debug("Trafalgar touched Anna Lise");
|
2025-09-17 13:59:21 +02:00
|
|
|
}
|
2025-09-11 17:03:56 +02:00
|
|
|
}
|