137 lines
4.0 KiB
C#
137 lines
4.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
[RequireComponent(typeof(NPC))]
|
|
public class NPCFollowing : MonoBehaviour
|
|
{
|
|
private Rigidbody2D myRigidbody;
|
|
public Animator anim;
|
|
|
|
public Vector3 homePosition;
|
|
public Transform targetPosition;
|
|
|
|
public AStarPathfindingAgent agent;
|
|
|
|
//public GameObject player;
|
|
|
|
public bool approaching = false;
|
|
public bool isAfterAction = false;
|
|
public bool isDuringConversation = false;
|
|
public float actionRadius = 1.5f;
|
|
|
|
void Awake()
|
|
{
|
|
agent = GetComponent<AStarPathfindingAgent>();
|
|
}
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
isAfterAction = System.Convert.ToBoolean(PlayerPrefs.GetInt(gameObject.GetComponent<NPC>().name + ".FirstDialogue"));
|
|
gameObject.GetComponent<NPC>().State = NPCStateEnum.Walking;
|
|
|
|
myRigidbody = GetComponent<Rigidbody2D>();
|
|
anim = GetComponent<Animator>();
|
|
homePosition = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z);
|
|
targetPosition = GameObject.FindWithTag("Player").transform;
|
|
}
|
|
|
|
// Update is called once per frame
|
|
public void Update()
|
|
{
|
|
HandleState();
|
|
}
|
|
|
|
public void CheckDistance()
|
|
{
|
|
StopAllCoroutines();
|
|
|
|
if (isAfterAction == false)
|
|
{
|
|
if (approaching && Vector2.Distance(targetPosition.position, transform.position) >= actionRadius)
|
|
{
|
|
agent.FindPath();
|
|
|
|
StartCoroutine(agent.FollowPath());
|
|
}
|
|
else if (approaching)
|
|
{
|
|
//start dialogue here we want uga bunga
|
|
|
|
gameObject.GetComponent<NPC>().State = NPCStateEnum.Talking;
|
|
}
|
|
}
|
|
|
|
else if (!approaching && isAfterAction)
|
|
{
|
|
if (Vector2.Distance(transform.position, homePosition) > 0.05)
|
|
{
|
|
transform.position = Vector2.MoveTowards(transform.position, homePosition, 2 * Time.deltaTime);
|
|
}
|
|
else
|
|
{
|
|
gameObject.GetComponent<NPC>().State = NPCStateEnum.Pending;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void OnTriggerExit2D(Collider2D other)
|
|
{
|
|
// don't listen when component is disabled
|
|
if (!gameObject.GetComponent<NPCFollowing>().enabled)
|
|
return;
|
|
|
|
if (approaching && !isAfterAction)
|
|
{
|
|
gameObject.GetComponent<NpcDialogueManager>().Dialogue.BreakDialogueStep();
|
|
isDuringConversation = false;
|
|
gameObject.GetComponent<NPC>().State = NPCStateEnum.Walking;
|
|
}
|
|
}
|
|
|
|
public void HandleState()
|
|
{
|
|
switch (gameObject.GetComponent<NPC>().State)
|
|
{
|
|
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()
|
|
{
|
|
if (approaching == true && isDuringConversation == false)
|
|
{
|
|
isDuringConversation = true;
|
|
gameObject.GetComponent<NpcDialogueManager>().Dialogue.StartDialogue();
|
|
}
|
|
}
|
|
|
|
public void AfterAction()
|
|
{
|
|
approaching = false;
|
|
isAfterAction = true;
|
|
isDuringConversation = false;
|
|
PlayerPrefs.SetInt(gameObject.name + ".FirstDialogue", System.Convert.ToInt32(isAfterAction));
|
|
gameObject.GetComponent<NPC>().State = NPCStateEnum.Walking;
|
|
}
|
|
}
|