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.
113 lines
4.0 KiB
113 lines
4.0 KiB
3 years ago
|
using UnityEngine;
|
||
|
|
||
|
namespace ET
|
||
|
{
|
||
|
public class CameraComponentAwakeSystem : AwakeSystem<CameraComponent>
|
||
|
{
|
||
|
public override void Awake(CameraComponent self)
|
||
|
{
|
||
|
self.Awake();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
[FriendClass(typeof(CameraComponent))]
|
||
|
[FriendClass(typeof(GlobalComponent))]
|
||
|
[FriendClass(typeof(MapComponent))]
|
||
|
public static class CameraComponentSystem
|
||
|
{
|
||
|
public static void Awake(this CameraComponent self)
|
||
|
{
|
||
|
self.MainCamera = GlobalComponent.Instance.MainCamera.GetComponent<Camera>();
|
||
|
self.MapComp = self.ZoneScene().CurrentScene().GetComponent<MapComponent>();
|
||
|
|
||
|
var size = self.MapComp.MapSize;
|
||
|
float MapHalfHeight = size.y / 2;
|
||
|
float MapWidthToHeight = size.x / 2 / self.MainCamera.aspect;
|
||
|
|
||
|
self.MaxCameraOrthoSize = Mathf.Min(MapHalfHeight, MapWidthToHeight);
|
||
|
self.MinCameraOrthoSize = 7;
|
||
|
self.CameraHalfHeight = self.MainCamera.orthographicSize;
|
||
|
self.CameraHalfWidth = self.MainCamera.aspect * self.CameraHalfHeight;
|
||
|
}
|
||
|
|
||
|
public static void ZoomCamera(this CameraComponent self, float delta) {
|
||
|
|
||
|
float size = self.MainCamera.orthographicSize;
|
||
|
size += delta;
|
||
|
size = Mathf.Clamp(size, self.MinCameraOrthoSize, self.MaxCameraOrthoSize);
|
||
|
self.SetOrthoSize(size);
|
||
|
self.SetCameraPos(self.ClampCameraTargetPos(self.MainCamera.transform.position));
|
||
|
}
|
||
|
|
||
|
public static void SetCameraPosClamped(this CameraComponent self, Vector3 pos)
|
||
|
{
|
||
|
self.SetCameraPos(self.ClampCameraTargetPos(pos));
|
||
|
}
|
||
|
|
||
|
public static void SetCameraPos(this CameraComponent self, Vector3 pos)
|
||
|
{
|
||
|
self.MainCamera.transform.position = pos;
|
||
|
Game.EventSystem.Publish(new EventType.CameraPosChange(){ZoneScene = self.ZoneScene()});
|
||
|
}
|
||
|
|
||
|
public static Vector3 ScreenToWorldPoint(this CameraComponent self, Vector3 pos)
|
||
|
{
|
||
|
return self.MainCamera.ScreenToWorldPoint(pos);
|
||
|
}
|
||
|
|
||
|
public static Vector2 WorldToScreenPoint(this CameraComponent self, Vector2 pos)
|
||
|
{
|
||
|
return self.MainCamera.WorldToScreenPoint(pos);
|
||
|
}
|
||
|
|
||
|
public static Vector3 GetCameraPos(this CameraComponent self)
|
||
|
{
|
||
|
return self.MainCamera.transform.position;
|
||
|
}
|
||
|
|
||
|
public static void SetOrthoSize(this CameraComponent self, float size)
|
||
|
{
|
||
|
self.MainCamera.orthographicSize = size;
|
||
|
self.CameraHalfHeight = size;
|
||
|
self.CameraHalfWidth = self.MainCamera.aspect * size;
|
||
|
}
|
||
|
|
||
|
public static float GetOrthoSize(this CameraComponent self)
|
||
|
{
|
||
|
return self.MainCamera.orthographicSize;
|
||
|
}
|
||
|
|
||
|
public static Vector3 ClampCameraTargetPos(this CameraComponent self, Vector3 targetPos)
|
||
|
{
|
||
|
float minPosX = self.MapComp.MapMinPos.x + self.CameraHalfWidth;
|
||
|
float maxPosX = self.MapComp.MapMaxPos.x - self.CameraHalfWidth;
|
||
|
float minPosY = self.MapComp.MapMinPos.y + self.CameraHalfHeight;
|
||
|
float maxPosY = self.MapComp.MapMaxPos.y - self.CameraHalfHeight;
|
||
|
|
||
|
targetPos.x = Mathf.Clamp(targetPos.x, minPosX, maxPosX);
|
||
|
targetPos.y = Mathf.Clamp(targetPos.y, minPosY, maxPosY);
|
||
|
|
||
|
return targetPos;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 存储相机参数
|
||
|
/// </summary>
|
||
|
/// <param name="self"></param>
|
||
|
public static void StoreCameraInfo(this CameraComponent self)
|
||
|
{
|
||
|
self.BackupPos = self.MainCamera.transform.position;
|
||
|
self.BackupOrthoSize = self.GetOrthoSize();
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 还原相机参数
|
||
|
/// </summary>
|
||
|
/// <param name="self"></param>
|
||
|
public static void RestoreCameraInfo(this CameraComponent self)
|
||
|
{
|
||
|
self.SetOrthoSize(self.BackupOrthoSize);
|
||
|
self.SetCameraPos(self.BackupPos);
|
||
|
}
|
||
|
}
|
||
|
}
|