TheInitialProject/Assets/Scripts/Component/MainPlayerComponent.cs

135 lines
4.0 KiB
C#
Raw Permalink Normal View History

2024-10-23 16:59:02 +08:00
using CaoCao.Event;
using CaoCao.Runtime;
//using Cysharp.Threading.Tasks;
//using FairyGUI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Game
{
public class MainPlayerComponent : GameComponent
{
private Transform mainCamer;
public GameObject Role;
private Animator _animator;
private UnityEngine.AI.NavMeshAgent _agent;
private bool _bStartMove = false;
private Vector3 _vMoveOffset = Vector3.zero;
private float _rotationSpeed = 50;
//public async UniTask LoadMainRole()
//{
// mainCamer = Camera.main.transform;
// var handle = ResMgr.LoadAsyn<GameObject>("Assets/GameAssets/role/Prefab/role_Skin1");
// await handle.ToUniTask();
// var go = handle.AssetObject as GameObject;
// Role = Object.Instantiate(go, transform);
// Role.transform.position = new Vector3(43.16f, 0.23f, 10.24f);
// mainCamer.parent.position = Role.transform.position;
// _agent = Role.AddComponent<UnityEngine.AI.NavMeshAgent>();
// _agent.angularSpeed += 50;
// _agent.updateRotation = false;
// _animator = Role.GetComponent<Animator>();
// _bStartMove = false;
// _vMoveOffset = Vector3.zero;
// AppEntry.Event.Subscribe(PlayerMoveEventArgs.EventId, OnMoveCallback);
//}
//private void OnMoveCallback(object sender, LogicEventArgs e)
//{
// PlayerMoveEventArgs args = (PlayerMoveEventArgs)e;
// if (args == null)
// return;
// if (args.mIsMove)
// {
// if (args.mMoveDir == Vector3.zero)
// return;
// if (!_animator.GetBool("Run"))
// {
// _animator.SetBool("Run", true);
// _agent.isStopped = false;
// }
// }
// else
// {
// _animator.SetBool("Run", false);
// }
// Log.Debug($"{args.mIsMove}, {args.mMoveDir}");
// _bStartMove = args.mIsMove;
// _vMoveOffset = args.mMoveDir;
//}
public Vector2 GetClickScreenPos()
{
//<2F>ƶ<EFBFBD><C6B6><EFBFBD>
if (Application.platform == RuntimePlatform.Android ||
Application.platform == RuntimePlatform.IPhonePlayer)
{
if (Input.touchCount == 0)
return Vector2.zero;
// <20><>ָ<EFBFBD>մ<EFBFBD><D5B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><C4BB>ʱ<EFBFBD><CAB1> <20><>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>Ļ<EFBFBD><C4BB><EFBFBD>ƶ<EFBFBD>
if (Input.GetTouch(0).phase != TouchPhase.Began
&& Input.GetTouch(0).phase != TouchPhase.Moved)
return Vector2.zero;
return Input.GetTouch(0).position;
}
//<2F><><EFBFBD><EFBFBD>ƽ̨
else
{
if (!Input.GetMouseButtonDown(0))
return Vector2.zero;
return Input.mousePosition;
}
}
Vector2 clickPos;
private void FixedUpdate()
{
if (_bStartMove)
Move(Role.transform.position + _vMoveOffset);
else if(_agent != null)
{
if (!_agent.isStopped)
{
_agent.isStopped = true;
Move(Role.transform.position);
}
}
}
public void Move(Vector3 pos)
{
// ֱ<><D6B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD><C4B3><EFBFBD>
Role.transform.forward = _vMoveOffset;
_agent.SetDestination(pos);
}
private void Update()
{
if (mainCamer == null)
return;
if (Role == null)
return;
mainCamer.parent.position = Role.transform.position;
}
}
}