Scriptum/Assets/Scripts/Player.cs

214 lines
4.9 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2022-04-07 23:46:09 +02:00
public class Player : MonoBehaviour
{
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;
float speedLimiter = 0.7f;
float inputHorizontal;
float inputVertical;
public GameObject Panel;
private bool inRange = false;
public ParticleSystem dmgParticleSystem;
2022-06-12 20:14:11 +02:00
public FloatValue maxHealth;
public float currentHealth;
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
private bool canWalk = true;
2022-06-21 03:57:04 +02:00
public static void putPlayerInCollider()
{
playerInCollider = true;
}
public static void takePlayerFromCollider()
{
playerInCollider = false;
}
void Start()
{
int continued = PlayerPrefs.GetInt("continued");
if (continued == 1)
{
currentHealth = PlayerPrefs.GetFloat("health-S");
}
else
{
currentHealth = PlayerPrefs.GetFloat("health");
}
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);
healthBar.SetHealth(currentHealth);
walkSpeed = 4f;
}
public void TakeDamage(float damage)
{
2022-06-12 20:14:11 +02:00
currentHealth = currentHealth - damage;
healthBar.SetHealth(currentHealth);
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)
{
Panel.SetActive(true);
walkSpeed = 0f;
}
}
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-06-14 21:20:02 +02:00
if (attackSword)
2022-05-31 23:53:48 +02:00
{
myAnimator.SetTrigger("attack");
}
2022-06-14 21:20:02 +02:00
if (attackFist)
{
myAnimator.SetTrigger("attackFist");
}
2022-06-21 03:57:04 +02:00
if (pickaxeInUse)
2022-06-14 21:20:02 +02:00
{
2022-06-21 03:57:04 +02:00
myAnimator.SetTrigger("pickaxe");
2022-05-31 23:53:48 +02:00
}
}
2022-06-21 03:57:04 +02:00
private void HandleInput()
{
2022-06-21 03:57:04 +02:00
if (Input.GetKeyDown(KeyCode.Space))
{
2022-06-21 03:57:04 +02:00
attackSword = true;
}
2022-06-21 03:57:04 +02:00
if (Input.GetKeyDown(KeyCode.LeftShift))
{
2022-06-21 03:57:04 +02:00
attackFist = true;
}
2022-06-21 03:57:04 +02:00
if(Input.GetKeyDown(KeyCode.X))
{
2022-06-21 03:57:04 +02:00
pickaxeInUse = true;
}
2022-06-21 03:57:04 +02:00
}
2022-06-21 03:57:04 +02:00
private void ResetValues()
{
attackSword = false;
attackFist = false;
pickaxeInUse = false;
}
2022-06-13 17:38:09 +02:00
2022-06-21 03:57:04 +02:00
void Update()
{
//if (canWalk == true)
//{
inputHorizontal = Input.GetAxisRaw("Horizontal");
inputVertical = Input.GetAxisRaw("Vertical");
2022-06-21 03:57:04 +02:00
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);
}
2022-06-21 03:57:04 +02:00
if (inputHorizontal == 1 || inputHorizontal == -1 || inputVertical == 1 || inputVertical == -1)
{
2022-06-21 03:57:04 +02:00
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)
{
2022-06-21 03:57:04 +02:00
if(currentHealth < 10)
{
2022-06-21 03:57:04 +02:00
currentHealth = currentHealth + 1;
healthBar.SetHealth(currentHealth);
timerTick = 0f;
}
}
}
2022-06-21 03:57:04 +02:00
PlayerPrefs.SetFloat("health", currentHealth);
2022-06-21 03:57:04 +02:00
//}
HandleInput();
}
void FixedUpdate()
{
if(canWalk == true)
{
if (inputHorizontal != 0 || inputVertical != 0)
{
2022-06-21 03:57:04 +02:00
if (inputHorizontal != 0 && inputVertical != 0)
{
inputHorizontal *= speedLimiter;
inputVertical *= speedLimiter;
}
rb.velocity = new Vector2(inputHorizontal * walkSpeed, inputVertical * walkSpeed);
}
2022-06-21 03:57:04 +02:00
else
{
rb.velocity = new Vector2(0f, 0f);
}
}
HandleAttacks();
ResetValues();
}
public void SaveCheckpoint()
{
PlayerPrefs.SetFloat("health-S", currentHealth);
}
}