CharacterInteractions.cs 6.6 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
11
    private Text speechBox;
    private GameObject faceCamDisplay;
12
    private Image LevelMeter;
13
14
15
    //character response stuff
    private Image responsePanel;
    public Button[] playerResponses;
16
17

    //Components
18
    public Character characterScript;
19
    private CharacterMovement moveScript;
20
    public Camera faceCam;
21
    private Animator anim;
22

23
    //utility
24
25
26
    private bool interactable;
    private bool interacting;
    private float speechBoxTimer;
27
    private bool wasInteracting;//prevents other characters from removing the dialogue box that this character is using
28

29
    // Use this for initialization
30
    void Awake()
31
32
33
    {
        moveScript = GetComponent<CharacterMovement>();
        characterScript = GetComponent<Character>();
34
        anim = GetComponentInChildren<Animator>();
35
        
36
        faceCamDisplay = GameObject.FindWithTag("FaceCam");
37
        DialogueUI = GameObject.FindWithTag("DialogueUI");
38
        responsePanel = GameObject.FindWithTag("ResponsePanel").GetComponent<Image>();
39
        speechBox = GameObject.FindWithTag("CharacterSpeech").GetComponent<Text>();
40
41
        LevelMeter = GameObject.FindWithTag("LevelMeter").GetComponent<Image>();

42
43
44
45
46
    }
    private void Start() {
        interactButton.gameObject.SetActive(false);
        DialogueUI.gameObject.SetActive(false);
        //playerbutton1.gameObject.SetActive(false);
47
        
48
49
50
51
52
    }

    // Update is called once per frame
    void Update()
    {
53
        animationStuff();
54
55
56
        //do things while the player is in range of the character
        if (interactable)
        {
57
58
            if (!interacting) {
                //if player presses up, begin the interaction.
mmaluschnig's avatar
mmaluschnig committed
59
                if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.E)) {
60
                    interacting = true;
61
                    setText(speechBox, characterScript.GreetingMessage(), Mathf.Infinity, characterScript.getResponses());
62
63
                    interactButton.gameObject.SetActive(false);
                }
64
                //generate speech every so often while the player is nearby
65
66
                if (Time.time % 5 < 0.01) {
                    if (Random.Range(0f, 1f) < 0.3) {
67
                        setText(speechBox, characterScript.ProximityMessage(), 2, characterScript.getResponses());
68
69
                    }
                }
70
            } else {//if the player is currently interacting
mmaluschnig's avatar
mmaluschnig committed
71
                if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.E)) {
72
                    setText(speechBox, characterScript.converse(), Mathf.Infinity, characterScript.getResponses());
73
                }
74
75
            }
        }
76
77
78
79
80
81
82
83
        //deactivate the dialogue UI 
        if (wasInteracting) {
            if (Time.time > speechBoxTimer) {
                speechBoxTimer = Time.time + 5;
                DialogueUI.gameObject.SetActive(false);
                wasInteracting = false;
            }
        }
84
85
86
87
88
89
90
91
92
93
94
95
96
    }
    //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(){
97
        //speechBox.gameObject.SetActive(false);
98
        interactButton.gameObject.SetActive(true);
99
100
        faceCam.gameObject.SetActive(true);
        updateFaceCamDisplay();
101
        DialogueUI.gameObject.SetActive(false);
102
103
104
105
    }
    //player exits characters range
    void exitInteractable(){
        interactButton.gameObject.SetActive(false);
106
        faceCam.gameObject.SetActive(false);
107
        DialogueUI.gameObject.SetActive(false);
108
109
        if (interacting)
        {
110
            setText(speechBox, characterScript.GoodbyeMessage(), 2, characterScript.getResponses());
111
112
        }
        else{
113
            setText(speechBox, characterScript.butthurtGoodbye(), 2, characterScript.getResponses());
114
115
116
117
        }
        interacting = false;
    }
    //sets the text of a given text box and sets it to active.
118
    void setText(Text box, string text, float uptime, string[] replies){
119
120
121
122
        speechBoxTimer = Time.time + uptime;
        wasInteracting = true;
        DialogueUI.gameObject.SetActive(true);
        updateFaceCamDisplay();
123
        box.text = text;
124
        LevelMeter.fillAmount = characterScript.getLevel();
125
        print(text);
126
        //setting player reply options
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
        for (int i = 0; i < playerResponses.Length; i++) {
            //find spacing to arrange buttons
            float spacing = responsePanel.rectTransform.rect.width / (replies.Length + 1);
            //set the text on the reply buttons
            if (i < replies.Length) {
                //arrange buttons
                Vector2 newPos = playerResponses[i].GetComponent<RectTransform>().localPosition;
                float panelEdge = responsePanel.rectTransform.rect.xMin;
                newPos.x = panelEdge + (spacing * (i+1));
                playerResponses[i].GetComponent<RectTransform>().localPosition = newPos;

                playerResponses[i].gameObject.SetActive(true);
                playerResponses[i].GetComponentInChildren<Text>().text = replies[i];
            } else {
                playerResponses[i].gameObject.SetActive(false);
            }

144
        }
145
    }
146
147
148

    public void updateFaceCamDisplay() {
        //set the sign of the scale of the faceDisplay to be equal to the sign of the characters transform
149
150
151
152
153
154
155
156
        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;
157
158
        }
    }
159
    public void ButtonPress() {
160
        if (interacting) {
161
            setText(speechBox, characterScript.converse(), Mathf.Infinity, characterScript.getResponses());
162
        }
163
    }
164
165
166
167
168
169
170
171

    public void animationStuff() {
        if (interacting) {
            anim.SetBool("Talking", true);
        } else {
            anim.SetBool("Talking", false);
        }
    }
172
}