111 lines
3.9 KiB
C#
111 lines
3.9 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Text;
|
||
using UnityEngine;
|
||
|
||
namespace ET
|
||
{
|
||
public static class MoveHelper
|
||
{
|
||
public const float AtkDis = 2.7f * 2.7f;
|
||
|
||
public static ETTask<bool> MoveTo(Unit unit, Vector2 target, ETCancellationToken token = null)
|
||
{
|
||
UnitScene unitScene = unit.GetComponent<UnitScene>();
|
||
Vector2 pos = unitScene.Position;
|
||
int yAngle = pos.x <= target.x ? 0 : 180;
|
||
return MoveTo(unit, target, yAngle);
|
||
}
|
||
public static async ETTask<bool> MoveTo(Unit unit, Vector2 target, int yAngle, ETCancellationToken token = null)
|
||
{
|
||
try
|
||
{
|
||
UnitScene unitScene = unit.GetComponent<UnitScene>();
|
||
unitScene.targetPos = target;
|
||
unitScene.isMoved = true;
|
||
var ret = await FindPathMoveToAsync(unit, target, token);
|
||
unitScene.YAngle = yAngle;
|
||
unitScene.isMoved = false;
|
||
return ret;
|
||
//int index = 0;
|
||
//foreach (var u in team.GetUnits())
|
||
//{
|
||
// if (u.IsTransfer) continue;
|
||
// if (u.IsTeamLeader)
|
||
// {
|
||
// }
|
||
// else
|
||
// {
|
||
// await TimerComponent.Instance.WaitAsync(300);
|
||
// UnitScene unitScene = u.GetComponent<UnitScene>();
|
||
// Vector2 delta = CalucateDistanceXandY(unitScene.Position, target);
|
||
// Vector2 targetPos = new Vector2(target.x - delta.x * ++index, target.y - delta.y * index);
|
||
// FindPathMoveToAsync(u, targetPos, token).Coroutine();
|
||
// unitScene.YAngle = yAngle;
|
||
// }
|
||
//}
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
Log.Error(e);
|
||
}
|
||
return false;
|
||
}
|
||
// 可以多次调用,多次调用的话会取消上一次的协程
|
||
public static async ETTask<bool> FindPathMoveToAsync(this Unit unit, Vector3 target, ETCancellationToken cancellationToken = null)
|
||
{
|
||
float speed = ConstDefine.MoveSpeed;
|
||
|
||
using var list = ListComponent<Vector3>.Create();
|
||
UnitScene unitScene = unit.GetComponent<UnitScene>();
|
||
list.List.Add(unitScene.Position);
|
||
list.List.Add(target);
|
||
|
||
// 广播寻路路径
|
||
M2C_PathfindingResult m2CPathfindingResult = new M2C_PathfindingResult
|
||
{
|
||
Id = unit.Id,
|
||
|
||
X = unitScene.X,
|
||
Y = unitScene.Y,
|
||
|
||
|
||
TX = target.x,
|
||
TY = target.y,
|
||
MoveSpeed = ConstDefine.MoveSpeed
|
||
};
|
||
var brocast = unit.GetComponent<BrocastComponent>();
|
||
brocast.Brocast(m2CPathfindingResult);
|
||
|
||
|
||
bool ret = await unit.GetComponent<MoveComponent>().MoveToAsync(list.List, speed, cancellationToken);
|
||
if (ret) // 如果返回false,说明被其它移动取消了,这时候不需要通知客户端stop
|
||
{
|
||
SendStop(unit, 0);
|
||
}
|
||
return ret;
|
||
}
|
||
|
||
public static void Stop(this Unit unit)
|
||
{
|
||
unit.GetComponent<MoveComponent>().Stop();
|
||
SendStop(unit, 0);
|
||
}
|
||
private static void SendStop(Unit unit, int error)
|
||
{
|
||
if (!unit.IsTeamLeader) return;
|
||
UnitScene unitScene = unit.GetComponent<UnitScene>();
|
||
var brocast = unit.GetComponent<BrocastComponent>();
|
||
brocast.Brocast(new M2C_Stop()
|
||
{
|
||
Error = error,
|
||
Id = unit.Id,
|
||
X = unitScene.X,
|
||
Y = unitScene.Y,
|
||
|
||
YAngle = unitScene.YAngle
|
||
});
|
||
}
|
||
}
|
||
}
|