122 lines
3.6 KiB
C#
122 lines
3.6 KiB
C#
using Unity.Mathematics;
|
|
using UnityEngine;
|
|
|
|
public class PlayerController : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
float _speed = 5.0f;
|
|
|
|
private Vector3 _destPos;
|
|
|
|
public enum PlayerState
|
|
{
|
|
Idle,
|
|
Move,
|
|
Die
|
|
}
|
|
|
|
PlayerState _playerState = PlayerState.Idle;
|
|
|
|
void Start()
|
|
{
|
|
// Managers.Input.KeyAction -= OnKeyboard;
|
|
// Managers.Input.KeyAction += OnKeyboard;
|
|
Managers.Input.MouseAction -= OnMouseClicked;
|
|
Managers.Input.MouseAction += OnMouseClicked;
|
|
|
|
// Managers.Resource.Instantiate("UI/UI_Button");
|
|
// Managers.UI.ShowPopupUI<UI_Button>("UI_Button");
|
|
// Managers.UI.ClosePopupUI();
|
|
|
|
//이게 안정 버전
|
|
// UI_Button button = Managers.UI.ShowPopupUI<UI_Button>("UI_Button");
|
|
// Managers.UI.ClosePopupUI(button);
|
|
|
|
Managers.UI.ShowSceneUI<UI_Inven>("UI_Inven");
|
|
}
|
|
|
|
|
|
|
|
void UpdateIdle()
|
|
{
|
|
// 애니메이션 처리
|
|
// wait_run_ratio = Mathf.Lerp(wait_run_ratio, 0.0f, 8.0f * Time.deltaTime);
|
|
Animator anim = GetComponent<Animator>();
|
|
anim.SetFloat("speed", 0);
|
|
}
|
|
void UpdateMove()
|
|
{
|
|
Vector3 dir = _destPos - transform.position;
|
|
if (dir.magnitude < 0.001f)
|
|
{
|
|
_playerState = PlayerState.Idle;
|
|
}
|
|
else
|
|
{
|
|
float moveDist = Mathf.Clamp(_speed * Time.deltaTime, 0, dir.magnitude);
|
|
transform.position += dir.normalized * moveDist;
|
|
|
|
// 그래서 이렇게 부드럽게 회전
|
|
Vector3 targetDir = new Vector3(dir.x, 0, dir.z).normalized;
|
|
if (targetDir != Vector3.zero)
|
|
{
|
|
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(targetDir), 0.15f);
|
|
}
|
|
}
|
|
|
|
// 애니메이션 처리
|
|
// wait_run_ratio = Mathf.Lerp(wait_run_ratio, 1.0f, 8.0f * Time.deltaTime);
|
|
Animator anim = GetComponent<Animator>();
|
|
anim.SetFloat("speed", _speed);
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
switch (_playerState)
|
|
{
|
|
case PlayerState.Die:
|
|
break;
|
|
case PlayerState.Idle:
|
|
UpdateIdle();
|
|
break;
|
|
case PlayerState.Move:
|
|
UpdateMove();
|
|
break;
|
|
}
|
|
}
|
|
|
|
void OnMouseClicked(Define.MouseEvent evt)
|
|
{
|
|
// 클릭 뿐 아니라 마우스 누르고 있어도 이동이 가능하게 하기 위해서 일단 날리기
|
|
// if (evt != Define.MouseEvent.Click) return;
|
|
if(_playerState == PlayerState.Die) return;
|
|
|
|
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
|
Debug.DrawRay(Camera.main.transform.position, ray.direction * 100.0f, Color.blue, 1.0f);
|
|
|
|
RaycastHit hit;
|
|
if(Physics.Raycast(ray, out hit, 100.0f, LayerMask.GetMask("Wall")))
|
|
{
|
|
_destPos = hit.point;
|
|
_playerState = PlayerState.Move;
|
|
}
|
|
}
|
|
|
|
// void OnKeyboard()
|
|
// {
|
|
// if (Input.GetKey(KeyCode.W)) { Move(Vector3.forward); }
|
|
// else if (Input.GetKey(KeyCode.S)) { Move(Vector3.back); }
|
|
// else if (Input.GetKey(KeyCode.A)) { Move(Vector3.left); }
|
|
// else if (Input.GetKey(KeyCode.D)) { Move(Vector3.right); }
|
|
// _moveToDest = false;
|
|
// }
|
|
// void Move(Vector3 vector)
|
|
// {
|
|
// transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(vector), 0.15f);
|
|
// transform.position += vector * (Time.deltaTime * _speed);
|
|
// }
|
|
|
|
|
|
|
|
}
|