Files
taptap2024_GJ_chidouren/Assets/Scripts/Game/Component/PlayerEntity.cs
2024-10-21 16:20:37 +08:00

57 lines
2.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Framework.Timer;
using Framework.Utils.Extend;
using UnityEngine;
namespace Game.Component
{
public class PlayerEntity : MonoBehaviour
{
public float speed = 10;
public Rigidbody2D rigidbody2D;
private float speedBuffOffset;
private TimeHandler speedBuffTimer;
public float CurSpeed => this.speed *
(1 + (this.speedBuffTimer?.IsPlaying ?? false ? this.speedBuffOffset * (1 - this.speedBuffTimer.CurProgress) : 0));
public void AddSpeedBuff (float duration = 3 , float offset = 0.5f)
{
this.speedBuffOffset = offset;
this.speedBuffTimer?.Kill ();
this.speedBuffTimer = GameUpdateMgr.Instance.CreateTimer (duration , null);
}
public void OnMove (Vector2 vector)
{
var t = this.transform;
// 获取前方方向的世界坐标
Vector3 forward = (Vector3)vector;
//根据遥感向量获取一个01的移动速度系数
float speedOffset = forward.magnitude;
Quaternion targetRotation = Quaternion.FromToRotation (Vector3.up , forward.normalized);
transform.rotation = Quaternion.Lerp (transform.rotation , targetRotation , 0.25f);
// Debug.DrawLine (transform.position , transform.position + forward , Color.magenta);
this.transform.Translate ( Vector3.up * speedOffset * CurSpeed * Time.deltaTime);
// this.rigidbody2D.MovePosition (t.position + ((Vector3)vector * speed * Time.deltaTime));
// Vector3 v = target.position - transform.position;
// v.z = 0; // 确保向量在2D平面上
// float angle = Vector3.SignedAngle(Vector3.up, v, Vector3.forward);
// Quaternion rotation = Quaternion.Euler(0, 0, angle);
t.position = t.position.SetZ (0);
}
public void OnMoveEnd ()
{
this.transform.rotation = Quaternion.Euler (0 , 0 , this.transform.rotation.eulerAngles.z);
}
public void RefreshInit ()
{
this.speedBuffTimer?.Kill ();
this.speedBuffTimer = null;
this.speedBuffOffset = 0;
//主角出生初始化
}
}
}