/
Player.cs
1014 lines (792 loc) · 34.1 KB
/
Player.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
/* This file is part of the "Tanks Multiplayer" project by Rebound Games.
* You are only allowed to use these resources if you've bought them from the Unity Asset Store.
* You shall not license, sublicense, sell, resell, transfer, assign, distribute or
* otherwise make available to any third party the Service or the Content. */
using System;
using UnityEngine;
using UnityEngine.UI;
using Photon;
using UnityEngine.Events;
using System.Collections.Generic;
using System.Collections;
namespace TanksMP
{
/// <summary>
/// Networked player class implementing movement control and shooting.
/// Contains both server and client logic in an authoritative approach.
/// </summary>
public class Player : PunBehaviour
{
/// <summary>
/// UI Text displaying the player name.
/// </summary>
public Text label;
/// <summary>
/// Maximum health value at game start.
/// </summary>
public int maxHealth = 10;
public int Hpbonus = 0;
/// <summary>
/// Current turret rotation and shooting direction.
/// </summary>
[HideInInspector]
public short turretRotation;
/// <summary>
/// Delay between shots.
/// </summary>
public float fireRate = 0.75f;
/// <summary>
/// Movement speed in all directions.
/// </summary>
public float moveSpeed = 8f;
public int Damage = 0;
/// <summary>
/// UI Slider visualizing health value.
/// </summary>
public Slider healthSlider;
/// <summary>
/// UI Slider visualizing shield value.
/// </summary>
public Slider shieldSlider;
/// <summary>
/// Clip to play when a shot has been fired.
/// </summary>
public AudioClip shotClip;
/// <summary>
/// Clip to play on player death.
/// </summary>
public AudioClip explosionClip;
/// <summary>
/// Object to spawn on shooting.
/// </summary>
public GameObject shotFX;
/// <summary>
/// Object to spawn on player death.
/// </summary>
public GameObject explosionFX;
/// <summary>
/// Turret to rotate with look direction.
/// </summary>
public Transform turret;
public GameObject ZoneBome;
/// <summary>
/// Position to spawn new bullets at.
/// </summary>
public Transform shotPos;
public Transform shotPos2;
public Transform lastProjectilePosition;
private Vector3 Vec_ZoneBom;
/// <summary>
/// Array of available bullets for shooting.
/// </summary>
public GameObject[] bullets;
public int currentBullet = 1;
public int StateWepon = 0;
/// <summary>
/// MeshRenderers that should be highlighted in team color.
/// </summary>
public Material[] material;
public MeshRenderer[] renderers;
public float defaultBulletSpeed = 10;
/// <summary>
/// Last player gameobject that killed this one.
/// </summary>
[HideInInspector]
public GameObject killedBy;
/// <summary>
/// Reference to the camera following component.
/// </summary>
[HideInInspector]
public FollowTarget camFollow;
//timestamp when next shot should happen
private float nextFire;
private float m_BulletSpeed;
//reference to this rigidbody
#pragma warning disable 0649
private Rigidbody rb;
#pragma warning restore 0649
private List<Vector3> point = new List<Vector3>();
private LineRenderer renderLine;
private bool drawLine;
public bool landmine = false;
public bool projectile = false;
public int multiplyDamage_Status = 1;
private int Hp_Status = 0;
private int Speed_Status = 0;
//initialize server values for this player
protected virtual void Awake()
{
//only let the master do initialization
if(!PhotonNetwork.isMasterClient)
return;
//set players current health value after joining
GetView().SetHealth(maxHealth);
MVC.EventManager.onHitTriggerEvent.AddListener(Apply);
}
/// <summary>
/// Initialize synced values on every client.
/// Initialize camera and input for this local client.
/// </summary>
void Start()
{
m_BulletSpeed = defaultBulletSpeed;
lastProjectilePosition.SetParent(null);
renderLine = GetComponent<LineRenderer>();
//get corresponding team and colorize renderers in team color
Team team = GameManager.GetInstance().teams[GetView().GetTeam()];
// for(int i = 0; i < renderers.Length; i++)
// renderers[i].material = team.material;
//set name in label
label.text = GetView().GetName();
//call hooks manually to update
// OnHealthChange(GetView().GetHealth());
OnHealthChange(maxHealth);
OnShieldChange(GetView().GetShield());
//called only for this client
if (!photonView.isMine)
return;
//set a global reference to the local player
GameManager.GetInstance().localPlayer = this;
//get components and set camera target
rb = GetComponent<Rigidbody>();
camFollow = Camera.main.GetComponent<FollowTarget>();
camFollow.target = turret;
//initialize input controls for mobile devices
//[0]=left joystick for movement, [1]=right joystick for shooting
#if !UNITY_STANDALONE && !UNITY_WEBGL
GameManager.GetInstance().ui.controls[0].onDrag += Move;
GameManager.GetInstance().ui.controls[0].onDragEnd += MoveEnd;
GameManager.GetInstance().ui.controls[1].onDragBegin += ShootBegin;
GameManager.GetInstance().ui.controls[1].onDrag += RotateTurret;
GameManager.GetInstance().ui.controls[1].onDrag += ShootAndLandMineUi;
GameManager.GetInstance().ui.controls[1].onEnter += ShootAndLandMineUi;
GameManager.GetInstance().ui.controls[1].onEnterDown += DrawLineProjectileUi;
GameManager.GetInstance().ui.controls[1].onEnterUp += ShootProjectileUi;
#endif
StartCoroutine(OnProjectileShootRoutine());
}
void OnEnable()
{
OnHealthChange(maxHealth);
StartCoroutine(OnProjectileShootRoutine());
}
/// <summary>
/// This method gets called whenever player properties have been changed on the network.
/// </summary>
// public override void OnPhotonPlayerPropertiesChanged(object[] playerAndUpdatedProps)******************************
// {
// //only react on property changes for this player
// PhotonPlayer player = playerAndUpdatedProps[0] as PhotonPlayer;
// if(player != photonView.owner)
// return;
//
// //update values that could change any time for visualization to stay up to date
// OnHealthChange(player.GetHealth());
// OnShieldChange(player.GetShield());
// }
//this method gets called multiple times per second, at least 10 times or more
void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
//here we send the turret rotation angle to other clients
stream.SendNext(turretRotation);
}
else
{
//here we receive the turret rotation angle from others and apply it
this.turretRotation = (short)stream.ReceiveNext();
OnTurretRotation();
}
}
//continously check for input on desktop platforms
#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBGL
void FixedUpdate()
{
//skip further calls for remote clients
if (!photonView.isMine)
{
//keep turret rotation updated for all clients
OnTurretRotation();
return;
}
//movement variables
Vector2 moveDir;
Vector2 turnDir;
//reset moving input when no arrow keys are pressed down
if (Input.GetAxisRaw("Horizontal") == 0 && Input.GetAxisRaw("Vertical") == 0)
{
moveDir.x = 0;
moveDir.y = 0;
}
else
{
//read out moving directions and calculate force
moveDir.x = Input.GetAxis("Horizontal");
moveDir.y = Input.GetAxis("Vertical");
Move(moveDir);
}
//cast a ray on a plane at the mouse position for detecting where to shoot
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Plane plane = new Plane(Vector3.up, Vector3.up);
float distance = 0f;
Vector3 hitPos = Vector3.zero;
//the hit position determines the mouse position in the scene
if (plane.Raycast(ray, out distance))
{
hitPos = ray.GetPoint(distance) - transform.position;
}
//we've converted the mouse position to a direction
turnDir = new Vector2(hitPos.x, hitPos.z);
//rotate turret to look at the mouse direction
RotateTurret(new Vector2(hitPos.x, hitPos.z));
//----------------------------------------------------------------------
// UIJoystick Uijoy = FindObjectOfType<UIJoystick>();
// //shoot bullet on left mouse click
// if (projectile == false && landmine == false)
// {
// Uijoy.UIBullet();
//
// if (CrossPlatformInputManager.GetButton("Fire"))
// {
// Debug.Log("fire");
// Shoot();
// }
//
// }
// else if (projectile == true && landmine == false)
// {
// Uijoy.UIProjectile();
//
// if (CrossPlatformInputManager.GetButton("Fire"))
// {
// m_BulletSpeed += Time.deltaTime * 5f;
// DrawLineProjectile();
// Debug.Log("Projectile");
// }
// else if (CrossPlatformInputManager.GetButtonUp("Fire"))
// {
//
// ShootProjectile();
// Debug.Log("ProjectileUp");
// projectile = false;
//
// m_BulletSpeed = defaultBulletSpeed;
// renderLine.numPositions = 0;
//
// lastProjectilePosition.gameObject.SetActive(false);
//
//
//
// }
// }
// else if (landmine == true && projectile == false)
// {
// Uijoy.UILandmine();
//
// if (CrossPlatformInputManager.GetButton("Fire"))
// {
// Debug.Log("Landmine");
// // PoolManager.Spawn(landMine, transform.position, Quaternion.identity);
// GameObject obj = PoolManager.Spawn(bullets[2], transform.position, Quaternion.identity);
//
// obj.GetComponent<Landmine>().stateWepon = StateWepon;
//
// landmine = false;
//
// }
// }
// else
// {
// landmine = false;
// projectile = false;
// }
//replicate input to mobile controls for illustration purposes
#if UNITY_EDITOR
GameManager.GetInstance().ui.controls[0].position = moveDir;
GameManager.GetInstance().ui.controls[1].position = turnDir;
#endif
}
#endif
//--------------------------------------------------
private float GetMaxHeightFromLineProjectile()
{
Vector3 g = Physics.gravity;
Vector3 p1 = shotPos2.position;
Vector3 u = m_BulletSpeed * shotPos2.forward;
int bulletLayer = LayerMask.NameToLayer("Bullet");
float h = 0;
for(float t = 0; t < 2f; t += 0.01f)
{
Vector3 s = (u * t) + (0.5f * g * t * t);
Vector3 p2 = p1 + s;
RaycastHit hit;
if (Physics.Linecast(p1, p2, out hit))
{
if (hit.collider.gameObject.layer != bulletLayer)
{
break;
}
}
if (p2.y - shotPos2.position.y > h)
{
h = p2.y - shotPos2.position.y;
}
// point.Add(p2);
p1 = p2;
// Vec_ZoneBom = p2;
u = u + (g * t);
}
return h+shotPos2.position.y;
}
public void DrawLineProjectile()
{
// int speed = 15;
Vector3 g = Physics.gravity;
Vector3 p1 = shotPos2.position;
Vector3 u = m_BulletSpeed * shotPos2.forward;
point.Clear();
int bulletLayer = LayerMask.NameToLayer("Bullet");
for(float t = 0; t < 2f; t += 0.01f)
{
Vector3 s = (u * t) + (0.5f * g * t * t);
Vector3 p2 = p1 + s;
RaycastHit hit;
if (Physics.Linecast(p1, p2, out hit))
{
point.Add(hit.point + Vector3.up);
if (hit.collider.gameObject.layer != bulletLayer)
{
break;
}
}
point.Add(p2);
p1 = p2;
Vec_ZoneBom = p2;
u = u + (g * t);
}
Vector3 last = point[point.Count - 1];
lastProjectilePosition.gameObject.SetActive(true);
lastProjectilePosition.position = last;
renderLine.positionCount = point.Count;
renderLine.SetPositions(point.ToArray());
}
//-------------------------------------------------
/// <summary>
/// Helper method for getting the current object owner.
/// </summary>
public PhotonView GetView()
{
return this.photonView;
}
//moves rigidbody in the direction passed in
void Move(Vector2 direction = default(Vector2))
{
//if direction is not zero, rotate player in the moving direction relative to camera
if (direction != Vector2.zero)
transform.rotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.y))
* Quaternion.Euler(0, camFollow.camTransform.eulerAngles.y, 0);
//create movement vector based on current rotation and speed
Vector3 movementDir = transform.forward * moveSpeed * Time.deltaTime;
//apply vector to rigidbody position
rb.MovePosition(rb.position + movementDir);
}
//on movement drag ended
void MoveEnd()
{
//reset rigidbody physics values
rb.velocity = Vector3.zero;
rb.angularVelocity = Vector3.zero;
}
//rotates turret to the direction passed in
void RotateTurret(Vector2 direction = default(Vector2))
{
//don't rotate without values
if (direction == Vector2.zero)
return;
//get rotation value as angle out of the direction we received
turretRotation = (short)Quaternion.LookRotation(new Vector3(direction.x, 0, direction.y)).eulerAngles.y;
OnTurretRotation();
}
//on shot drag start set small delay for first shot
void ShootBegin()
{
nextFire = Time.time + 0.25f;
}
//shoots a bullet in the direction passed in
//we do not rely on the current turret rotation here, because we send the direction
//along with the shot request to the server to absolutely ensure a synced shot position
public void Shoot(Vector2 direction = default(Vector2))
{
//if shot delay is over
if (Time.time > nextFire)
{
//set next shot timestamp
nextFire = Time.time + fireRate;
//send current client position and turret rotation along to sync the shot position
//also we are sending it as a short array (only x,z - skip y) to save additional bandwidth
short[] pos = new short[] { (short)(shotPos.position.x * 10), (short)(shotPos.position.z * 10)};
//send shot request with origin to server
this.photonView.RPC("CmdShoot", PhotonTargets.AllViaServer, pos, turretRotation);
}
}
//called on the server first but forwarded to all clients
[PunRPC]
protected void CmdShoot(short[] position, short angle)
{
//get current bullet type
// int currentBullet = GetView().GetBullet();
int currentBullet = 0;
//calculate center between shot position sent and current server position (factor 0.6f = 40% client, 60% server)
//this is done to compensate network lag and smoothing it out between both client/server positions
Vector3 shotCenter = Vector3.Lerp(shotPos.position, new Vector3(position[0]/10f, shotPos.position.y, position[1]/10f), 0.6f);
Quaternion syncedRot = turret.rotation = Quaternion.Euler(0, angle, 0);
//spawn bullet using pooling
GameObject obj = PoolManager.Spawn(bullets[currentBullet], shotCenter, syncedRot);
obj.GetComponent<Bullet>().owner = gameObject;
obj.GetComponent<Bullet>().damage = Damage * multiplyDamage_Status;
//check for current ammunition
//let the server decrease special ammunition, if present
if (PhotonNetwork.isMasterClient && currentBullet != 0)
{
//if ran out of ammo: reset bullet automatically
GetView().DecreaseAmmo(1);
}
//send event to all clients for spawning effects
if (shotFX || shotClip)
RpcOnShot();
MVC.EventManager.onCheckAmountIndex.Invoke(currentBullet);
}
IEnumerator OnProjectileShootRoutine()
{
while (true)
{
UIJoystick Uijoy = FindObjectOfType<UIJoystick>();
//shoot bullet on left mouse click
if (projectile == false && landmine == false)
{
Uijoy.UIBullet();
if (Input.GetButton("Fire1"))
Shoot();
}
else if (projectile == true && landmine == false)
{
Uijoy.UIProjectile();
if (Input.GetButton("Fire1"))
{
m_BulletSpeed += Time.deltaTime * 5f;
DrawLineProjectile();
}
else if (Input.GetButtonUp("Fire1"))
{
ShootProjectile();
projectile = false;
m_BulletSpeed = defaultBulletSpeed;
renderLine.positionCount = 0;
lastProjectilePosition.gameObject.SetActive(false);
yield return new WaitForSeconds(fireRate);
}
}
else if (landmine == true && projectile == false)
{
Uijoy.UILandmine();
if (Input.GetButton("Fire1"))
{
// PoolManager.Spawn(landMine, transform.position, Quaternion.identity);
GameObject obj = PoolManager.Spawn(bullets[2], transform.position, Quaternion.identity);
obj.GetComponent<Landmine>().stateWepon = StateWepon;
landmine = false;
}
}
else
{
landmine = false;
projectile = false;
}
yield return null;
}
}
public void ShootAndLandMineUi(Vector2 direction = default(Vector2))
{
UIJoystick Uijoy = FindObjectOfType<UIJoystick>();
//shoot bullet on left mouse click
if (projectile == false && landmine == false)
{
Uijoy.UIBullet();
Shoot();
}
else if (landmine == true && projectile == false)
{
GameObject obj = PoolManager.Spawn(bullets[2], transform.position, Quaternion.identity);
obj.GetComponent<Landmine>().stateWepon = StateWepon;
landmine = false;
}
}
public void DrawLineProjectileUi()
{
if (projectile == true && landmine == false)
{
m_BulletSpeed += Time.deltaTime * 5f;
DrawLineProjectile();
}
}
public void ShootProjectileUi()
{
if (projectile == true && landmine == false)
{
ShootProjectile();
projectile = false;
m_BulletSpeed = defaultBulletSpeed;
renderLine.positionCount = 0;
lastProjectilePosition.gameObject.SetActive(false);
}
}
//---------------------------------------------------------------------------------------------
public void ShootProjectile(Vector2 direction = default(Vector2))
{
{
short[] pos = new short[] { (short)(shotPos2.position.x * 10), (short)(shotPos2.position.z * 10)};
//send shot request with origin to server
this.photonView.RPC("CmdShootProjectile", PhotonTargets.AllViaServer, pos, turretRotation);
}
}
//called on the server first but forwarded to all clients
[PunRPC]
protected void CmdShootProjectile(short[] position, short angle)
{
//get current bullet type
//calculate center between shot position sent and current server position (factor 0.6f = 40% client, 60% server)
//this is done to compensate network lag and smoothing it out between both client/server positions
if (currentBullet > 0)
{
Vector3 shotCenter = Vector3.Lerp(shotPos2.position, new Vector3(position[0] / 10f, shotPos2.position.y, position[1] / 10f), 0.6f);
Quaternion syncedRot = turret.rotation = Quaternion.Euler(0, angle, 0);
//spawn bullet using pooling
GameObject obj = PoolManager.Spawn(bullets[1], shotCenter, syncedRot);
obj.GetComponent<Bullet>().owner = gameObject;
// obj.GetComponent<Bullet>().damage = Damage;
obj.GetComponent<Bullet_Projectile>().stateWepon = StateWepon;
obj.GetComponent<Bullet_Projectile>().MoveProjectile(shotPos2.forward.normalized, m_BulletSpeed);
// obj.GetComponent<Bullet>().transform.rotation = shotPos2.rotation;
GameObject zone = Instantiate(ZoneBome, Vec_ZoneBom, Quaternion.identity);
BomZone bomZone = zone.GetComponent<BomZone>();
bomZone.owner = obj;
}
//bomZone.alive = true;
//check for current ammunition
//let the server decrease special ammunition, if present
if (PhotonNetwork.isMasterClient && currentBullet != 0)
{
//if ran out of ammo: reset bullet automatically
GetView().DecreaseAmmo(1);
}
//send event to all clients for spawning effects
if (shotFX || shotClip)
RpcOnShot();
}
//---------------------------------------------------------------------------------------------
//called on all clients after bullet spawn
//spawn effects or sounds locally, if set
protected void RpcOnShot()
{
if (shotFX) PoolManager.Spawn(shotFX, shotPos.position, Quaternion.identity);
if (shotClip) AudioManager.Play3D(shotClip, shotPos.position, 0.1f);
}
//hook for updating turret rotation locally
void OnTurretRotation()
{
//we don't need to check for local ownership when setting the turretRotation,
//because OnPhotonSerializeView PhotonStream.isWriting == true only applies to the owner
turret.rotation = Quaternion.Euler(0, turretRotation, 0);
}
//hook for updating health locally
//(the actual value updates via player properties)
public void OnHealthChange(int value)
{
healthSlider.value = (float)value / maxHealth;
}
//hook for updating shield locally
//(the actual value updates via player properties)
protected void OnShieldChange(int value)
{
shieldSlider.value = value;
}
/// <summary>
/// Server only: calculate damage to be taken by the Player,
/// triggers score increase and respawn workflow on death.
/// </summary>
public void TakeDamage(Bullet bullet)
{
//store network variables temporary
// int health = GetView().GetHealth();
float health = healthSlider.value * maxHealth;
int shield = GetView().GetShield();
//reduce shield on hit
if (shield > 0)
{
GetView().DecreaseShield(1);
return;
}
//substract health by damage
//locally for now, to only have one update later on
health -= (bullet.damage + bullet.Bonusdamage);
//bullet killed the player
if (health <= 0)
{
//the game is already over so don't do anything
if(GameManager.GetInstance().IsGameOver()) return;
//get killer and increase score for that team
Player other = bullet.owner.GetComponent<Player>();
int otherTeam = other.GetView().GetTeam();
PhotonNetwork.room.AddScore(otherTeam);
//the maximum score has been reached now
if(GameManager.GetInstance().IsGameOver())
{
//close room for joining players
PhotonNetwork.room.IsOpen = false;
//tell all clients the winning team
this.photonView.RPC("RpcGameOver", PhotonTargets.All, (byte)otherTeam);
return;
}
//the game is not over yet, reset runtime values
//also tell all clients to despawn this player
// GetView().SetHealth(maxHealth);
OnHealthChange(maxHealth);
GetView().SetBullet(0);
this.photonView.RPC("RpcRespawn", PhotonTargets.All);
}
else
{
//we didn't die, set health to new value
// GetView().SetHealth(health);
OnHealthChange((int)health);
}
}
//called on all clients on both player death and respawn
//only difference is that on respawn, the client sends the request
[PunRPC]
protected virtual void RpcRespawn()
{
//toggle visibility for player gameobject (on/off)
gameObject.SetActive(!gameObject.activeInHierarchy);
bool isActive = gameObject.activeInHierarchy;
//the player has been killed
if (!isActive)
{
//detect whether the current user was responsible for the kill
//yes, that's my kill: take thumbnail via EveryPlay
if (killedBy == GameManager.GetInstance().localPlayer.gameObject)
UnityEveryplayManager.TakeThumbnail();
if (explosionFX)
{
//spawn death particles locally using pooling and colorize them in the player's team color
GameObject particle = PoolManager.Spawn(explosionFX, transform.position, transform.rotation);
ParticleColor pColor = particle.GetComponent<ParticleColor>();
if (pColor) pColor.SetColor(GameManager.GetInstance().teams[GetView().GetTeam()].material.color);
}
//play sound clip on player death
if (explosionClip) AudioManager.Play3D(explosionClip, transform.position);
}
//further changes only affect the local client
if (!photonView.isMine)
return;
//local player got respawned so reset states
if (isActive == true)
ResetPosition();
else
{
//local player was killed, set camera to follow the killer
camFollow.target = killedBy.transform;
//hide input controls and other HUD elements
camFollow.HideMask(true);
//display respawn window (only for local player)
GameManager.GetInstance().DisplayDeath();
}
}
/// <summary>
/// Command telling the server and all others that this client is ready for respawn.
/// This is when the respawn delay is over or a video ad has been watched.
/// </summary>
public void CmdRespawn()
{
this.photonView.RPC("RpcRespawn", PhotonTargets.AllViaServer);
}
/// <summary>
/// Repositions in team area and resets camera & input variables.
/// This should only be called for the local player.
/// </summary>
public void ResetPosition()
{
//start following the local player again
camFollow.target = turret;
camFollow.HideMask(false);
//get team area and reposition it there
transform.position = GameManager.GetInstance().GetSpawnPosition(GetView().GetTeam());
//reset forces modified by input
rb.velocity = Vector3.zero;
rb.angularVelocity = Vector3.zero;
transform.rotation = Quaternion.identity;
//reset input left over
GameManager.GetInstance().ui.controls[0].OnEndDrag(null);
GameManager.GetInstance().ui.controls[1].OnEndDrag(null);
}
//called on all clients on game end providing the winning team
[PunRPC]
protected void RpcGameOver(byte teamIndex)
{
//display game over window
GameManager.GetInstance().DisplayGameOver(teamIndex);
}
public void Apply(GameObject obj,Collider col)
{
MVC.Model.ItemModel model = obj.GetComponent< MVC.Model.ItemModel>();
Player player = col.GetComponent<Player>();
if(player)
{
int value = player.GetView().GetAmmo();
int index = player.GetView().GetBullet();
if (value != model.Amount && index != model.BulletIndex)
{
player.GetView().SetAmmo(model.Amount, model.BulletIndex);
model.spawner.photonView.RPC("Destroy", PhotonTargets.All);
}
}
}
public void UpstatusPlayer()
{
multiplyDamage_Status += GameManager.multiplyDamage_Status;
maxHealth += GameManager.Hp_Status;
moveSpeed += (float) GameManager.Speed_Status;
OnHealthChange(maxHealth);
}
public void changeElement(int element)
{
if (element == 1)
{
for (int i = 0; i < renderers.Length; i++)
{
renderers[i].material = material[0];
}
Time.timeScale = 1;
maxHealth += 50;
Damage += 10;
ModelWepon.AddDamageElement += 10;
StateWepon = 1;
OnHealthChange(maxHealth);
}
else if(element == 2)
{
for (int i = 0; i < renderers.Length; i++)
{
renderers[i].material = material[1];
}
Time.timeScale = 1;
maxHealth += 100;
Damage += 6;