728x90

일단 이미 몬스터 안에 타겟이미지때문에 캔버스가 있기때문에 새로만들지않고

타겟이미지에서 이미 업데이트 문을 쓰고 있기 때문에 스크립트 그대로 사용하려고한다.

 

타겟이미지 스크립트에 

 

  public Image hpbar; // fillamount 값 조정할거라 이미지 받아와주고 
    public GameObject HpBar; // hp바 프레임 배경 다가져오기위해 Gameobject로 받아주고 
    
    void Start()
    {
        hpbar.fillAmount=(float)Monster.instance.stat.curHp/(float)Monster.instance.stat.maxHp; // 시작시 fillAmount = 몬스터 현제체력/몬스터 체력
    }
     void Update()
    {
        if(inventory.activeSelf == false)//인벤토리 창이 안켜져있을때 작동하게끔
        {
        targetimg.transform.position=Camera.main.WorldToScreenPoint(transform.position);
        hpbar.fillAmount = Mathf.Lerp(hpbar.fillAmount, (float)Monster.instance.stat.curHp / (float)Monster.instance.stat.maxHp, Time.deltaTime*10);// 체력바가 부드럽게 줄기위해서 Lerp 를 사용하였다.
        HpBar.transform.position=Camera.main.WorldToScreenPoint(transform.position+new Vector3(0,2,0));//몬스터 y축으로 2 더높게 뜨게 설정하였다.
        }
    }

 

하면 이런식으로 작동 된다.

 

 

 

이젠 플레이어 차례이다

 

  public Image Myhpbar; //캐릭터 위 hp바
    public Image Mympbar;//캐릭터 위 mp바
    public Image hpbar;//상단 hp바
    public Image mpbar; //상단 mp바
    public Text hp, mp;//상단 hp mp 표시 텍스트
    
    void Awake()
    {
        stat = new Stat();// 스탯 뉴 스탯 
        stat = stat.SetUnitStat(unitCode); // 스탯에 SetUnitStat 호출 (유닛코드로 구별하여) 유니티에서 wolf 로 설정해주면됨 
        myanim = GetComponent<Animator>();
        Myhpbar.fillAmount = (float)stat.curHp / (float)stat.maxHp;
        hpbar.fillAmount = (float)stat.curHp / (float)stat.maxHp;
        Mympbar.fillAmount = (float)stat.curMp / (float)stat.maxMp;
        mpbar.fillAmount= (float)stat.curMp / (float)stat.maxMp;
        hp.text=stat.curHp+"/"+stat.maxHp.ToString();
        mp.text = stat.curMp + "/" + stat.maxMp.ToString();
        CanMove = true;
       
      
    }
    
   public void myCondition()
    {
        Myhpbar.fillAmount = Mathf.Lerp(Myhpbar.fillAmount, (float)stat.curHp / (float)stat.maxHp, Time.deltaTime * 10f);//내 hp바 계산
        hpbar.fillAmount = Mathf.Lerp(Myhpbar.fillAmount, (float)stat.curHp / (float)stat.maxHp, Time.deltaTime * 10f);//상단 hp바 계산
        Mympbar.fillAmount = Mathf.Lerp(Mympbar.fillAmount, (float)stat.curMp / (float)stat.maxMp, Time.deltaTime * 10f);//내 mp바 계산
        mpbar.fillAmount = Mathf.Lerp(Mympbar.fillAmount, (float)stat.curMp / (float)stat.maxMp, Time.deltaTime * 10f);//상단 mp바 계산
        hp.text = stat.curHp + "/" + stat.maxHp.ToString();//상단 hp text표현
        mp.text = stat.curMp + "/" + stat.maxMp.ToString();//상단 mp text표현 
    }
    
     private void Update()
    {
        myCondition();
        MyNickname.transform.position = Camera.main.WorldToScreenPoint(this.transform.position+new Vector3(0,2.7f,0)); // 아이콘 닉네임 hp,mp 정렬후 내캐릭터위치 따라가게 
    }

 

 

