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;
public ParticleSystem dmgParticleSystem;
private float x;
private float y;
public float speedy = 300f;
public Vector3 temp1;
public HealthBar healthBar;
private static bool attackSword;
@ -33,7 +38,7 @@ public class Player : MonoBehaviour
private bool startRegen = false;
private bool canWalk = true;
private bool canWalk;
[Header("Player Starts values")]
public const int BaseHealth = 10;
@ -74,6 +79,7 @@ public class Player : MonoBehaviour
void Start()
{
canWalk = true;
Panel = GameObject.FindObjectsOfType<GameObject>(true).Where(sr => sr.gameObject.name == "YouDied").ToArray()[0];
healthBar = (HealthBar)FindObjectOfType<HealthBar>();
levelBar = (LevelBar)FindObjectOfType<LevelBar>();
@ -105,40 +111,40 @@ public class Player : MonoBehaviour
//DEFAULT CONTROLS
if (!PlayerPrefs.HasKey("Interaction"))
{
PlayerPrefs.SetString("Interaction","E");
PlayerPrefs.SetString("Interaction", "E");
}
if (!PlayerPrefs.HasKey("Attack"))
{
PlayerPrefs.SetString("Attack","Space");
PlayerPrefs.SetString("Attack", "Space");
}
if (!PlayerPrefs.HasKey("Skills"))
{
PlayerPrefs.SetString("Skills","U");
PlayerPrefs.SetString("Skills", "U");
}
if (!PlayerPrefs.HasKey("Inventory"))
{
PlayerPrefs.SetString("Inventory","I");
PlayerPrefs.SetString("Inventory", "I");
}
if (!PlayerPrefs.HasKey("Settings"))
{
PlayerPrefs.SetString("Settings","Escape");
PlayerPrefs.SetString("Settings", "Escape");
}
if (!PlayerPrefs.HasKey("Quests"))
{
PlayerPrefs.SetString("Quests","Q");
PlayerPrefs.SetString("Quests", "Q");
}
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"))
{
PlayerPrefs.SetString("ExpHlth","Tab");
PlayerPrefs.SetString("ExpHlth", "Tab");
}
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;
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)
{
Panel.SetActive(true);
}
walkSpeed = 0f;
canWalk = false;
gameObject.GetComponent<Animator>().enabled = false;
}
}
@ -330,12 +342,13 @@ public class Player : MonoBehaviour
if (attackFist)
{
myAnimator.SetTrigger("attackFist");
} else if (attackSword &&
EquipmentUIManager.Instance.GetList().Count() != 0 &&
EquipmentUIManager.Instance.GetList()
.Where(el => el.Key == (int)EquipmentPanelSlotsTypeEnum.WeaponSlot && el.Value != null).Count() > 0 &&
EquipmentUIManager.Instance.GetList()
.Where(el => el.Key == (int)EquipmentPanelSlotsTypeEnum.WeaponSlot && el.Value != null).First().Value.Name.Equals("Pickaxe"))
}
else if (attackSword &&
EquipmentUIManager.Instance.GetList().Count() != 0 &&
EquipmentUIManager.Instance.GetList()
.Where(el => el.Key == (int)EquipmentPanelSlotsTypeEnum.WeaponSlot && el.Value != null).Count() > 0 &&
EquipmentUIManager.Instance.GetList()
.Where(el => el.Key == (int)EquipmentPanelSlotsTypeEnum.WeaponSlot && el.Value != null).First().Value.Name.Equals("Pickaxe"))
{
myAnimator.SetTrigger("pickaxe");
}
@ -354,7 +367,7 @@ public class Player : MonoBehaviour
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))
{
@ -379,107 +392,133 @@ public class Player : MonoBehaviour
void Update()
{
if (lvlUp == true)
if (currentHealth <= 0)
{
PlayerPrefs.SetInt("LvlUpPopUp", 1);
exp = 0;
lvlUp = false;
walkSpeed = 0f;
PlayerPrefs.SetFloat("speed", 0.0f);
temp1.x = x;
temp1.y = y;
transform.position = Vector3.MoveTowards(transform.position, temp1, speedy * Time.deltaTime);
}
ManageHealth();
ManageDefense();
ManageIntelligence();
ManageStrength();
ManageSpeed();
if (canWalk == true)
else
{
inputHorizontal = Input.GetAxisRaw("Horizontal");
inputVertical = Input.GetAxisRaw("Vertical");
// speed calculated in ManageSpeed function
walkSpeed = PlayerPrefs.GetFloat("speed");
myAnimator.SetFloat("moveX", inputHorizontal * walkSpeed);
myAnimator.SetFloat("moveY", inputVertical * walkSpeed);
if (inputHorizontal != 0)
if (lvlUp == true)
{
myAnimator.SetFloat("speed", walkSpeed);
}
else if (inputVertical != 0)
{
myAnimator.SetFloat("speed", walkSpeed);
}
else
{
myAnimator.SetFloat("speed", 0);
PlayerPrefs.SetInt("LvlUpPopUp", 1);
exp = 0;
lvlUp = false;
}
if (inputHorizontal == 1 || inputHorizontal == -1 || inputVertical == 1 || inputVertical == -1)
{
myAnimator.SetFloat("lastMoveX", inputHorizontal);
myAnimator.SetFloat("lastMoveY", inputVertical);
}
ManageHealth();
ManageDefense();
ManageIntelligence();
ManageStrength();
ManageSpeed();
timerRegen += Time.deltaTime;
if (timerRegen >= waitRegen)
if (canWalk == true)
{
startRegen = true;
}
currentHealth = PlayerPrefs.GetFloat("health");
maxHealth = PlayerPrefs.GetFloat("maxHealth");
if (startRegen == true)
{
timerTick += Time.deltaTime;
if (timerTick >= waitTick)
inputHorizontal = Input.GetAxisRaw("Horizontal");
inputVertical = Input.GetAxisRaw("Vertical");
// speed calculated in ManageSpeed function
walkSpeed = PlayerPrefs.GetFloat("speed");
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);
PlayerPrefs.SetInt("lvl", lvl);
PlayerPrefs.SetFloat("maxExp", maxExp);
PlayerPrefs.SetFloat("maxHealth", maxHealth);
HandleInput();
}
HandleInput();
}
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;
inputVertical *= speedLimiter;
if (inputHorizontal != 0 && inputVertical != 0)
{
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()
@ -537,7 +576,8 @@ public class Player : MonoBehaviour
attackValue = attackCalculator.Calculate(equippedItem.Value);
PlayerPrefs.SetFloat("attackValue", attackValue);
} else
}
else
{
throw new System.Exception("Attack Calculation Error");
}
@ -566,7 +606,8 @@ public class Player : MonoBehaviour
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.ArmorSlot && el.Value != null).Count() == 0
){
)
{
defenseValue = defenseCalculator.CalculateWithoutItem();
@ -583,7 +624,8 @@ public class Player : MonoBehaviour
.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).First().Value.EquipmentType == EquipmentTypeEnum.Chest)
) {
)
{
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;