260 lines
6.7 KiB
C#
260 lines
6.7 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using System.Linq;
|
|
|
|
public class Player : MonoBehaviour
|
|
{
|
|
|
|
Rigidbody2D rb;
|
|
private Animator myAnimator;
|
|
public float walkSpeed = 4f;
|
|
float speedLimiter = 0.7f;
|
|
float inputHorizontal;
|
|
float inputVertical;
|
|
public GameObject Panel;
|
|
private bool inRange = false;
|
|
public ParticleSystem dmgParticleSystem;
|
|
|
|
public FloatValue maxHealth;
|
|
public float currentHealth=10;
|
|
public HealthBar healthBar;
|
|
|
|
private static bool attackSword;
|
|
private bool attackFist;
|
|
private static bool pickaxeInUse;
|
|
private static bool playerInCollider;
|
|
|
|
|
|
private float timerRegen = 0f;
|
|
private float timerTick = 0f;
|
|
private float waitRegen = 8.0f;
|
|
private float waitTick = 1.0f;
|
|
|
|
private bool startRegen = false;
|
|
|
|
private bool canWalk = true;
|
|
|
|
public float exp=0;
|
|
public int lvl;
|
|
public LevelBar levelBar;
|
|
public FloatValue minPlayerExp;
|
|
|
|
public static void putPlayerInCollider()
|
|
{
|
|
playerInCollider = true;
|
|
}
|
|
|
|
public static void takePlayerFromCollider()
|
|
{
|
|
playerInCollider = false;
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
Panel = GameObject.FindObjectsOfType<GameObject>(true).Where(sr => sr.gameObject.name == "YouDied").ToArray()[0];
|
|
healthBar = (HealthBar)FindObjectOfType<HealthBar>();
|
|
levelBar = (LevelBar)FindObjectOfType<LevelBar>();
|
|
if (OnMapAppearanceMethod.Gateway == OnMapAppearanceMethodEnum.NewGame)
|
|
{
|
|
currentHealth = PlayerPrefs.GetFloat("health");
|
|
exp = PlayerPrefs.GetFloat("exp");
|
|
}
|
|
else
|
|
{
|
|
currentHealth = PlayerPrefs.GetFloat("health-S");
|
|
exp = PlayerPrefs.GetFloat("exp-S");
|
|
}
|
|
ManageLevels(exp);
|
|
rb = gameObject.GetComponent<Rigidbody2D>();
|
|
myAnimator = GetComponent<Animator>();
|
|
healthBar.SetMaxHealth(maxHealth.initialValue);
|
|
healthBar.SetHealth(currentHealth);
|
|
levelBar.SetStartExp(minPlayerExp.initialValue);
|
|
levelBar.SetExp(exp);
|
|
walkSpeed = 4f;
|
|
}
|
|
|
|
public void GetExp(float expValue)
|
|
{
|
|
exp = exp + expValue;
|
|
levelBar.SetExp(exp);
|
|
}
|
|
|
|
public void ManageLevels(float exp)
|
|
{
|
|
if (exp < 20)
|
|
{
|
|
lvl = 1;
|
|
}
|
|
else if (exp < 50)
|
|
{
|
|
lvl = 2;
|
|
}
|
|
}
|
|
|
|
public void TakeDamage(float damage, bool isPanelEnabled = true)
|
|
{
|
|
currentHealth = currentHealth - damage;
|
|
healthBar.SetHealth(currentHealth);
|
|
|
|
var em = dmgParticleSystem.emission;
|
|
em.enabled = true;
|
|
StartCoroutine(Timer());
|
|
timerRegen = 0.0f;
|
|
startRegen = false;
|
|
if (currentHealth <= 0)
|
|
{
|
|
if(isPanelEnabled)
|
|
{
|
|
Panel.SetActive(true);
|
|
}
|
|
walkSpeed = 0f;
|
|
canWalk = false;
|
|
}
|
|
}
|
|
|
|
IEnumerator Timer()
|
|
{
|
|
yield return new WaitForSeconds(0.2f);
|
|
var em = dmgParticleSystem.emission;
|
|
em.enabled = false;
|
|
}
|
|
|
|
private void HandleAttacks()
|
|
{
|
|
if (canWalk == true)
|
|
{
|
|
if (EquipmentManager.Instance._weapon == null)
|
|
{
|
|
return;
|
|
}
|
|
if(!EquipmentManager.Instance._weapon)
|
|
return;
|
|
|
|
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");
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
private void HandleInput()
|
|
{
|
|
if (Input.GetKeyDown(KeyCode.Space))
|
|
{
|
|
attackSword = true;
|
|
}
|
|
if (Input.GetKeyDown(KeyCode.LeftShift))
|
|
{
|
|
attackFist = true;
|
|
}
|
|
if(Input.GetKeyDown(KeyCode.X))
|
|
{
|
|
pickaxeInUse = true;
|
|
}
|
|
}
|
|
|
|
private void ResetValues()
|
|
{
|
|
attackSword = false;
|
|
attackFist = false;
|
|
pickaxeInUse = false;
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
ManageLevels(exp);
|
|
if (canWalk == true)
|
|
{
|
|
|
|
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);
|
|
}
|
|
|
|
timerRegen += Time.deltaTime;
|
|
if(timerRegen >= waitRegen)
|
|
{
|
|
startRegen = true;
|
|
}
|
|
|
|
if (startRegen == true)
|
|
{
|
|
timerTick += Time.deltaTime;
|
|
if(timerTick >= waitTick)
|
|
{
|
|
if(currentHealth < 10)
|
|
{
|
|
currentHealth = currentHealth + 1;
|
|
healthBar.SetHealth(currentHealth);
|
|
timerTick = 0f;
|
|
}
|
|
}
|
|
}
|
|
PlayerPrefs.SetFloat("health", currentHealth);
|
|
PlayerPrefs.SetFloat("exp", exp);
|
|
|
|
}
|
|
|
|
HandleInput();
|
|
}
|
|
|
|
void FixedUpdate()
|
|
{
|
|
if(canWalk == true)
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
|
|
HandleAttacks();
|
|
ResetValues();
|
|
}
|
|
|
|
public void SaveCheckpoint()
|
|
{
|
|
PlayerPrefs.SetFloat("health-S", currentHealth);
|
|
PlayerPrefs.SetFloat("exp-S", exp);
|
|
}
|
|
}
|