Scriptum/Assets/Scripts/Player.cs
2022-06-14 21:20:02 +02:00

176 lines
4.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
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;
public HealthBar healthBar;
private bool attackSword;
private bool attackFist;
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;
void Start()
{
rb = gameObject.GetComponent<Rigidbody2D>();
myAnimator = GetComponent<Animator>();
currentHealth = maxHealth.initialValue;
healthBar.SetMaxHealth(maxHealth.initialValue);
walkSpeed = 4f;
}
public void TakeDamage(float damage)
{
currentHealth = currentHealth - damage;
healthBar.SetHealth(currentHealth);
var em = dmgParticleSystem.emission;
em.enabled = true;
StartCoroutine(Timer());
timerRegen = 0.0f;
startRegen = false;
if (currentHealth <= 0)
{
Panel.SetActive(true);
walkSpeed = 0f;
}
}
IEnumerator Timer()
{
yield return new WaitForSeconds(0.2f);
var em = dmgParticleSystem.emission;
em.enabled = false;
}
private void HandleAttacks()
{
if (attackSword)
{
myAnimator.SetTrigger("attack");
}
if (attackFist)
{
myAnimator.SetTrigger("attackFist");
}
}
private void HandleInput()
{
if (Input.GetKeyDown(KeyCode.Space))
{
attackSword = true;
}
if (Input.GetKeyDown(KeyCode.LeftShift))
{
attackFist = true;
}
}
private void ResetValues()
{
attackSword = false;
attackFist = false;
}
void Update()
{
//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;
}
}
}
//}
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();
}
}