-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCardManager.cs
298 lines (251 loc) · 10.8 KB
/
CardManager.cs
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
using UdonSharp;
using UnityEngine;
using UnityEngine.UI;
using VRC.SDKBase;
using VRC.Udon;
using VRC.Udon.Common.Interfaces;
[AddComponentMenu("")]
public class CardManager : UdonSharpBehaviour
{
public Manager manager;
public GameObject cardPrefab;
public GameObject smallCardPrefab;
public GameObject selectionIndicatorPrefab;
public Canvas agentView;
public Canvas operatorView;
public LayoutManager layoutManager;
private GameObject[] agentCards;
private GameObject[] operatorCards;
private GameObject selectionIndicator;
private int blueHits;
private int redHits;
[UdonSynced]
public int awaitCardID;
private int cardID;
[UdonSynced]
public string asyncCardEvent;
private string cardEvent;
private bool awaitWordsUpdate;
private float update;
public void SetCardEvent(string incEvnt)
{
Debug.Log("Emitting card event: " + incEvnt);
asyncCardEvent = incEvnt;
}
public void Start()
{
reset();
}
public void Update()
{
update += Time.deltaTime;
if (update > 0.5f)
{
update = 0f;
if (asyncCardEvent != cardEvent)
{
Debug.Log("Event has shown up: " + asyncCardEvent);
string[] evnt = asyncCardEvent.Split(',');
cardEvent = asyncCardEvent;
switch (evnt[0])
{
case "MakeAllCards":
string[] words = new string[evnt.Length - 1];
for (int i = 1; i < evnt.Length; i++)
{
words[i - 1] = evnt[i];
}
MakeAllCards(words);
break;
case "MoveCursor":
MoveCursor(int.Parse(evnt[1]));
break;
case "SetCardBlue":
setCardBlue(agentCards[int.Parse(evnt[1])]);
break;
case "SetCardRed":
setCardRed(agentCards[int.Parse(evnt[1])]);
break;
case "SetCardGrey":
setCardGrey(agentCards[int.Parse(evnt[1])]);
break;
}
}
}
}
public void Setup()
{
Debug.Log("Setting up card manager");
this.reset();
this.createCards();
}
public void Teardown()
{
this.reset();
if (selectionIndicator != null)
{
Destroy(selectionIndicator);
selectionIndicator = null;
}
}
public void BlueHit()
{
Debug.Log("Blue hit");
this.blueHits = this.blueHits + 1;
this.checkIfGameWon();
}
public void RedHit()
{
Debug.Log("Red hit");
this.redHits = this.redHits + 1;
this.checkIfGameWon();
}
public void CivilianHit()
{
Debug.Log("Civilian hit");
}
public void MoveCursor(int cardID)
{
Debug.Log("Moving cursor!");
GameObject card = this.agentCards[cardID];
if (this.selectionIndicator == null)
{
Debug.Log("Selector is null: instantiating");
this.selectionIndicator = VRCInstantiate(this.selectionIndicatorPrefab);
}
this.selectionIndicator.transform.SetParent(agentView.transform);
RectTransform transform = this.selectionIndicator.GetComponent<RectTransform>();
transform.localRotation = new Quaternion();
transform.anchoredPosition3D = new Vector3((float)(cardID % 5 * transform.sizeDelta.x + 0.5 * transform.sizeDelta.x), (float)(-cardID / 5 * transform.sizeDelta.y + -0.5 * transform.sizeDelta.y), 0);
}
public void DestroyCursor()
{
Destroy(this.selectionIndicator);
this.selectionIndicator = null;
}
private void setCardBlue(GameObject card)
{
card.GetComponent<Image>().color = new Color(0, 0, 255, 255);
}
private void setCardRed(GameObject card)
{
card.GetComponent<Image>().color = new Color(255, 0, 0, 255);
}
private void setCardGrey(GameObject card)
{
card.GetComponent<Image>().color = new Color(120, 120, 120, 255);
}
private void setCardBlack(GameObject card)
{
card.GetComponent<Image>().color = new Color(0, 0, 0, 255);
}
private void reset()
{
awaitCardID = -1;
cardID = -1;
if (this.agentCards != null && this.operatorCards != null)
{
for (int i = 0; i < this.agentCards.Length; i++)
{
if (this.agentCards != null)
{
Destroy(this.agentCards[i]);
}
if (this.operatorCards[i] != null)
{
Destroy(this.operatorCards[i]);
}
}
}
this.agentCards = new GameObject[25];
this.operatorCards = new GameObject[25];
this.blueHits = 0;
this.redHits = 0;
}
private string[] possibleWords()
{
string wordStr = "Hollywood,Well,Foot,New York,Spring,Court,Tube,Point,Tablet,Slip,Date,Drill,Lemon,Bell,Screen,Fair,Torch,State,Match,Iron,Block,France,Australia,Limousine,Stream,Glove,Nurse,Leprechaun,Play,Tooth,Arm,Bermuda,Diamond,Whale,Comic,Mammoth,Green,Pass,Missile,Paste,Drop,Pheonix,Marble,Staff,Figure,Park,Centaur,Shadow,Fish,Cotton,Egypt,Theater,Scale,Fall,Track,Force,Dinosaur,Bill,Mine,Turkey,March,Contract,Bridge,Robin,Line,Plate,Band,Fire,Bank,Boom,Cat,Shot,Suit,Chocolate,Roulette,Mercury,Moon,Net,Lawyer,Satellite,Angel,Spider,Germany,Fork,Pitch,King,Crane,Trip,Dog,Conductor,Part,Bugle,Witch,Ketchup,Press,Spine,Worm,Alps,Bond,Pan,Beijing,Racket,Cross,Seal,Aztec,Maple,Parachute,Hotel,Berry,Soldier,Ray,Post,Greece,Square,Mass,Bat,Wave,Car,Smuggler,England,Crash,Tail,Card,Horn,Capital,Fence,Deck,Buffalo,Microscope,Jet,Duck,Ring,Train,Field,Gold,Tick,Check,Queen,Strike,Kangaroo,Spike,Scientist,Engine,Shakespeare,Wind,Kid,Embassy,Robot,Note,Ground,Draft,Ham,War,Mouse,Center,Chick,China,Bolt,Spot,Piano,Pupil,Plot,Lion,Police,Head,Litter,Concert,Mug,Vacuum,Atlantis,Straw,Switch,Skyscraper,Laser,Scuba Diver,Africa,Plastic,Dwarf,Lap,Life,Honey,Horseshoe,Unicorn,Spy,Pants,Wall,Paper,Sound,Ice,Tag,Web,Fan,Orange,Temple,Canada,Scorpion,Undertaker,Mail,Europe,Soul,Apple,Pole,Tap,Mouth,Ambulance,Dress,Ice Cream,Rabbit,Buck,Agent,Sock,Nut,Boot,Ghost,Oil,Superhero,Code,Kiwi,Hospital,Saturn,Film,Button,Snowman,Helicopter,Loch Ness,Log,Princess,Time,Cook,Revolution,Shoe,Mole,Spell,Grass,Washer,Game,Beat,Hole,Horse,Pirate,Link,Dance,Fly,Pit,Server,School,Lock,Brush,Pool,Star,Jam,Organ,Berlin,Face,Luck,Amazon,Cast,Gas,Club,Sink,Water,Chair,Shark,Jupiter,Copper,Jack,Platypus,Stick,Olive,Grace,Bear,Glass,Row,Pistol,London,Rock,Van,Vet,Beach,Charge,Port,Disease,Palm,Moscow,Pin,Washington,Pyramid,Opera,Casino,Pilot,String,Night,Chest,Yard,Teacher,Pumpkin,Thief,Bark,Bug,Mint,Cycle,Telescope,Calf,Air,Box,Mount,Thumb,Antarctica,Trunk,Snow,Penguin,Root,Bar,File,Hawk,Battery,Compound,Slug,Octopus,Whip,America,Ivory,Pound,Sub,Cliff,Lab,Eagle,Genius,Ship,Dice,Hood,Heart,Novel,Pipe,Himalayas,Crown,Round,India,Needle,Shop,Watch,Lead,Tie,Table,Cell,Cover,Czech,Back,Bomb,Ruler,Forest,Bottle,Space,Hook,Doctor,Ball,Bow,Degree,Rome,Plane,Giant,Nail,Dragon,Stadium,Flute,Carrot,Wake,Fighter,Model,Tokyo,Eye,Mexico,Hand,Swing,Key,Alien,Tower,Poison,Cricket,Cold,Knife,Church,Board,Cloak,Ninja,Olympus,Belt,Light,Death,Stock,Millionaire,Day,Knight,Pie,Bed,Circle,Rose,Change,Cap,Triangle";
return wordStr.Split(',');
}
private void checkIfGameWon()
{
Debug.Log("Score is: BLUE " + blueHits + " and RED " + redHits);
if (this.blueHits >= 9)
{
this.manager.BlueWin();
}
else if (this.redHits >= 8)
{
this.manager.RedWin();
}
}
private void createCards()
{
int[] idxArr = new int[25];
string[] possWords = possibleWords();
for (int i = 0; i < idxArr.Length; i++)
{
int rnd = Random.Range(0, possWords.Length);
string word = possWords[rnd];
if (word == "")
{
i--;
continue;
}
possWords[rnd] = "";
idxArr[i] = rnd;
}
string unfinishedWordIdxs = idxArr[0].ToString();
for (int i = 1; i < idxArr.Length; i++)
{
unfinishedWordIdxs = unfinishedWordIdxs + "," + idxArr[i].ToString();
}
asyncCardEvent = "MakeAllCards," + unfinishedWordIdxs;
Debug.Log("Emitting: " + asyncCardEvent);
}
public void MakeAllCards(string[] wordsArray)
{
Debug.Log("Creating for words: " + wordsArray.Length);
for (int i = 0; i < wordsArray.Length; i++)
{
string wordIdx = wordsArray[i];
if (wordIdx == "") { break; }
var agCard = this.createCard(this.agentView, this.cardPrefab, int.Parse(wordIdx), i);
var opCard = this.createCard(this.operatorView, this.smallCardPrefab, int.Parse(wordIdx), i);
this.agentCards[i] = agCard;
this.operatorCards[i] = opCard;
int type = this.layoutManager.CardType(i);
if (type == 0)
{
this.setCardBlue(this.operatorCards[i]);
}
else if (type == 1)
{
this.setCardRed(this.operatorCards[i]);
}
else if (type == 2)
{
this.setCardGrey(this.operatorCards[i]);
}
else
{
this.setCardBlack(this.operatorCards[i]);
}
}
Debug.Log("Done!");
}
private GameObject createCard(Canvas canvas, GameObject prefab, int cardIdx, int cardIndex)
{
string[] possWordsArr = possibleWords();
string word = possWordsArr[cardIdx];
GameObject card = VRCInstantiate(prefab);
card.transform.SetParent(canvas.transform);
RectTransform transform = card.GetComponent<RectTransform>();
transform.anchoredPosition3D = Vector3.zero;
transform.localRotation = new Quaternion();
transform.anchoredPosition3D = new Vector3((float)(cardIndex % 5 * transform.sizeDelta.x + 0.5 * transform.sizeDelta.x), (float)(-cardIndex / 5 * transform.sizeDelta.y + -0.5 * transform.sizeDelta.y), 0);
Text text = card.GetComponentInChildren<Text>();
text.text = word;
return card;
}
}