Scriptum/Assets/Scripts/NPCs' Scripts/NPCFollowing.cs
KrolMel cd678552bb Added Enemies in Cave
Also prepared Wizard Script
2022-12-11 23:06:16 +01:00

154 lines
4.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NPCFollowing : MonoBehaviour
{
private Rigidbody2D myRigidbody;
public Animator anim;
public Transform 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.0f;
public NPCStateEnum state = NPCStateEnum.Pending;
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"));
state = NPCStateEnum.Walking;
myRigidbody = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
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)
{
Debug.Log(agent);
agent.FindPath();
//transform.position = Vector2.MoveTowards(transform.position, target.position, moveSpeed * Time.deltaTime);
StartCoroutine(agent.FollowPath());
}
/* else if (Vector2.Distance(target.position, transform.position) > chaseRadius)
{
//Debug.Log(Vector2.Distance(transform.position, path[currentPoint].position));
if (Vector2.Distance(transform.position, path[currentPoint].position) > roundingDistance)
{
StopAllCoroutines();
transform.position = Vector2.MoveTowards(transform.position, path[currentPoint].position, 2 * Time.deltaTime);
}
else
{
changeGoal();
}
}*/
else if (approaching)
{
//start dialogue here we want uga bunga
state = NPCStateEnum.Talking;
}
}
else if (!approaching && isAfterAction)
{
if (Vector2.Distance(transform.position, homePosition.position) > 1)
{
transform.position = Vector2.MoveTowards(transform.position, homePosition.position, 2 * Time.deltaTime);
}
else
{
state = NPCStateEnum.Pending;
}
}
}
public void OnTriggerExit2D(Collider2D other)
{
if (approaching && !isAfterAction)
{
gameObject.GetComponent<NpcDialogueManager>().Dialogue.BreakDialogueStep();
isDuringConversation = false;
state = NPCStateEnum.Walking;
}
}
public void HandleState()
{
switch (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;
PlayerPrefs.SetInt(gameObject.name + ".FirstDialogue", System.Convert.ToInt32(isAfterAction));
state = NPCStateEnum.Walking;
}
public void SaveCheckpoint()
{
//PlayerPrefs.SetInt(enemyName + "-S", isKilled);
}
}