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.

219 lines
9.2 KiB

using FairyGUI;
using UnityEngine;
using UnityEngine.EventSystems;
namespace ET
{
using FUISynthesis;
using FUICuisine;
using FUIBuilding;
using FUIUnitName;
public class BuildingViewComponentAwakeSystem : AwakeSystem<BuildingViewComponent>
{
public override void Awake(BuildingViewComponent self)
{
self.Awake();
}
}
public class BuildingViewComponentDestroySystem : DestroySystem<BuildingViewComponent>
{
public override void Destroy(BuildingViewComponent self)
{
self.Destroy();
}
}
[FriendClass(typeof(Building))]
[FriendClass(typeof(BuildingViewComponent))]
[FriendClass(typeof(MapComponent))]
[FriendClass(typeof(OperationComponent))]
[FriendClass(typeof(FUIUnitNamePanelComponent))]
[FriendClass(typeof(FUI_UnitNamePanel))]
[FriendClass(typeof(FUI_BuildingName))]
public static class BuildingViewComponentSystem
{
public static void Awake(this BuildingViewComponent self)
{
var buildingInfo = self.GetParent<Building>();
self.CreateBuilding(buildingInfo);
}
public static void CreateBuilding(this BuildingViewComponent self, Building buildingInfo)
{
self.buildingInfo = buildingInfo;
var buildingConfig = StructureConfigCategory.Instance.Get(buildingInfo.ConfigId);
// 在地图中创建建筑实例
var mapComp = self.ZoneScene().CurrentScene().GetComponent<MapComponent>();
GameObject building = self.InstantiateSync(buildingConfig.Prefab.ToPrefabAddress(), mapComp.BuildingRoot);
building.transform.position = new Vector3(buildingInfo.Position.x, buildingInfo.Position.y, 0);
self.BuildingRoot = building;
var sprRender = self.BuildingRoot.transform.Find("Build_Base").GetComponent<SpriteRenderer>();
Vector2Int size = mapComp.GetSizeBySpriteCollider(sprRender);
#if CLIENTTEST
var indicator = BuildingViewHelper.CreateOcupyPlaceInicator("OccupyPlaceInidcator", new Color(1, 0, 0, 0.3f));
indicator.transform.parent = self.BuildingRoot.transform;
indicator.transform.localPosition = Vector3.zero;
BuildingViewHelper.UpdateIndicatorLocal(indicator, mapComp, building.transform.position, size);
#endif
// 隐藏进度显示
self.BuildingRoot.transform.Find("Build_Base/BuildingProgress").gameObject.SetActive(false);
mapComp.MarkPlaceOnGrid(self.BuildingRoot.transform.position, size);
Physics2D.SyncTransforms();
mapComp.UpdateNavMesh2d();
// add click event
// https://docs.unity3d.com/2018.3/Documentation/ScriptReference/EventSystems.EventTrigger.html
EventTrigger trigger = self.BuildingRoot.transform.Find("Build_Base/SpriteClick").GetComponent<EventTrigger>();
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.PointerClick;
entry.callback.AddListener((data) =>
{
self.OnSpriteClicked(self);
});
trigger.triggers.Add(entry);
}
public static void Upgrade(this BuildingViewComponent self)
{
self.RemoveBuilding();
self.CreateBuilding(self.buildingInfo);
}
public static void OnSpriteClicked(this BuildingViewComponent self, BuildingViewComponent buildingViewComponent)
{
if (Stage.isTouchOnUI)
{
return;
}
var operationComponent = self.ZoneScene().CurrentScene().GetComponent<OperationComponent>();
// 防止拖动后又响应点击事件
if (operationComponent.IsDragging)
{
return;
}
var buildingInfo = buildingViewComponent.buildingInfo;
//根据不同的建筑类型打开不同的界面
StructureConfig structureConfig = StructureConfigCategory.Instance.Get(buildingInfo.ConfigId);
//TODO - 需要安排一个枚举方便区分建筑类型
switch (structureConfig.Type)
{
case (int)StructureTypeEnum.FABRICATION:
{
//if(structureConfig.Special == 0)
//{
// var SynthesisWindowComp = self.ZoneScene().GetComponent<FUIComponent>().ShowWindow<FUI_SynthesisWindow, FUISynthesisWindowComponent>();
// SynthesisWindowComp.ShowInfo(buildingInfo.ConfigId);
//}else
if (structureConfig.Special == 4)
{
var CuisineWindowComp = self.ZoneScene().GetComponent<FUIComponent>().ShowWindow<FUI_CuisineWindow, FUICuisineWindowComponent>();
CuisineWindowComp.ShowInfoAsync(buildingInfo).Coroutine();
}
else
{
var buildingInfoWindowComp = self.ZoneScene().GetComponent<FUIComponent>().ShowWindow<FUI_BuildingInfoWindow, FUIBuildingInfoWindowComponent>();
buildingInfoWindowComp.ShowInfo(buildingInfo);
}
}
break;
case (int) StructureTypeEnum.PRODUCTION:
{
if (structureConfig.Special == (int) StructureSpecialEnum.FARMLAND)
{
var farmlandWin = self.ZoneScene().GetComponent<FUIComponent>()
.ShowWindow<FUI_FarmlandWindow, FUIFarmlandWindowComponent>();
farmlandWin.ShowInfo(buildingInfo);
}
}
break;
default:
{
var buildingInfoWindowComp = self.ZoneScene().GetComponent<FUIComponent>().ShowWindow<FUI_BuildingInfoWindow, FUIBuildingInfoWindowComponent>();
buildingInfoWindowComp.ShowInfo(buildingInfo);
}
break;
}
}
public static Vector3 GetWorkPos(this BuildingViewComponent self)
{
return self.BuildingRoot.transform.position;
}
public static void Destroy(this BuildingViewComponent self)
{
if (self.Parent == null)
{
return;
}
self.RemoveBuilding();
}
public static void RemoveBuilding(this BuildingViewComponent self)
{
var mapComp = self.ZoneScene()?.CurrentScene()?.GetComponent<MapComponent>();
if (mapComp == null)
{
return;
}
FUIUnitNamePanelComponent unitNamePanelComponent = FUIComponent.Instance.GetUI(FUI_UnitNamePanel.UIResName) as FUIUnitNamePanelComponent;
unitNamePanelComponent.FUIUnitNamePanel.FGComp.RemoveChild(self.UnitName.FGComp);
self.UnitName.Dispose();
var sprRender = self.BuildingRoot.GetComponentInChildren<SpriteRenderer>();
mapComp.MarkPlaceOnGrid(self.BuildingRoot.transform.position, mapComp.GetSizeBySpriteCollider(sprRender), false);
self.BuildingRoot.SetActive(false);
GameObject.Destroy(self.BuildingRoot);
Physics2D.SyncTransforms();
mapComp.UpdateNavMesh2d();
}
public static Vector2 GetNamePos(this BuildingViewComponent self)
{
var sprRender = self.BuildingRoot.GetComponentInChildren<SpriteRenderer>();
Vector3 max = sprRender.bounds.max;
Vector3 min = sprRender.bounds.min;
return new Vector2((min.x + max.x) / 2, max.y);
}
public static async ETTask CreateName(this BuildingViewComponent self)
{
FUI_BuildingName nameEntity =
await FUIHelper.CreateInstanceAsync<FUI_BuildingName>(self.buildingInfo, FUI_BuildingName.UIPackageName, FUI_BuildingName.UIResName);
FUIUnitNamePanelComponent unitNamePanelComponent = FUIComponent.Instance.GetUI(FUI_UnitNamePanel.UIResName) as FUIUnitNamePanelComponent;
unitNamePanelComponent.FUIUnitNamePanel.FGComp.AddChild(nameEntity.FGComp);
var buildingConfig = StructureConfigCategory.Instance.Get(self.buildingInfo.ConfigId);
nameEntity.m_Name.text = buildingConfig.Name;
self.UnitName = nameEntity;
self.UpdateNamePos();
}
public static void UpdateNamePos(this BuildingViewComponent self)
{
var topPos = self.GetNamePos();
var cameraComp = self.ZoneScene()?.CurrentScene()?.GetComponent<CameraComponent>();
topPos = cameraComp.WorldToScreenPoint(topPos);
topPos.y = Screen.height - topPos.y;
if (self.UnitName != null)
{
self.UnitName.FGComp.xy = GRoot.inst.GlobalToLocal(topPos);
self.UnitName.FGComp.scale = Vector2.one * 10 / cameraComp.GetOrthoSize();
}
}
}
}