CharacterInteractions.cs 10.2 KB
Newer Older
1
2
3
4
5
6
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CharacterInteractions : MonoBehaviour {
7
8
    //Character Speech UI
    private GameObject DialogueUI;
9
    public Button interactButton;
10
    public Image LevelUpImage;
11
12
    private Text speechBox;
    private GameObject faceCamDisplay;
13
    private Image LevelMeter;
14
    private InputField inputName;
15
    private string currentSpeech;
16
17
18
    //character response stuff
    private Image responsePanel;
    public Button[] playerResponses;
19
20

    //Components
21
    public Character characterScript;
22
    private CharacterMovement moveScript;
23
    public Camera faceCam;
24
    private Animator anim;
25

26
    //utility
27
28
29
    private bool interactable;
    private bool interacting;
    private float speechBoxTimer;
30
    private bool wasInteracting;//prevents other characters from removing the dialogue box that this character is using
31
    
32

33
    // Use this for initialization
34
    void Awake()
35
36
37
    {
        moveScript = GetComponent<CharacterMovement>();
        characterScript = GetComponent<Character>();
38
        anim = GetComponentInChildren<Animator>();
39
        
40
        faceCamDisplay = GameObject.FindWithTag("FaceCam");
41
        DialogueUI = GameObject.FindWithTag("DialogueUI");
42
        responsePanel = GameObject.FindWithTag("ResponsePanel").GetComponent<Image>();
43
        speechBox = GameObject.FindWithTag("CharacterSpeech").GetComponent<Text>();
44
        LevelMeter = GameObject.FindWithTag("LevelMeter").GetComponent<Image>();
45
        inputName = GameObject.FindWithTag("playerInput").GetComponent<InputField>();
46

47
48
49
50
    }
    private void Start() {
        interactButton.gameObject.SetActive(false);
        DialogueUI.gameObject.SetActive(false);
51
        inputName.gameObject.SetActive(false);
52
        //playerbutton1.gameObject.SetActive(false);
53
54
55
56
        StartCoroutine(checkLevelUp());
    }

    private IEnumerator checkLevelUp() {
57
58
59
        yield return new WaitForSeconds(0.1f);
        LevelUpImage.gameObject.SetActive(GameManager.hasLeveledUp(characterScript.getName()));
        /*if (characterScript.getName() != "Ahava"){
60
61
62
            LevelUpImage.gameObject.SetActive(false);
        } else {
            LevelUpImage.gameObject.SetActive(GameManager.hasLeveledUp(characterScript.getName()));
63
        }*/
64
        StopCoroutine(checkLevelUp());
65
    }
66
    
67
68
69
70

    // Update is called once per frame
    void Update()
    {
71
        animationStuff();
72
        if (GameManager.ConversationStage == 1 && characterScript.getName() != "Ahava") {
mmaluschnig's avatar
mmaluschnig committed
73
74
            if (GameManager.HasTalkedToAhava)
            {
75
                LevelUpImage.gameObject.SetActive(GameManager.hasLeveledUp(characterScript.getName()));
mmaluschnig's avatar
mmaluschnig committed
76
77
78
79
            }
            else if (characterScript.getLevel() >= 2)
            {
            }else{
80
81
82
                LevelUpImage.gameObject.SetActive(false);
            }
        }
83
84
85
        //do things while the player is in range of the character
        if (interactable)
        {
86
87
            if (!interacting) {
                //if player presses up, begin the interaction.
mmaluschnig's avatar
mmaluschnig committed
88
                if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.E)) {
89
                    interacting = true;
90
                    setText(speechBox, characterScript.GreetingMessage(), Mathf.Infinity, characterScript.getResponses());
91
                    interactButton.gameObject.SetActive(false);
mmaluschnig's avatar
mmaluschnig committed
92
                    if (GameManager.HasTalkedToAhava || characterScript.getName() == "Ahava")
mmaluschnig's avatar
mmaluschnig committed
93
94
95
                    {
                        GameManager.setLeveledUp(characterScript.getName(), false);
                    }
mmaluschnig's avatar
mmaluschnig committed
96

97
                    LevelUpImage.gameObject.SetActive(false);
98
                }
99
                //generate speech every so often while the player is nearby
100
101
                if (Time.time % 5 < 0.01) {
                    if (Random.Range(0f, 1f) < 0.3) {
102
                        setText(speechBox, characterScript.ProximityMessage(), 2, characterScript.getResponses());
103
104
                    }
                }
105
            } else {//if the player is currently interacting
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
                if (!DaytimeManager.enteringName) {//conversing
                    if (Input.GetKeyDown(KeyCode.Alpha1))
                    {
                        ButtonPress(1);
                    }
                    if (Input.GetKeyDown(KeyCode.Alpha2))
                    {
                        ButtonPress(2);
                    }
                    if (Input.GetKeyDown(KeyCode.Alpha3))
                    {
                        ButtonPress(3);
                    }
                    if (Input.GetKeyDown(KeyCode.Alpha4))
                    {
                        ButtonPress(4);
122
                    }
123

124
                }
125
126
            }
        }
127
128
129
130
131
132
133
134
        //deactivate the dialogue UI 
        if (wasInteracting) {
            if (Time.time > speechBoxTimer) {
                speechBoxTimer = Time.time + 5;
                DialogueUI.gameObject.SetActive(false);
                wasInteracting = false;
            }
        }
