174 lines
3.8 KiB
C#
174 lines
3.8 KiB
C#
|
|
using System.Collections;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using UnityEngine;
|
|||
|
|
using Mapbox.Unity.Map;
|
|||
|
|
using Mapbox.Utils;
|
|||
|
|
using Mapbox.Unity.Utilities;
|
|||
|
|
using System.Linq;
|
|||
|
|
|
|||
|
|
|
|||
|
|
namespace Ride.Scripts
|
|||
|
|
{
|
|||
|
|
public class PlayerController : MonoBehaviour
|
|||
|
|
{
|
|||
|
|
[Header("Character")]
|
|||
|
|
[SerializeField]
|
|||
|
|
GameObject character;
|
|||
|
|
[SerializeField]
|
|||
|
|
float characterSpeed;
|
|||
|
|
[SerializeField]
|
|||
|
|
Animator characterAnimator;
|
|||
|
|
|
|||
|
|
//[Header("References")]
|
|||
|
|
//[SerializeField]
|
|||
|
|
//AstronautDirections directions;
|
|||
|
|
//[SerializeField]
|
|||
|
|
//Transform startPoint;
|
|||
|
|
//[SerializeField]
|
|||
|
|
//Transform endPoint;
|
|||
|
|
[SerializeField]
|
|||
|
|
AbstractMap map;
|
|||
|
|
[SerializeField]
|
|||
|
|
GameObject rayPlane;
|
|||
|
|
//[SerializeField]
|
|||
|
|
//Transform _movementEndPoint;
|
|||
|
|
|
|||
|
|
[SerializeField]
|
|||
|
|
LayerMask layerMask;
|
|||
|
|
|
|||
|
|
Ray ray;
|
|||
|
|
RaycastHit hit;
|
|||
|
|
LayerMask raycastPlane;
|
|||
|
|
float clicktime;
|
|||
|
|
bool moving;
|
|||
|
|
bool characterDisabled;
|
|||
|
|
private List<Vector2d> rideList;
|
|||
|
|
void Start()
|
|||
|
|
{
|
|||
|
|
characterAnimator = GetComponentInChildren<Animator>();
|
|||
|
|
//初始化玩家坐标
|
|||
|
|
currentlatlong = map.WorldToGeoPosition(transform.localPosition);
|
|||
|
|
nextlatlong = currentlatlong;
|
|||
|
|
totalDistance = 0;
|
|||
|
|
//StartCoroutine("Timer");
|
|||
|
|
//if (!Application.isEditor)
|
|||
|
|
//{
|
|||
|
|
// this.enabled = false;
|
|||
|
|
// return;
|
|||
|
|
//}
|
|||
|
|
}
|
|||
|
|
//gameStatus idle start pause finish
|
|||
|
|
|
|||
|
|
private Vector2d currentlatlong; //当前坐标
|
|||
|
|
private Vector2d nextlatlong; //下一个点的坐标
|
|||
|
|
private double totalDistance;
|
|||
|
|
void Update()
|
|||
|
|
{
|
|||
|
|
if (characterDisabled)
|
|||
|
|
return;
|
|||
|
|
|
|||
|
|
//CamControl();
|
|||
|
|
|
|||
|
|
bool click = false;
|
|||
|
|
|
|||
|
|
if (Input.GetMouseButtonDown(0))
|
|||
|
|
{
|
|||
|
|
clicktime = Time.time;
|
|||
|
|
}
|
|||
|
|
if (Input.GetMouseButtonUp(0))
|
|||
|
|
{
|
|||
|
|
if (Time.time - clicktime > 0.15f)
|
|||
|
|
{
|
|||
|
|
click = true;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
//开始骑行
|
|||
|
|
//if (click)
|
|||
|
|
{
|
|||
|
|
StartCoroutine("Walk");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
IEnumerator Walk()
|
|||
|
|
{
|
|||
|
|
yield return new WaitForSeconds(1f);
|
|||
|
|
//读取当前速度以及骑行距离
|
|||
|
|
double distance = MockData.CalculateSpeed(Random.Range(100,300), 10, 100, 65, 7) / 3600;
|
|||
|
|
if (totalDistance + distance <= MockData.totaldistance)
|
|||
|
|
{
|
|||
|
|
totalDistance += distance;
|
|||
|
|
//计算下一个点的位置
|
|||
|
|
nextlatlong = MockData.Along(totalDistance);
|
|||
|
|
|
|||
|
|
if (distance > 0)
|
|||
|
|
{
|
|||
|
|
CamControl();
|
|||
|
|
//设定动画状态
|
|||
|
|
characterAnimator.SetBool("IsRide", true);
|
|||
|
|
nextPos = map.GeoToWorldPosition(nextlatlong);
|
|||
|
|
//转向
|
|||
|
|
StartCoroutine(LookAtNextPos());
|
|||
|
|
//让人物移动 TODO 可以继续分点增加动画的流畅度
|
|||
|
|
transform.localPosition = nextPos;
|
|||
|
|
//StartCoroutine(MoveTo());
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
moving = false;
|
|||
|
|
characterAnimator.SetBool("IsRide", false);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
moving = false;
|
|||
|
|
characterAnimator.SetBool("IsRide", false);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#region Character : Movement
|
|||
|
|
List<Vector3> futurePositions = new List<Vector3>();
|
|||
|
|
bool interruption;
|
|||
|
|
|
|||
|
|
|
|||
|
|
Vector3 nextPos;
|
|||
|
|
Vector3 prevPos;
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region Character : Rotation
|
|||
|
|
IEnumerator LookAtNextPos()
|
|||
|
|
{
|
|||
|
|
Quaternion neededRotation = Quaternion.LookRotation(character.transform.position - nextPos);
|
|||
|
|
Quaternion thisRotation = character.transform.localRotation;
|
|||
|
|
|
|||
|
|
float t = 0;
|
|||
|
|
while (t < 1.0f)
|
|||
|
|
{
|
|||
|
|
t += Time.deltaTime / 0.25f;
|
|||
|
|
var rotationValue = Quaternion.Slerp(thisRotation, neededRotation, t);
|
|||
|
|
character.transform.rotation = Quaternion.Euler(0, rotationValue.eulerAngles.y, 0);
|
|||
|
|
yield return null;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region CameraControl
|
|||
|
|
[Header("CameraSettings")]
|
|||
|
|
[SerializeField]
|
|||
|
|
Camera cam;
|
|||
|
|
Vector3 previousPos = Vector3.zero;
|
|||
|
|
Vector3 deltaPos = Vector3.zero;
|
|||
|
|
|
|||
|
|
void CamControl()
|
|||
|
|
{
|
|||
|
|
if (cam != null)
|
|||
|
|
{
|
|||
|
|
deltaPos = transform.position - previousPos;
|
|||
|
|
//deltaPos.y = 0;
|
|||
|
|
cam.transform.position = Vector3.Lerp(cam.transform.position, cam.transform.position + deltaPos, Time.time);
|
|||
|
|
previousPos = transform.position;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|