zxl
/
CTT
forked from Cal/CTT
1
0
Fork 0
CTT/Server/Hotfix/Game/Handler/Map/Frame_ClickMapHandler.cs

55 lines
2.2 KiB
C#

using System;
using UnityEngine;
namespace ET
{
[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);
foreach (Unit item in team.GetUnits())
{
if (item.IsTeamLeader)
continue;
await TimerComponent.Instance.WaitAsync(300);
if (!item)
continue;
UnitScene unitScene = item.GetComponent<UnitScene>();
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));
}
}
}