기본지원UI, UI연동

sejun-Lee·2025년 5월 12일

UnityEngine

목록 보기
10/12

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

public class PlayerPresenter : MonoBehaviour
{
    [Header("Model")]
    [SerializeField] PlayerModel model;

    [Header("View")]
    [SerializeField] TMP_Text playerHPTextUI;
    [SerializeField] TMP_Text playerMaxTextUI;
    [SerializeField] Slider playerHPSliderUI;
    //[SerializeField] TMP_Text playerSpeedTextUI;
    [SerializeField] TMP_Text playerJumpTextUI;
    private int jump = 0;

    private void OnEnable()
    {
        model.OnHpChanged += SetHP;
        model.OnMaxHPChanged += SetMaxHP;
        SetMaxHP(model.MaxHP);
        SetHP(model.HP);
    }

    private void OnDisable()
    {
        model.OnHpChanged -= SetHP;
        model.OnMaxHPChanged -= SetMaxHP;
    }

    private void Update()
    {
        //SetSpeed(model.Velocity);

        if (Input.GetKeyDown(KeyCode.Space)){
            SetJumpCount();
        }
        
    }

    public void SetHP(int hp)
    {
        playerHPTextUI.text = $"{hp}";
        playerHPSliderUI.value = hp;
    }

    public void SetMaxHP(int maxHP)
    {
        playerMaxTextUI.text = $"{maxHP}";
        playerHPSliderUI.maxValue = maxHP;
    }

    //public void SetSpeed(Vector3 velocity)
    //{
    //    playerSpeedTextUI.text = $"{velocity.magnitude}";
    //}

    public void SetJumpCount()
    {
        jump += 1;  // 점프시 카운트
		playerJumpTextUI.text = $"jump : {jump}";   // 현제 카운트 시각화
       
    }
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerModel : MonoBehaviour
{
    [SerializeField] int hp;
    public int HP { set { hp = value; OnHpChanged?.Invoke(hp); } get { return hp; } }
    public event Action<int> OnHpChanged;	// 이벤트 

    [SerializeField] int maxHP;
    public int MaxHP { set { maxHP = value; OnMaxHPChanged?.Invoke(maxHP); } get { return maxHP; } }
    public event Action<int> OnMaxHPChanged;

    [SerializeField] Rigidbody rigid;
    public Vector3 Velocity { set { rigid.velocity = value; OnVelocityChanged?.Invoke(rigid.velocity); } get { return rigid.velocity; } }
    public Action<Vector3> OnVelocityChanged;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// 컨트롤러 : 논리로직(동작)
public class PlayerController : MonoBehaviour
{
    [SerializeField] PlayerModel model;
    [SerializeField] Rigidbody rigid;
    [SerializeField] float jumpPower;

    private void Update()
    {
        //Move();  
        
        if (Input.GetKeyDown(KeyCode.Space))
        {
            rigid.AddForce(Vector3.up * jumpPower, ForceMode.Impulse);  // Impulse = 한번 펑! 힘을 주는 것
        }
        if (Input.GetKeyDown(KeyCode.A))
        {
            model.HP += 1;
        }
        if (Input.GetKeyDown(KeyCode.D))
        {
            model.HP -= 1;
        }
    }

    private void Move()
    {
        float xInput = Input.GetAxis("Horizontal");
        float zInput = Input.GetAxis("Vertical");

        Vector3 dir = new Vector3(xInput, 0, zInput);
        if (dir.sqrMagnitude > 1)
        {
            dir = dir.normalized;
        }

        model.Velocity = Vector3.MoveTowards(model.Velocity, dir * 5, 10 * Time.deltaTime);
    }
}
profile
초보 개발자

0개의 댓글