135
136
137
138
139
140
141
142
143
144
145
146
147
    }
    //characterMovement script sets this for us when the player
    //enters or exits characters range
    public void setInteractable(bool value){
        interactable = value;
        if(interactable){
            enterInteractable();
        }else{
            exitInteractable();
        }
    }
    //player enters characters range
    void enterInteractable(){
148
        //speechBox.gameObject.SetActive(false);
149
        interactButton.gameObject.SetActive(true);
150
151
        faceCam.gameObject.SetActive(true);
        updateFaceCamDisplay();
152
        DialogueUI.gameObject.SetActive(false);
153
154
155
156
    }
    //player exits characters range
    void exitInteractable(){
        interactButton.gameObject.SetActive(false);
157
        faceCam.gameObject.SetActive(false);
158
        DialogueUI.gameObject.SetActive(false);
159
160
        if (interacting)
        {
161
            setText(speechBox, characterScript.GoodbyeMessage(), 2, characterScript.getResponses());
162
163
        }
        else{
164
            setText(speechBox, characterScript.butthurtGoodbye(), 2, characterScript.getResponses());
165
166
167
168
        }
        interacting = false;
    }
    //sets the text of a given text box and sets it to active.
169
    void setText(Text box, string text, float uptime, string[] replies){
170
171
        //speechBoxTimer = Time.time + uptime;
        speechBoxTimer = Mathf.Infinity;
172
173
174
        wasInteracting = true;
        DialogueUI.gameObject.SetActive(true);
        updateFaceCamDisplay();
175
        box.text = text;
176
        currentSpeech = text;
177
        LevelMeter.fillAmount = characterScript.getLevel() % 1;
178
        //print(text);
179
        //setting player reply options
180
181
        for (int i = 0; i < playerResponses.Length; i++) {
            //find spacing to arrange buttons
182
183
184
            float sideSpacing = 90;
            float spacing = (responsePanel.rectTransform.rect.width - 2*sideSpacing) / (replies.Length - 1);
            
185
186
            //set the text on the reply buttons
            if (i < replies.Length) {
187
188
189
190
191
192
193
194
195
196
197
198
199
                if (replies.Length == 1)
                {
                    float YPos = playerResponses[i].GetComponent<RectTransform>().localPosition.y;
                    playerResponses[i].GetComponent<RectTransform>().localPosition = new Vector3(0,YPos,0);
                }
                else
                {
                    //arrange buttons
                    Vector2 newPos = playerResponses[i].GetComponent<RectTransform>().localPosition;
                    float panelEdge = responsePanel.rectTransform.rect.xMin;
                    newPos.x = panelEdge + sideSpacing + (spacing * i);
                    playerResponses[i].GetComponent<RectTransform>().localPosition = newPos;
                }
200
201

                playerResponses[i].gameObject.SetActive(true);
202
203
204
205
206
207
                if (!DaytimeManager.enteringName)
                {
                    playerResponses[i].GetComponentInChildren<Text>().text = "(" + (i + 1) + ") " + replies[i];
                }else{
                    playerResponses[i].GetComponentInChildren<Text>().text = "(Enter)" + replies[i];
                }
208

209
210
211
212
            } else {
                playerResponses[i].gameObject.SetActive(false);
            }

213
        }
214
    }
215
216
217

    public void updateFaceCamDisplay() {
        //set the sign of the scale of the faceDisplay to be equal to the sign of the characters transform
218
219
220
221
222
223
224
225
        if (DialogueUI.gameObject.activeSelf) {
            Vector3 newScale = faceCamDisplay.transform.localScale;
            if (transform.localScale.x > 0) {
                newScale.x = Mathf.Abs(newScale.x);
            } else {
                newScale.x = (-1) * Mathf.Abs(newScale.x);
            }
            faceCamDisplay.transform.localScale = newScale;
226
227
        }
    }
228
    public void ButtonPress(int buttonNum) {
229
230
231
232
233
234
235
        if (interacting && buttonNum <= 3) {
            converse();
        }
    }

    public void ProgressConversation(){
        if(characterScript.getName() == "Ahava"){
236
            setText(speechBox, GameManager.playerName + "? " + characterScript.converse(), Mathf.Infinity, characterScript.getResponses());
237
        }
238
    }
239

240
    private void converse(){
241
242
243
244
245
246
247
248
249
250
        //stage 1, ahava asks player for name
        if (characterScript.getName() == "Ahava" && GameManager.ConversationStage == 1)
        {
            askPlayerForName();
            setText(speechBox, characterScript.converse(), Mathf.Infinity, characterScript.getResponses());
        }
        else
        {
            setText(speechBox, characterScript.converse(), Mathf.Infinity, characterScript.getResponses());
        }
251
252
    }

253
254
255
256
257
258
259
    public void animationStuff() {
        if (interacting) {
            anim.SetBool("Talking", true);
        } else {
            anim.SetBool("Talking", false);
        }
    }
260
261
262
263
264
265
266
267
268
269
270

    private bool askPlayerForName() {
        if (GameManager.speechState == 5) {
            //create input field for player name
            DaytimeManager.enteringName = true;
            inputName.gameObject.SetActive(true);
            inputName.ActivateInputField();
            return true;
        }
        return false;
    }
271
}