| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- using System;
- using System.Threading;
- using UnityEngine;
- namespace ET
- {
- [ObjectSystem]
- public class MoveComponentUpdateSystem : UpdateSystem<MoveComponent>
- {
- public override void Update(MoveComponent self)
- {
- self.Update();
- }
- }
- public class MoveComponent : Entity
- {
- public Vector3 Target;
- // 开启移动协程的时间
- public long StartTime;
- // 开启移动协程的Unit的位置
- public Vector3 StartPos;
- public long needTime;
-
- public ETTaskCompletionSource moveTcs;
- public void Update()
- {
- if (this.moveTcs == null)
- {
- return;
- }
-
- Unit unit = this.GetParent<Unit>();
- long timeNow = TimeHelper.Now();
- if (timeNow - this.StartTime >= this.needTime)
- {
- unit.Position = this.Target;
- ETTaskCompletionSource tcs = this.moveTcs;
- this.moveTcs = null;
- tcs.SetResult();
- return;
- }
- float amount = (timeNow - this.StartTime) * 1f / this.needTime;
- unit.Position = Vector3.Lerp(this.StartPos, this.Target, amount);
- }
- public ETTask MoveToAsync(Vector3 target, float speedValue, CancellationToken cancellationToken)
- {
- Unit unit = this.GetParent<Unit>();
-
- if ((target - this.Target).magnitude < 0.1f)
- {
- return ETTask.CompletedTask;
- }
-
- this.Target = target;
-
- this.StartPos = unit.Position;
- this.StartTime = TimeHelper.Now();
- float distance = (this.Target - this.StartPos).magnitude;
- if (Math.Abs(distance) < 0.1f)
- {
- return ETTask.CompletedTask;
- }
-
- this.needTime = (long)(distance / speedValue * 1000);
-
- this.moveTcs = new ETTaskCompletionSource();
-
- cancellationToken.Register(() =>
- {
- this.moveTcs = null;
- });
- return this.moveTcs.Task;
- }
- }
- }
|