File size: 4,571 Bytes
c80142e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.InputSystem;
using System.Collections.Generic;
using TMPro;

public class CrabInterface : MonoBehaviour
{
    public TextMeshProUGUI boardText; 
    public Button clearButton;
    public Button confirmButton;
    public CrabHandler crab;
    private HashSet<string> displayedWords = new HashSet<string>();
    private const int maxWords = 2;
    private GameManager gm;
    public Animator animator;
    public SpriteRenderer spriteRender;

    private void Start()
    {
        GameObject uiCamObj = GameObject.FindGameObjectWithTag("UI");
        Canvas canvas = GetComponent<Canvas>();
        canvas.renderMode = RenderMode.ScreenSpaceCamera;
        canvas.worldCamera = uiCamObj.GetComponent<Camera>();
        canvas.planeDistance = 1f;
        canvas.sortingLayerName = "UI";
        canvas.sortingOrder = 500;

        var renderers = GetComponentsInChildren<SpriteRenderer>();
        foreach (var renderer in renderers)
        {
            renderer.sortingLayerName = "UI";
            renderer.sortingOrder += 500;
        }


        LogManager logManager = FindObjectOfType<LogManager>();
        gm = FindObjectOfType<GameManager>();

        if (logManager != null)
        {
            logManager.SetCrabInterface(this); 
        }

        if (clearButton != null)
        {
            clearButton.onClick.AddListener(ClearBoard);
        }

        if (confirmButton != null)
        {
            confirmButton.onClick.AddListener(ConfirmBoardText);
        }


    }

    private void Update()
    {
        
    }

    public void AddWordToBoard(string word)
    {
        if (displayedWords.Count >= maxWords)
        {
            return; 
        }

        if (!displayedWords.Contains(word))
        {
            displayedWords.Add(word);
            boardText.text = string.Join(", ", displayedWords);
            GameManager.WordEffect effect = gm.GetEffectForWord(word);

            if (effect != null)
            {
                if (effect.affectFlee) {
                  
                    switch (effect.crabBehavior)
                    {
                        case CrabBehavior.Flee:
                        case CrabBehavior.GoTo:
                        case CrabBehavior.Follow:
                            SetBehavior(1);
                            Debug.Log("WE FOLLOW MUSK ON TWITTER");
                            break;
                        case CrabBehavior.PickingUp:
                            SetBehavior(4);
                            Debug.Log("WE PICK UP THE PHONE");
                            break;
                        case CrabBehavior.DropItem:
                            SetBehavior(3);
                            Debug.Log("WE DROP TILTED TOWERS");
                            break;
                        case CrabBehavior.StandStill:
                            SetBehavior(2);
                            Debug.Log("FREEZE!");
                            break;
                        default:
                            SetBehavior(0); 
                            break;
                    }
                }

                if (effect.affectTarget)
                {
                    spriteRender.sprite = effect.wordSprite;

                    if (spriteRender.sprite != null) {
                        float targetSize = 7f;
                        Vector2 spriteSize = effect.wordSprite.bounds.size;

                        float scaleFactor = targetSize / Mathf.Max(spriteSize.x, spriteSize.y);
                        spriteRender.transform.localScale = new Vector3(scaleFactor, scaleFactor, 1);
                    }
                }

            }
            else
            {
                Debug.Log("MISSING MY LOVE");
            }
        }
    }

    public void SetBehavior(int behavior)
    {
        if (animator != null)
        {
            animator.SetFloat("BehaviorIndex", behavior);
        }
    }

    public void ClearBoard()
    {
        displayedWords.Clear();
        boardText.text = "";
        SetBehavior(0);
        spriteRender.sprite = null;
    }

    public void ConfirmBoardText()
    {
        if (crab == null || gm == null)
        {
            return;
        }

        List<string> wordsList = new List<string>(displayedWords);
        crab.ApplyBoardTextEffects(wordsList);
        ClearBoard();
    }
}