public ABPath Move(BaseUnit unit, Vector3 pos, float speed = 1.0f) { if (pos.IsInv()) { return(null); } if (UnitMoveCoroutineHandle.IsRunning) { BattleCoroutine.Kill(UnitMoveCoroutineHandle); Callback_OnMoveEnd?.Invoke(PreMoveUnit); } var path = StartABPath(unit.Pos, pos, null); UnitMoveCoroutineHandle = BattleCoroutine.Run(MoveAlongPath(unit, path, speed)); return(path); }
IEnumerator <float> MoveAlongPath(BaseUnit unit, ABPath path, float speed) { if (path.error || path.vectorPath.Count == 0) { throw new ArgumentException("Cannot follow an empty path"); } PreMoveUnit = unit; Callback_OnMoveStart?.Invoke(unit); // Very simple movement, just interpolate using a catmull rom spline float distanceAlongSegment = 0; for (int i = 0; i < path.vectorPath.Count - 1; i++) { var p0 = path.vectorPath[Mathf.Max(i - 1, 0)]; // Start of current segment var p1 = path.vectorPath[i]; // End of current segment var p2 = path.vectorPath[i + 1]; var p3 = path.vectorPath[Mathf.Min(i + 2, path.vectorPath.Count - 1)]; var segmentLength = Vector3.Distance(p1, p2); while (distanceAlongSegment < segmentLength) { var interpolatedPoint = MathUtil.CatmullRom(p0, p1, p2, p3, distanceAlongSegment / segmentLength); var targetRot = Quaternion.LookRotation((p2 - p1).SetY(0), Vector3.up); unit.Rot = Quaternion.Slerp(unit.Rot, targetRot, Time.deltaTime * 10); unit.transform.position = interpolatedPoint; Callback_OnMovingAlone?.Invoke(unit, p0, p1, p2, p3); yield return(Timing.WaitForOneFrame); distanceAlongSegment += Time.deltaTime * speed; } distanceAlongSegment -= segmentLength; } Vector3 target = path.vectorPath[path.vectorPath.Count - 1]; unit.Pos = target; Callback_OnMoveEnd?.Invoke(unit); }
protected virtual void OnMoveEnd() { BaseAStarMgr.GlobalMoveState.Remove(); BaseAStarMgr.SpeedUp(1.0f); StateMachine.CurStateData?.OnMoveEnd(); CalcCurNode(); CalcCurBlock(); GrabNewQuateration(); ChangeToEndState(); if (MoveTarget_IsInTarget()) { CancleMoveTarget(); } Callback_OnMoveEnd?.Invoke(); if (UseFollowCoroutine) { BattleCoroutine.Kill(FollowPathCoroutine); } }
protected virtual void OnMoveEnd() { MoveType = BasicMoveType2D.None; Callback_OnMoveEnd?.Invoke(); }