2022-03-24 18:37:17 +01:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
2022-10-01 15:39:24 +02:00
|
|
|
using System.Linq;
|
2022-03-24 18:37:17 +01:00
|
|
|
|
2022-04-07 23:46:09 +02:00
|
|
|
public class Player : MonoBehaviour
|
2022-03-24 18:37:17 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
Rigidbody2D rb;
|
2022-04-07 23:46:09 +02:00
|
|
|
private Animator myAnimator;
|
2022-06-09 21:51:48 +02:00
|
|
|
public float walkSpeed = 4f;
|
2022-03-24 18:37:17 +01:00
|
|
|
float speedLimiter = 0.7f;
|
|
|
|
float inputHorizontal;
|
|
|
|
float inputVertical;
|
2022-06-04 13:59:18 +02:00
|
|
|
public GameObject Panel;
|
|
|
|
private bool inRange = false;
|
|
|
|
public ParticleSystem dmgParticleSystem;
|
|
|
|
|
2022-06-12 20:14:11 +02:00
|
|
|
public FloatValue maxHealth;
|
2022-10-01 19:28:26 +02:00
|
|
|
public float currentHealth=10;
|
2022-06-12 20:14:11 +02:00
|
|
|
public HealthBar healthBar;
|
2022-06-21 03:57:04 +02:00
|
|
|
private static bool attackSword;
|
2022-06-14 21:20:02 +02:00
|
|
|
private bool attackFist;
|
2022-06-21 03:57:04 +02:00
|
|
|
private static bool pickaxeInUse;
|
|
|
|
private static bool playerInCollider;
|
|
|
|
|
2022-06-13 17:38:09 +02:00
|
|
|
|
|
|
|
private float timerRegen = 0f;
|
|
|
|
private float timerTick = 0f;
|
|
|
|
private float waitRegen = 8.0f;
|
|
|
|
private float waitTick = 1.0f;
|
2022-06-12 20:14:11 +02:00
|
|
|
|
2022-06-13 17:38:09 +02:00
|
|
|
private bool startRegen = false;
|
2022-06-12 20:14:11 +02:00
|
|
|
|
2022-06-06 16:21:26 +02:00
|
|
|
private bool canWalk = true;
|
2022-10-16 21:35:56 +02:00
|
|
|
|
|
|
|
public int exp;
|
|
|
|
public int lvl;
|
2022-06-21 03:57:04 +02:00
|
|
|
|
|
|
|
public static void putPlayerInCollider()
|
|
|
|
{
|
|
|
|
playerInCollider = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void takePlayerFromCollider()
|
|
|
|
{
|
|
|
|
playerInCollider = false;
|
|
|
|
}
|
|
|
|
|
2022-03-24 18:37:17 +01:00
|
|
|
void Start()
|
|
|
|
{
|
2022-10-01 15:39:24 +02:00
|
|
|
Panel = GameObject.FindObjectsOfType<GameObject>(true).Where(sr => sr.gameObject.name == "YouDied").ToArray()[0];
|
|
|
|
healthBar = (HealthBar)FindObjectOfType<HealthBar>();
|
2022-10-01 19:28:26 +02:00
|
|
|
if (OnMapAppearanceMethod.Gateway == OnMapAppearanceMethodEnum.NewGame)
|
2022-06-15 19:54:44 +02:00
|
|
|
{
|
2022-10-16 21:35:56 +02:00
|
|
|
currentHealth = PlayerPrefs.GetFloat("health");
|
|
|
|
exp = PlayerPrefs.GetInt("exp");
|
2022-06-15 19:54:44 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-10-01 19:28:26 +02:00
|
|
|
currentHealth = PlayerPrefs.GetFloat("health-S");
|
2022-10-16 21:35:56 +02:00
|
|
|
exp = PlayerPrefs.GetInt("exp-S");
|
2022-06-15 19:54:44 +02:00
|
|
|
}
|
2022-10-16 21:35:56 +02:00
|
|
|
ManageLevels(exp);
|
2022-03-24 18:37:17 +01:00
|
|
|
rb = gameObject.GetComponent<Rigidbody2D>();
|
2022-04-07 23:46:09 +02:00
|
|
|
myAnimator = GetComponent<Animator>();
|
2022-06-12 20:14:11 +02:00
|
|
|
healthBar.SetMaxHealth(maxHealth.initialValue);
|
2022-06-15 16:32:14 +02:00
|
|
|
healthBar.SetHealth(currentHealth);
|
2022-06-12 11:28:14 +02:00
|
|
|
walkSpeed = 4f;
|
2022-03-24 18:37:17 +01:00
|
|
|
}
|
|
|
|
|
2022-10-16 21:35:56 +02:00
|
|
|
public void GetExp(int expValue)
|
|
|
|
{
|
|
|
|
exp = exp + expValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void ManageLevels(int exp)
|
|
|
|
{
|
|
|
|
if (exp < 20)
|
|
|
|
{
|
|
|
|
lvl = 1;
|
|
|
|
}
|
|
|
|
else if (exp < 50)
|
|
|
|
{
|
|
|
|
lvl = 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-02 18:45:58 +02:00
|
|
|
public void TakeDamage(float damage, bool isPanelEnabled = true)
|
2022-06-04 13:59:18 +02:00
|
|
|
{
|
2022-06-12 20:14:11 +02:00
|
|
|
currentHealth = currentHealth - damage;
|
|
|
|
healthBar.SetHealth(currentHealth);
|
2022-10-01 15:39:24 +02:00
|
|
|
|
2022-06-04 13:59:18 +02:00
|
|
|
var em = dmgParticleSystem.emission;
|
|
|
|
em.enabled = true;
|
|
|
|
StartCoroutine(Timer());
|
2022-06-13 17:38:09 +02:00
|
|
|
timerRegen = 0.0f;
|
|
|
|
startRegen = false;
|
2022-06-12 20:14:11 +02:00
|
|
|
if (currentHealth <= 0)
|
2022-06-04 13:59:18 +02:00
|
|
|
{
|
2022-10-02 18:45:58 +02:00
|
|
|
if(isPanelEnabled)
|
|
|
|
{
|
|
|
|
Panel.SetActive(true);
|
|
|
|
}
|
2022-06-06 16:21:26 +02:00
|
|
|
walkSpeed = 0f;
|
2022-10-16 21:35:56 +02:00
|
|
|
canWalk = false;
|
2022-06-04 13:59:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IEnumerator Timer()
|
|
|
|
{
|
|
|
|
yield return new WaitForSeconds(0.2f);
|
|
|
|
var em = dmgParticleSystem.emission;
|
|
|
|
em.enabled = false;
|
|
|
|
}
|
|
|
|
|
2022-05-31 23:53:48 +02:00
|
|
|
private void HandleAttacks()
|
|
|
|
{
|
2022-10-16 21:35:56 +02:00
|
|
|
if (canWalk == true)
|
2022-05-31 23:53:48 +02:00
|
|
|
{
|
2022-10-16 21:35:56 +02:00
|
|
|
if (EquipmentManager.Instance._weapon.Name.Equals("pickaxe_test") && attackSword)
|
|
|
|
{
|
|
|
|
myAnimator.SetTrigger("pickaxe");
|
|
|
|
}
|
|
|
|
else if (attackSword && EquipmentManager.Instance._weapon.Name.Equals("Basic_Sword"))
|
|
|
|
{
|
|
|
|
myAnimator.SetTrigger("attack");
|
|
|
|
}
|
|
|
|
else if (attackFist)
|
|
|
|
{
|
|
|
|
myAnimator.SetTrigger("attackFist");
|
|
|
|
}
|
2022-05-31 23:53:48 +02:00
|
|
|
}
|
2022-10-10 20:44:52 +02:00
|
|
|
|
2022-05-31 23:53:48 +02:00
|
|
|
}
|
|
|
|
|
2022-06-21 03:57:04 +02:00
|
|
|
private void HandleInput()
|
2022-03-24 18:37:17 +01:00
|
|
|
{
|
2022-10-01 15:39:24 +02:00
|
|
|
if (Input.GetKeyDown(KeyCode.Space))
|
|
|
|
{
|
|
|
|
attackSword = true;
|
|
|
|
}
|
|
|
|
if (Input.GetKeyDown(KeyCode.LeftShift))
|
|
|
|
{
|
|
|
|
attackFist = true;
|
|
|
|
}
|
|
|
|
if(Input.GetKeyDown(KeyCode.X))
|
|
|
|
{
|
|
|
|
pickaxeInUse = true;
|
|
|
|
}
|
2022-06-21 03:57:04 +02:00
|
|
|
}
|
2022-10-01 15:39:24 +02:00
|
|
|
|
|
|
|
private void ResetValues()
|
2022-06-21 03:57:04 +02:00
|
|
|
{
|
2022-10-01 15:39:24 +02:00
|
|
|
attackSword = false;
|
|
|
|
attackFist = false;
|
|
|
|
pickaxeInUse = false;
|
2022-03-24 18:37:17 +01:00
|
|
|
}
|
|
|
|
|
2022-10-01 15:39:24 +02:00
|
|
|
void Update()
|
2022-03-24 18:37:17 +01:00
|
|
|
{
|
2022-10-16 21:35:56 +02:00
|
|
|
ManageLevels(exp);
|
|
|
|
if (canWalk == true)
|
|
|
|
{
|
2022-10-01 15:39:24 +02:00
|
|
|
|
|
|
|
inputHorizontal = Input.GetAxisRaw("Horizontal");
|
|
|
|
inputVertical = Input.GetAxisRaw("Vertical");
|
|
|
|
|
|
|
|
myAnimator.SetFloat("moveX", inputHorizontal * walkSpeed);
|
|
|
|
myAnimator.SetFloat("moveY", inputVertical * walkSpeed);
|
|
|
|
if (inputHorizontal != 0)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
2022-06-21 03:57:04 +02:00
|
|
|
|
2022-10-01 15:39:24 +02:00
|
|
|
timerRegen += Time.deltaTime;
|
|
|
|
if(timerRegen >= waitRegen)
|
|
|
|
{
|
|
|
|
startRegen = true;
|
|
|
|
}
|
2022-06-21 03:57:04 +02:00
|
|
|
|
2022-10-01 15:39:24 +02:00
|
|
|
if (startRegen == true)
|
2022-03-24 18:37:17 +01:00
|
|
|
{
|
2022-10-01 15:39:24 +02:00
|
|
|
timerTick += Time.deltaTime;
|
|
|
|
if(timerTick >= waitTick)
|
2022-06-06 16:21:26 +02:00
|
|
|
{
|
2022-10-01 15:39:24 +02:00
|
|
|
if(currentHealth < 10)
|
|
|
|
{
|
|
|
|
currentHealth = currentHealth + 1;
|
|
|
|
healthBar.SetHealth(currentHealth);
|
|
|
|
timerTick = 0f;
|
|
|
|
}
|
2022-03-24 18:37:17 +01:00
|
|
|
}
|
|
|
|
}
|
2022-10-01 15:39:24 +02:00
|
|
|
PlayerPrefs.SetFloat("health", currentHealth);
|
2022-10-16 21:35:56 +02:00
|
|
|
PlayerPrefs.SetInt("exp", exp);
|
2022-03-24 18:37:17 +01:00
|
|
|
|
2022-10-16 21:35:56 +02:00
|
|
|
}
|
2022-06-21 03:57:04 +02:00
|
|
|
|
2022-10-01 15:39:24 +02:00
|
|
|
HandleInput();
|
|
|
|
}
|
2022-06-21 03:57:04 +02:00
|
|
|
|
2022-10-01 15:39:24 +02:00
|
|
|
void FixedUpdate()
|
2022-06-15 19:54:44 +02:00
|
|
|
{
|
2022-10-01 15:39:24 +02:00
|
|
|
if(canWalk == true)
|
2022-06-21 03:57:04 +02:00
|
|
|
{
|
2022-10-01 15:39:24 +02:00
|
|
|
if (inputHorizontal != 0 || inputVertical != 0)
|
|
|
|
{
|
|
|
|
if (inputHorizontal != 0 && inputVertical != 0)
|
|
|
|
{
|
|
|
|
inputHorizontal *= speedLimiter;
|
|
|
|
inputVertical *= speedLimiter;
|
|
|
|
}
|
|
|
|
rb.velocity = new Vector2(inputHorizontal * walkSpeed, inputVertical * walkSpeed);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rb.velocity = new Vector2(0f, 0f);
|
|
|
|
}
|
2022-06-21 03:57:04 +02:00
|
|
|
}
|
2022-10-01 15:39:24 +02:00
|
|
|
|
|
|
|
HandleAttacks();
|
|
|
|
ResetValues();
|
2022-06-15 19:54:44 +02:00
|
|
|
}
|
2022-10-01 15:39:24 +02:00
|
|
|
|
|
|
|
public void SaveCheckpoint()
|
2022-06-21 03:57:04 +02:00
|
|
|
{
|
2022-10-01 15:39:24 +02:00
|
|
|
PlayerPrefs.SetFloat("health-S", currentHealth);
|
2022-10-16 21:35:56 +02:00
|
|
|
PlayerPrefs.SetInt("exp-S", exp);
|
2022-06-21 03:57:04 +02:00
|
|
|
}
|
2022-03-24 18:37:17 +01:00
|
|
|
}
|