You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
6.4 KiB

using FairyGUI;
using UnityEngine;
namespace ET
{
public class MapUnitOperationComponentAwakeSystem: AwakeSystem<MapUnitOperationComponent>
{
public override void Awake(MapUnitOperationComponent self)
{
self.Awake();
}
}
public class MapUnitOperationComponentDestroySystem: DestroySystem<MapUnitOperationComponent>
{
public override void Destroy(MapUnitOperationComponent self)
{
self.Destroy();
}
}
[FriendClass(typeof(MapUnitOperationComponent))]
[FriendClass(typeof(CameraComponent))]
public static class MapUnitOperationComponentSystem
{
public static void Awake(this MapUnitOperationComponent self)
{
self.CameraComp = self.ZoneScene().CurrentScene().GetComponent<CameraComponent>();
self.MapComp = self.CameraComp.MapComp;
// 操作事件
Stage.inst.onTouchBegin.Add(self.OnTouchBegin);
Stage.inst.onTouchMove.Add(self.OnTouchMove);
Stage.inst.onTouchEnd.Add(self.OnTouchEnd);
}
public static void Edit(this MapUnitOperationComponent self, Transform unitRoot, SpriteRenderer unitSR)
{
self.CurEditUnitRoot = unitRoot;
self.CurEditUnitSR = unitSR;
self.CurEditUnitSize = self.MapComp.GetSizeBySpriteCollider(self.CurEditUnitSR);
self.MapComp.ShowGrid();
self.MapComp.ShowIndicator();
}
public static void OnTouchBegin(this MapUnitOperationComponent self, EventContext context)
{
if (!Stage.isTouchOnUI)
{
self.MovePos.Set(context.inputEvent.x, Screen.height - context.inputEvent.y);
self.UpdateUnitPos(self.MovePos);
self.IsTouchDown = true;
}
}
public static void OnTouchMove(this MapUnitOperationComponent self, EventContext context)
{
if (self.IsTouchDown)
{
self.InputPos.Set(context.inputEvent.x, context.inputEvent.y);
self.MovePos.Set(context.inputEvent.x, Screen.height - context.inputEvent.y);
self.CheckToMoveCamera(self.InputPos);
self.UpdateUnitPos(self.MovePos);
}
}
public static void OnTouchEnd(this MapUnitOperationComponent self, EventContext context)
{
self.IsTouchDown = false;
}
// 移动到屏幕边界时需要判断是否移动相机看到更边上的地图
public static void CheckToMoveCamera(this MapUnitOperationComponent self, Vector2 gesturePos)
{
var sprRender = self.CurEditUnitSR;
Bounds bounds = sprRender.sprite.bounds;
var size = bounds.size;
var unitPos = self.CurEditUnitRoot.position;
var cameraPos = self.CameraComp.GetCameraPos();
self.DeltaPos.Set(0, 0, 0);
float topOffset = (unitPos.y + size.y) - (cameraPos.y + self.CameraComp.CameraHalfHeight);
float bottomOffset = (cameraPos.y - self.CameraComp.CameraHalfHeight) - (unitPos.y - size.y / 4) ;
float moveDelta = self.CameraMoveSpeed;
if (topOffset > 0 || bottomOffset > 0)
{
if (topOffset > bottomOffset)
{
self.DeltaPos.y = moveDelta;
} else if (topOffset < bottomOffset)
{
self.DeltaPos.y = -moveDelta;
}
}
if ((unitPos.x + size.x) >= (cameraPos.x + self.CameraComp.CameraHalfWidth))
{
self.DeltaPos.x = moveDelta;
} else if ((unitPos.x - size.x) <= (cameraPos.x - self.CameraComp.CameraHalfWidth))
{
self.DeltaPos.x = -moveDelta;
}
Vector3 targetCameraPos = self.CameraComp.GetCameraPos() + self.DeltaPos;
self.CameraComp.SetCameraPosClamped(targetCameraPos);
}
public static void UpdateUnitPos(this MapUnitOperationComponent self, Vector2 screenPos)
{
Vector3 worldPos = self.CameraComp.ScreenToWorldPoint(screenPos);
worldPos.z = 0;
var sprRender = self.CurEditUnitSR;
var sprite = sprRender.sprite;
Bounds bounds = sprite.bounds;
Vector4 border = sprite.border;
worldPos = self.MapComp.AlignPlacePosition(worldPos, self.CurEditUnitSize);
self.CurEditUnitRoot.position = worldPos;
self.UnitMinPos = sprRender.transform.TransformPoint(bounds.center - new Vector3(bounds.extents.x - border.x / sprite.pixelsPerUnit, bounds.extents.y - border.y / sprite.pixelsPerUnit));
self.UnitMaxPos = sprRender.transform.TransformPoint(bounds.center + new Vector3(bounds.extents.x - border.z / sprite.pixelsPerUnit, bounds.extents.y - border.w / sprite.pixelsPerUnit));
self.UnitMidPos = (self.UnitMaxPos + self.UnitMinPos) / 2;
self.MapComp.UpdateIndicator(worldPos, self.CurEditUnitSize);
self.UnitPosUpdate?.Invoke();
}
public static bool IsPlaceValid(this MapUnitOperationComponent self)
{
return self.MapComp.IsPlaceValid(self.CurEditUnitRoot.position, self.CurEditUnitSize);
}
public static Vector2 GetMidLeftPos(this MapUnitOperationComponent self)
{
var midPos = self.UnitMidPos;
Vector2 midLeft = new Vector2(self.UnitMinPos.x , midPos.y);
midLeft = self.CameraComp.WorldToScreenPoint(midLeft);
return midLeft;
}
public static Vector2 GetMidRightPos(this MapUnitOperationComponent self)
{
var midPos = self.UnitMidPos;
Vector2 midRight = new Vector2(self.UnitMaxPos.x , midPos.y);
midRight = self.CameraComp.WorldToScreenPoint(midRight);
return midRight;
}
public static void Destroy(this MapUnitOperationComponent self)
{
// 操作事件
Stage.inst.onTouchBegin.Remove(self.OnTouchBegin);
Stage.inst.onTouchMove.Remove(self.OnTouchMove);
Stage.inst.onTouchEnd.Remove(self.OnTouchEnd);
self.MapComp.HideGrid();
self.MapComp.HideIndicator();
}
}
}