Scriptum/Assets/Scripts/NPCs' Scripts/NPCFollowing.cs

189 lines
5.6 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
2023-01-05 14:19:56 +01:00
using GUI_Scripts.ProceduralGeneration;
using UnityEngine;
2022-12-20 04:37:00 +01:00
[RequireComponent(typeof(NPC))]
[RequireComponent(typeof(NpcDialogueManager))]
public class NPCFollowing : MonoBehaviour
{
private Rigidbody2D myRigidbody;
public Animator anim;
2022-12-20 04:37:00 +01:00
public Vector3 homePosition;
public Transform targetPosition;
public AStarPathfindingAgent agent;
2022-12-20 04:37:00 +01:00
//public GameObject player;
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;
void Awake()
{
2022-12-20 04:37:00 +01:00
agent = GetComponent<AStarPathfindingAgent>();
}
// 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"));
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);
targetPosition = GameObject.FindWithTag("Player").transform;
if(!isAfterAction)
StartCoroutine(WaitBeforStartingAction(5f));
}
// Update is called once per frame
public void Update()
{
HandleState();
}
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);
}
}
public void CheckDistance()
{
2023-01-05 14:19:56 +01:00
anim.SetBool("isRunning", true);
StopAllCoroutines();
2022-12-20 04:37:00 +01:00
if (isAfterAction == false)
{
if (approaching && Vector2.Distance(targetPosition.position, transform.position) >= actionRadius)
{
agent.FindPath();
2022-12-20 04:37:00 +01:00
StartCoroutine(agent.FollowPath());
2023-01-05 14:19:56 +01:00
}
else if (approaching)
{
2023-01-02 19:31:45 +01:00
//start dialogue here
2022-12-20 04:37:00 +01:00
gameObject.GetComponent<NPC>().State = NPCStateEnum.Talking;
2023-01-05 14:53:42 +01:00
if (targetPosition.position.y > transform.position.y)
{
anim.SetBool("TurnFront",true);
anim.SetBool("isRunning", false);
}
else
{
anim.SetBool("TurnFront",false);
anim.SetBool("isRunning", false);
}
2023-01-02 19:31:45 +01:00
agent.path.Clear(); // if we are able to talgk we dont want go go further player
}
}
else if (!approaching && isAfterAction)
{
2023-01-02 19:31:45 +01:00
Debug.Log(Vector2.Distance(transform.position, homePosition));
Debug.Log(homePosition);
2023-01-05 21:19:05 +01:00
if (Vector2.Distance(transform.position, homePosition) > 0.95)
{
2023-01-02 19:31:45 +01:00
//transform.position = Vector2.MoveTowards(transform.position, homePosition, 2 * Time.deltaTime);
agent.point = homePosition;
agent.FindPoint();
StartCoroutine(agent.FollowPath());
}
else
{
2023-01-05 21:19:05 +01:00
anim.SetBool("isRunning", false);
anim.SetBool("TurnFront", false);
2022-12-20 04:37:00 +01:00
gameObject.GetComponent<NPC>().State = NPCStateEnum.Pending;
}
}
}
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;
if (approaching && !isAfterAction)
{
gameObject.GetComponent<NpcDialogueManager>().Dialogue.BreakDialogueStep();
isDuringConversation = false;
2022-12-20 04:37:00 +01:00
gameObject.GetComponent<NPC>().State = NPCStateEnum.Walking;
}
}
public void HandleState()
{
2022-12-20 04:37:00 +01:00
switch (gameObject.GetComponent<NPC>().State)
{
case NPCStateEnum.Walking:
{
CheckDistance();
break;
}
case NPCStateEnum.Talking:
{
DoAction();
break;
}
case NPCStateEnum.Pending:
{
break;
}
case NPCStateEnum.Attacking:
{
break;
}
default:
{
Debug.Log("fancy text nie wiem co zrobic");
break;
}
}
}
public void DoAction()
{
if (approaching == true && isDuringConversation == false && gameObject.GetComponent<NpcDialogueManager>().CanBeOpened)
{
isDuringConversation = true;
gameObject.GetComponent<NpcDialogueManager>().Dialogue.StartDialogue();
}
}
public void AfterAction()
{
approaching = false;
isAfterAction = true;
2022-12-20 04:37:00 +01:00
isDuringConversation = false;
PlayerPrefs.SetInt(gameObject.name + ".FirstDialogue", System.Convert.ToInt32(isAfterAction));
2022-12-20 04:37:00 +01:00
gameObject.GetComponent<NPC>().State = NPCStateEnum.Walking;
}
}