using System; using Framework.Timer; using Framework.Utils.Extend; using Game.EventDefine; 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; //根据遥感向量获取一个0,1的移动速度系数 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; //主角出生初始化 } private void OnTriggerEnter2D (Collider2D other) { if (other.CompareTag ("Enemy")) { //死亡特效 GameEventDefine.ChangeGameFsm.SendMessage (GameState.FieldGame); } } } }