
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 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()
{
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 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()
{
if (Input.GetKeyDown(KeyCode.Space))
{
rigid.AddForce(Vector3.up * jumpPower, ForceMode.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);
}
}