2022-12-11 23:06:16 +01:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
|
2022-12-20 04:37:00 +01:00
|
|
|
[RequireComponent(typeof(NPC))]
|
2022-12-20 15:23:09 +01:00
|
|
|
[RequireComponent(typeof(NpcDialogueManager))]
|
2022-12-11 23:06:16 +01:00
|
|
|
public class NPCFollowing : MonoBehaviour
|
|
|
|
{
|
|
|
|
private Rigidbody2D myRigidbody;
|
|
|
|
public Animator anim;
|
|
|
|
|
2022-12-20 04:37:00 +01:00
|
|
|
public Vector3 homePosition;
|
2022-12-11 23:06:16 +01:00
|
|
|
public Transform targetPosition;
|
|
|
|
|
|
|
|
public AStarPathfindingAgent agent;
|
|
|
|
|
2022-12-20 04:37:00 +01:00
|
|
|
//public GameObject player;
|
2022-12-11 23:06:16 +01:00
|
|
|
|
|
|
|
public bool approaching = false;
|
|
|
|
public bool isAfterAction = false;
|
|
|
|
public bool isDuringConversation = false;
|
2022-12-20 04:37:00 +01:00
|
|
|
public float actionRadius = 1.5f;
|
2022-12-11 23:06:16 +01:00
|
|
|
|
|
|
|
void Awake()
|
|
|
|
{
|
2022-12-20 04:37:00 +01:00
|
|
|
agent = GetComponent<AStarPathfindingAgent>();
|
2022-12-11 23:06:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start is called before the first frame update
|
|
|
|
void Start()
|
|
|
|
{
|
2022-12-24 20:16:41 +01:00
|
|
|
isAfterAction = System.Convert.ToBoolean(PlayerPrefs.GetInt(gameObject.GetComponent<NPC>().name + ".FirstDialogue"));
|
2022-12-20 15:23:09 +01:00
|
|
|
|
2022-12-11 23:06:16 +01:00
|
|
|
myRigidbody = GetComponent<Rigidbody2D>();
|
|
|
|
anim = GetComponent<Animator>();
|
2022-12-20 04:37:00 +01:00
|
|
|
homePosition = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z);
|
2022-12-11 23:06:16 +01:00
|
|
|
targetPosition = GameObject.FindWithTag("Player").transform;
|
2022-12-20 15:23:09 +01:00
|
|
|
|
|
|
|
if(!isAfterAction)
|
|
|
|
StartCoroutine(WaitBeforStartingAction(5f));
|
|
|
|
|
2022-12-11 23:06:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
public void Update()
|
|
|
|
{
|
|
|
|
HandleState();
|
|
|
|
}
|
|
|
|
|
2022-12-20 15:23:09 +01:00
|
|
|
private IEnumerator WaitBeforStartingAction(float waitTime)
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
yield return new WaitForSeconds(waitTime);
|
|
|
|
|
|
|
|
// Start story
|
|
|
|
approaching = true;
|
|
|
|
gameObject.GetComponent<NPC>().State = NPCStateEnum.Walking;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private IEnumerator Wait(float waitTime)
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
yield return new WaitForSeconds(waitTime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-11 23:06:16 +01:00
|
|
|
public void CheckDistance()
|
|
|
|
{
|
|
|
|
StopAllCoroutines();
|
2022-12-20 04:37:00 +01:00
|
|
|
|
2022-12-11 23:06:16 +01:00
|
|
|
if (isAfterAction == false)
|
|
|
|
{
|
|
|
|
if (approaching && Vector2.Distance(targetPosition.position, transform.position) >= actionRadius)
|
|
|
|
{
|
|
|
|
agent.FindPath();
|
2022-12-20 04:37:00 +01:00
|
|
|
|
2022-12-11 23:06:16 +01:00
|
|
|
StartCoroutine(agent.FollowPath());
|
|
|
|
}
|
|
|
|
else if (approaching)
|
|
|
|
{
|
|
|
|
//start dialogue here we want uga bunga
|
|
|
|
|
2022-12-20 04:37:00 +01:00
|
|
|
gameObject.GetComponent<NPC>().State = NPCStateEnum.Talking;
|
2022-12-11 23:06:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (!approaching && isAfterAction)
|
|
|
|
{
|
2022-12-20 04:37:00 +01:00
|
|
|
if (Vector2.Distance(transform.position, homePosition) > 0.05)
|
2022-12-11 23:06:16 +01:00
|
|
|
{
|
2022-12-20 04:37:00 +01:00
|
|
|
transform.position = Vector2.MoveTowards(transform.position, homePosition, 2 * Time.deltaTime);
|
2022-12-11 23:06:16 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-12-20 04:37:00 +01:00
|
|
|
gameObject.GetComponent<NPC>().State = NPCStateEnum.Pending;
|
2022-12-11 23:06:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnTriggerExit2D(Collider2D other)
|
|
|
|
{
|
2022-12-20 04:37:00 +01:00
|
|
|
// don't listen when component is disabled
|
|
|
|
if (!gameObject.GetComponent<NPCFollowing>().enabled)
|
|
|
|
return;
|
|
|
|
|
2022-12-11 23:06:16 +01:00
|
|
|
if (approaching && !isAfterAction)
|
|
|
|
{
|
|
|
|
gameObject.GetComponent<NpcDialogueManager>().Dialogue.BreakDialogueStep();
|
|
|
|
isDuringConversation = false;
|
2022-12-20 04:37:00 +01:00
|
|
|
gameObject.GetComponent<NPC>().State = NPCStateEnum.Walking;
|
2022-12-11 23:06:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void HandleState()
|
|
|
|
{
|
2022-12-20 04:37:00 +01:00
|
|
|
switch (gameObject.GetComponent<NPC>().State)
|
2022-12-11 23:06:16 +01:00
|
|
|
{
|
|
|
|
case NPCStateEnum.Walking:
|
|
|
|
{
|
|
|
|
CheckDistance();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NPCStateEnum.Talking:
|
|
|
|
{
|
|
|
|
DoAction();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NPCStateEnum.Pending:
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
Debug.Log("fancy text nie wiem co zrobic");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void DoAction()
|
|
|
|
{
|
2022-12-20 15:23:09 +01:00
|
|
|
if (approaching == true && isDuringConversation == false && gameObject.GetComponent<NpcDialogueManager>().CanBeOpened)
|
2022-12-11 23:06:16 +01:00
|
|
|
{
|
|
|
|
isDuringConversation = true;
|
|
|
|
gameObject.GetComponent<NpcDialogueManager>().Dialogue.StartDialogue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void AfterAction()
|
|
|
|
{
|
|
|
|
approaching = false;
|
|
|
|
isAfterAction = true;
|
2022-12-20 04:37:00 +01:00
|
|
|
isDuringConversation = false;
|
2022-12-11 23:06:16 +01:00
|
|
|
PlayerPrefs.SetInt(gameObject.name + ".FirstDialogue", System.Convert.ToInt32(isAfterAction));
|
2022-12-20 04:37:00 +01:00
|
|
|
gameObject.GetComponent<NPC>().State = NPCStateEnum.Walking;
|
2022-12-11 23:06:16 +01:00
|
|
|
}
|
|
|
|
}
|