diff --git a/Assets/Scripts/Player.cs b/Assets/Scripts/Player.cs index e0a8aebe..68003f82 100644 --- a/Assets/Scripts/Player.cs +++ b/Assets/Scripts/Player.cs @@ -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,13 +38,13 @@ public class Player : MonoBehaviour private bool startRegen = false; - private bool canWalk = true; + private bool canWalk; [Header("Player Starts values")] public const int BaseHealth = 10; public const float BaseSpeed = 4f; - + [Header("Player Skills Points")] public int healthPoints; public int defensePoints; @@ -74,6 +79,7 @@ public class Player : MonoBehaviour void Start() { + canWalk = true; Panel = GameObject.FindObjectsOfType(true).Where(sr => sr.gameObject.name == "YouDied").ToArray()[0]; healthBar = (HealthBar)FindObjectOfType(); levelBar = (LevelBar)FindObjectOfType(); @@ -101,44 +107,44 @@ public class Player : MonoBehaviour ManageLevels(exp); - + //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"); } // -------------------------------------------------------------------- @@ -286,7 +292,7 @@ public class Player : MonoBehaviour defensePoints = PlayerPrefs.GetInt(SkillsPointsManger.PLAYER_SKILS_DEFENSE_POINTS); Debug.Log($"Minion Damage: {damage}; \n\tDefense: {PlayerPrefs.GetFloat("defenseValue")}"); - + damage = damage - PlayerPrefs.GetFloat("defenseValue"); damage = damage < 0 ? 0 : damage; @@ -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().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() @@ -535,9 +574,10 @@ public class Player : MonoBehaviour var attackCalculator = new AttackCalcullator(); attackValue = attackCalculator.Calculate(equippedItem.Value); - + PlayerPrefs.SetFloat("attackValue", attackValue); - } else + } + else { throw new System.Exception("Attack Calculation Error"); } @@ -566,24 +606,26 @@ 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(); PlayerPrefs.SetFloat("defenseValue", defenseValue); } else 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).First().Value.EquipmentType == EquipmentTypeEnum.Helmet) || - + (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).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; @@ -658,7 +700,7 @@ public class Player : MonoBehaviour //Debug.Log("Speed: " + PlayerPrefs.GetFloat("speed")); } - + // nowe - analogicznie jak w ataku }