using System.Collections.Generic; using ET.EventType; using NUnit.Framework; using UnityEngine; namespace ET { [FriendClass(typeof(Building))] public class AfterCreateBuilding_AddComponent : AEvent { protected override void Run(AfterCreateBuilding args) { var unit = args.Unit; var building = args.Building; // 升级的过程中不显示原先的建筑 if (building.IsUpgrade == 0) { var buildingViewComp = building.AddComponent(); buildingViewComp.CreateName().Coroutine(); // 处理农田 var farmland = building.GetChildByType(); if (farmland != null) { farmland.AddComponent(); } // 在不是升级且是新建的建筑时处理田的管理 if (args.IsNew) { ProcessCabin(unit, building); ProcessFarmland(unit, building); } } // update map info } // 处理田间小屋的逻辑 private void ProcessCabin(Unit unit, Building building) { var cabin = building.GetChildByType(); if (cabin == null) { return; } var mapComp = unit.ZoneScene().CurrentScene().GetComponent(); if (mapComp == null) { return; } Vector2Int cabinSize = new Vector2Int(cabin.GetRange(), cabin.GetRange()); var cabinRect = mapComp.GetUnitRectIndicesOfCenter(building.Position, cabinSize); var farmlands = unit.GetGrandChildren(); List inAreaFarmlandIds = new(); foreach (Farmland farmland in farmlands) { var farmlandBuilding = farmland.GetParent(); var mapPos = mapComp.WorldToMapPos(farmlandBuilding.Position); if (mapComp.IsPosInRect(mapPos, cabinRect)) { inAreaFarmlandIds.Add(farmland.Id); } } if (inAreaFarmlandIds.Count > 0) { CabinHelper.AddFarmland(unit, cabin, inAreaFarmlandIds).Coroutine(); } } // 处理田的逻辑,如果在田间小屋的周围需要被管理 private void ProcessFarmland(Unit unit, Building building) { var farmland = building.GetChildByType(); if (farmland == null) { return; } var mapComp = unit.ZoneScene().CurrentScene().GetComponent(); if (mapComp == null) { return; } var cabins = unit.GetGrandChildren(); List inAreaFarmlandIds = new(); Vector2Int cabinSize = new Vector2Int(); foreach (Cabin cabin in cabins) { cabinSize.Set(cabin.GetRange(), cabin.GetRange()); inAreaFarmlandIds.Clear(); var cabinBuilding = cabin.GetParent(); var cabinRect = mapComp.GetUnitRectIndicesOfCenter(cabinBuilding.Position, cabinSize); var mapPos = mapComp.WorldToMapPos(building.Position); if (mapComp.IsPosInRect(mapPos, cabinRect)) { inAreaFarmlandIds.Add(farmland.Id); CabinHelper.AddFarmland(unit, cabin, inAreaFarmlandIds).Coroutine(); break; } } } } }