137 lines
3.8 KiB
C#
137 lines
3.8 KiB
C#
using System.Numerics;
|
|
using TreeEditor;
|
|
using UnityEngine;
|
|
using Quaternion = UnityEngine.Quaternion;
|
|
using Vector3 = UnityEngine.Vector3;
|
|
public class PlayerController : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
float _speed = 10.0f;
|
|
|
|
private Vector3 _destPos;
|
|
|
|
void Start()
|
|
{
|
|
// Managers.Input.KeytAction -= OnKyeboard;
|
|
// Managers.Input.KeytAction += OnKyeboard;
|
|
Managers.Input.MouseAction -= OnMouseClicked;
|
|
Managers.Input.MouseAction += OnMouseClicked;
|
|
}
|
|
|
|
public enum PlayerState
|
|
{
|
|
Die,
|
|
Moving,
|
|
Idle,
|
|
}
|
|
|
|
PlayerState _state = PlayerState.Idle;
|
|
|
|
void UpdateDie()
|
|
{
|
|
// 아무것도 못함
|
|
}
|
|
|
|
void UpdateMoving()
|
|
{
|
|
Vector3 dir = _destPos - transform.position;
|
|
if (dir.magnitude < 0.0001f)
|
|
{
|
|
_state = PlayerState.Idle;
|
|
}
|
|
else
|
|
{
|
|
float moveDist = Mathf.Clamp(_speed * Time.deltaTime, 0, dir.magnitude);
|
|
transform.position += dir.normalized * moveDist;
|
|
|
|
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(dir), 20 * Time.deltaTime);
|
|
// transform.LookAt(_destPos);
|
|
}
|
|
|
|
// 애니메이션
|
|
Animator anim = GetComponent<Animator>();
|
|
|
|
// 현재 게임 상태에 대한 정보를 넘겨준다.
|
|
anim.SetFloat("speed", _speed);
|
|
}
|
|
|
|
void UpdateIdle()
|
|
{
|
|
|
|
// 애니메이션
|
|
Animator anim = GetComponent<Animator>();
|
|
|
|
anim.SetFloat("speed", 0);
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
switch (_state)
|
|
{
|
|
case PlayerState.Die:
|
|
UpdateDie();
|
|
break;
|
|
case PlayerState.Moving:
|
|
UpdateMoving();
|
|
break;
|
|
case PlayerState.Idle:
|
|
UpdateIdle();
|
|
break;
|
|
}
|
|
}
|
|
|
|
// void OnKyeboard()
|
|
// {
|
|
// if (Input.GetKey(KeyCode.W))
|
|
// {
|
|
// transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.forward), 0.2f);
|
|
// transform.position += (Vector3.forward * Time.deltaTime * _speed);
|
|
// }
|
|
//
|
|
// if (Input.GetKey(KeyCode.S))
|
|
// {
|
|
// transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.back), 0.2f);
|
|
// transform.position += (Vector3.back * Time.deltaTime * _speed);
|
|
// }
|
|
//
|
|
// if (Input.GetKey(KeyCode.A))
|
|
// {
|
|
// transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.left), 0.2f);
|
|
// transform.position += (Vector3.left * Time.deltaTime * _speed);
|
|
// }
|
|
//
|
|
// if (Input.GetKey(KeyCode.D))
|
|
// {
|
|
// transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.right), 0.2f);
|
|
// transform.position += (Vector3.right * Time.deltaTime * _speed);
|
|
// }
|
|
//
|
|
// _moveToDest = false;
|
|
// }
|
|
|
|
void OnMouseClicked(Define.MouseEvent evt)
|
|
{
|
|
if (_state == PlayerState.Die)
|
|
{
|
|
return;
|
|
}
|
|
|
|
// if (evt != Define.MouseEvent.Click)
|
|
// {
|
|
// return;
|
|
// }
|
|
|
|
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
|
|
|
Debug.DrawRay(Camera.main.transform.position, ray.direction * 100.0f, Color.red, 1.0f);
|
|
|
|
RaycastHit hit;
|
|
if(Physics.Raycast(ray, out hit, 100.0f, LayerMask.GetMask("Wall")));
|
|
{
|
|
_destPos = hit.point;
|
|
_state = PlayerState.Moving;
|
|
// Debug.Log($"Raycast Camera @ {hit.collider.gameObject.tag}");
|
|
}
|
|
}
|
|
}
|