PO/Assets/Scripts/Player.cs

65 lines
1.7 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Player : MonoBehaviour
{
public static Player main;
[Header("dont touch this. standart preset, change session")]
[SerializeField] private Text playerName;
[SerializeField] private Text playerMaxHp;
[SerializeField] private Text playerHp;
[SerializeField] private Slider playerHealthbar;
public PlayerConfig playerConfig;
public Transform playerPosition;
[SerializeField] private int hp;
private void Awake()
{
if (main != null && main != this)
{
Debug.LogWarning("2 session on the scene");
Destroy(this);
return;
}
main = this;
}
public void NewInformation()
{
SpriteRenderer _sprRend = playerPosition.GetComponent<SpriteRenderer>();
var _character = playerConfig.characterCharacteristics;
_sprRend.sprite = _character.sprite;
playerName.text = _character.namePlayer;
playerMaxHp.text = _character.maxHp.ToString();
playerHp.text = playerMaxHp.text;
playerHealthbar.maxValue = _character.maxHp;
playerHealthbar.value = playerHealthbar.maxValue;
hp = _character.maxHp;
}
public void ChangeHp(int damage)
{
hp -= damage;
if (hp <= 0)
{
hp = 0;
Death();
}
else if (hp > playerConfig.characterCharacteristics.maxHp)
{
hp = playerConfig.characterCharacteristics.maxHp;
}
playerMaxHp.text = hp.ToString();
playerHealthbar.value = hp;
}
public void Death()
{
Session.instance.Defeat();
}
}