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,12 +342,13 @@ public class Player : MonoBehaviour
if (attackFist) if (attackFist)
{ {
myAnimator.SetTrigger("attackFist"); myAnimator.SetTrigger("attackFist");
} else if (attackSword && }
EquipmentUIManager.Instance.GetList().Count() != 0 && else if (attackSword &&
EquipmentUIManager.Instance.GetList() EquipmentUIManager.Instance.GetList().Count() != 0 &&
.Where(el => el.Key == (int)EquipmentPanelSlotsTypeEnum.WeaponSlot && el.Value != null).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).First().Value.Name.Equals("Pickaxe")) EquipmentUIManager.Instance.GetList()
.Where(el => el.Key == (int)EquipmentPanelSlotsTypeEnum.WeaponSlot && el.Value != null).First().Value.Name.Equals("Pickaxe"))
{ {
myAnimator.SetTrigger("pickaxe"); myAnimator.SetTrigger("pickaxe");
} }
@ -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,107 +392,133 @@ public class Player : MonoBehaviour
void Update() void Update()
{ {
if (currentHealth <= 0)
if (lvlUp == true)
{ {
PlayerPrefs.SetInt("LvlUpPopUp", 1); walkSpeed = 0f;
exp = 0; PlayerPrefs.SetFloat("speed", 0.0f);
lvlUp = false; temp1.x = x;
temp1.y = y;
transform.position = Vector3.MoveTowards(transform.position, temp1, speedy * Time.deltaTime);
} }
else
ManageHealth();
ManageDefense();
ManageIntelligence();
ManageStrength();
ManageSpeed();
if (canWalk == true)
{ {
inputHorizontal = Input.GetAxisRaw("Horizontal");
inputVertical = Input.GetAxisRaw("Vertical");
// speed calculated in ManageSpeed function if (lvlUp == true)
walkSpeed = PlayerPrefs.GetFloat("speed");
myAnimator.SetFloat("moveX", inputHorizontal * walkSpeed);
myAnimator.SetFloat("moveY", inputVertical * walkSpeed);
if (inputHorizontal != 0)
{ {
myAnimator.SetFloat("speed", walkSpeed); PlayerPrefs.SetInt("LvlUpPopUp", 1);
} exp = 0;
else if (inputVertical != 0) lvlUp = false;
{
myAnimator.SetFloat("speed", walkSpeed);
}
else
{
myAnimator.SetFloat("speed", 0);
} }
if (inputHorizontal == 1 || inputHorizontal == -1 || inputVertical == 1 || inputVertical == -1) ManageHealth();
{ ManageDefense();
myAnimator.SetFloat("lastMoveX", inputHorizontal); ManageIntelligence();
myAnimator.SetFloat("lastMoveY", inputVertical); ManageStrength();
} ManageSpeed();
timerRegen += Time.deltaTime; if (canWalk == true)
if (timerRegen >= waitRegen)
{ {
startRegen = true;
}
currentHealth = PlayerPrefs.GetFloat("health"); inputHorizontal = Input.GetAxisRaw("Horizontal");
maxHealth = PlayerPrefs.GetFloat("maxHealth"); inputVertical = Input.GetAxisRaw("Vertical");
if (startRegen == true)
{ // speed calculated in ManageSpeed function
timerTick += Time.deltaTime; walkSpeed = PlayerPrefs.GetFloat("speed");
if (timerTick >= waitTick)
myAnimator.SetFloat("moveX", inputHorizontal * walkSpeed);
myAnimator.SetFloat("moveY", inputVertical * walkSpeed);
if (inputHorizontal != 0)
{ {
if (currentHealth < maxHealth) myAnimator.SetFloat("speed", walkSpeed);
}
else if (inputVertical != 0)
{
myAnimator.SetFloat("speed", walkSpeed);
}
else
{
myAnimator.SetFloat("speed", 0);
}
if (inputHorizontal == 1 || inputHorizontal == -1 || inputVertical == 1 || inputVertical == -1)
{
myAnimator.SetFloat("lastMoveX", inputHorizontal);
myAnimator.SetFloat("lastMoveY", inputVertical);
}
timerRegen += Time.deltaTime;
if (timerRegen >= waitRegen)
{
startRegen = true;
}
currentHealth = PlayerPrefs.GetFloat("health");
maxHealth = PlayerPrefs.GetFloat("maxHealth");
if (startRegen == true)
{
timerTick += Time.deltaTime;
if (timerTick >= waitTick)
{ {
currentHealth = currentHealth + 1; if (currentHealth < maxHealth)
if (currentHealth > maxHealth)
{ {
currentHealth = maxHealth; currentHealth = currentHealth + 1;
if (currentHealth > maxHealth)
{
currentHealth = maxHealth;
}
PlayerPrefs.SetFloat("health", currentHealth);
timerTick = 0f;
} }
PlayerPrefs.SetFloat("health", currentHealth);
timerTick = 0f;
} }
} }
PlayerPrefs.SetFloat("health", currentHealth);
PlayerPrefs.SetFloat("exp", exp);
PlayerPrefs.SetInt("lvl", lvl);
PlayerPrefs.SetFloat("maxExp", maxExp);
PlayerPrefs.SetFloat("maxHealth", maxHealth);
} }
PlayerPrefs.SetFloat("health", currentHealth);
PlayerPrefs.SetFloat("exp", exp); HandleInput();
PlayerPrefs.SetInt("lvl", lvl);
PlayerPrefs.SetFloat("maxExp", maxExp);
PlayerPrefs.SetFloat("maxHealth", maxHealth);
} }
HandleInput();
} }
void FixedUpdate() void FixedUpdate()
{ {
if (canWalk == true) if (currentHealth <= 0)
{ {
if (inputHorizontal != 0 || inputVertical != 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 (inputHorizontal != 0 && inputVertical != 0) if (inputHorizontal != 0 || inputVertical != 0)
{ {
inputHorizontal *= speedLimiter; if (inputHorizontal != 0 && inputVertical != 0)
inputVertical *= speedLimiter; {
inputHorizontal *= speedLimiter;
inputVertical *= speedLimiter;
}
rb.velocity = new Vector2(inputHorizontal * walkSpeed, inputVertical * walkSpeed);
}
else
{
rb.velocity = new Vector2(0f, 0f);
} }
rb.velocity = new Vector2(inputHorizontal * walkSpeed, inputVertical * walkSpeed);
}
else
{
rb.velocity = new Vector2(0f, 0f);
} }
HandleAttacks();
ResetValues();
} }
HandleAttacks();
ResetValues();
} }
public void SaveCheckpoint() public void SaveCheckpoint()
@ -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;