CharacterInteractions.cs 4.9 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
12
13
14
    private Text speechBox;
    private GameObject faceCamDisplay;
    private Button playerbutton1;

    //old speech boxes
15
    public Text secondarySpeechBox;
16
17

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

22
    //utility
23
24
25
26
    private bool interactable;
    private bool interacting;
    private float proximityTimer;
    private float speechBoxTimer;
27
28
29
    private bool wasInteracting;//prevents other characters from removing the dialogue box that this character is using
    
    
30
    // Use this for initialization
31
    void Awake()
32
33
34
    {
        moveScript = GetComponent<CharacterMovement>();
        characterScript = GetComponent<Character>();
35
        
36
        faceCamDisplay = GameObject.FindWithTag("FaceCam");
37
38
39
40
41
42
43
44
45
        DialogueUI = GameObject.FindWithTag("DialogueUI");
        playerbutton1 = GameObject.FindWithTag("Button1").GetComponent<Button>();
        speechBox = GameObject.FindWithTag("CharacterSpeech").GetComponent<Text>();
    }
    private void Start() {
        interactButton.gameObject.SetActive(false);
        secondarySpeechBox.gameObject.SetActive(false);
        DialogueUI.gameObject.SetActive(false);
        //playerbutton1.gameObject.SetActive(false);
46
47
48
49
50
51
52
53
    }

    // Update is called once per frame
    void Update()
    {
        //do things while the player is in range of the character
        if (interactable)
        {
54
55
56
57
            if (!interacting) {
                //if player presses up, begin the interaction.
                if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow)) {
                    interacting = true;
58
                    setText(speechBox, characterScript.GreetingMessage(), 5);
59
60
                    interactButton.gameObject.SetActive(false);
                }
61
                //generate speech every so often while the player is nearby
62
63
                if (Time.time % 5 < 0.01) {
                    if (Random.Range(0f, 1f) < 0.3) {
64
                        setText(speechBox, characterScript.ProximityMessage(), 2);
65
66
                    }
                }
67
68
69
            } else {//if the player is currently interacting
                playerbutton1.gameObject.SetActive(true);
                if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow)) {
70
                    setText(speechBox, characterScript.converse(), 5);
71
                }
72
73
            }
        }
74
75
76
77
78
79
80
81
        //deactivate the dialogue UI 
        if (wasInteracting) {
            if (Time.time > speechBoxTimer) {
                speechBoxTimer = Time.time + 5;
                DialogueUI.gameObject.SetActive(false);
                wasInteracting = false;
            }
        }
82
83
84
85
86
87
88
89
90
91
92
93
94
    }
    //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(){
95
        //speechBox.gameObject.SetActive(false);
96
        interactButton.gameObject.SetActive(true);
97
98
        faceCam.gameObject.SetActive(true);
        updateFaceCamDisplay();
99
        DialogueUI.gameObject.SetActive(false);
100
101
102
103
    }
    //player exits characters range
    void exitInteractable(){
        interactButton.gameObject.SetActive(false);
104
        faceCam.gameObject.SetActive(false);
105
        DialogueUI.gameObject.SetActive(false);
106
107
        if (interacting)
        {
108
            setText(speechBox, characterScript.GoodbyeMessage(), 2);
109
110
        }
        else{
111
            setText(speechBox, characterScript.butthurtGoodbye(), 2);
112
113
114
115
        }
        interacting = false;
    }
    //sets the text of a given text box and sets it to active.
116
117
118
119
120
    void setText(Text box, string text, float uptime){
        speechBoxTimer = Time.time + uptime;
        wasInteracting = true;
        DialogueUI.gameObject.SetActive(true);
        updateFaceCamDisplay();
121
122
123
        box.text = text;
        print(text);
    }
124
125
126

    public void updateFaceCamDisplay() {
        //set the sign of the scale of the faceDisplay to be equal to the sign of the characters transform
127
128
129
130
131
132
133
134
        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;
135
136
        }
    }
137
}