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.

115 lines
3.8 KiB

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