-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDialogueManager.cs
More file actions
172 lines (152 loc) · 4.12 KB
/
Copy pathDialogueManager.cs
File metadata and controls
172 lines (152 loc) · 4.12 KB
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
using UnityEngine.Events;
using UnityEngine.SceneManagement;
using TMPro;
public class DialogueManager : MonoBehaviour
{
public static DialogueManager Instance;
public TextMeshProUGUI speakerName, dialogueText;
public RawImage imageHolder;
public GameObject canvas;
public float dialogueSpeed;
private Player playerRef;
private int currentLine;
private string[] dialogueLines;
private string[] dialogueLinesAfterAnimation;
private bool isTyping;
private bool animationPlayed;
private bool lastDialogueSaid;
private string endAnimationName;
private bool hasKey;
private UnityEvent endEvent;
private void Start()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
else
Destroy(gameObject);
currentLine = 0;
canvas.SetActive(false);
playerRef = GameObject.FindGameObjectWithTag("Player")?.GetComponent<Player>();
}
private void OnEnable()
{
SceneManager.sceneLoaded += OnSceneLoaded;
}
void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
if (scene.buildIndex != 0)
playerRef = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
}
private void Update()
{
if (!canvas.activeInHierarchy) return;
if (!isTyping && currentLine == 0)
{
NextSentence();
}
else if (!isTyping && Input.GetKeyDown(KeyCode.E))
{
NextSentence();
}
}
public void NextSentence()
{
if (currentLine < dialogueLines.Length)
{
dialogueText.text = "";
if (canvas.activeInHierarchy)
StartCoroutine(WriteSentence());
}
else
{
if (endEvent != null)
endEvent.Invoke();
if (!animationPlayed && endAnimationName != "" && canvas.activeInHierarchy)
{
StartCoroutine(PlayAnimation());
}
else
HideDialogue();
}
}
public void SetEvent(UnityEvent unityEvent)
{
endEvent = unityEvent;
}
IEnumerator PlayAnimation()
{
playerRef.StopMove();
playerRef.PlayAnimation(endAnimationName);
yield return new WaitForSeconds(playerRef.GetAnimationLength(endAnimationName) - 2f);
animationPlayed = true;
if (dialogueLinesAfterAnimation.Length > 0)
{
currentLine = 0;
isTyping = false;
dialogueLines = dialogueLinesAfterAnimation;
lastDialogueSaid = true;
}
if (lastDialogueSaid)
{
playerRef.StartMove();
if (hasKey)
Inventory.instance.ShowKey();
}
}
IEnumerator WriteSentence()
{
isTyping = true;
foreach (char lineCharacter in dialogueLines[currentLine].ToCharArray())
{
dialogueText.text += lineCharacter;
yield return new WaitForSeconds(dialogueSpeed);
}
isTyping = false;
currentLine++;
}
public void ShowDialogue(CharacterName characterName, string[] lines, string[] endlines, string animationName, bool hasKey)
{
this.hasKey = hasKey;
endAnimationName = animationName;
currentLine = 0;
dialogueLines = lines;
dialogueLinesAfterAnimation = endlines;
isTyping = false;
speakerName.text = characterName.ToString();
imageHolder.texture = GetImage(characterName);
canvas.SetActive(true);
playerRef.StopMove();
}
public void HideDialogue()
{
currentLine = 0;
isTyping = false;
dialogueText.text = "";
canvas.SetActive(false);
playerRef.StartMove();
lastDialogueSaid = false;
animationPlayed = false;
}
private Texture GetImage(CharacterName charName)
{
Texture2D t;
// #if UNITY_EDITOR
// t = (Texture2D)AssetDatabase.LoadAssetAtPath("Assets/Resourses/Textures/Characters/" + charName + ".png", typeof(Texture2D));
// return t;
// #endif
t = Resources.Load<Texture2D>("Characters/" + charName) as Texture2D;
return t;
}
private void OnDisable()
{
SceneManager.sceneLoaded -= OnSceneLoaded;
}
}