Limited movement when health <= 0

This commit is contained in:
Alicja 2023-01-16 00:22:07 +01:00
parent 7ad703817c
commit 38540b2eab

View File

@ -16,6 +16,11 @@ public class Player : MonoBehaviour
private bool inRange = false; private bool inRange = false;
public ParticleSystem dmgParticleSystem; public ParticleSystem dmgParticleSystem;
private float x;
private float y;
public float speedy = 300f;
public Vector3 temp1;
public HealthBar healthBar; public HealthBar healthBar;
private static bool attackSword; private static bool attackSword;
@ -33,7 +38,7 @@ public class Player : MonoBehaviour
private bool startRegen = false; private bool startRegen = false;
private bool canWalk = true; private bool canWalk;
[Header("Player Starts values")] [Header("Player Starts values")]
public const int BaseHealth = 10; public const int BaseHealth = 10;
@ -74,6 +79,7 @@ public class Player : MonoBehaviour
void Start() void Start()
{ {
canWalk = true;
Panel = GameObject.FindObjectsOfType<GameObject>(true).Where(sr => sr.gameObject.name == "YouDied").ToArray()[0]; Panel = GameObject.FindObjectsOfType<GameObject>(true).Where(sr => sr.gameObject.name == "YouDied").ToArray()[0];
healthBar = (HealthBar)FindObjectOfType<HealthBar>(); healthBar = (HealthBar)FindObjectOfType<HealthBar>();
levelBar = (LevelBar)FindObjectOfType<LevelBar>(); levelBar = (LevelBar)FindObjectOfType<LevelBar>();
@ -105,40 +111,40 @@ public class Player : MonoBehaviour
//DEFAULT CONTROLS //DEFAULT CONTROLS
if (!PlayerPrefs.HasKey("Interaction")) if (!PlayerPrefs.HasKey("Interaction"))
{ {
PlayerPrefs.SetString("Interaction","E"); PlayerPrefs.SetString("Interaction", "E");
} }
if (!PlayerPrefs.HasKey("Attack")) if (!PlayerPrefs.HasKey("Attack"))
{ {
PlayerPrefs.SetString("Attack","Space"); PlayerPrefs.SetString("Attack", "Space");
} }
if (!PlayerPrefs.HasKey("Skills")) if (!PlayerPrefs.HasKey("Skills"))
{ {
PlayerPrefs.SetString("Skills","U"); PlayerPrefs.SetString("Skills", "U");
} }
if (!PlayerPrefs.HasKey("Inventory")) if (!PlayerPrefs.HasKey("Inventory"))
{ {
PlayerPrefs.SetString("Inventory","I"); PlayerPrefs.SetString("Inventory", "I");
} }
if (!PlayerPrefs.HasKey("Settings")) if (!PlayerPrefs.HasKey("Settings"))
{ {
PlayerPrefs.SetString("Settings","Escape"); PlayerPrefs.SetString("Settings", "Escape");
} }
if (!PlayerPrefs.HasKey("Quests")) if (!PlayerPrefs.HasKey("Quests"))
{ {
PlayerPrefs.SetString("Quests","Q"); PlayerPrefs.SetString("Quests", "Q");
} }
if (PlayerPrefs.HasKey("Quests")) if (PlayerPrefs.HasKey("Quests"))
{ {
TaskUIManager.Instance.keyToOpen = (KeyCode) System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Quests")); TaskUIManager.Instance.keyToOpen = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Quests"));
} }
if (!PlayerPrefs.HasKey("ExpHlth")) if (!PlayerPrefs.HasKey("ExpHlth"))
{ {
PlayerPrefs.SetString("ExpHlth","Tab"); PlayerPrefs.SetString("ExpHlth", "Tab");
} }
if (!PlayerPrefs.HasKey("EXP HEALTH")) if (!PlayerPrefs.HasKey("EXP HEALTH"))
{ {
PlayerPrefs.SetString("EXP HEALTH","Tab"); PlayerPrefs.SetString("EXP HEALTH", "Tab");
} }
// -------------------------------------------------------------------- // --------------------------------------------------------------------
@ -303,12 +309,18 @@ public class Player : MonoBehaviour
startRegen = false; startRegen = false;
if (currentHealth <= 0) if (currentHealth <= 0)
{ {
x = transform.position.x;
y = transform.position.y;
temp1.x = x;
temp1.y = y;
transform.position = Vector3.MoveTowards(transform.position, temp1, speedy * Time.deltaTime);
if (isPanelEnabled) if (isPanelEnabled)
{ {
Panel.SetActive(true); Panel.SetActive(true);
} }
walkSpeed = 0f; walkSpeed = 0f;
canWalk = false; canWalk = false;
gameObject.GetComponent<Animator>().enabled = false;
} }
} }
@ -330,7 +342,8 @@ public class Player : MonoBehaviour
if (attackFist) if (attackFist)
{ {
myAnimator.SetTrigger("attackFist"); myAnimator.SetTrigger("attackFist");
} else if (attackSword && }
else if (attackSword &&
EquipmentUIManager.Instance.GetList().Count() != 0 && EquipmentUIManager.Instance.GetList().Count() != 0 &&
EquipmentUIManager.Instance.GetList() EquipmentUIManager.Instance.GetList()
.Where(el => el.Key == (int)EquipmentPanelSlotsTypeEnum.WeaponSlot && el.Value != null).Count() > 0 && .Where(el => el.Key == (int)EquipmentPanelSlotsTypeEnum.WeaponSlot && el.Value != null).Count() > 0 &&
@ -354,7 +367,7 @@ public class Player : MonoBehaviour
private void HandleInput() private void HandleInput()
{ {
KeyCode keyToAttack = (KeyCode) System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Attack")); KeyCode keyToAttack = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Attack"));
if (Input.GetKeyDown(keyToAttack)) if (Input.GetKeyDown(keyToAttack))
{ {
@ -379,6 +392,17 @@ public class Player : MonoBehaviour
void Update() void Update()
{ {
if (currentHealth <= 0)
{
walkSpeed = 0f;
PlayerPrefs.SetFloat("speed", 0.0f);
temp1.x = x;
temp1.y = y;
transform.position = Vector3.MoveTowards(transform.position, temp1, speedy * Time.deltaTime);
}
else
{
if (lvlUp == true) if (lvlUp == true)
{ {
@ -459,8 +483,21 @@ public class Player : MonoBehaviour
HandleInput(); HandleInput();
} }
}
void FixedUpdate() void FixedUpdate()
{ {
if (currentHealth <= 0)
{
walkSpeed = 0f;
PlayerPrefs.SetFloat("speed", 0.0f);
temp1.x = x;
temp1.y = y;
transform.position = Vector3.MoveTowards(transform.position, temp1, speedy * Time.deltaTime);
}
else
{
if (canWalk == true) if (canWalk == true)
{ {
if (inputHorizontal != 0 || inputVertical != 0) if (inputHorizontal != 0 || inputVertical != 0)
@ -482,6 +519,8 @@ public class Player : MonoBehaviour
ResetValues(); ResetValues();
} }
}
public void SaveCheckpoint() public void SaveCheckpoint()
{ {
currentHealth = PlayerPrefs.GetFloat("health"); currentHealth = PlayerPrefs.GetFloat("health");
@ -537,7 +576,8 @@ public class Player : MonoBehaviour
attackValue = attackCalculator.Calculate(equippedItem.Value); attackValue = attackCalculator.Calculate(equippedItem.Value);
PlayerPrefs.SetFloat("attackValue", attackValue); PlayerPrefs.SetFloat("attackValue", attackValue);
} else }
else
{ {
throw new System.Exception("Attack Calculation Error"); throw new System.Exception("Attack Calculation Error");
} }
@ -566,7 +606,8 @@ public class Player : MonoBehaviour
if (EquipmentUIManager.Instance.GetList().Count() == 0 || if (EquipmentUIManager.Instance.GetList().Count() == 0 ||
EquipmentUIManager.Instance.GetList().Where(el => el.Key == (int)EquipmentPanelSlotsTypeEnum.HelmetSlot && el.Value != null).Count() == 0 && EquipmentUIManager.Instance.GetList().Where(el => el.Key == (int)EquipmentPanelSlotsTypeEnum.HelmetSlot && el.Value != null).Count() == 0 &&
EquipmentUIManager.Instance.GetList().Where(el => el.Key == (int)EquipmentPanelSlotsTypeEnum.ArmorSlot && el.Value != null).Count() == 0 EquipmentUIManager.Instance.GetList().Where(el => el.Key == (int)EquipmentPanelSlotsTypeEnum.ArmorSlot && el.Value != null).Count() == 0
){ )
{
defenseValue = defenseCalculator.CalculateWithoutItem(); defenseValue = defenseCalculator.CalculateWithoutItem();
@ -583,7 +624,8 @@ public class Player : MonoBehaviour
.Where(el => el.Key == (int)EquipmentPanelSlotsTypeEnum.ArmorSlot && el.Value != null).Count() > 0 && .Where(el => el.Key == (int)EquipmentPanelSlotsTypeEnum.ArmorSlot && el.Value != null).Count() > 0 &&
EquipmentUIManager.Instance.GetList() EquipmentUIManager.Instance.GetList()
.Where(el => el.Key == (int)EquipmentPanelSlotsTypeEnum.ArmorSlot && el.Value != null).First().Value.EquipmentType == EquipmentTypeEnum.Chest) .Where(el => el.Key == (int)EquipmentPanelSlotsTypeEnum.ArmorSlot && el.Value != null).First().Value.EquipmentType == EquipmentTypeEnum.Chest)
) { )
{
var helmetSlot = EquipmentUIManager.Instance.GetList().Where(el => el.Key == (int)EquipmentPanelSlotsTypeEnum.HelmetSlot && el.Value != null); var helmetSlot = EquipmentUIManager.Instance.GetList().Where(el => el.Key == (int)EquipmentPanelSlotsTypeEnum.HelmetSlot && el.Value != null);
var helmet = (helmetSlot.Count() != 0) ? helmetSlot.First().Value.Value : 0; var helmet = (helmetSlot.Count() != 0) ? helmetSlot.First().Value.Value : 0;