CharacterInteractions.cs 10.5 KB
Newer Older
1
2
3
4
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
mmaluschnig's avatar
mmaluschnig committed
5
using UnityEngine.SceneManagement;
6
7

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

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

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

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

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

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

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

98
                    LevelUpImage.gameObject.SetActive(false);
99
                }
100
                //generate speech every so often while the player is nearby
101
102
                if (Time.time % 5 < 0.01) {
                    if (Random.Range(0f, 1f) < 0.3) {
103
                        setText(speechBox, characterScript.ProximityMessage(), 2, characterScript.getResponses());
104
105
                    }
                }
106
            } else {//if the player is currently interacting
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
                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);
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
        if (interacting && buttonNum <= 3) {
230
            converse();
231
232
233
234
            if (GameManager.ConversationStage == 4 && characterScript.getName() == "Ahava" && GameManager.speechState == 1 && !GameManager.hasCompletedTheGame) {
                SceneManager.LoadScene("BeardMenu");
                GameManager.hasCompletedTheGame = true;
            }
235
236
237
238
239
        }
    }

    public void ProgressConversation(){
        if(characterScript.getName() == "Ahava"){
240
            setText(speechBox, GameManager.playerName + "? " + characterScript.converse(), Mathf.Infinity, characterScript.getResponses());
241
        }
242
    }
243

244
    private void converse(){
245
246
247
248
249
250
251
252
253
254
        //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());
        }
255
256
    }

257
258
259
260
261
262
263
    public void animationStuff() {
        if (interacting) {
            anim.SetBool("Talking", true);
        } else {
            anim.SetBool("Talking", false);
        }
    }
264
265

    private bool askPlayerForName() {
266
        if (GameManager.speechState == 7) {
267
268
269
270
271
272
273
274
            //create input field for player name
            DaytimeManager.enteringName = true;
            inputName.gameObject.SetActive(true);
            inputName.ActivateInputField();
            return true;
        }
        return false;
    }
275
}