-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUnit.cs
1752 lines (1424 loc) · 55.2 KB
/
Unit.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
/*
Code for a turn-based tactics game by Ian Snyder
*/
using UnityEngine;
using UnityEngine.UI;
using Random=UnityEngine.Random;
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
public class Unit : MonoBehaviour {
public bool isAI = false;
public int movementPerDecisionPoint = 5;
public int totalHP = 5;
public int meleeSkill = 50;
public int ballisticSkill = 50;
public int agility = 50;
public int xp = 0;
public int level = 1;
public int xpForThisBattle = 0;
// ABILITIES
public List<string> abilityStrings = new List<string>();
public List<Ability> abilities = new List<Ability>();
public List<int> abilityPoints = new List<int> (); // How many Ability Points each ability has accumulated
// Equipped abilities
// 0, 1= Active NOTE these are actually Job Ability Categories for the Active slot, which contains mutiple Abilities
// 2 = Reactive
// 3 = Passive
public List<string> equippedAbilityStrings = new List<string>();
public List<Ability> equippedAbilities = new List<Ability> ();
public List<AbilityCategory> equippedAbilCats = new List<AbilityCategory> ();
// Equipped Equipment!
public List<string> equipmentStrings = new List<string>();
public List<GameObject> equipmentGOs = new List<GameObject> ();
public GameObject shield;
public float jumpHeight = 2;
public string myClanName;
public Clan myClan;
public List<string> jobsLearned = new List<string> (); // Each job that this unit knows
public string currJobString;
public Job currJob;
public GameObject currJobGO;
public string visualPrefabString;
private GameObject visualPrefab;
public Sprite portrait;
// Not to be modified in the editor!!
public int DONT_MODIFY_BELOW_THIS;
public Weapon equippedWeapon;
public Weapon setAsideWeapon = null; // When we attack with an ability "weapon", set aside the currently equipped weapon here, then set it back once the ability attack is over
public bool shieldActive = false;
public List<Weapon> weapons = new List<Weapon> ();
public GameObject aoeTrigger;
public List<string> itemStrings = new List<string>();
public List<Item> items = new List<Item>();
public Item currItem = null;
// COUNTERING
public bool startedCounter = false;
public bool mainPhase = false;
public bool selectingWeapon = false;
private bool selectingMagic = false;
public bool selectingItems = false;
public bool selectingFacing = false;
public int coverModifier = 0;
public bool pinned = false;
public bool down = false;
public bool outOfAction = false;
public bool selectingTarget = false;
public bool aoeAttack = false;
public bool moving = false;
public bool attacking = false;
public bool meleeAttacking = false;
public bool running = false;
public bool healing = false;
public bool recruiting = false; // Are we attempting to recruit an enemy unit?
public float maxMeleeElevationDifference = 1f;
public bool confirmingAction = false;
public bool carryingIntel = false;
public int teamID = 0;
public int currHP;
public int decisionPoints = 2;
public Transform spriteRef;
private bool flipped = false;
private bool tweenRunning = false;
public bool losTooltipActive = false;
//public int numOfHeals = 1;
private int healAmount = 3;
public AI myAI;
public float moveAnimationSpeed = 0.5f; // the time in seconds the tweens will take to complete (to each block)
public Vector2 gridPosition = Vector2.zero;
public Vector3 moveDestination;
//movement animation
public List<Vector3> positionQueue = new List<Vector3>();
//
public Animator spriteAnimator;
public int diceNumSides = 100;
public bool choseToWait = false;
private SpriteRenderer weaponSprite;
public GameObject progressIcon;
public int weaponRangeModifier;
public bool completedActionDelayRunning = false;
void Awake () {
moveDestination = transform.position;
}
// Use this for initialization
void Start () {
if( !isAI)
myClan = GameObject.Find ("CLAN_" + myClanName).GetComponent<Clan> ();
currHP = totalHP;
if (GameManager.instance) {
GameManager.instance.AddUnit (this);
progressIcon = (GameObject)Instantiate (Resources.Load ("Helpers/ProgressCircle", typeof(GameObject)));
progressIcon.transform.position = this.transform.position - Vector3.up * .49f;
progressIcon.transform.parent = this.transform;
}
tweenRunning = false;
// Add our box collider
BoxCollider b = gameObject.AddComponent<BoxCollider> ();
if(b) {
b.size = new Vector3(.4f, 1.0f, .4f);
b.center = new Vector3(0f, 0f, 0f);
}
// Add an AI component if this is an AI
if( isAI ) {
gameObject.AddComponent<AI>();
myAI = gameObject.GetComponent<AI>();
}
// Initialize weapons
UpdateWeapons ();
// Initialize armor and accessory equipment slots
UpdateArmorAndAccessories();
UpdateAbilsFromEquipment ();
// SHIELD
if( shield != null ){ // Give this unit a shield
shield = (GameObject)Instantiate(Resources.Load("Equipment/Shield", typeof(GameObject)));
shield.transform.position = this.transform.position;
shield.transform.rotation = this.transform.rotation;
shield.transform.parent = this.transform;
}
// Initialize items
UpdateItemsFromStrings();
// Initialize Equipped Abilities
UpdateAbilitiesFromStrings ();
UpdateEquippedAbils ();
// VISUAL PREFAB LOADING
GameObject chessPiece = (GameObject)Instantiate(Resources.Load("Puppets/" + visualPrefabString), transform.position , transform.rotation );
chessPiece.transform.parent = this.gameObject.transform;
spriteRef = chessPiece.transform;
spriteAnimator = spriteRef.GetComponent<Animator>();
// Attach SortingLayerFixing to each sprite of the visual object
Transform[] children = gameObject.GetComponentsInChildren<Transform>();
foreach(Transform child in children){
if (child.GetComponent<Renderer>() != null)
child.gameObject.AddComponent<SortingLayerFixing>();
}
// Set values for this unit based on their current job
SetValsFromJob();
}
// Update is called once per frame
void Update () {
spriteRef.rotation = Camera.main.transform.rotation;
spriteRef.position = this.transform.position - (Vector3.up * .4f);
// flipped?
if ( Vector3.Angle(this.transform.right, Camera.main.transform.forward) > 90f) {
if( flipped){
//Debug.Log("where");
spriteRef.transform.localScale = new Vector3(spriteRef.transform.localScale.x, spriteRef.transform.localScale.y, 1);
flipped = false;
}
} else {
if( !flipped){
spriteRef.transform.localScale = new Vector3(spriteRef.transform.localScale.x, spriteRef.transform.localScale.y, -1);
spriteRef.transform.Rotate( new Vector3( 0, 180, 0));
flipped = true;
} else {
spriteRef.transform.Rotate( new Vector3( 0, 180, 0));
spriteRef.transform.localScale = new Vector3(spriteRef.transform.localScale.x, spriteRef.transform.localScale.y, -1);
}
}
}
public void TurnStart(){
// Turn on the command panel for player units
if( !isAI){
if( !GameManager.instance.inbattleMenuManager.commandPanelOn)
GameManager.instance.inbattleMenuManager.ToggleCommandPanel();
}
if( !GameManager.instance.inbattleMenuManager.currUnitInfoPanelOn)
GameManager.instance.inbattleMenuManager.ToggleCurrUnitInfoPanel();
choseToWait = false;
moving = false;
attacking = false;
meleeAttacking = false;
running = false;
AttackingWithAoE( false );
selectingTarget = false;
//spriteAnimator.SetBool("Aiming", false);
selectingWeapon = false;
selectingMagic = false;
selectingItems = false;
healing = false;
currItem = null;
GameManager.instance.targetedTile = null;
GameManager.instance.selectionMarker.transform.localScale = Vector3.zero;
GameManager.instance.selectionMarker.transform.position = this.gameObject.transform.position + 1.5f * Vector3.up;
GameManager.instance.selectionMarker.transform.parent = this.gameObject.transform;
iTween.ScaleTo( GameManager.instance.selectionMarker, Vector3.one - .8f * Vector3.one + .3f * Vector3.up, .3f );
// Look at the unit that just started it's turn
GameManager.instance.FocusOnPos( transform.position, 1f);
if( pinned ) {
decisionPoints--;
pinned = false;
if(!down)
UnpinAnimation();
}
if( down ) {
decisionPoints = 1;
}
if (shield != null)
shield.SetActive (false);
// If we're AI, start workin'
if( isAI){
myAI.startDelayTimerIsRunning = true;
}
mainPhase = true;
}
public virtual void TurnUpdate () {
if( decisionPoints <= 0 ){
if( !selectingFacing ){
SelectingFacing( true );
}
// If we're AI, stop workin'
if( isAI){
myAI.startDelayTimerIsRunning = false;
myAI.showingDelayTimerIsRunning = false;
myAI.decisionDelayTimerIsRunning = false;
myAI.aiState = 0;
}
}
if (positionQueue.Count > 0) {
if(!tweenRunning){
float elevationDiff = positionQueue[0].y - transform.position.y ;
if ( elevationDiff < -jumpHeight ) {
tweenComplete (true);
} else {
iTween.MoveTo(this.gameObject, iTween.Hash("position", positionQueue[0],
"easeType", "easeOutQuart",
"time", moveAnimationSpeed,
"orienttopath", true,
"axis", "y",
"oncomplete", "tweenComplete",
"oncompleteparams", false));
tweenRunning = true;
}
}
}
if( isAI && !outOfAction){
myAI.AIUpdate();
}
}
public void tweenComplete( bool hopped ) {
//Debug.Log ("completed");
if( !hopped)
transform.position = positionQueue[0];
positionQueue.RemoveAt(0);
tweenRunning = false;
if( carryingIntel && !hopped)
Intel.instance.SetPos( transform.position + Vector3.up * 2 );
if (positionQueue.Count == 0 ) { // Finished moving
SetOccupiedToTeamID();
// If we stopped on the tile where the intel was dropped, pick it up!
if( gridPosition.x > -9000 && GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y].intelDroppedHere ){
carryingIntel = true;
Intel.instance.SetPos( transform.position + Vector3.up * 2 );
GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y].intelDroppedHere = false;
GameObject intelPickupParticle = (GameObject)Instantiate(Resources.Load("Helpers/IntelPickupParticle", typeof(GameObject)));
intelPickupParticle.transform.position = this.transform.position;
Destroy( intelPickupParticle, 5);
}
if( !GameManager.instance.somebodyWon ){
// If we ran to this position, remove an extra DP
if( GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y].runDestination ){
decisionPoints--;
}
if ( decisionPoints > 0) {
GameManager.instance.FocusOnPos( transform.position, 1);
if( isAI ){
myAI.startDelayTimerIsRunning = true;
}
}
StartCoroutine(CompletedAction());
}
}
}
public IEnumerator CompletedAction(){
GameManager.instance.targetUnit = null;
completedActionDelayRunning = true;
if (GameManager.instance.counteringUnits.Count > 0) {
for( int i=0; i<GameManager.instance.counteringUnits.Count;i++) {
Unit counteringUnit = GameManager.instance.counteringUnits [i];
if ( counteringUnit != GameManager.instance.currUnit) { // Don't let the attacking unit counter a counter
if (!counteringUnit.outOfAction) { // Don't allow a killed unit to counter!
bool needToCounter = true;
// Give time for counter-attacking unit to counter
while(needToCounter){
if( !startedCounter){
startedCounter = true;
yield return new WaitForSeconds(1.0f);
GameManager.instance.targetUnit = this;
Debug.Log ("Counter attack!");
Debug.Log (counteringUnit.name);
GameManager.instance.ShowMessageInWorld( "COUNTER!", counteringUnit.transform.position, Color.yellow);
counteringUnit.FaceTarget ();
BeingAttacked( counteringUnit );
needToCounter = false;
}
yield return null;
}
}
}
}
}
yield return new WaitForSeconds(1.0f);
completedActionDelayRunning = false;
decisionPoints--;
moving = false;
attacking = false;
meleeAttacking = false;
selectingTarget = false;
selectingWeapon = false;
selectingMagic = false;
selectingItems = false;
AttackingWithAoE( false );
GameManager.instance.counteringUnits.Clear ();
if (setAsideWeapon) { // If we set aside our equipped weapon to use an ability weapon, re-equip it!
equippedWeapon = setAsideWeapon;
setAsideWeapon = null;
}
// If nobody's won, keep goin'
if (!GameManager.instance.somebodyWon) {
if (!outOfAction) {
GameManager.instance.inbattleMenuManager.UpdateCurrUnitInfoPanel ();
if (!isAI && decisionPoints > 0) {
if (!GameManager.instance.inbattleMenuManager.commandPanelOn)
GameManager.instance.inbattleMenuManager.ToggleCommandPanel ();
}
}
}
}
public void FaceTarget(){
// Rotate to face target
Vector3 target = GameManager.instance.targetUnit.transform.position;
Vector3 correctedTarget = new Vector3 (target.x, transform.position.y, target.z);
transform.LookAt( correctedTarget );
}
public void SetTeam(int TeamID) {
teamID = TeamID;
}
public bool CanCounter(){
if (equippedAbilityStrings [2] == "ABL_Counter")
return true;
return false;
}
public void HitFX(){
iTween.ShakePosition(this.gameObject,new Vector3(.2f,0.2f,0.2f),.4f);
}
public virtual void MeleeHitFX() {
iTween.PunchRotation(this.gameObject, iTween.Hash("x", 90f, "time", 2.5f ) );
iTween.ShakePosition(Camera.main.gameObject,new Vector3(.4f,0f,0f),.4f);
}
public void MercyKillFX(){
iTween.ShakePosition(Camera.main.gameObject,new Vector3(.4f,0f,0f),.4f);
GameObject mercyParticle = (GameObject)Instantiate(Resources.Load("Helpers/MercyKillParticle", typeof(GameObject)));
mercyParticle.transform.position = this.transform.position;
Destroy( mercyParticle, 5);
}
public void DownAnimation() {
down = true;
//Debug.Log ("downanimation");
iTween.ShakePosition(Camera.main.gameObject,new Vector3(.4f,0f,0f),.4f);
GameObject downParticle = (GameObject)Instantiate(Resources.Load("Helpers/DownParticle", typeof(GameObject)));
downParticle.transform.position = this.transform.position;
Destroy( downParticle, 5);
BoxCollider b = this.GetComponent<Collider>() as BoxCollider;
if(b) {
b.size = new Vector3(.4f, .5f, .4f);
b.center = new Vector3(0f, -.25f, 0f);
}
}
public void PinAnimation() {
//Debug.Log ("PinAnimation");
iTween.ShakePosition(Camera.main.gameObject,new Vector3(.2f,0f,0f),.2f);
GameObject pinnedParticle = (GameObject)Instantiate(Resources.Load("Helpers/PinnedParticle", typeof(GameObject)));
pinnedParticle.transform.position = this.transform.position;
Destroy( pinnedParticle, 5);
BoxCollider b = this.GetComponent<Collider>() as BoxCollider;
if(b) {
b.size = new Vector3(.4f, .5f, .4f);
b.center = new Vector3(0f, -.25f, 0f);
}
}
public void UnpinAnimation() {
pinned = false;
GameObject unpinnedParticle = (GameObject)Instantiate(Resources.Load("Helpers/RecoverParticle", typeof(GameObject)));
unpinnedParticle.transform.position = this.transform.position;
Destroy( unpinnedParticle, 5);
BoxCollider b = this.GetComponent<Collider>() as BoxCollider;
if(b) {
b.size = new Vector3(.4f, 1.0f, .4f);
b.center = new Vector3(0f, 0f, 0f);
}
}
public virtual void SetOccupiedToTeamID() {
if( !outOfAction ) {
GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y].occupied = teamID;
GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y].occupyingUnit = this;
GameManager.instance.targetedTile = null;
}
}
public void KillFX(){
iTween.ShakePosition(Camera.main.gameObject,new Vector3(.4f,0f,0f),.4f);
GameObject killedParticle = (GameObject)Instantiate(Resources.Load("Helpers/KilledParticle", typeof(GameObject)));
killedParticle.transform.position = this.transform.position;
Destroy( killedParticle, 5);
}
public virtual void OutOfAction() {
if( !outOfAction) {
// Add our equipment to the player's clan stores if we're an enemy
if (isAI) {
foreach (string s in itemStrings) {
if( !string.IsNullOrEmpty(s))
GameManager.instance.itemsAndEquipmentCollected.Add (s);
}
foreach (string s2 in equipmentStrings) {
if( !string.IsNullOrEmpty(s2))
GameManager.instance.itemsAndEquipmentCollected.Add (s2);
}
}
//If we have the intel, drop it on this tile
if( carryingIntel ){
GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y].intelDroppedHere = true;
Intel.instance.SetPos( GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y].transform.position + Vector3.up * 2 );
carryingIntel = false;
}
// If a unit is killed on it's own turn, end it's turn :P
if (GameManager.instance.currUnit == this) {
EndTurn ();
}
outOfAction = true;
iTween.FadeTo( spriteRef.gameObject, 0, 2 );
Invoke( "MoveForDeath", 2);
GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y].occupied = -1;
GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y].occupyingUnit = null;
gridPosition = new Vector2(-9999f, -9999f);
GameManager.instance.CheckForWinner();
}
}
void MoveForDeath(){
transform.position = new Vector3( -9999, -9999, -9999);
}
public virtual void EndTurn() {
// Re-activate our shield if it was de-activated this turn
if (shield != null)
shield.SetActive (true);
GameManager.instance.removeTileHighlights();
// Tell the progress timer how much DP we used this turn before resetting
progressIcon.GetComponent<TestProgress>().pastTurnDPRemaining = decisionPoints;
decisionPoints = 2;
//movePoints = 1;
//attackPoints = 1;
moving = false;
attacking = false;
meleeAttacking = false;
GameManager.instance.counteringUnits.Clear ();
GameManager.instance.targetUnit = null;
GameManager.instance.endTurnDelayTimerIsRunning = true;
mainPhase = false;
GameManager.instance.targetedTile = null;
currItem = null;
if( GameManager.instance.inbattleMenuManager.currUnitInfoPanelOn)
GameManager.instance.inbattleMenuManager.ToggleCurrUnitInfoPanel();
}
void Reload( Weapon weapon ) {
Debug.Log("Reloaded!");
AudioSource.PlayClipAtPoint( GameManager.instance.audioReload , Camera.main.transform.position);
for( int i = 0; i < weapon.clipSize; i++ ) {
if( weapon.totalAmmoRemaining > 0 ) {
weapon.ammoInClip++;
weapon.totalAmmoRemaining--;
}
}
GameObject reloadParticle = (GameObject)Instantiate(Resources.Load("Helpers/ReloadParticle", typeof(GameObject)));
reloadParticle.transform.position = this.transform.position;
Destroy( reloadParticle, 5);
CancelAction();
StartCoroutine(CompletedAction());
}
public virtual void AttackingWithAoE( bool casting ) {
if( casting ){
GameManager.instance.aoeTrigger = equippedWeapon.aoeTrigger;
aoeAttack = true;
if( equippedWeapon.areaOfEffect)
equippedWeapon.aoeTrigger.SetActive( true );
} else {
aoeAttack = false;
if (equippedWeapon) {
if( equippedWeapon.areaOfEffect)
equippedWeapon.aoeTrigger.SetActive( false );
}
GameManager.instance.aoeTrigger = null;
}
}
public void BeingAttacked( Unit attackingUnit ) {
if (!outOfAction) {
// If we're being melee'd and we're down, MERCY KILL US!
if( attackingUnit.equippedWeapon.melee && down ){
OutOfAction();
MercyKillFX();
} else { // Otherwise do all the attack logic :P
StartCoroutine( IndividualAttackLogic( attackingUnit));
}
GameManager.instance.SetUnitOccupyingSpaces();
if( startedCounter ){
startedCounter = false;
}
}
}
IEnumerator IndividualAttackLogic( Unit attackingUnit){
int shotsToFire = attackingUnit.equippedWeapon.shotsPerAP;
if (attackingUnit.equippedWeapon.ammoInClip < shotsToFire && !attackingUnit.equippedWeapon.melee)
shotsToFire = attackingUnit.equippedWeapon.ammoInClip;
for (int i = 0; i < shotsToFire; i++) {
if (outOfAction)
break;
if( CanCounter()){ // Check if we have the Counter ability
// Check if we're in melee range
Tile myTile = GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y];
bool inMeleeRange = false;
foreach (Tile t in myTile.neighbors) {
if (t.occupied > -1) {
if (t.occupyingUnit == attackingUnit ) {
if (Mathf.Abs (myTile.elevation - t.elevation) < maxMeleeElevationDifference) {
GameManager.instance.counteringUnits.Add (this);
}
}
}
}
}
// If it's not a melee weapon or AoE play a gunshot and subtract ammo
if( !attackingUnit.equippedWeapon.melee && !attackingUnit.aoeAttack) {
AudioSource.PlayClipAtPoint( GameManager.instance.audioGunshot , Camera.main.transform.position);
attackingUnit.equippedWeapon.ammoInClip --;
} else { // If this is melee or AoE, there is no cover modifier
if( attackingUnit.equippedWeapon.melee || attackingUnit.aoeAttack)
AudioSource.PlayClipAtPoint( GameManager.instance.audioSword , Camera.main.transform.position);
coverModifier = 0;
}
//attack logic
//roll to hit
if ( GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y].shortRange ) { // Is the attacker shooting from short range?
weaponRangeModifier = attackingUnit.equippedWeapon.shortToHit;
} else { // If not, they're shooting from long range
weaponRangeModifier = attackingUnit.equippedWeapon.longToHit;
}
int diceRoll = Random.Range( 1, diceNumSides+1);
// CHANGE CHANCE OF HITTING BASED ON FACING DIRECTION
// HACK If the attacking unit is behind us, Increase chance of hitting
if( GameManager.CheckRelativePos( attackingUnit.transform, transform) == -1){
diceRoll += 20;
Debug.Log ("Diceroll +20: " + diceRoll);
}
// If the attacking unit is to the side, increase chance of hitting
if( GameManager.CheckRelativePos( attackingUnit.transform, transform) == 0 ){
diceRoll += 10;
Debug.Log ("Diceroll +10: " + diceRoll);
}
bool hit;
if( attackingUnit.equippedWeapon.melee ){ // If this is a melee attack use the melee hit rules
hit = diceRoll >= 100 - attackingUnit.agility - agility - attackingUnit.meleeSkill;
} else { // If its not melee, use the projectile hit rules
hit = diceRoll + weaponRangeModifier + coverModifier >= 100 - attackingUnit.ballisticSkill;
}
// Debug.Log ("dice Roll: " + diceRoll);
// Debug.Log ("Cover Mod: " + coverModifier);
// Debug.Log ("Wep Range Mod: " + weaponRangeModifier);
bool criticalHit = false;
if( diceRoll > 95 ){ // If we roll a "natural 20" the hit auto lands and causes double dmg
criticalHit = true;
hit = true;
}
if (hit) {
//pinned = true;
//Debug.Log(attackingUnit.unitName + " hit " + unitName + "!");
int dmgToDeal = (int)GameManager.instance.DamageToDeal( attackingUnit );
if( criticalHit ) // Double damage
dmgToDeal *= 2;
TakeDamage (dmgToDeal, attackingUnit);
bool knockback = false;
if (attackingUnit.equippedWeapon.abilityWeapon) { // If we're using an ability weapon, check it's effects
foreach (string s in attackingUnit.equippedWeapon.GetComponent<Ability>().effects) {
if (s == "knockback")
knockback = true;
}
}
if (criticalHit && !outOfAction) { // If it was a critical hit, knock us back
knockback = true;
}
// Knockback if possible
if (knockback) {
Tile knockbackTile = GameManager.instance.map [(int)gridPosition.x] [(int)gridPosition.y].KnockbackToNeighbor (attackingUnit.transform.position);
// Move us if it isn't the same tile that we're on now
if (knockbackTile != GameManager.instance.map [(int)gridPosition.x] [(int)gridPosition.y]) {
GameManager.instance.knockbackAttackedUnit (this, attackingUnit, knockbackTile);
// If we're knocked back this cancels counter-attacks
if (GameManager.instance.counteringUnits.Contains (this)) {
GameManager.instance.counteringUnits.Remove (this);
}
}
} else {
HitFX (); // Only do the HitFX if we're not knocked back (strange things happen with the Tweens happening on top of eachother)
}
//Debug.Log(currUnit.unitName + " successfuly hit " + target.unitName + " for " + amountOfDamage + " damage!");
} else {
GameObject missedParticle = (GameObject)Instantiate(Resources.Load("Helpers/MissedParticle", typeof(GameObject)));
missedParticle.transform.position = this.transform.position;
Destroy( missedParticle, 5);
//Debug.Log(attackingUnit.unitName + " missed " + unitName + "!");
}
yield return new WaitForSeconds( attackingUnit.equippedWeapon.firingRate);
}
StartCoroutine( GameManager.instance.currUnit.CompletedAction() );
}
public void TakeDamage( int amt, Unit attackingUnit ){
currHP -= amt;
GameManager.instance.ShowMessageInWorld( ("-" + amt).ToString() + " HP", transform.position, Color.red);
if( currHP <= 0 ){ // we dead!
// Give the attacking unit XP if the unit is killed, if it's not a friendly unit
if( attackingUnit.teamID != teamID)
attackingUnit.xpForThisBattle += 5;
KillFX();
OutOfAction();
}
}
public void HealFromDown() {
Unit healingUnit = GameManager.instance.currUnit;
GameManager.instance.removeTileHighlights();
if( down ){
down = false;
//pinned = true;
PinAnimation();
}
meleeSkill = 3;
ballisticSkill = 3;
currHP += healAmount;
if( currHP > totalHP )
currHP = totalHP;
GameObject healedParticle = (GameObject)Instantiate(Resources.Load("Helpers/HealedParticle", typeof(GameObject)));
healedParticle.transform.position = this.transform.position;
Destroy( healedParticle, 5);
//healingUnit.numOfHeals--;
StartCoroutine( healingUnit.CompletedAction());
//if( healingUnit.currItem != null)
//healingUnit.currItem.Use();
healingUnit.healing = false;
healingUnit.selectingTarget = false;
healingUnit.selectingMagic = false;
healingUnit.selectingItems = false;
}
public void AttemptToRecruit() {
Unit recruitingUnit = GameManager.instance.currUnit;
GameManager.instance.removeTileHighlights();
int randRoll = Random.Range (1, 101);
if (randRoll > (int)(currHP/totalHP*100)) { // Chance to recruit, for now just more likely as they get weaker
teamID = recruitingUnit.teamID;
isAI = false;
Destroy (gameObject.GetComponent<AI> ());
myClan = recruitingUnit.myClan;
// Add it to the clan
Clan recruitingClan = GameManager.instance.team1Clan; // For now, always the player's clan
recruitingClan.units.Add( name );
// Add equipment and items to clan
foreach (string e in equipmentStrings) {
if (!string.IsNullOrEmpty (e)) { // Don't add empty strings!
if (recruitingClan.equipNames.Contains (e)) { // If the clan is already holding equipment of this type, just add it to held and equipped
int equipIndex = recruitingClan.equipNames.IndexOf (e);
recruitingClan.equipHeld [equipIndex]++;
recruitingClan.equipEquipped [equipIndex]++;
} else { // If the clan doesn't have any of this equipment, add it!
recruitingClan.equipNames.Add( e );
recruitingClan.equipHeld.Add (1);
recruitingClan.equipEquipped.Add (1);
}
}
}
foreach (string i in itemStrings) {
if (!string.IsNullOrEmpty (i)) { // Don't add empty strings!
if (recruitingClan.itemStrings.Contains (i)) { // If the clan is already holding items of this type, just add it to held and equipped
int itemIndex = recruitingClan.itemStrings.IndexOf (i);
recruitingClan.itemsHeld [itemIndex]++;
recruitingClan.itemsEquipped [itemIndex]++;
} else { // If the clan doesn't have any of this item, add it!
recruitingClan.itemStrings.Add( i );
recruitingClan.itemsHeld.Add (1);
recruitingClan.itemsEquipped.Add (1);
}
}
}
// Add my known jobs to the clan's known jobs
foreach (string job in jobsLearned) {
if (!string.IsNullOrEmpty (job)) { // Don't add empty strings!
if (!recruitingClan.knownJobs.Contains (job)) { // If the clan doesn't know this job, add it!
recruitingClan.knownJobs.Add (job);
}
}
}
GameManager.instance.CheckForWinner ();
}
GameObject recruitParticle = (GameObject)Instantiate(Resources.Load("Helpers/HealedParticle", typeof(GameObject)));
recruitParticle.transform.position = this.transform.position;
Destroy( recruitParticle, 5);
StartCoroutine( recruitingUnit.CompletedAction());
recruitingUnit.recruiting = false;
recruitingUnit.selectingTarget = false;
recruitingUnit.selectingMagic = false;
recruitingUnit.selectingItems = false;
}
public void SelectMove(){
if (!moving) {
moving = true;
attacking = false;
meleeAttacking = false;
running = false;
selectingTarget = true;
// Give the run option only for players
if( decisionPoints > 1 && !isAI ){
GameManager.instance.highlightTilesAt(gridPosition, GameManager.blueColor, movementPerDecisionPoint*2);
} else {
GameManager.instance.highlightTilesAt(gridPosition, GameManager.blueColor, movementPerDecisionPoint);
}
}
}
public void SelectCrawl(){
if (!moving) {
GameManager.instance.removeTileHighlights();
moving = true;
attacking = false;
meleeAttacking = false;
selectingTarget = true;
GameManager.instance.highlightTilesAt(gridPosition, GameManager.blueColor, movementPerDecisionPoint/2);
}
}
public void SelectHeal( int amount ){
if (!attacking) {
healAmount = amount;
GameManager.instance.removeTileHighlights();
moving = false;
attacking = false;
meleeAttacking = false;
running = false;
healing = true;
selectingTarget = true;
GameManager.instance.highlightTilesAt(gridPosition,GameManager.whiteColor, 1);
}
}
public void SelectRecruit( ){
if (!attacking) {
GameManager.instance.removeTileHighlights();
moving = false;
attacking = false;
meleeAttacking = false;
running = false;
healing = false;
recruiting = true;
selectingTarget = true;
GameManager.instance.highlightTilesAt(gridPosition,GameManager.whiteColor, 1);
}
}
public void SelectWeapon( int weaponSlot, Weapon passedWep = null ){
Weapon selectedWeapon = weapons[0];
if( weaponSlot == 2)
selectedWeapon = weapons[1];
if( weaponSlot == 3)
selectedWeapon = weapons[2];
if( weaponSlot == 99) // Special case for grenade
selectedWeapon = GetGrenade();
//Debug.Log (selectedWeapon.weaponName);
// If we have a shield, disable it while we attack!
if (shield != null) {
shield.SetActive(false);
}
if (passedWep) { // If we've been passed a Weapon, equip that. This is only done for Ability Weapons for now, so we also do extra stuff for that
if (equippedWeapon) // If we already have a weapon equipped, set that one aside
setAsideWeapon = equippedWeapon;
selectedWeapon = passedWep;
}