powerfun-unity/Assets/Scenes/Ride/Scripts/PlayerController.cs

157 lines
3.8 KiB
C#
Raw Normal View History

2021-03-22 19:20:51 +08:00
using System.Collections;
using UnityEngine;
using Mapbox.Unity.Map;
using Mapbox.Utils;
2021-03-23 16:07:31 +08:00
using Assets.Scripts.Apis.Models;
using static Assets.Scripts.Apis.Models.MapDataModel;
2021-03-22 19:20:51 +08:00
2021-03-23 16:07:31 +08:00
namespace Assets.Scenes.Ride.Scripts
2021-03-22 19:20:51 +08:00
{
public class PlayerController : MonoBehaviour
{
[Header("Character")]
[SerializeField]
GameObject character;
[SerializeField]
float characterSpeed;
[SerializeField]
Animator characterAnimator;
[SerializeField]
AbstractMap map;
2021-03-23 16:07:31 +08:00
Vector3 nextPos;
Vector3 prePos;
private Vector2d currentlatlong; //当前坐标
private Vector2d nextlatlong; //下一个点的坐标
private double totalDistance;
private int ticks;//秒数
public float timer = 1.0f;//计时器
2021-03-22 19:20:51 +08:00
void Start()
{
characterAnimator = GetComponentInChildren<Animator>();
2021-03-23 16:07:31 +08:00
InitializePlayer();
}
//初始化人物
void InitializePlayer()
{
//获取玩家初始距离
2021-03-22 19:20:51 +08:00
currentlatlong = map.WorldToGeoPosition(transform.localPosition);
nextlatlong = currentlatlong;
totalDistance = 0;
2021-03-23 16:07:31 +08:00
ticks = 0;
2021-03-22 19:20:51 +08:00
}
2021-03-23 16:07:31 +08:00
2021-03-22 19:20:51 +08:00
void Update()
{
2021-03-23 16:07:31 +08:00
timer -= Time.deltaTime;
if (timer <= 0 && GameManger.IsStart)//定时器
2021-03-22 19:20:51 +08:00
{
2021-03-23 16:07:31 +08:00
ticks++;
Riding();
timer = 1.0f;
2021-03-22 19:20:51 +08:00
}
2021-03-23 16:07:31 +08:00
}
//计算当前玩家高度和坡度
Item find(double totalDistance)
{
double sumDistance = 0;
MapDataModel mapDataModel = GameManger.MapData;
foreach (var item in mapDataModel.List)
{
sumDistance += item.Distance;
if (totalDistance < sumDistance)
2021-03-22 19:20:51 +08:00
{
2021-03-23 16:07:31 +08:00
return item;
2021-03-22 19:20:51 +08:00
}
}
2021-03-23 16:07:31 +08:00
return mapDataModel.List[0];
2021-03-22 19:20:51 +08:00
}
2021-03-23 16:07:31 +08:00
void Riding()
2021-03-22 19:20:51 +08:00
{
2021-03-23 16:07:31 +08:00
//yield return new WaitForSeconds(1.0f);
////读取当前速度以及骑行距离
MapDataModel mapDataModel = GameManger.MapData;
Item nextRange = find(totalDistance);
double elevation = nextRange.Elevation;
double gradev = nextRange.Grade;
double power = Random.Range(700, 2000);//TODO
double distance = Helper.CalculateSpeed(elevation, gradev, power, 65, 7) / 3600;
characterAnimator.SetBool("IsRide", false);
totalDistance += distance;
if (totalDistance <= mapDataModel.TotalDistance)
2021-03-22 19:20:51 +08:00
{
if (distance > 0)
{
2021-03-23 16:07:31 +08:00
var v = GameManger.Along(totalDistance);
StartCoroutine(CamControl());
2021-03-22 19:20:51 +08:00
//设定动画状态
characterAnimator.SetBool("IsRide", true);
2021-03-23 16:07:31 +08:00
nextPos = map.GeoToWorldPosition(v);
prePos = transform.localPosition;
2021-03-22 19:20:51 +08:00
//转向
StartCoroutine(LookAtNextPos());
2021-03-23 16:07:31 +08:00
//移动
StartCoroutine(MoveTo());
2021-03-22 19:20:51 +08:00
}
}
2021-03-23 16:07:31 +08:00
}
IEnumerator MoveTo()
{
//让人物移动分点增加动画的流畅度
float t = 0;
while (t < 1)
2021-03-22 19:20:51 +08:00
{
2021-03-23 16:07:31 +08:00
t += Time.deltaTime;
Vector3 v = Vector3.Lerp(transform.localPosition, nextPos, t);
transform.localPosition = v;
yield return null;
2021-03-22 19:20:51 +08:00
}
}
#region Character : Rotation
IEnumerator LookAtNextPos()
{
2021-03-23 16:07:31 +08:00
Quaternion neededRotation = Quaternion.LookRotation(transform.localPosition - nextPos);
2021-03-22 19:20:51 +08:00
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;
2021-03-23 16:07:31 +08:00
IEnumerator CamControl()
2021-03-22 19:20:51 +08:00
{
if (cam != null)
{
2021-03-23 16:07:31 +08:00
float t = 0;
while (t < 1.0f)
{
t += Time.deltaTime / 0.5f;
deltaPos = transform.position - previousPos;
//deltaPos.y = 0;
cam.transform.position = Vector3.Lerp(cam.transform.position, cam.transform.position + deltaPos, t);
previousPos = transform.position;
yield return null;
}
2021-03-22 19:20:51 +08:00
}
}
#endregion
}
}