-
Notifications
You must be signed in to change notification settings - Fork 0
/
LiveDxInteractPress.cs
177 lines (161 loc) · 5.86 KB
/
LiveDxInteractPress.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class LiveDxInteractPress : MonoBehaviour
{
private DialogueSystem.DialogueCondition npcState;
[Header("UI Game Objects")]
[SerializeField] GameObject UI;
[SerializeField] TextMeshProUGUI dialogueTextUI;
[Header("Dialogue Objects")]
[SerializeField] DxObject firstContact;
[SerializeField] DxObject questAvailable;
[SerializeField] DxObject questActive;
[SerializeField] DxObject questEnd;
[SerializeField] DxObject idle;
[Header("Item Management: Quests")]
[SerializeField] InventoryManager inventory;
[SerializeField] ItemClass objectiveItem;
[SerializeField] GameObject[] worldObject;
private bool nearPlayer;
private DxObject activeDx;
private int activeDxIndex;
[SerializeField] GameObject goPoofPrefab;
private GameObject goPoof;
private void Start()
{
nearPlayer = false;
// default NPC state to First Contact
npcState = DialogueSystem.DialogueCondition.FirstContact;
activeDx = firstContact;
activeDxIndex = 0;
// hide UI
UI.SetActive(false);
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.E) && nearPlayer)
{
CheckActiveDx();
switch (npcState)
{
case DialogueSystem.DialogueCondition.FirstContact:
InitiateDialogue(activeDx);
break;
case DialogueSystem.DialogueCondition.QuestAvailable:
InitiateDialogue(activeDx);
break;
case DialogueSystem.DialogueCondition.QuestActive:
InitiateDialogue(activeDx);
break;
case DialogueSystem.DialogueCondition.QuestEnd:
InitiateDialogue(activeDx);
inventory.Remove(objectiveItem); // remove quest objective
if (questEnd.dialogueText.Count - 1 == activeDxIndex)
{
if (worldObject != null) // check if a world object is affected
{
for (int x = 0; x <= worldObject.Length; x++) // loop through all world objects in list
{
if (x < worldObject.Length)
{
GameEvents.current.UpdateSprite(worldObject[x], 1);
}
}
}
if (goPoofPrefab != null && !goPoof)
{
goPoof = Instantiate(goPoofPrefab, this.transform.position, Quaternion.identity);
StartCoroutine(DestroyParticle());
}
if (GetComponent<LiveSpriteSwap>() != null)
{
Debug.Log("spriteswap attached to: " + gameObject);
GameEvents.current.UpdateSprite(gameObject, 1);
}
}
break;
case DialogueSystem.DialogueCondition.Idle:
InitiateDialogue(activeDx);
break;
}
}
}
#region Collision Logic
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Player")
{
nearPlayer = true;
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.tag == "Player")
{
UI.SetActive(false);
activeDxIndex = 0;
nearPlayer = false;
}
}
#endregion
void InitiateDialogue(DxObject dialogue)
{
UI.SetActive(true);
if (dialogue != null && activeDxIndex < (dialogue.dialogueText.Count - 1))
{
dialogueTextUI.text = dialogue.dialogueText[activeDxIndex]; // set the UI text to read the dialogue text
activeDxIndex += 1; // increase the counter
}
else if (activeDxIndex == dialogue.dialogueText.Count - 1)
{
dialogueTextUI.text = dialogue.dialogueText[activeDxIndex]; // set the UI text to read the dialogue text
activeDxIndex += 1;
}
else if (activeDxIndex == dialogue.dialogueText.Count)
{
npcState = dialogue.endCondition;
UI.SetActive(false);
activeDxIndex = 0;
}
}
void CheckActiveDx()
{ // makes certain that the current dialogue variable matches the current
// NPC state and checks for objectives
switch (npcState)
{
case DialogueSystem.DialogueCondition.FirstContact:
activeDx = firstContact;
break;
case DialogueSystem.DialogueCondition.QuestAvailable:
activeDx = questAvailable;
break;
case DialogueSystem.DialogueCondition.QuestActive:
{
activeDx = questActive;
if (objectiveItem != null)
{
SlotClass slot = inventory.Contains(objectiveItem);
if (slot != null)
{
activeDx = questEnd;
npcState = DialogueSystem.DialogueCondition.QuestEnd;
}
}
}
break;
case DialogueSystem.DialogueCondition.QuestEnd:
activeDx = questEnd;
break;
case DialogueSystem.DialogueCondition.Idle:
activeDx = idle;
break;
}
}
IEnumerator DestroyParticle()
{
yield return new WaitForSeconds(1.5f);
Destroy(goPoof);
}
}