2021-04-08 20:09:59 +08:00
|
|
|
|
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;
|
2021-04-09 00:48:56 +08:00
|
|
|
|
|
|
|
|
|
public static ETTask<bool> MoveTo(Unit unit, Vector2 target, ETCancellationToken token = null)
|
|
|
|
|
{
|
2021-04-08 20:09:59 +08:00
|
|
|
|
UnitScene unitScene = unit.GetComponent<UnitScene>();
|
|
|
|
|
Vector2 pos = unitScene.Position;
|
2021-04-24 17:39:11 +08:00
|
|
|
|
int yAngle = pos.x <= target.x? 0 : 180;
|
2021-04-09 00:48:56 +08:00
|
|
|
|
return MoveTo(unit, target, yAngle);
|
2021-04-08 20:09:59 +08:00
|
|
|
|
}
|
2021-04-24 17:39:11 +08:00
|
|
|
|
|
2021-04-09 00:48:56 +08:00
|
|
|
|
public static async ETTask<bool> MoveTo(Unit unit, Vector2 target, int yAngle, ETCancellationToken token = null)
|
2021-04-08 20:09:59 +08:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-04-10 19:49:32 +08:00
|
|
|
|
UnitScene unitScene = unit.GetComponent<UnitScene>();
|
|
|
|
|
unitScene.targetPos = target;
|
|
|
|
|
unitScene.isMoved = true;
|
2021-04-11 19:50:39 +08:00
|
|
|
|
bool ret = await FindPathMoveToAsync(unit, target, token);
|
2021-04-10 19:49:32 +08:00
|
|
|
|
unitScene.YAngle = yAngle;
|
|
|
|
|
unitScene.isMoved = false;
|
2021-04-09 00:48:56 +08:00
|
|
|
|
return ret;
|
2021-04-08 20:09:59 +08:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Log.Error(e);
|
|
|
|
|
}
|
2021-04-24 17:39:11 +08:00
|
|
|
|
|
2021-04-09 00:48:56 +08:00
|
|
|
|
return false;
|
2021-04-08 20:09:59 +08:00
|
|
|
|
}
|
2021-04-24 17:39:11 +08:00
|
|
|
|
|
2021-04-08 20:09:59 +08:00
|
|
|
|
// 可以多次调用,多次调用的话会取消上一次的协程
|
2021-04-15 00:12:07 +08:00
|
|
|
|
private static async ETTask<bool> FindPathMoveToAsync(this Unit unit, Vector3 target, ETCancellationToken cancellationToken = null)
|
2021-04-08 20:09:59 +08:00
|
|
|
|
{
|
|
|
|
|
float speed = ConstDefine.MoveSpeed;
|
|
|
|
|
|
2021-04-11 19:50:39 +08:00
|
|
|
|
using ListComponent<Vector3> list = ListComponent<Vector3>.Create();
|
2021-04-08 20:09:59 +08:00
|
|
|
|
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
|
|
|
|
|
};
|
2021-04-11 19:50:39 +08:00
|
|
|
|
BrocastComponent brocast = unit.GetComponent<BrocastComponent>();
|
2021-04-08 20:09:59 +08:00
|
|
|
|
brocast.Brocast(m2CPathfindingResult);
|
|
|
|
|
|
|
|
|
|
bool ret = await unit.GetComponent<MoveComponent>().MoveToAsync(list.List, speed, cancellationToken);
|
|
|
|
|
if (ret) // 如果返回false,说明被其它移动取消了,这时候不需要通知客户端stop
|
|
|
|
|
{
|
2021-04-09 00:48:56 +08:00
|
|
|
|
SendStop(unit, 0);
|
2021-04-08 20:09:59 +08:00
|
|
|
|
}
|
2021-04-24 17:39:11 +08:00
|
|
|
|
|
2021-04-09 00:48:56 +08:00
|
|
|
|
return ret;
|
2021-04-08 20:09:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Stop(this Unit unit)
|
|
|
|
|
{
|
|
|
|
|
unit.GetComponent<MoveComponent>().Stop();
|
|
|
|
|
SendStop(unit, 0);
|
|
|
|
|
}
|
2021-04-24 17:39:11 +08:00
|
|
|
|
|
2021-04-08 20:09:59 +08:00
|
|
|
|
private static void SendStop(Unit unit, int error)
|
|
|
|
|
{
|
|
|
|
|
if (!unit.IsTeamLeader) return;
|
|
|
|
|
UnitScene unitScene = unit.GetComponent<UnitScene>();
|
2021-04-11 19:50:39 +08:00
|
|
|
|
BrocastComponent brocast = unit.GetComponent<BrocastComponent>();
|
2021-04-08 20:09:59 +08:00
|
|
|
|
brocast.Brocast(new M2C_Stop()
|
|
|
|
|
{
|
|
|
|
|
Error = error,
|
|
|
|
|
Id = unit.Id,
|
|
|
|
|
X = unitScene.X,
|
|
|
|
|
Y = unitScene.Y,
|
|
|
|
|
YAngle = unitScene.YAngle
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-04-24 17:39:11 +08:00
|
|
|
|
|
|
|
|
|
public static async ETVoid TeamMoveTo(Team team, Vector2 target)
|
|
|
|
|
{
|
|
|
|
|
var list = team.GetUnits();
|
|
|
|
|
for (var curr = list.First; curr != null; curr = curr.Next)
|
|
|
|
|
{
|
|
|
|
|
Unit item = curr.Value;
|
|
|
|
|
long unitInstanceId = item.InstanceId;
|
|
|
|
|
await TimerComponent.Instance.WaitAsync(300);
|
|
|
|
|
if (!item)
|
|
|
|
|
continue;
|
|
|
|
|
if (unitInstanceId != item.InstanceId) continue;
|
|
|
|
|
UnitScene unitScene = item.GetComponent<UnitScene>();
|
|
|
|
|
Vector2 delta = MoveHelper.CalucateDistanceXandY(unitScene.Position, target);
|
|
|
|
|
int index = team.GetIndex(item.Id);
|
|
|
|
|
Vector2 targetPos = new(target.x - delta.x * index, target.y - delta.y * index);
|
|
|
|
|
MoveHelper.MoveTo(item, targetPos).Coroutine(); // 移动到目标点, 返回false表示协程取消
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Vector2 CalucateDistanceXandY(Vector2 posA, Vector2 posB)
|
|
|
|
|
{
|
|
|
|
|
const float TeamDistanceBetweenUnits = 0.8f;
|
|
|
|
|
float dx = posB.x - posA.x;
|
|
|
|
|
int positive = dx >= 0? 1 : -1;
|
|
|
|
|
float gradient = (posB.y - posA.y) / dx;
|
|
|
|
|
return new Vector2(positive * TeamDistanceBetweenUnits / Mathf.Sqrt(1 + gradient * gradient)
|
|
|
|
|
, positive * gradient * TeamDistanceBetweenUnits / Mathf.Sqrt(1 + gradient * gradient));
|
|
|
|
|
}
|
2021-04-08 20:09:59 +08:00
|
|
|
|
}
|
2021-04-24 17:39:11 +08:00
|
|
|
|
}
|