-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerDeathManagement.cs
98 lines (89 loc) · 3.49 KB
/
PlayerDeathManagement.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace Asteroids
{
public class PlayerDeathManagement : MonoBehaviour
{
[SerializeField]
private GameReset gameReset;
[SerializeField]
private GameSounds gameSounds;
private PlayerData playerData;
private PlayerRenderer playerRenderer;
private PlayerForceField playerForceField;
private PlayerUI playerUI;
private PlayerInitialization playerInitialization;
[field: SerializeField]
public GameObject ExplosionPlayer { get; private set; }
private Rigidbody rb;
private BoxCollider boxCollider;
void Start()
{
playerData = GetComponent<PlayerData>();
playerRenderer = GetComponent<PlayerRenderer>();
playerForceField = GetComponent<PlayerForceField>();
playerUI = GetComponent<PlayerUI>();
playerInitialization = GetComponent<PlayerInitialization>();
rb = GetComponent<Rigidbody>();
boxCollider = GetComponent<BoxCollider>();
}
public void DestroyPlayer()
{
if (playerData.PlayerInvulnerable || playerForceField.ForceField.activeInHierarchy) return;
ExplosionPlayer.SetActive(true);
foreach (ParticleSystem ps in ExplosionPlayer.GetComponentsInChildren<ParticleSystem>())
{
ps.Play();
}
gameSounds.ExplosionSound.Play();
playerData.Lives--;
Image[] livesImages = playerUI.LivesContainer.GetComponentsInChildren<Image>();
for (int i = 0; i < livesImages.Length; i++)
{
if (i >= playerData.Lives) livesImages[i].enabled = false;
}
//If dead restart game and stats
if (playerData.Lives == 0)
{
//We take player out of screen to make it dissapear
transform.position = Vector3.one * 1000f;
StartCoroutine(gameReset.WaitForRestartGame());
}
else
{
//Make dissapear player and reappear in 3 seconds with forcefield, and 2 seconds later forcefield dissapears
StartCoroutine(DisablePlayer());
StartCoroutine(playerForceField.Invulnerable());
}
}
public void DisablePlayerInmediately()
{
playerData.PlayerDisabled = true;
rb.velocity = Vector3.zero;
playerRenderer.GetComponent<SpriteRenderer>().enabled = false;
boxCollider.enabled = false;
playerRenderer.Rocket.SetActive(false);
}
public void EnablePlayerInmediately()
{
ExplosionPlayer.SetActive(false);
playerRenderer.GetComponent<SpriteRenderer>().enabled = true;
boxCollider.enabled = true;
transform.position = playerInitialization.StartingPos;
transform.rotation = playerInitialization.StartingRot;
playerRenderer.Rocket.SetActive(true);
rb.velocity = Vector3.zero;
playerData.PlayerDisabled = false;
StartCoroutine(playerForceField.Invulnerable());
playerForceField.ForceField.SetActive(true);
}
public IEnumerator DisablePlayer()
{
DisablePlayerInmediately();
yield return new WaitForSeconds(3f);
EnablePlayerInmediately();
}
}
}