forked from Angus-Fan/TurnBasedStrategyGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tileMapScript.cs
1125 lines (936 loc) · 42.4 KB
/
tileMapScript.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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class tileMapScript : MonoBehaviour
{
//Reference holders for the other two scripts that are currently running
//alongside this script
[Header("Manager Scripts")]
public battleManagerScript BMS;
public gameManagerScript GMS;
//List of tiles that are used to generate the map
//Try chaging tilesTypes to enum later
[Header("Tiles")]
public Tile[] tileTypes;
public int[,] tiles;
//This is used when the game starts and there are pre-existing units
//It uses this variable to check if there are any units and then maps them to the proper tiles
[Header("Units on the board")]
public GameObject unitsOnBoard;
//This 2d array is the list of tile gameObjects on the board
public GameObject[,] tilesOnMap;
//This 2d array is the list of quadUI gameObjects on the board
public GameObject[,] quadOnMap;
public GameObject[,] quadOnMapForUnitMovementDisplay;
public GameObject[,] quadOnMapCursor;
//public is only to set them in the inspector, if you change these to private then you will
//need to re-enable them in the inspector
//Game object that is used to overlay onto the tiles to show possible movement
public GameObject mapUI;
//Game object that is used to highlight the mouse location
public GameObject mapCursorUI;
//Game object that is used to highlight the path the unit is taking
public GameObject mapUnitMovementUI;
//Nodes along the path of shortest path from the pathfinding
public List<Node> currentPath = null;
//Node graph for pathfinding purposes
public Node[,] graph;
//containers (parent gameObjects) for the UI tiles
[Header("Containers")]
public GameObject tileContainer;
public GameObject UIQuadPotentialMovesContainer;
public GameObject UIQuadCursorContainer;
public GameObject UIUnitMovementPathContainer;
//Set in the inspector, might change this otherwise.
//This is the map size (please put positive numbers it probably wont work well with negative numbers)
[Header("Board Size")]
public int mapSizeX;
public int mapSizeY;
//In the update() function mouse down raycast sets this unit
[Header("Selected Unit Info")]
public GameObject selectedUnit;
//These two are set in the highlightUnitRange() function
//They are used for other things as well, mainly to check for movement, or finalize function
public HashSet<Node> selectedUnitTotalRange;
public HashSet<Node> selectedUnitMoveRange;
public bool unitSelected = false;
public int unitSelectedPreviousX;
public int unitSelectedPreviousY;
public GameObject previousOccupiedTile;
//public AudioSource selectedSound;
//public AudioSource unselectedSound;
//public area to set the material for the quad material for UI purposes
[Header("Materials")]
public Material greenUIMat;
public Material redUIMat;
public Material blueUIMat;
private void Start()
{
//Get the battlemanager running
//BMS = GetComponent<battleManagerScript>();
//GMS = GetComponent<gameManagerScript>();
//Generate the map info that will be used
generateMapInfo();
//Generate pathfinding graph
generatePathFindingGraph();
//With the generated info this function will read the info and produce the map
generateMapVisuals();
//Check if there are any pre-existing units on the board
setIfTileIsOccupied();
}
private void Update()
{
//If input is left mouse down then select the unit
if (Input.GetMouseButtonDown(0))
{
if (selectedUnit == null)
{
//mouseClickToSelectUnit();
mouseClickToSelectUnitV2();
}
//After a unit has been selected then if we get a mouse click, we need to check if the unit has entered the selection state (1) 'Selected'
//Move the unit
else if (selectedUnit.GetComponent<UnitScript>().unitMoveState == selectedUnit.GetComponent<UnitScript>().getMovementStateEnum(1) && selectedUnit.GetComponent<UnitScript>().movementQueue.Count == 0)
{
if ( selectTileToMoveTo())
{
//selectedSound.Play();
Debug.Log("movement path has been located");
unitSelectedPreviousX = selectedUnit.GetComponent<UnitScript>().x;
unitSelectedPreviousY = selectedUnit.GetComponent<UnitScript>().y;
previousOccupiedTile = selectedUnit.GetComponent<UnitScript>().tileBeingOccupied;
selectedUnit.GetComponent<UnitScript>().setWalkingAnimation();
moveUnit();
StartCoroutine(moveUnitAndFinalize());
//The moveUnit function calls a function on the unitScriptm when the movement is completed the finalization is called from that script.
}
}
//Finalize the movement
else if(selectedUnit.GetComponent<UnitScript>().unitMoveState == selectedUnit.GetComponent<UnitScript>().getMovementStateEnum(2))
{
finalizeOption();
}
}
//Unselect unit with the right click
if (Input.GetMouseButtonDown(1))
{
if (selectedUnit != null)
{
if (selectedUnit.GetComponent<UnitScript>().movementQueue.Count == 0 && selectedUnit.GetComponent<UnitScript>().combatQueue.Count==0)
{
if (selectedUnit.GetComponent<UnitScript>().unitMoveState != selectedUnit.GetComponent<UnitScript>().getMovementStateEnum(3))
{
//unselectedSound.Play();
selectedUnit.GetComponent<UnitScript>().setIdleAnimation();
deselectUnit();
}
}
else if (selectedUnit.GetComponent<UnitScript>().movementQueue.Count == 1)
{
selectedUnit.GetComponent<UnitScript>().visualMovementSpeed = 0.5f;
}
}
}
}
//This is from quill18Create's tutorial
//You can find it by searching for grid based movement on youtube, he goes into explaining how everything works
//The map layouts a bit different
//all this does is set the tiles[x,y] to the corresponding tile
public void generateMapInfo()
{
tiles = new int[mapSizeX, mapSizeY];
for (int x = 0; x < mapSizeX; x++)
{
for (int y = 0; y < mapSizeY; y++)
{
tiles[x, y] = 0;
}
}
tiles[2, 7] = 2;
tiles[3, 7] = 2;
tiles[6, 7] = 2;
tiles[7, 7] = 2;
tiles[2, 2] = 2;
tiles[3, 2] = 2;
tiles[6, 2] = 2;
tiles[7, 2] = 2;
tiles[0, 3] = 3;
tiles[1, 3] = 3;
tiles[0, 2] = 3;
tiles[1, 2] = 3;
tiles[0, 6] = 3;
tiles[1, 6] = 3;
tiles[2, 6] = 3;
tiles[0, 7] = 3;
tiles[1, 7] = 3;
tiles[2, 3] = 3;
tiles[0, 4] = 1;
tiles[0, 5] = 1;
tiles[1, 4] = 1;
tiles[1, 5] = 1;
tiles[2, 4] = 3;
tiles[2, 5] = 3;
tiles[4, 4] = 1;
tiles[5, 4] = 1;
tiles[4, 5] = 1;
tiles[5, 5] = 1;
tiles[7, 3] = 3;
tiles[8, 3] = 3;
tiles[9, 3] = 3;
tiles[8, 2] = 3;
tiles[9, 2] = 3;
tiles[7, 4] = 3;
tiles[7, 5] = 3;
tiles[7, 6] = 3;
tiles[8, 6] = 3;
tiles[9, 6] = 3;
tiles[8, 7] = 3;
tiles[9, 7] = 3;
tiles[8, 4] = 1;
tiles[8, 5] = 1;
tiles[9, 4] = 1;
tiles[9, 5] = 1;
}
//Creates the graph for the pathfinding, it sets up the neighbours
//This is also from Quill18Create's tutorial
public void generatePathFindingGraph()
{
graph = new Node[mapSizeX, mapSizeY];
//initialize graph
for (int x = 0; x < mapSizeX; x++)
{
for (int y = 0; y < mapSizeY; y++)
{
graph[x, y] = new Node();
graph[x, y].x = x;
graph[x, y].y = y;
}
}
//calculate neighbours
for (int x = 0; x < mapSizeX; x++)
{
for (int y = 0; y < mapSizeY; y++)
{
//X is not 0, then we can add left (x - 1)
if (x > 0)
{
graph[x, y].neighbours.Add(graph[x - 1, y]);
}
//X is not mapSizeX - 1, then we can add right (x + 1)
if (x < mapSizeX-1)
{
graph[x, y].neighbours.Add(graph[x + 1, y]);
}
//Y is not 0, then we can add downwards (y - 1 )
if (y > 0)
{
graph[x, y].neighbours.Add(graph[x, y - 1]);
}
//Y is not mapSizeY -1, then we can add upwards (y + 1)
if (y < mapSizeY - 1)
{
graph[x, y].neighbours.Add(graph[x, y + 1]);
}
}
}
}
//In:
//Out: void
//Desc: This instantiates all the information for the map, the UI Quads and the map tiles
public void generateMapVisuals()
{
//generate list of actual tileGameObjects
tilesOnMap = new GameObject[mapSizeX, mapSizeY];
quadOnMap = new GameObject[mapSizeX, mapSizeY];
quadOnMapForUnitMovementDisplay = new GameObject[mapSizeX, mapSizeY];
quadOnMapCursor = new GameObject[mapSizeX, mapSizeY];
int index;
for (int x = 0; x < mapSizeX; x++)
{
for (int y = 0; y < mapSizeY; y++)
{
index = tiles[x, y];
GameObject newTile = Instantiate(tileTypes[index].tileVisualPrefab, new Vector3(x, 0, y), Quaternion.identity);
newTile.GetComponent<ClickableTileScript>().tileX = x;
newTile.GetComponent<ClickableTileScript>().tileY = y;
newTile.GetComponent<ClickableTileScript>().map = this;
newTile.transform.SetParent(tileContainer.transform);
tilesOnMap[x, y] = newTile;
GameObject gridUI = Instantiate(mapUI, new Vector3(x, 0.501f, y),Quaternion.Euler(90f,0,0));
gridUI.transform.SetParent(UIQuadPotentialMovesContainer.transform);
quadOnMap[x, y] = gridUI;
GameObject gridUIForPathfindingDisplay = Instantiate(mapUnitMovementUI, new Vector3(x, 0.502f, y), Quaternion.Euler(90f, 0, 0));
gridUIForPathfindingDisplay.transform.SetParent(UIUnitMovementPathContainer.transform);
quadOnMapForUnitMovementDisplay[x, y] = gridUIForPathfindingDisplay;
GameObject gridUICursor = Instantiate(mapCursorUI, new Vector3(x, 0.503f, y), Quaternion.Euler(90f, 0, 0));
gridUICursor.transform.SetParent(UIQuadCursorContainer.transform);
quadOnMapCursor[x, y] = gridUICursor;
}
}
}
//Moves the unit
public void moveUnit()
{
if (selectedUnit != null)
{
selectedUnit.GetComponent<UnitScript>().MoveNextTile();
}
}
//In: the x and y of a tile
//Out: vector 3 of the tile in world space, theyre .75f off of zero
//Desc: returns a vector 3 of the tile in world space, theyre .75f off of zero
public Vector3 tileCoordToWorldCoord(int x, int y)
{
return new Vector3(x, 0.75f, y);
}
//In:
//Out: void
//Desc: sets the tile as occupied, if a unit is on the tile
public void setIfTileIsOccupied()
{
foreach (Transform team in unitsOnBoard.transform)
{
//Debug.Log("Set if Tile is Occupied is Called");
foreach (Transform unitOnTeam in team) {
int unitX = unitOnTeam.GetComponent<UnitScript>().x;
int unitY = unitOnTeam.GetComponent<UnitScript>().y;
unitOnTeam.GetComponent<UnitScript>().tileBeingOccupied = tilesOnMap[unitX, unitY];
tilesOnMap[unitX, unitY].GetComponent<ClickableTileScript>().unitOnTile = unitOnTeam.gameObject;
}
}
}
//In: x and y position of the tile to move to
//Out: void
//Desc: generates the path for the selected unit
//Think this one is also partially from Quill18Create's tutorial
public void generatePathTo(int x, int y)
{
if (selectedUnit.GetComponent<UnitScript>().x == x && selectedUnit.GetComponent<UnitScript>().y == y){
Debug.Log("clicked the same tile that the unit is standing on");
currentPath = new List<Node>();
selectedUnit.GetComponent<UnitScript>().path = currentPath;
return;
}
if (unitCanEnterTile(x, y) == false)
{
//cant move into something so we can probably just return
//cant set this endpoint as our goal
return;
}
selectedUnit.GetComponent<UnitScript>().path = null;
currentPath = null;
//from wiki dijkstra's
Dictionary<Node, float> dist = new Dictionary<Node, float>();
Dictionary<Node, Node> prev = new Dictionary<Node, Node>();
Node source = graph[selectedUnit.GetComponent<UnitScript>().x, selectedUnit.GetComponent<UnitScript>().y];
Node target = graph[x, y];
dist[source] = 0;
prev[source] = null;
//Unchecked nodes
List<Node> unvisited = new List<Node>();
//Initialize
foreach (Node n in graph)
{
//Initialize to infite distance as we don't know the answer
//Also some places are infinity
if (n != source)
{
dist[n] = Mathf.Infinity;
prev[n] = null;
}
unvisited.Add(n);
}
//if there is a node in the unvisited list lets check it
while (unvisited.Count > 0)
{
//u will be the unvisited node with the shortest distance
Node u = null;
foreach (Node possibleU in unvisited)
{
if (u == null || dist[possibleU] < dist[u])
{
u = possibleU;
}
}
if (u == target)
{
break;
}
unvisited.Remove(u);
foreach (Node n in u.neighbours)
{
//float alt = dist[u] + u.DistanceTo(n);
float alt = dist[u] + costToEnterTile(n.x, n.y);
if (alt < dist[n])
{
dist[n] = alt;
prev[n] = u;
}
}
}
//if were here we found shortest path, or no path exists
if (prev[target] == null)
{
//No route;
return;
}
currentPath = new List<Node>();
Node curr = target;
//Step through the current path and add it to the chain
while (curr != null)
{
currentPath.Add(curr);
curr = prev[curr];
}
//Now currPath is from target to our source, we need to reverse it from source to target.
currentPath.Reverse();
selectedUnit.GetComponent<UnitScript>().path = currentPath;
}
//In: tile's x and y position
//Out: cost that is requiredd to enter the tile
//Desc: checks the cost of the tile for a unit to enter
public float costToEnterTile(int x, int y)
{
if (unitCanEnterTile(x, y) == false)
{
return Mathf.Infinity;
}
//Gotta do the math here
Tile t = tileTypes[tiles[x, y]];
float dist = t.movementCost;
return dist;
}
//change this when we add movement types
//In: tile's x and y position
//Out: true or false if the unit can enter the tile that was entered
//Desc: if the tile is not occupied by another team's unit, then you can walk through and if the tile is walkable
public bool unitCanEnterTile(int x, int y)
{
if (tilesOnMap[x, y].GetComponent<ClickableTileScript>().unitOnTile != null)
{
if (tilesOnMap[x, y].GetComponent<ClickableTileScript>().unitOnTile.GetComponent<UnitScript>().teamNum != selectedUnit.GetComponent<UnitScript>().teamNum)
{
return false;
}
}
return tileTypes[tiles[x, y]].isWalkable;
}
//In:
//Out: void
//Desc: uses a raycast to see where the mouse is pointing, this is used to select units
public void mouseClickToSelectUnit()
{
GameObject tempSelectedUnit;
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
//Debug.Log(hit.transform.tag);
if (unitSelected == false)
{
if (hit.transform.gameObject.CompareTag("Tile"))
{
if (hit.transform.GetComponent<ClickableTileScript>().unitOnTile != null)
{
tempSelectedUnit = hit.transform.GetComponent<ClickableTileScript>().unitOnTile;
if (tempSelectedUnit.GetComponent<UnitScript>().unitMoveState == tempSelectedUnit.GetComponent<UnitScript>().getMovementStateEnum(0)
&& tempSelectedUnit.GetComponent<UnitScript>().teamNum == GMS.currentTeam
)
{
disableHighlightUnitRange();
selectedUnit = tempSelectedUnit;
selectedUnit.GetComponent<UnitScript>().map = this;
selectedUnit.GetComponent<UnitScript>().setMovementState(1);
unitSelected = true;
highlightUnitRange();
}
}
}
else if (hit.transform.parent != null && hit.transform.parent.gameObject.CompareTag("Unit"))
{
tempSelectedUnit = hit.transform.parent.gameObject;
if (tempSelectedUnit.GetComponent<UnitScript>().unitMoveState == tempSelectedUnit.GetComponent<UnitScript>().getMovementStateEnum(0)
&& tempSelectedUnit.GetComponent<UnitScript>().teamNum == GMS.currentTeam
)
{
disableHighlightUnitRange();
selectedUnit = tempSelectedUnit;
selectedUnit.GetComponent<UnitScript>().setMovementState(1);
//These were here before I don't think they do anything the unit location is set beforehand
//selectedUnit.GetComponent<UnitScript>().x = (int)selectedUnit.transform.position.x;
// selectedUnit.GetComponent<UnitScript>().y = (int)selectedUnit.transform.position.z;
selectedUnit.GetComponent<UnitScript>().map = this;
unitSelected = true;
highlightUnitRange();
}
}
}
}
}
//In:
//Out: void
//Desc: finalizes the movement, sets the tile the unit moved to as occupied, etc
public void finalizeMovementPosition()
{
tilesOnMap[selectedUnit.GetComponent<UnitScript>().x, selectedUnit.GetComponent<UnitScript>().y].GetComponent<ClickableTileScript>().unitOnTile = selectedUnit;
//After a unit has been moved we will set the unitMoveState to (2) the 'Moved' state
selectedUnit.GetComponent<UnitScript>().setMovementState(2);
highlightUnitAttackOptionsFromPosition();
highlightTileUnitIsOccupying();
}
//In:
//Out: void
//Desc: selects a unit based on the cursor from the other script
public void mouseClickToSelectUnitV2()
{
if (unitSelected == false && GMS.tileBeingDisplayed!=null)
{
if (GMS.tileBeingDisplayed.GetComponent<ClickableTileScript>().unitOnTile != null)
{
GameObject tempSelectedUnit = GMS.tileBeingDisplayed.GetComponent<ClickableTileScript>().unitOnTile;
if (tempSelectedUnit.GetComponent<UnitScript>().unitMoveState == tempSelectedUnit.GetComponent<UnitScript>().getMovementStateEnum(0)
&& tempSelectedUnit.GetComponent<UnitScript>().teamNum == GMS.currentTeam
)
{
disableHighlightUnitRange();
//selectedSound.Play();
selectedUnit = tempSelectedUnit;
selectedUnit.GetComponent<UnitScript>().map = this;
selectedUnit.GetComponent<UnitScript>().setMovementState(1);
selectedUnit.GetComponent<UnitScript>().setSelectedAnimation();
unitSelected = true;
highlightUnitRange();
}
}
}
}
//In:
//Out: void
//Desc: finalizes the player's option, wait or attack
public void finalizeOption()
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
HashSet<Node> attackableTiles = getUnitAttackOptionsFromPosition();
if (Physics.Raycast(ray, out hit))
{
//This portion is to ensure that the tile has been clicked
//If the tile has been clicked then we need to check if there is a unit on it
if (hit.transform.gameObject.CompareTag("Tile"))
{
if (hit.transform.GetComponent<ClickableTileScript>().unitOnTile != null)
{
GameObject unitOnTile = hit.transform.GetComponent<ClickableTileScript>().unitOnTile;
int unitX = unitOnTile.GetComponent<UnitScript>().x;
int unitY = unitOnTile.GetComponent<UnitScript>().y;
if (unitOnTile == selectedUnit)
{
disableHighlightUnitRange();
Debug.Log("ITS THE SAME UNIT JUST WAIT");
selectedUnit.GetComponent<UnitScript>().wait();
selectedUnit.GetComponent<UnitScript>().setWaitIdleAnimation();
selectedUnit.GetComponent<UnitScript>().setMovementState(3);
deselectUnit();
}
else if (unitOnTile.GetComponent<UnitScript>().teamNum != selectedUnit.GetComponent<UnitScript>().teamNum && attackableTiles.Contains(graph[unitX,unitY]))
{
if (unitOnTile.GetComponent<UnitScript>().currentHealthPoints > 0)
{
Debug.Log("We clicked an enemy that should be attacked");
Debug.Log(selectedUnit.GetComponent<UnitScript>().currentHealthPoints);
StartCoroutine(BMS.attack(selectedUnit, unitOnTile));
StartCoroutine(deselectAfterMovements(selectedUnit, unitOnTile));
}
}
}
}
else if (hit.transform.parent != null && hit.transform.parent.gameObject.CompareTag("Unit"))
{
GameObject unitClicked = hit.transform.parent.gameObject;
int unitX = unitClicked.GetComponent<UnitScript>().x;
int unitY = unitClicked.GetComponent<UnitScript>().y;
if (unitClicked == selectedUnit)
{
disableHighlightUnitRange();
Debug.Log("ITS THE SAME UNIT JUST WAIT");
selectedUnit.GetComponent<UnitScript>().wait();
selectedUnit.GetComponent<UnitScript>().setWaitIdleAnimation();
selectedUnit.GetComponent<UnitScript>().setMovementState(3);
deselectUnit();
}
else if (unitClicked.GetComponent<UnitScript>().teamNum != selectedUnit.GetComponent<UnitScript>().teamNum && attackableTiles.Contains(graph[unitX, unitY]))
{
if (unitClicked.GetComponent<UnitScript>().currentHealthPoints > 0)
{
Debug.Log("We clicked an enemy that should be attacked");
Debug.Log("Add Code to Attack enemy");
//selectedUnit.GetComponent<UnitScript>().setAttackAnimation();
StartCoroutine(BMS.attack(selectedUnit, unitClicked));
// selectedUnit.GetComponent<UnitScript>().wait();
//Check if soemone has won
//GMS.checkIfUnitsRemain();
StartCoroutine(deselectAfterMovements(selectedUnit, unitClicked));
}
}
}
}
}
//In:
//Out: void
//Desc: de-selects the unit
public void deselectUnit()
{
if (selectedUnit != null)
{
if (selectedUnit.GetComponent<UnitScript>().unitMoveState == selectedUnit.GetComponent<UnitScript>().getMovementStateEnum(1))
{
disableHighlightUnitRange();
disableUnitUIRoute();
selectedUnit.GetComponent<UnitScript>().setMovementState(0);
selectedUnit = null;
unitSelected = false;
}
else if (selectedUnit.GetComponent<UnitScript>().unitMoveState == selectedUnit.GetComponent<UnitScript>().getMovementStateEnum(3) )
{
disableHighlightUnitRange();
disableUnitUIRoute();
unitSelected = false;
selectedUnit = null;
}
else
{
disableHighlightUnitRange();
disableUnitUIRoute();
tilesOnMap[selectedUnit.GetComponent<UnitScript>().x, selectedUnit.GetComponent<UnitScript>().y].GetComponent<ClickableTileScript>().unitOnTile = null;
tilesOnMap[unitSelectedPreviousX, unitSelectedPreviousY].GetComponent<ClickableTileScript>().unitOnTile = selectedUnit;
selectedUnit.GetComponent<UnitScript>().x = unitSelectedPreviousX;
selectedUnit.GetComponent<UnitScript>().y = unitSelectedPreviousY;
selectedUnit.GetComponent<UnitScript>().tileBeingOccupied = previousOccupiedTile;
selectedUnit.transform.position = tileCoordToWorldCoord(unitSelectedPreviousX, unitSelectedPreviousY);
selectedUnit.GetComponent<UnitScript>().setMovementState(0);
selectedUnit = null;
unitSelected = false;
}
}
}
//In:
//Out: void
//Desc: highlights the units range options (this is the portion shown in the video)
public void highlightUnitRange()
{
HashSet<Node> finalMovementHighlight = new HashSet<Node>();
HashSet<Node> totalAttackableTiles = new HashSet<Node>();
HashSet<Node> finalEnemyUnitsInMovementRange = new HashSet<Node>();
int attRange = selectedUnit.GetComponent<UnitScript>().attackRange;
int moveSpeed = selectedUnit.GetComponent<UnitScript>().moveSpeed;
Node unitInitialNode = graph[selectedUnit.GetComponent<UnitScript>().x, selectedUnit.GetComponent<UnitScript>().y];
finalMovementHighlight = getUnitMovementOptions();
totalAttackableTiles = getUnitTotalAttackableTiles(finalMovementHighlight, attRange, unitInitialNode);
//Debug.Log("There are this many available tiles for the unit: "+finalMovementHighlight.Count);
foreach (Node n in totalAttackableTiles)
{
if (tilesOnMap[n.x, n.y].GetComponent<ClickableTileScript>().unitOnTile != null)
{
GameObject unitOnCurrentlySelectedTile = tilesOnMap[n.x, n.y].GetComponent<ClickableTileScript>().unitOnTile;
if (unitOnCurrentlySelectedTile.GetComponent<UnitScript>().teamNum != selectedUnit.GetComponent<UnitScript>().teamNum)
{
finalEnemyUnitsInMovementRange.Add(n);
}
}
}
highlightEnemiesInRange(totalAttackableTiles);
//highlightEnemiesInRange(finalEnemyUnitsInMovementRange);
highlightMovementRange(finalMovementHighlight);
//Debug.Log(finalMovementHighlight.Count);
selectedUnitMoveRange = finalMovementHighlight;
//This final bit sets the selected Units tiles, which can be accessible in other functions
//Probably bad practice, but I'll need to get things to work for now (2019-09-30)
selectedUnitTotalRange = getUnitTotalRange(finalMovementHighlight, totalAttackableTiles);
//Debug.Log(unionTiles.Count);
//Debug.Log("exiting the while loop");
//This will for each loop will highlight the movement range of the units
}
//In:
//Out: void
//Desc: disables the quads that are being used to highlight position
public void disableUnitUIRoute()
{
foreach(GameObject quad in quadOnMapForUnitMovementDisplay)
{
if (quad.GetComponent<Renderer>().enabled == true)
{
quad.GetComponent<Renderer>().enabled = false;
}
}
}
//In:
//Out: HashSet<Node> of the tiles that can be reached by unit
//Desc: returns the hashSet of nodes that the unit can reach from its position
public HashSet<Node> getUnitMovementOptions()
{
float[,] cost = new float[mapSizeX, mapSizeY];
HashSet<Node> UIHighlight = new HashSet<Node>();
HashSet<Node> tempUIHighlight = new HashSet<Node>();
HashSet<Node> finalMovementHighlight = new HashSet<Node>();
int moveSpeed = selectedUnit.GetComponent<UnitScript>().moveSpeed;
Node unitInitialNode = graph[selectedUnit.GetComponent<UnitScript>().x, selectedUnit.GetComponent<UnitScript>().y];
///Set-up the initial costs for the neighbouring nodes
finalMovementHighlight.Add(unitInitialNode);
foreach (Node n in unitInitialNode.neighbours)
{
cost[n.x, n.y] = costToEnterTile(n.x, n.y);
//Debug.Log(cost[n.x, n.y]);
if (moveSpeed - cost[n.x, n.y] >= 0)
{
UIHighlight.Add(n);
}
}
finalMovementHighlight.UnionWith(UIHighlight);
while (UIHighlight.Count != 0)
{
foreach (Node n in UIHighlight)
{
foreach (Node neighbour in n.neighbours)
{
if (!finalMovementHighlight.Contains(neighbour))
{
cost[neighbour.x, neighbour.y] = costToEnterTile(neighbour.x, neighbour.y) + cost[n.x, n.y];
//Debug.Log(cost[neighbour.x, neighbour.y]);
if (moveSpeed - cost[neighbour.x, neighbour.y] >= 0)
{
//Debug.Log(cost[neighbour.x, neighbour.y]);
tempUIHighlight.Add(neighbour);
}
}
}
}
UIHighlight = tempUIHighlight;
finalMovementHighlight.UnionWith(UIHighlight);
tempUIHighlight = new HashSet<Node>();
}
Debug.Log("The total amount of movable spaces for this unit is: " + finalMovementHighlight.Count);
Debug.Log("We have used the function to calculate it this time");
return finalMovementHighlight;
}
//In: finalMovement highlight and totalAttackabletiles
//Out: a hashSet of nodes that are the combination of the two inputs
//Desc: returns the unioned hashSet
public HashSet<Node> getUnitTotalRange(HashSet<Node> finalMovementHighlight, HashSet<Node> totalAttackableTiles)
{
HashSet<Node> unionTiles = new HashSet<Node>();
unionTiles.UnionWith(finalMovementHighlight);
//unionTiles.UnionWith(finalEnemyUnitsInMovementRange);
unionTiles.UnionWith(totalAttackableTiles);
return unionTiles;
}
//In: finalMovement highlight, the attack range of the unit, and the initial node that the unit was standing on
//Out: hashSet Node of the total attackable tiles for the unit
//Desc: returns a set of nodes that represent the unit's total attackable tiles
public HashSet<Node> getUnitTotalAttackableTiles(HashSet<Node> finalMovementHighlight, int attRange, Node unitInitialNode)
{
HashSet<Node> tempNeighbourHash = new HashSet<Node>();
HashSet<Node> neighbourHash = new HashSet<Node>();
HashSet<Node> seenNodes = new HashSet<Node>();
HashSet<Node> totalAttackableTiles = new HashSet<Node>();
foreach (Node n in finalMovementHighlight)
{
neighbourHash = new HashSet<Node>();
neighbourHash.Add(n);
for (int i = 0; i < attRange; i++)
{
foreach (Node t in neighbourHash)
{
foreach (Node tn in t.neighbours)
{
tempNeighbourHash.Add(tn);
}
}
neighbourHash = tempNeighbourHash;
tempNeighbourHash = new HashSet<Node>();
if (i < attRange - 1)
{
seenNodes.UnionWith(neighbourHash);
}
}
neighbourHash.ExceptWith(seenNodes);
seenNodes = new HashSet<Node>();
totalAttackableTiles.UnionWith(neighbourHash);
}
totalAttackableTiles.Remove(unitInitialNode);
//Debug.Log("The unit node has this many attack options" + totalAttackableTiles.Count);
return (totalAttackableTiles);
}
//In:
//Out: hashSet of nodes get all the attackable tiles from the current position
//Desc: returns a set of nodes that are all the attackable tiles from the units current position
public HashSet<Node> getUnitAttackOptionsFromPosition()
{
HashSet<Node> tempNeighbourHash = new HashSet<Node>();
HashSet<Node> neighbourHash = new HashSet<Node>();
HashSet<Node> seenNodes = new HashSet<Node>();
Node initialNode = graph[selectedUnit.GetComponent<UnitScript>().x, selectedUnit.GetComponent<UnitScript>().y];
int attRange = selectedUnit.GetComponent<UnitScript>().attackRange;
neighbourHash = new HashSet<Node>();
neighbourHash.Add(initialNode);
for (int i = 0; i < attRange; i++)
{
foreach (Node t in neighbourHash)
{
foreach (Node tn in t.neighbours)
{
tempNeighbourHash.Add(tn);
}
}
neighbourHash = tempNeighbourHash;
tempNeighbourHash = new HashSet<Node>();
if (i < attRange - 1)
{
seenNodes.UnionWith(neighbourHash);
}
}
neighbourHash.ExceptWith(seenNodes);
neighbourHash.Remove(initialNode);
return neighbourHash;
}
//In:
//Out: hashSet node that the unit is currently occupying
//Desc: returns a set of nodes of the tile that the unit is occupying
public HashSet<Node> getTileUnitIsOccupying()
{
int x = selectedUnit.GetComponent<UnitScript>().x;
int y = selectedUnit.GetComponent<UnitScript>().y;
HashSet<Node> singleTile = new HashSet<Node>();
singleTile.Add(graph[x, y]);
return singleTile;
}
//In:
//Out: void
//Desc: highlights the selected unit's options
public void highlightTileUnitIsOccupying()
{
if (selectedUnit != null)
{
highlightMovementRange(getTileUnitIsOccupying());
}
}
//In:
//Out: void
//Desc: highlights the selected unit's attackOptions from its position
public void highlightUnitAttackOptionsFromPosition()
{
if (selectedUnit != null)
{
highlightEnemiesInRange(getUnitAttackOptionsFromPosition());
}
}
//In: Hash set of the available nodes that the unit can range
//Out: void - it changes the quadUI property in the gameworld to visualize the selected unit's movement
//Desc: This function highlights the selected unit's movement range
public void highlightMovementRange(HashSet<Node> movementToHighlight)
{
foreach (Node n in movementToHighlight)
{