이제는 아이템 착용시 스탯을 올려주도록 해볼예정이다

캐릭터매니저 스크립트에

 

using Newtonsoft.Json;
using System.IO;

private void Start()
    {
        if (instance == null)
        {
            instance = this;
        }
        stat = new Stat();// 스탯 뉴 스탯 
        
        
        stat = stat.SetUnitStat(unitCode); // 스탯에 SetUnitStat 호출 (유닛코드로 구별하여) 유니티에서 wolf 로 설정해주면됨 
        Load();//json 데이터가 있으면 로드 
     
        
        myanim = GetComponent<Animator>();
        Myhpbar.fillAmount = (float)stat.curHp / (float)stat.maxHp;
        hpbar.fillAmount = (float)stat.curHp / (float)stat.maxHp;
        Mympbar.fillAmount = (float)stat.curMp / (float)stat.maxMp;
        mpbar.fillAmount = (float)stat.curMp / (float)stat.maxMp;
        hp.text = stat.curHp + "/" + stat.maxHp.ToString();
        mp.text = stat.curMp + "/" + stat.maxMp.ToString();
        CanMove = true;
        
    }
 private void OnApplicationQuit()//게임 종료시 스탯 save
    {
        Save(); 
    }
void Save()
    {
        string jdata = JsonConvert.SerializeObject(stat); // jdata 에 내 스탯 넣어주기
        File.WriteAllText(Application.dataPath + "/Resources/Mystat.txt", jdata); // 이파일을 Resources 폴더에 Mystat jdata로 저장해준다 

    }
    void Load()
    {
        string jdata = File.ReadAllText(Application.dataPath + "/Resources/Mystat.txt"); // 내 스탯을 읽어오고 
        stat = JsonConvert.DeserializeObject<Stat>(jdata); // Convert하여 stat에 넣어준다 

    }

아이템 스크립트는 armor만 보여주겠다 나머지는 똑같기때문에 

case "Armor": // 타입이 Armor 라면 
                     UsingItem = MyItemList.Find(x => x.Type == "Armor" && x.isUsing == true); // 내아이템중 타입이 아머이면서 사용중인것을 찾는다.
                    if(UsingItem != null && CurItem != UsingItem) // 만약 사용중인Armor 가 없고 클릭한 아이템 사용중인것과 같지않다면 
                    {
                    CharacterManger.instance.stat.maxHp -= int.Parse(UsingItem.value);//사용중인 armor value값 빼주기
                        UsingItem.isUsing = false; //사용중인 armor using false
                    CharacterManger.instance.stat.maxHp += int.Parse(CurItem.value);// 클릭한 armor value값 더해주기
                    CurItem.isUsing = !CurItem.isUsing; // 현재 클릭한 슬롯 false 면 true 로 true 면 false 로 변경. 이하동문 
                    }
                    else
                    {
                   
                    CurItem.isUsing = !CurItem.isUsing; // 현재 클릭한 슬롯 false 면 true 로 true 면 false 로 변경. 이하동문 
                    if (CurItem.isUsing == true)// 클릭했을때 true로 변경된다면
                    {
                        CharacterManger.instance.stat.maxHp += int.Parse(CurItem.value);// 클릭한 armor value값 더해주기
                    }
                    else//클릭했을때 false 라면
                    {
                        CharacterManger.instance.stat.maxHp -= int.Parse(UsingItem.value);//클릭한 armor value값 빼주기
                    }
                }
                    break;

 

 

그래서 이렇게 진행하면

 

 

 

 

이런식으로 스탯이 저장되고 로드된다.

 

구현이다끝나면 이 json data도 서버에 보관시키고 서버에서 불러오도록하여 수정이 어렵게할거다.

 

내일은 데미지 text 구현, 퀵슬롯 구현 할 예정이다.

반응형