-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathQuestEditorData.cs
587 lines (518 loc) · 17 KB
/
QuestEditorData.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections.Generic;
using Assets.Scripts.Content;
using Assets.Scripts.UI;
// This class manages the Quest editor Interface
// FIXME: Rename, not a good name any more
public class QuestEditorData {
// Not used yet
//private readonly StringKey COMPONENT_TO_DELETE = new StringKey("val", "COMPONENT_TO_DELETE");
// This is the currently selected component
public EditorComponent selection;
// The selection stack is used for the 'back' button
public Stack<EditorComponent> selectionStack;
// Start the editor
public QuestEditorData()
{
Game.Get().qed = this;
selectionStack = new Stack<EditorComponent>();
// Start at the quest component
SelectQuest();
}
// Start the editor from old version
public QuestEditorData(QuestEditorData old)
{
Game.Get().qed = this;
selectionStack = new Stack<EditorComponent>();
if (old == null || old.selection == null)
{
// Start at the quest component
SelectQuest();
}
else
{
selectionStack = old.selectionStack;
selectionStack.Push(old.selection);
Back();
}
}
// Update component selection
// Used to save selection history
public void NewSelection(EditorComponent c)
{
// selection starts at null
if (selection != null)
{
selectionStack.Push(selection);
}
selection = c;
}
// Go back in the selection stack
public static void Back()
{
Game game = Game.Get();
// Check if there is anything to go back to
if (game.qed.selectionStack.Count == 0)
{
// Reset on quest selection
game.qed.selection = new EditorComponentQuest();
return;
}
game.qed.selection = game.qed.selectionStack.Pop();
// Quest is OK
if (game.qed.selection is EditorComponentQuest)
{
game.qed.selection.Update();
}
else if (game.CurrentQuest.qd.components.ContainsKey(game.qed.selection.name))
{
// Existing component
game.qed.selection.Update();
}
else
{
// History item has been deleted/renamed, go back further
Back();
}
}
// Open component selection top level
// Menu for selection of all component types, includes delete options
public static void TypeSelect(string type = "")
{
if (GameObject.FindGameObjectWithTag(Game.DIALOG) != null)
{
return;
}
Game game = Game.Get();
UIWindowSelectionListTraits select = new UIWindowSelectionListTraits(SelectComponent, CommonStringKeys.SELECT_ITEM);
select.AddNewComponentItem("Tile");
select.AddNewComponentItem("Token");
select.AddNewComponentItem("Spawn");
select.AddNewComponentItem("Event");
select.AddNewComponentItem("CustomMonster");
select.AddNewComponentItem("UI");
select.AddNewComponentItem("QItem");
if (game.gameType is D2EGameType || game.gameType is IAGameType)
{
select.AddNewComponentItem("Activation");
select.AddNewComponentItem("Door");
select.AddNewComponentItem("MPlace");
}
else
{
select.AddNewComponentItem("Puzzle");
}
Dictionary<string, IEnumerable<string>> traits = new Dictionary<string, IEnumerable<string>>();
traits.Add(CommonStringKeys.TYPE.Translate(), new string[] { "Quest" });
select.AddItem(CommonStringKeys.QUEST.Translate(), "Quest", traits);
foreach (QuestData.QuestComponent c in game.CurrentQuest.qd.components.Values)
{
if (!(c is PerilData))
{
select.AddItem(c);
}
}
select.Draw();
if (type.Length > 0)
{
select.SelectTrait(CommonStringKeys.TYPE.Translate(), new StringKey("val", type.ToUpper()).Translate());
}
}
// Select a component for editing
public static void SelectComponent(string name)
{
Game game = Game.Get();
QuestEditorData qed = game.qed;
// Quest is a special component
if (name.Equals("Quest"))
{
SelectQuest();
return;
}
// These are special strings for creating new objects
if (name.Equals("{NEW:Tile}"))
{
qed.NewTile();
return;
}
if (name.Equals("{NEW:Door}"))
{
qed.NewDoor();
return;
}
if (name.Equals("{NEW:Token}"))
{
qed.NewToken();
return;
}
if (name.Equals("{NEW:UI}"))
{
qed.NewUI();
return;
}
if (name.Equals("{NEW:Spawn}"))
{
qed.NewSpawn();
return;
}
if (name.Equals("{NEW:MPlace}"))
{
qed.NewMPlace();
return;
}
if (name.Equals("{NEW:QItem}"))
{
qed.NewItem();
return;
}
if (name.Equals("{NEW:CustomMonster}"))
{
qed.NewCustomMonster();
return;
}
if (name.Equals("{NEW:Activation}"))
{
qed.NewActivation();
return;
}
if (name.Equals("{NEW:Event}"))
{
qed.NewEvent();
return;
}
if (name.Equals("{NEW:Puzzle}"))
{
qed.NewPuzzle();
return;
}
// This may happen to due rename/delete
if (!game.CurrentQuest.qd.components.ContainsKey(name))
{
SelectQuest();
}
// Determine the component type and select
if (game.CurrentQuest.qd.components[name] is QuestData.Tile)
{
SelectAsTile(name);
return;
}
if (game.CurrentQuest.qd.components[name] is QuestData.Door)
{
SelectAsDoor(name);
return;
}
if (game.CurrentQuest.qd.components[name] is QuestData.Token)
{
SelectAsToken(name);
return;
}
if (game.CurrentQuest.qd.components[name] is QuestData.UI)
{
SelectAsUI(name);
return;
}
if (game.CurrentQuest.qd.components[name] is QuestData.Spawn)
{
SelectAsSpawn(name);
return;
}
if (game.CurrentQuest.qd.components[name] is QuestData.MPlace)
{
SelectAsMPlace(name);
return;
}
if (game.CurrentQuest.qd.components[name] is QuestData.Puzzle)
{
SelectAsPuzzle(name);
return;
}
if (game.CurrentQuest.qd.components[name] is QuestData.QItem)
{
SelectAsItem(name);
return;
}
if (game.CurrentQuest.qd.components[name] is QuestData.CustomMonster)
{
SelectAsCustomMonster(name);
return;
}
if (game.CurrentQuest.qd.components[name] is QuestData.Activation)
{
SelectAsActivation(name);
return;
}
if (game.CurrentQuest.qd.components[name] is QuestData.Event)
{
SelectAsEvent(name);
return;
}
}
public static void SelectQuest()
{
Game game = Game.Get();
game.qed.NewSelection(new EditorComponentQuest());
}
public static void SelectAsTile(string name)
{
Game game = Game.Get();
game.qed.NewSelection(new EditorComponentTile(name));
}
public static void SelectAsDoor(string name)
{
Game game = Game.Get();
game.qed.NewSelection(new EditorComponentDoor(name));
}
public static void SelectAsToken(string name)
{
Game game = Game.Get();
game.qed.NewSelection(new EditorComponentToken(name));
}
public static void SelectAsUI(string name)
{
Game game = Game.Get();
game.qed.NewSelection(new EditorComponentUI(name));
}
public static void SelectAsEvent(string name)
{
Game game = Game.Get();
game.qed.NewSelection(new EditorComponentEvent(name));
}
public static void SelectAsSpawn(string name)
{
Game game = Game.Get();
game.qed.NewSelection(new EditorComponentSpawn(name));
}
public static void SelectAsMPlace(string name)
{
Game game = Game.Get();
game.qed.NewSelection(new EditorComponentMPlace(name));
}
public static void SelectAsPuzzle(string name)
{
Game game = Game.Get();
game.qed.NewSelection(new EditorComponentPuzzle(name));
}
public static void SelectAsItem(string name)
{
Game game = Game.Get();
game.qed.NewSelection(new EditorComponentItem(name));
}
public static void SelectAsCustomMonster(string name)
{
Game game = Game.Get();
game.qed.NewSelection(new EditorComponentCustomMonster(name));
}
public static void SelectAsActivation(string name)
{
Game game = Game.Get();
game.qed.NewSelection(new EditorComponentActivation(name));
}
// Create a new tile, use next available number
public void NewTile()
{
Game game = Game.Get();
int index = 0;
while (game.CurrentQuest.qd.components.ContainsKey("Tile" + index))
{
index++;
}
QuestData.Tile tile = new QuestData.Tile("Tile" + index);
game.CurrentQuest.qd.components.Add("Tile" + index, tile);
CameraController cc = GameObject.FindObjectOfType<CameraController>();
tile.location.x = game.gameType.TileRound() * Mathf.Round(cc.gameObject.transform.position.x / game.gameType.TileRound());
tile.location.y = game.gameType.TileRound() * Mathf.Round(cc.gameObject.transform.position.y / game.gameType.TileRound());
game.CurrentQuest.Add("Tile" + index);
SelectComponent("Tile" + index);
}
public void NewDoor()
{
Game game = Game.Get();
int index = 0;
while (game.CurrentQuest.qd.components.ContainsKey("Door" + index))
{
index++;
}
QuestData.Door door = new QuestData.Door("Door" + index);
game.CurrentQuest.qd.components.Add("Door" + index, door);
CameraController cc = GameObject.FindObjectOfType<CameraController>();
door.location.x = game.gameType.SelectionRound() * Mathf.Round(cc.gameObject.transform.position.x / game.gameType.SelectionRound());
door.location.y = game.gameType.SelectionRound() * Mathf.Round(cc.gameObject.transform.position.y / game.gameType.SelectionRound());
game.CurrentQuest.Add("Door" + index);
SelectComponent("Door" + index);
}
public void NewToken()
{
Game game = Game.Get();
int index = 0;
while (game.CurrentQuest.qd.components.ContainsKey("Token" + index))
{
index++;
}
QuestData.Token token = new QuestData.Token("Token" + index);
game.CurrentQuest.qd.components.Add("Token" + index, token);
CameraController cc = GameObject.FindObjectOfType<CameraController>();
token.location.x = game.gameType.SelectionRound() * Mathf.Round(cc.gameObject.transform.position.x / game.gameType.SelectionRound());
token.location.y = game.gameType.SelectionRound() * Mathf.Round(cc.gameObject.transform.position.y / game.gameType.SelectionRound());
game.CurrentQuest.Add("Token" + index);
SelectComponent("Token" + index);
}
public void NewUI()
{
Game game = Game.Get();
int index = 0;
while (game.CurrentQuest.qd.components.ContainsKey("UI" + index))
{
index++;
}
QuestData.UI ui = new QuestData.UI("UI" + index);
ui.verticalUnits = true;
ui.richText = true;
game.CurrentQuest.qd.components.Add("UI" + index, ui);
SelectComponent("UI" + index);
}
public void NewSpawn()
{
Game game = Game.Get();
int index = 0;
while (game.CurrentQuest.qd.components.ContainsKey("Spawn" + index))
{
index++;
}
game.CurrentQuest.qd.components.Add("Spawn" + index, new QuestData.Spawn("Spawn" + index));
SelectComponent("Spawn" + index);
}
public void NewMPlace()
{
Game game = Game.Get();
int index = 0;
while (game.CurrentQuest.qd.components.ContainsKey("MPlace" + index))
{
index++;
}
game.CurrentQuest.qd.components.Add("MPlace" + index, new QuestData.MPlace("MPlace" + index));
SelectComponent("MPlace" + index);
}
public void NewEvent()
{
Game game = Game.Get();
int index = 0;
while (game.CurrentQuest.qd.components.ContainsKey("Event" + index))
{
index++;
}
game.CurrentQuest.qd.components.Add("Event" + index, new QuestData.Event("Event" + index));
SelectComponent("Event" + index);
}
public void NewPuzzle()
{
Game game = Game.Get();
int index = 0;
while (game.CurrentQuest.qd.components.ContainsKey("Puzzle" + index))
{
index++;
}
game.CurrentQuest.qd.components.Add("Puzzle" + index, new QuestData.Puzzle("Puzzle" + index));
SelectComponent("Puzzle" + index);
}
public void NewItem()
{
Game game = Game.Get();
int index = 0;
while (game.CurrentQuest.qd.components.ContainsKey("QItem" + index))
{
index++;
}
game.CurrentQuest.qd.components.Add("QItem" + index, new QuestData.QItem("QItem" + index));
SelectComponent("QItem" + index);
}
public void NewCustomMonster()
{
Game game = Game.Get();
int index = 0;
while (game.CurrentQuest.qd.components.ContainsKey("CustomMonster" + index))
{
index++;
}
game.CurrentQuest.qd.components.Add("CustomMonster" + index, new QuestData.CustomMonster("CustomMonster" + index));
SelectComponent("CustomMonster" + index);
}
public void NewActivation()
{
Game game = Game.Get();
int index = 0;
while (game.CurrentQuest.qd.components.ContainsKey("Activation" + index))
{
index++;
}
game.CurrentQuest.qd.components.Add("Activation" + index, new QuestData.Activation("Activation" + index));
SelectComponent("Activation" + index);
}
// Item selected from list for deletion
public static void DeleteCurrentComponent()
{
Game game = Game.Get();
string name = game.qed.selection.name;
Destroyer.Dialog();
if (name.Length == 0) return;
// Remove all references to the deleted component
foreach (KeyValuePair<string, QuestData.QuestComponent> kv in game.CurrentQuest.qd.components)
{
kv.Value.RemoveReference(name);
}
// Remove the component
if (game.CurrentQuest.qd.components.ContainsKey(name))
{
game.CurrentQuest.qd.components.Remove(name);
}
LocalizationRead.dicts["qst"].RemoveKeyPrefix(name + ".");
// Clean up the current quest environment
game.CurrentQuest.Remove(name);
Back();
}
// Cancel a component selection, clean up
public static void Cancel()
{
foreach (GameObject go in GameObject.FindGameObjectsWithTag(Game.DIALOG))
Object.Destroy(go);
}
// This is called by game
public void MouseDown()
{
selection.MouseDown();
}
// This is called by game
public void RightClick()
{
if (GameObject.FindGameObjectWithTag(Game.DIALOG) != null)
{
return;
}
PointerEventData pointer = new PointerEventData(EventSystem.current);
pointer.position = Input.mousePosition;
List<RaycastResult> raycastResults = new List<RaycastResult>();
EventSystem.current.RaycastAll(pointer, raycastResults);
if (raycastResults.Count == 0) return;
UIWindowSelectionListTraits select = new UIWindowSelectionListTraits(SelectComponent, CommonStringKeys.SELECT_ITEM);
int count = 0;
string last = "";
foreach (RaycastResult hit in raycastResults)
{
foreach (KeyValuePair<string, Quest.BoardComponent> kv in Game.Get().CurrentQuest.boardItems)
{
if (kv.Value.unityObject == hit.gameObject)
{
if (kv.Key.IndexOf("UI") != 0)
{
last = kv.Key;
count++;
select.AddItem(Game.Get().CurrentQuest.qd.components[kv.Key]);
}
break;
}
}
}
if (count == 1) SelectComponent(last);
if (count > 1) select.Draw();
}
}