using System.Collections;
using System.Collections.Generic;
using GUI_Scripts.ProceduralGeneration;
using UnityEngine;
///
/// Note:
/// Wizard changing quest content after finishing second dialogue
/// Before that prev gest stay in Quest list
///
public enum WizzardMissionStateEnum
{
None = 0,
AcceptMission = 1,
WaitingForPeoggress = 2,
MainPlotIntroduction = 3
}
[RequireComponent(typeof(NPC))]
[RequireComponent(typeof(NpcMissionManager))]
public class WizardFirstQuestManager : 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; // after mission in this case
public WizzardMissionStateEnum MissionProggress = WizzardMissionStateEnum.None;
//public bool waitingForMissionEffects = false;
public bool isDuringConversation = false;
public float actionRadius = 1.5f;
void Awake()
{
agent = GetComponent();
}
// Start is called before the first frame update
void Start()
{
gameObject.GetComponent().OpenInDefaultWay = false;
myRigidbody = GetComponent();
anim = GetComponent();
homePosition = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z);
targetPosition = GameObject.FindWithTag("Player").transform;
if (PlayerPrefs.HasKey(gameObject.GetComponent().name + ".FirstMission"))
MissionProggress = (WizzardMissionStateEnum)PlayerPrefs.GetInt(gameObject.GetComponent().name + ".FirstMission");
// ODCZYTANIE STANU Z PlayerPrefs JEST NIEZPOJNE Z DANYMI ODCZYTYWANIYMI Z PLIKU O PROGRESSIE MISJI
// Player Prefs zapisuje po kliknieciu buttona w dailogu
// Mission & Quest - zapisuje po teleporcie albo recznym 'save'
if (MissionProggress == WizzardMissionStateEnum.None)
StartCoroutine(WaitBeforStartingAction(5f));
else if (MissionProggress == WizzardMissionStateEnum.WaitingForPeoggress || MissionProggress == WizzardMissionStateEnum.MainPlotIntroduction)
{
approaching = false;
gameObject.GetComponent().State = NPCStateEnum.Talking;
gameObject.GetComponent().OpenInDefaultWay = true;
}
}
// 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().State = NPCStateEnum.Walking;
}
}
private IEnumerator Wait(float waitTime)
{
while (true)
{
yield return new WaitForSeconds(waitTime);
}
}
public void CheckDistance()
{
anim.SetBool("isRunning", true);
StopAllCoroutines();
//if (isAfterAction == false && waitingForMissionEffects == false)
if (approaching && (MissionProggress == WizzardMissionStateEnum.None || MissionProggress == WizzardMissionStateEnum.AcceptMission))
{
if (approaching && Vector2.Distance(targetPosition.position, transform.position) >= actionRadius)
{
agent.FindPath();
StartCoroutine(agent.FollowPath());
}
else if (approaching)
{
//start dialogue here
gameObject.GetComponent().CanBeOpened = true;
gameObject.GetComponent().State = NPCStateEnum.Talking;
if (targetPosition.position.y > transform.position.y)
{
anim.SetBool("TurnFront", true);
anim.SetBool("isRunning", false);
}
else
{
anim.SetBool("TurnFront", false);
anim.SetBool("isRunning", false);
}
agent.path.Clear(); // if we are able to talgk we dont want go go further player
}
}
else if (MissionProggress == WizzardMissionStateEnum.WaitingForPeoggress)
{
if (Vector2.Distance(transform.position, homePosition) > 0.95)
{
//transform.position = Vector2.MoveTowards(transform.position, homePosition, 2 * Time.deltaTime);
GoToHomePosition();
}
else
{
anim.SetBool("isRunning", false);
anim.SetBool("TurnFront", false);
gameObject.GetComponent().State = NPCStateEnum.Pending;
}
}
else if (MissionProggress == WizzardMissionStateEnum.MainPlotIntroduction)
{
gameObject.GetComponent().State = NPCStateEnum.Talking;
agent.path.Clear();
}
}
public void OnTriggerExit2D(Collider2D other)
{
// don't listen when component is disabled
if (!gameObject.GetComponent().enabled || other.tag != "Player")
return;
if (gameObject.GetComponent().Mission != null && !gameObject.GetComponent().OpenInDefaultWay)
{
if (gameObject.GetComponent().State == NPCStateEnum.Talking)
{
gameObject.GetComponent().Mission.BreakDialogueStep();
isDuringConversation = false;
gameObject.GetComponent().State = NPCStateEnum.Walking;
}
// ...
}
}
public void HandleState()
{
switch (gameObject.GetComponent().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().CanBeOpened)
{
isDuringConversation = true;
gameObject.GetComponent().Mission.StartDialogue();
}
}
public void GoToHomePosition()
{
agent.point = homePosition;
agent.FindPoint();
StartCoroutine(agent.FollowPath());
}
public void AfterMissionAccepting()
{
var wizardOnScene = GameObject.FindGameObjectWithTag("NPCCollection").transform.Find("Wizard");
if (wizardOnScene == null)
return;
//wizardOnScene.GetComponent().isAfterAction = false;
wizardOnScene.GetComponent().approaching = false;
//wizardOnScene.GetComponent().waitingForMissionEffects = true;
wizardOnScene.GetComponent().State = NPCStateEnum.Walking; // go home position
wizardOnScene.GetComponent().OpenInDefaultWay = true;
wizardOnScene.GetComponent().MissionProggress = WizzardMissionStateEnum.WaitingForPeoggress;
PlayerPrefs.SetInt(gameObject.GetComponent().name + ".FirstMission", (int)wizardOnScene.GetComponent().MissionProggress);
}
// tylko pierwszy - drugi nie bedzie
public void AfterMeetingCondition()
{
var wizardOnScene = GameObject.FindGameObjectWithTag("NPCCollection").transform.Find("Wizard");
if (wizardOnScene == null)
return;
approaching = false;
//waitingForMissionEffects = false;
//isAfterAction = true;
wizardOnScene.GetComponent().MissionProggress = WizzardMissionStateEnum.MainPlotIntroduction;
PlayerPrefs.SetInt(gameObject.GetComponent().name + ".FirstMission", (int)wizardOnScene.GetComponent().MissionProggress);
}
}