using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class HedgehogEventManager : MonoBehaviour
{
private Dictionary cheatCodes;
private string currentInput = "";
private int maxCheatCodeLength = 3;
[SerializeField] GameObject hedgehogPrefab;
[SerializeField] GameObject loveBush;
[SerializeField] GameObject endPoint;
[SerializeField] NavMeshAgent hedgehogAgent;
[SerializeField] GameObject heartVFX;
[SerializeField] Transform heartVfxSpawnPoint;
[SerializeField] GameObject childSpawn;
public bool eventActive = false;
void Start()
{
// Define cheat codes and bind them to functions
cheatCodes = new Dictionary()
{
{"hug", TriggerHedgehogEvent } // Trigger event when typing "hug"
};
// Make sure VFX is disabled initially
if (heartVFX != null && heartVfxSpawnPoint != null)
{
heartVFX.SetActive(false);
}
}
void Update()
{
// Listen for typed input to detect cheat codes
foreach (char c in Input.inputString)
{
currentInput += c;
if (currentInput.Length > maxCheatCodeLength)
{
currentInput = currentInput.Substring(currentInput.Length - maxCheatCodeLength);
}
foreach (var cheatCode in cheatCodes)
{
if (currentInput.EndsWith(cheatCode.Key))
{
cheatCode.Value.Invoke();
}
}
}
}
void TriggerHedgehogEvent()
{
// Spawn a new hedgehog near the player
var bushPosition = new Vector3(transform.position.x - 5, transform.position.y, transform.position.z - 3);
GameObject newHedgehog = Instantiate(hedgehogPrefab, bushPosition, transform.rotation);
var newHedgehogAgent = newHedgehog.GetComponent();
// If the agent is valid and on the NavMesh, proceed with movement coroutine
if (newHedgehogAgent != null && newHedgehogAgent.isOnNavMesh)
{
StartCoroutine(MoveToLocationAndPause(newHedgehogAgent, loveBush.transform.position, 10f, endPoint.transform.position));
Debug.Log("Hedgehog is on a NavMesh");
}
}
IEnumerator MoveToLocationAndPause(NavMeshAgent agent, Vector3 targetPosition, float pauseDuration, Vector3 endPoint)
{
if (agent != null && agent.isOnNavMesh)
{
agent.SetDestination(targetPosition);
// Wait until the hedgehog reaches the bush
while (agent.pathPending || agent.remainingDistance > agent.stoppingDistance)
{
yield return null;
}
if (agent.isOnNavMesh)
{
eventActive = true;
agent.SetDestination(loveBush.transform.position);
agent.isStopped = true;
heartVFX.SetActive(true);
yield return new WaitForSeconds(pauseDuration); // Wait to simulate bonding moment
heartVFX.SetActive(false);
InstantiateSmallHedgehog(endPoint); // Spawn child
agent.isStopped = false;
}
else
{
Debug.LogError("Hedgehog is not on a NavMesh after reaching the destination.");
}
}
else
{
Debug.LogError("Agent is not properly initialized or off the NavMesh.");
}
}
void InstantiateSmallHedgehog(Vector3 endPoint)
{
// Instantiate a small version of the hedgehog
var smallHedgehog = Instantiate(hedgehogPrefab, endPoint, transform.rotation);
smallHedgehog.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
var smallHedgehogAgent = smallHedgehog.GetComponent();
if (smallHedgehogAgent != null && smallHedgehogAgent.isOnNavMesh)
{
smallHedgehogAgent.SetDestination(endPoint);
}
}
}