본문 바로가기
Unity

ㅂㅈㄷ

by 후야- 2024. 4. 1.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PlayerCS : MonoBehaviour
{
    public GameObject Monster;
    public string PlayerName;       // 플레이어 캐릭터의 이름

    public int Current_HP;          // 플레이어 캐릭터의 현재 체력
    public int HP;                  // 플레이어 캐릭터의 체력 (총)

    public int attack;              // 플레이어 공격력

    public Text _Name;              // 화면상에 나오는 캐릭터 이름
    public Text _HP;                // 화면상에 나오는 캐릭터의 체력

    public Animator Anim;

    public int LV = 1;                  // 플레이어 LV

    public float Current_XP;          // 현재 경험치
    public float XP = 50;                  // 총 경험치

    public Image XP_Bar;
    public Text LV_txt;

    public AudioSource Lv_sound;
    
    void Start()
    {
        Player_XP();
        _Name.text = PlayerName;
        _HP.text = Current_HP + " / " + HP;

        Monster = GameObject.FindGameObjectWithTag("monster");
    }

    void Update()
    {
        LV_txt.text = "LV " + LV;
        XP_Bar.fillAmount = Current_XP / XP;

        if (Monster != null)
        {
            if (Monster.GetComponent<Monsters>().Current_HP > 0)    // 몬스터가 죽지 않았을 때
            {
                Anim.SetInteger("AnimState", 1);
            }
        }
        else if (Monster == null)
        {
            Anim.SetInteger("AnimState", 2);
        }

    }

    public void AttackMonster()
    {
        Monster.GetComponent<Monsters>().Current_HP -= attack;
    }

    public void Player_XP()
    {
        XP = LV * 50;
    }

    public void LV_UP()
    {
        if (Current_XP >= XP)   // 현재 경험치가 총 경험치보다 많거나 같으면
        {
            Lv_sound.Play();
            Current_XP -= XP;   // 총 경험치 - 현재 경험치 x   >> 현재 경험치 - 총 경험치 o
            LV++;               // 레벨업

            Player_XP();
        }
    }
}

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Monsters : MonoBehaviour
{
    public GameObject Player;
    public string MonsterName;    // 몬스터 이름

    public int Current_HP;        // 몬스터 현재 hp
    public int HP;                // 몬스터 총 hp

    public int attack;            // 몬스터 공격력

    public Text _Name;
    public Text _HP;
    public StageManager Stage;

    public Animator _Anim;

    void Start()
    {
        _Name.text = MonsterName;


        Player = GameObject.FindGameObjectWithTag("player");
    }

    void Update()
    {
        _HP.text = Current_HP + " / " + HP;

        if (Current_HP <= 0)
        {
            _Anim.SetInteger("AnimState", 1);
            Player.GetComponent<PlayerCS>().Monster = null;
            
 
        }
    }

    public void Death()
    {
        var py = Player.GetComponent<PlayerCS>();
        _Name.gameObject.SetActive(false);
        _HP.gameObject.SetActive(false);
        Stage.StageNum++;
        Stage.StageInfo();

        py.Current_XP += 10;
        py.LV_UP();
    }

    public void OffObj()
    {
        this.gameObject.SetActive(false);    // 해당 오브젝트 비활성화
        Invoke("Regen", 2);    // 2초후 부활
    }

    public void Regen()      // 몬스터 부활
    {
        Current_HP = HP;
        _Name.gameObject.SetActive(true);
        _HP.gameObject.SetActive(true);
        this.gameObject.SetActive(true);
        Player.GetComponent<PlayerCS>().Monster = this.gameObject;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class StageManager : MonoBehaviour
{
    public int StageNum = 1;     // 기본값 1
    public Text Stage;    

    public void StageInfo()
    {
        Stage.text = "Stage " + StageNum;
    }
}