2021-04-10 19:49:32 +08:00
|
|
|
|
using System;
|
|
|
|
|
using UnityEngine;
|
2021-04-08 20:09:59 +08:00
|
|
|
|
|
|
|
|
|
namespace ET
|
|
|
|
|
{
|
2021-04-10 19:49:32 +08:00
|
|
|
|
[ActorMessageHandler]
|
|
|
|
|
public class Frame_ClickMapHandler : AMActorLocationHandler<Unit, Frame_ClickMap>
|
|
|
|
|
{
|
|
|
|
|
protected override async ETTask Run(Unit unit, Frame_ClickMap message)
|
|
|
|
|
{
|
|
|
|
|
if (!unit.IsTeamLeader) return;
|
|
|
|
|
if (unit.teamState != TeamState.None) return;
|
|
|
|
|
if (unit.isAI) return;
|
|
|
|
|
Vector2 target = new(message.UnitInfo.X, message.UnitInfo.Y);
|
|
|
|
|
MoveToTarget(unit, target, message.UnitInfo.YAngle).Coroutine();
|
|
|
|
|
await ETTask.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async ETVoid MoveToTarget(Unit unit, Vector2 target, int yAngle)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
MoveHelper.MoveTo(unit, target, yAngle).Coroutine();
|
|
|
|
|
Team team = TeamComponent.Instance.Get(unit.TeamLeaderId);
|
|
|
|
|
|
2021-04-11 19:50:39 +08:00
|
|
|
|
foreach (Unit item in team.GetUnits())
|
2021-04-10 19:49:32 +08:00
|
|
|
|
{
|
|
|
|
|
if (item.IsTeamLeader)
|
|
|
|
|
continue;
|
|
|
|
|
await TimerComponent.Instance.WaitAsync(300);
|
|
|
|
|
if (!item)
|
|
|
|
|
continue;
|
2021-04-11 19:50:39 +08:00
|
|
|
|
UnitScene unitScene = item.GetComponent<UnitScene>();
|
2021-04-10 19:49:32 +08:00
|
|
|
|
Vector2 delta = 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表示协程取消
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Log.Error(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private 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
|
|
|
|
}
|