-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharacter.java
More file actions
967 lines (952 loc) · 38.4 KB
/
Character.java
File metadata and controls
967 lines (952 loc) · 38.4 KB
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
import java.awt.*;
import javax.swing.*;
import java.awt.image.BufferedImage;
import javax.imageio.*;
import java.awt.geom.AffineTransform;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
/**
* The code for the user (does not include the move code)
*
* Useful Methods:
* setImagePosition: sets user picture for animation
* draw: draws user on graphical interface
* canPlaceBlock: if user can place block in desired position
* placeBlock: places block on graphical interface
* breakBlock: gets index of block user is interacting with
* canMove: checks if user/drone can travel in desired direction
* teleportMove: allows for teleportation travel
* replenishBlocks: restores # of blocks
* increaseBlocks: allows user to carry extra blocks
* before replenishing
* getZone: checks if user is in friendly territory
*
* @Avi and Amar Ruthen
* @12.22.2020
*/
public class Character
{
int xLoc, yLoc;
int squareSize;
int faceDirection;
int numBlocks, blocksAllowed, numImmovableBlocks;
boolean invisible;
double theta = 0.0;
BufferedImage charPic = null;
Grid grid;
/**
* Constructor the user object
* Initializes coordinates and block variables, transfers grid object/values
*/
public Character(int xLoc, int yLoc, int squareSize, Grid grid)
{
this.xLoc = xLoc;
this.yLoc = yLoc;
this.squareSize = squareSize;
this.grid = grid;
faceDirection = 0; //character faces up
blocksAllowed = 100; //Sets total blocks w/o replenishing
numBlocks = blocksAllowed;
numImmovableBlocks = 10; //Needed if immovable blocks is selected
invisible = false;
try
{
charPic = ImageIO.read(this.getClass().getResourceAsStream(
"resources/AquaTriangle.png")); //the name of the image
}
catch(Exception e){}
}
/**
* Toggles between four pictures of the same user graphics to give
* illusion of movement
*/
public void setImagePosition(int num)
{
switch(faceDirection)
{
case 0:
//Facing up, or starting position
try {
charPic = ImageIO.read(this.getClass().getResourceAsStream(
"resources/AquaTriangle.png")); //name of the image,
}
catch (Exception e) {}
break;
case 1:
//Facing down
try {
charPic = ImageIO.read(this.getClass().getResourceAsStream(
"resources/AquaTriangle 2.png")); //name of the image
}
catch (Exception e) {}
break;
case 2:
//Facing left
try {
charPic = ImageIO.read(this.getClass().getResourceAsStream(
"resources/AquaTriangle 3.png")); //name of the image
}
catch (Exception e) {}
break;
case 3:
//Facing right
try {
charPic = ImageIO.read(this.getClass().getResourceAsStream(
"resources/AquaTriangle 4.png")); //name of the image
}
catch (Exception e) {}
break;
default:
break;
}
}
/**
* Draws the location of the user at given location
* @param g allows for character to be drawn on graphics window
* @param d gives the display access to the user
*/
public void draw(Graphics g, Display d)
{
//location of this Character on grid:
int x = (int)(squareSize * xLoc);
int y = (int)(squareSize * yLoc);
//draw the image
g.drawImage(charPic, x, y, d);
}
/**
* Cycles through all given scenarios of whether the user can place a block:
* Cannot place if...
* Block occupies the space in front of direction user is facing
* Block placed would be outside the border
* Block placed is within 5x5 near either base spawn
* Number of blocks of that specific type is zero
*
* @param userBlocks, immovableBlocks, compBlocks, neutralBlocks sends all
* block types
* @param userBase, compBase sends the current updated base objects
* @param manualFD tracks which direction user is facing
* @param immovable checks whether the block in question is immovable
*/
public boolean canPlaceBlock(Block[] userBlocks, Block[] immovableBlocks,
Block[] compBlocks, Block[] neutralBlocks, Base userBase,
Base compBase, boolean immovable, int manualFD, int xLoc, int yLoc)
{
//manual Face Direction is to check if you can place a block for any
//adjacent location of your choice (for drones, specifically)
if (manualFD == -1)
manualFD = faceDirection;
//sets test xLocs and yLocs to character's xLoc and yLoc if method is
//not called by a drone
if (xLoc == -1)
xLoc = this.xLoc;
if (yLoc == -1)
yLoc = this.yLoc;
//if the user has no blocks to place, they cannot place blocks
if ((numBlocks == 0) && (!immovable) ||
(numImmovableBlocks == 0) && (immovable))
return false;
//user facing up
if (manualFD == 0)
{
//if the 5X5 base areas are above the user, cannot place blocks
if (Math.abs(userBase.yLoc + 1 - (yLoc - 1)) <= 2 &&
Math.abs(userBase.xLoc + 1 - xLoc) <= 2)
return false;
if (Math.abs(compBase.yLoc + 1 - (yLoc - 1)) <= 2 &&
Math.abs(compBase.xLoc + 1 - xLoc) <= 2)
return false;
//cannot place blocks beyond border edge
if (grid.toGridCoords(yLoc, false) == 0)
return false;
//cannot place blocks on top of other user blocks
for (Block uB : userBlocks)
{
if (uB != null)
{
if (uB.yLoc == yLoc - 1 && uB.xLoc == xLoc)
return false;
}
}
//cannot place blocks on top of other immovable blocks
for (Block iB : immovableBlocks)
{
if (iB != null)
{
if (iB.yLoc == yLoc + 1 && iB.xLoc == xLoc)
return false;
}
}
//cannot place blocks on top of computer blocks
for (Block cB : compBlocks)
{
if (cB != null)
{
if (cB.yLoc == yLoc - 1 && cB.xLoc == xLoc)
return false;
}
}
//cannot place blocks on top of neutral blocks
for (Block nB : neutralBlocks)
{
if (nB != null)
{
if (nB.yLoc == yLoc - 1 && nB.xLoc == xLoc)
return false;
}
}
}
//user is facing down
else if (manualFD == 1)
{
//cannot place blocks within 5X5 base area
if (Math.abs(userBase.yLoc + 1 - (yLoc + 1)) <= 2 &&
Math.abs(userBase.xLoc + 1 - xLoc) <= 2)
return false;
if (Math.abs(compBase.yLoc + 1 - (yLoc + 1)) <= 2 &&
Math.abs(compBase.xLoc + 1 - xLoc) <= 2)
return false;
//cannot place blocks beyond border edge
if(grid.toGridCoords(yLoc, false) == grid.gridSideLength - 1)
return false;
//cannot place blocks on top of other user blocks
for (Block uB : userBlocks)
{
if (uB != null)
{
if (uB.yLoc == yLoc + 1 && uB.xLoc == xLoc)
return false;
}
}
//cannot place blocks on top of other immovable blocks
for (Block iB : immovableBlocks)
{
if (iB != null)
{
if (iB.yLoc == yLoc + 1 && iB.xLoc == xLoc)
return false;
}
}
//cannot place blocks on top of other computer blocks
for (Block cB : compBlocks)
{
if (cB != null)
{
if (cB.yLoc == yLoc + 1 && cB.xLoc == xLoc)
return false;
}
}
//cannot place blocks on top of other neutral blocks
for (Block nB : neutralBlocks)
{
if (nB != null)
{
if (nB.yLoc == yLoc + 1 && nB.xLoc == xLoc)
return false;
}
}
}
//user facing left
else if (manualFD == 2)
{
//cannot place blocks within 5X5 base area
if (Math.abs(userBase.xLoc + 1 - (xLoc - 1)) <= 2 &&
Math.abs(userBase.yLoc + 1 - yLoc) <= 2)
return false;
if (Math.abs(compBase.xLoc + 1 - (xLoc - 1)) <= 2 &&
Math.abs(compBase.yLoc + 1 - yLoc) <= 2)
return false;
//cannot place blocks beyond the edge of the border
if(grid.toGridCoords(xLoc, true) == 0)
return false;
//cannot place blocks on top of other user blocks
for (Block uB : userBlocks)
{
if (uB != null)
{
if (uB.xLoc == xLoc - 1 && uB.yLoc == yLoc)
return false;
}
}
//cannot place blocks on top of other immovable blocks
for (Block iB : immovableBlocks)
{
if (iB != null)
{
if (iB.xLoc == xLoc - 1 && iB.yLoc == yLoc)
return false;
}
}
//cannot place blocks on top of other computer blocks
for (Block cB : compBlocks)
{
if (cB != null)
{
if (cB.xLoc == xLoc - 1 && cB.yLoc == yLoc)
return false;
}
}
//cannot place blocks on top of other neutral blocks
for (Block nB : neutralBlocks)
{
if (nB != null)
{
if (nB.xLoc == xLoc - 1 && nB.yLoc == yLoc)
return false;
}
}
}
//user facing right
else if (manualFD == 3)
{
//cannot place blocks within 5X5 base spawning area
if (Math.abs(userBase.xLoc + 1 - (xLoc + 1)) <= 2 &&
Math.abs(userBase.yLoc + 1 - yLoc) <= 2)
return false;
if (Math.abs(compBase.xLoc + 1 - (xLoc + 1)) <= 2 &&
Math.abs(compBase.yLoc + 1 - yLoc) <= 2)
return false;
//cannot place blocks beyond the edge of the border
if(grid.toGridCoords(xLoc, true) == grid.gridSideLength - 1)
return false;
//cannot place blocks on top of other user blocks
for (Block uB : userBlocks)
{
if (uB != null)
{
if (uB.xLoc == xLoc + 1 && uB.yLoc == yLoc)
return false;
}
}
//cannot place blocks on top of other immovable blocks
for (Block iB : immovableBlocks)
{
if (iB != null)
{
if (iB.xLoc == xLoc + 1 && iB.yLoc == yLoc)
return false;
}
}
//cannot place blocks on top of other computer blocks
for (Block cB : compBlocks)
{
if (cB != null)
{
if (cB.xLoc == xLoc + 1 && cB.yLoc == yLoc)
return false;
}
}
//cannot place blocks on top of other neutral blocks
for (Block nB : neutralBlocks)
{
if (nB != null)
{
if (nB.xLoc == xLoc + 1 && nB.yLoc == yLoc)
return false;
}
}
}
return true; //if none of the following conditions occur
}
/**
* Places block in location specified by user
* @param blockType is which array of blocks will be placed by the user
* @param blockIndex the index of last block placed in specified blockType
* @param userColor the color of the block (red-normal, gray-immovable)
* @param immovable denotes whether block can be broken
*
* @return new last blockIndex
*/
public int placeBlock(Block[] blockType, int blockIndex, Color userColor,
boolean immovable)
{
//user facing up
if (faceDirection == 0)
{
//places block above user
blockType[blockIndex] = new Block(xLoc, yLoc - 1, squareSize,
true, userColor, immovable);
}
//user facing down
else if (faceDirection == 1)
{
//places block below user
blockType[blockIndex] = new Block(xLoc, yLoc + 1, squareSize,
true, userColor, immovable);
}
//user facing left
else if (faceDirection == 2)
{
//places block to the left of the user
blockType[blockIndex] = new Block(xLoc - 1, yLoc, squareSize,
true, userColor, immovable);
}
//user facing right
else if (faceDirection == 3)
{
//places block to the right of the user
blockType[blockIndex] = new Block(xLoc + 1, yLoc, squareSize,
true, userColor, immovable);
}
if(!immovable)
numBlocks--; // immovable blocks stored in separate array
else
numImmovableBlocks--;
blockIndex++; //blockIndex tracks the next available space in userBlocks
return blockIndex;
}
/**
* Gives index and blockType of the block to be broken
*
* @param compBlocks is the computer blocks array
* @param neutralBlocks is the neutral blocks array
*
* @return a number that stores index of block and type of block
*/
public int breakBlock(Block[] compBlocks, Block[] neutralBlocks)
{
int index = 0; //tracks index of block broken
for (Block cB : compBlocks)
{
//cannot break immovable blocks
if (cB != null && !cB.immovable)
{
//user facing up
if (faceDirection == 0)
{
//is there a block above
if (grid.toGridCoords(cB.yLoc, false) -
grid.toGridCoords(yLoc, false) == -1 &&
grid.toGridCoords(cB.xLoc, true) ==
grid.toGridCoords(xLoc, true))
return 1009*(index + 1); //1009 indicates computer block
}
//user facing down
else if (faceDirection == 1)
{
//is there a block below
if (grid.toGridCoords(cB.yLoc, false) -
grid.toGridCoords(yLoc, false) == 1 &&
grid.toGridCoords(cB.xLoc, true) ==
grid.toGridCoords(xLoc, true))
return 1009*(index + 1);
}
//user facing left
if (faceDirection == 2)
{
//is there a block to the left
if (grid.toGridCoords(cB.xLoc, true) -
grid.toGridCoords(xLoc, true) == -1 &&
grid.toGridCoords(cB.yLoc, false) ==
grid.toGridCoords(yLoc, false))
return 1009*(index + 1);
}
//user facing right
else if (faceDirection == 3)
{
//is there a block to the right
if (grid.toGridCoords(cB.xLoc, true) -
grid.toGridCoords(xLoc, true) == 1 &&
grid.toGridCoords(cB.yLoc, false) ==
grid.toGridCoords(yLoc, false))
return 1009*(index + 1);
}
}
index++; //otherwise increases index
}
index = 0; //initializes index
for (Block nB : neutralBlocks)
{
//goes through same process
if (nB != null && !nB.immovable)
{
//user facing up
if (faceDirection == 0)
{
//is there a block above
if (grid.toGridCoords(nB.yLoc, false) -
grid.toGridCoords(yLoc, false) == -1 &&
grid.toGridCoords(nB.xLoc, true) ==
grid.toGridCoords(xLoc, true))
return 1013*(index + 1); //1009 indicates computer block
}
//user facing down
else if (faceDirection == 1)
{
//is there a block below
if (grid.toGridCoords(nB.yLoc, false) -
grid.toGridCoords(yLoc, false) == 1 &&
grid.toGridCoords(nB.xLoc, true) ==
grid.toGridCoords(xLoc, true))
return 1013*(index + 1);
}
//user facing left
if (faceDirection == 2)
{
//is there a block to the left
if (grid.toGridCoords(nB.xLoc, true) -
grid.toGridCoords(xLoc, true) == -1 &&
grid.toGridCoords(nB.yLoc, false) ==
grid.toGridCoords(yLoc, false))
return 1013*(index + 1);
}
//user facing right
else if (faceDirection == 3)
{
//is there a block to the right
if (grid.toGridCoords(nB.xLoc, true) -
grid.toGridCoords(xLoc, true) == 1 &&
grid.toGridCoords(nB.yLoc, false) ==
grid.toGridCoords(yLoc, false))
return 1013*(index + 1);
}
}
index++; //checks each block in array
}
return -1; //block not found
}
/**
* Tests if user can move in specified direction
* Returns false if a non-user block occupies the place they want to go
* Ignores if the movement is teleportation, which can go through blocks
*
* @param compBlock, neutralBlocks, immovableBlocks are all block arrays
* @param teleport checks if user is teleport-moving or moving normally
* @param manualFD is a manually inputted faceDirection (to check if
* blocks are in any place surrounding user or drone, not just in front)
*/
public boolean canMove(Block[] compBlocks, Block[] neutralBlocks,
Block[] immovableBlocks, boolean teleport, int manualFD)
{
//checks if user can move in any direction (manual input face direction)
if (manualFD == -1)
manualFD = faceDirection; //if no manual input, faceDirection is
//the user's face direction
//cehcks if user is on the edge of the map
switch (manualFD)
{
//checks above
case 0:
{
if (grid.toGridCoords(yLoc, false) == 0)
return false;
break;
}
//checks below
case 1:
{
if (grid.toGridCoords(yLoc, false) == grid.gridSideLength - 1)
return false;
break;
}
//checks to the left
case 2:
{
if (grid.toGridCoords(xLoc, true) == 0)
return false;
break;
}
//checks to the right
case 3:
{
if (grid.toGridCoords(xLoc, true) == grid.gridSideLength - 1)
return false;
break;
}
}
//checks if the user is colliding with any non-user blocks
if (!teleport)
{
for (Block cB : compBlocks)
{
if (cB != null)
{
//checks above
if (manualFD == 0) //block above
{
if (grid.toGridCoords(cB.yLoc, false) -
grid.toGridCoords(yLoc, false) == -1 &&
grid.toGridCoords(cB.xLoc, true) ==
grid.toGridCoords(xLoc, true))
return false;
}
else if (manualFD == 1) //block below
{
if (grid.toGridCoords(cB.yLoc, false) -
grid.toGridCoords(yLoc, false) == 1 &&
grid.toGridCoords(cB.xLoc, true) ==
grid.toGridCoords(xLoc, true))
return false;
}
else if (manualFD == 2) //block to the left
{
if (grid.toGridCoords(cB.xLoc, true) -
grid.toGridCoords(xLoc, true) == -1 &&
grid.toGridCoords(cB.yLoc, false) ==
grid.toGridCoords(yLoc, false))
return false;
}
else if (manualFD == 3) //block to the right
{
if (grid.toGridCoords(cB.xLoc, true) -
grid.toGridCoords(xLoc, true) == 1 &&
grid.toGridCoords(cB.yLoc, false) ==
grid.toGridCoords(yLoc, false))
return false;
}
}
}
//same process for neutral blocks
for (Block nB : neutralBlocks)
{
if (nB != null)
{
//checks above
if (manualFD == 0) //block above
{
if (grid.toGridCoords(nB.yLoc, false) -
grid.toGridCoords(yLoc, false) == -1 &&
grid.toGridCoords(nB.xLoc, true) ==
grid.toGridCoords(xLoc, true))
return false;
}
else if (manualFD == 1) //block below
{
if (grid.toGridCoords(nB.yLoc, false) -
grid.toGridCoords(yLoc, false) == 1 &&
grid.toGridCoords(nB.xLoc, true) ==
grid.toGridCoords(xLoc, true))
return false;
}
else if (manualFD == 2) //block to the left
{
if (grid.toGridCoords(nB.xLoc, true) -
grid.toGridCoords(xLoc, true) == -1 &&
grid.toGridCoords(nB.yLoc, false) ==
grid.toGridCoords(yLoc, false))
return false;
}
else if (manualFD == 3) //block to the right
{
if (grid.toGridCoords(nB.xLoc, true) -
grid.toGridCoords(xLoc, true) == 1 &&
grid.toGridCoords(nB.yLoc, false) ==
grid.toGridCoords(yLoc, false))
return false;
}
}
}
//same process for immovable blocks
for (Block iB : immovableBlocks)
{
if (iB != null)
{
//checks above
if (manualFD == 0) //block above
{
if (grid.toGridCoords(iB.yLoc, false) -
grid.toGridCoords(yLoc, false) == -1 &&
grid.toGridCoords(iB.xLoc, true) ==
grid.toGridCoords(xLoc, true))
return false;
}
else if (manualFD == 1) //block below
{
if (grid.toGridCoords(iB.yLoc, false) -
grid.toGridCoords(yLoc, false) == 1 &&
grid.toGridCoords(iB.xLoc, true) ==
grid.toGridCoords(xLoc, true))
return false;
}
else if (manualFD == 2) //block to the left
{
if (grid.toGridCoords(iB.xLoc, true) -
grid.toGridCoords(xLoc, true) == -1 &&
grid.toGridCoords(iB.yLoc, false) ==
grid.toGridCoords(yLoc, false))
return false;
}
else if (manualFD == 3) //block to the right
{
if (grid.toGridCoords(iB.xLoc, true) -
grid.toGridCoords(xLoc, true) == 1 &&
grid.toGridCoords(iB.yLoc, false) ==
grid.toGridCoords(yLoc, false))
return false;
}
}
}
}
return true; //otherwise, user can move
}
/**
* The code for teleportation travel (moves 5 blocks in faceDirection)
* User may pass through blocks unless it ends on top of a block, in which
* It travels less than 5 blocks until it reaches a space
* not occupied by a non-user block
*
* @param compBlock, neutralBlocks, immovableBlocks are all block arrays
*
* @return how many blocks the user can teleport
*/
public int teleportMove(Block[] compBlocks,
Block[] neutralBlocks,
Block[] immovableBlocks)
{
//to check if there are comp blocks 1-5 blocks ahead of faceDirection
boolean[] pos = new boolean[5];
//checks through all of the computer blocks
for (Block cB : compBlocks)
{
if (cB != null)
{
//all blocks in same column as user (facing up)
if (faceDirection == 0 && grid.toGridCoords(cB.xLoc, true) ==
grid.toGridCoords(xLoc, true))
{
// i = 5 since max of five moves when teleporting
for (int i = 1; i <= 5; i++)
{
//assesses if teleportation will hit the wall
if (grid.toGridCoords(yLoc, false) - i < 0)
pos[i - 1] = true;
if (grid.toGridCoords(cB.yLoc, false) -
grid.toGridCoords(yLoc, false) == -i)
pos[i - 1] = true;
}
}
//all blocks in same column as user (facing down)
else if (faceDirection == 1 &&
grid.toGridCoords(cB.xLoc, true) ==
grid.toGridCoords(xLoc, true))
{
for (int i = 1; i <= 5; i++)
{
//assesses if teleportation will hit the wall
if (grid.toGridCoords(yLoc, false) + i >
grid.gridSideLength - 1)
pos[i - 1] = true;
if (grid.toGridCoords(cB.yLoc, false) -
grid.toGridCoords(yLoc, false) == i)
pos[i - 1] = true;
}
}
//all blocks in same row as user (facing left)
else if (faceDirection == 2 &&
grid.toGridCoords(cB.yLoc, false) ==
grid.toGridCoords(yLoc, false))
{
for (int i = 1; i <= 5; i++)
{
//assesses if teleportation will hit the wall
if (grid.toGridCoords(xLoc, true) - i < 0)
pos[i - 1] = true;
if (grid.toGridCoords(cB.xLoc, false) -
grid.toGridCoords(xLoc, false) == -i)
pos[i - 1] = true;
}
}
//all blocks in same row as user (facing right)
else if (faceDirection == 3 &&
grid.toGridCoords(cB.yLoc, false) ==
grid.toGridCoords(yLoc, false))
{
//assesses if teleportation will hit the wall
for (int i = 1; i <= 5; i++)
{
if (grid.toGridCoords(xLoc, true) + i >
grid.gridSideLength - 1)
pos[i - 1] = true;
if (grid.toGridCoords(cB.xLoc, false) -
grid.toGridCoords(xLoc, false) == i)
pos[i - 1] = true;
}
}
}
}
//same process for neutral blocks
for (Block nB : neutralBlocks)
{
if (nB != null)
{
//all blocks in same column as user (facing up)
if (faceDirection == 0 && grid.toGridCoords(nB.xLoc, true) ==
grid.toGridCoords(xLoc, true))
{
// i = 5 since max of five moves when teleporting
for (int i = 1; i <= 5; i++)
{
//assesses if teleportation will hit the wall
if (grid.toGridCoords(yLoc, false) - i < 0)
pos[i - 1] = true;
if (grid.toGridCoords(nB.yLoc, false) -
grid.toGridCoords(yLoc, false) == -i)
pos[i - 1] = true;
}
}
//all blocks in same column as user (facing down)
else if (faceDirection == 1 &&
grid.toGridCoords(nB.xLoc, true) ==
grid.toGridCoords(xLoc, true))
{
for (int i = 1; i <= 5; i++)
{
//assesses if teleportation will hit the wall
if (grid.toGridCoords(yLoc, false) + i >
grid.gridSideLength - 1)
pos[i - 1] = true;
if (grid.toGridCoords(nB.yLoc, false) -
grid.toGridCoords(yLoc, false) == i)
pos[i - 1] = true;
}
}
//all blocks in same row as user (facing left)
else if (faceDirection == 2 &&
grid.toGridCoords(nB.yLoc, false) ==
grid.toGridCoords(yLoc, false))
{
for (int i = 1; i <= 5; i++)
{
//assesses if teleportation will hit the wall
if (grid.toGridCoords(xLoc, true) - i < 0)
pos[i - 1] = true;
if (grid.toGridCoords(nB.xLoc, false) -
grid.toGridCoords(xLoc, false) == -i)
pos[i - 1] = true;
}
}
//all blocks in same row as user (facing right)
else if (faceDirection == 3 &&
grid.toGridCoords(nB.yLoc, false) ==
grid.toGridCoords(yLoc, false))
{
//assesses if teleportation will hit the wall
for (int i = 1; i <= 5; i++)
{
if (grid.toGridCoords(xLoc, true) + i >
grid.gridSideLength - 1)
pos[i - 1] = true;
if (grid.toGridCoords(nB.xLoc, false) -
grid.toGridCoords(xLoc, false) == i)
pos[i - 1] = true;
}
}
}
}
//same process for immovable blocks
for (Block iB : immovableBlocks)
{
if (iB != null)
{
//all blocks in same column as user (facing up)
if (faceDirection == 0 && grid.toGridCoords(iB.xLoc, true) ==
grid.toGridCoords(xLoc, true))
{
// i = 5 since max of five moves when teleporting
for (int i = 1; i <= 5; i++)
{
//assesses if teleportation will hit the wall
if (grid.toGridCoords(yLoc, false) - i < 0)
pos[i - 1] = true;
if (grid.toGridCoords(iB.yLoc, false) -
grid.toGridCoords(yLoc, false) == -i)
pos[i - 1] = true;
}
}
//all blocks in same column as user (facing down)
else if (faceDirection == 1 &&
grid.toGridCoords(iB.xLoc, true) ==
grid.toGridCoords(xLoc, true))
{
for (int i = 1; i <= 5; i++)
{
//assesses if teleportation will hit the wall
if (grid.toGridCoords(yLoc, false) + i >
grid.gridSideLength - 1)
pos[i - 1] = true;
if (grid.toGridCoords(iB.yLoc, false) -
grid.toGridCoords(yLoc, false) == i)
pos[i - 1] = true;
}
}
//all blocks in same row as user (facing left)
else if (faceDirection == 2 &&
grid.toGridCoords(iB.yLoc, false) ==
grid.toGridCoords(yLoc, false))
{
for (int i = 1; i <= 5; i++)
{
//assesses if teleportation will hit the wall
if (grid.toGridCoords(xLoc, true) - i < 0)
pos[i - 1] = true;
if (grid.toGridCoords(iB.xLoc, false) -
grid.toGridCoords(xLoc, false) == -i)
pos[i - 1] = true;
}
}
//all blocks in same row as user (facing right)
else if (faceDirection == 3 &&
grid.toGridCoords(iB.yLoc, false) ==
grid.toGridCoords(yLoc, false))
{
//assesses if teleportation will hit the wall
for (int i = 1; i <= 5; i++)
{
if (grid.toGridCoords(xLoc, true) + i >
grid.gridSideLength - 1)
pos[i - 1] = true;
if (grid.toGridCoords(iB.xLoc, false) -
grid.toGridCoords(xLoc, false) == i)
pos[i - 1] = true;
}
}
}
}
//looks for the first position with no blocks up to five blocks ahead
int index = 0;
for (int i = 4; i >= 0; i--)
{
if (pos[i] == false)
{
index = i + 1;
i = -1; //breaks loop
}
}
return index; //number of blocks user will teleport ahead
}
/**
* Replenishes the number of blocks user can place
* Called upon when user is in their home base
*/
public void replenishBlocks()
{
numBlocks = blocksAllowed; //user's blocks goes back to 100
}
/**
* Allows user to carry extra blocks before needing to replenish
* Called upon by extra blocks powerup
*
* @param the new number of blocks user can store (is 200)
*/
public void increaseBlocksAllowed(int newSize)
{
blocksAllowed = newSize; //for extra blocks powerup
}
/**
* Determines if user is on the friendly side of the map
*
* @param grid allows for grid methods to be called
* @param userBase gives access to the userBase object
*
* @return true if user is on the same side as their base
*/
public boolean getZone(Grid grid, Base userBase)
{
//user is in the same zone as their base
if ((grid.toGridCoords(xLoc, true) + grid.toGridCoords(yLoc, false) <
grid.gridSideLength - 2 && userBase.userOnTop) ||
(grid.toGridCoords(xLoc, true) + grid.toGridCoords(yLoc, false) >
grid.gridSideLength - 1 && !userBase.userOnTop) )
return true;
return false;
}
}