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.
 
 
 
 
 
 

216 lines
7.7 KiB

using System.Collections.Generic;
using System.Linq;
namespace ET
{
[FriendClass(typeof (Construct))]
[FriendClass(typeof (Building))]
[FriendClass(typeof (Farmland))]
[FriendClass(typeof(Unit))]
public static class ConstructOperate
{
public static Construct CreateConstruct(Unit unit, int configId, float x, float y, long id = 0, long buildingId = 0)
{
// if (unit.Food < 0)
// {
// return null;
// }
var construct = unit.GetOrAddComponent<ConstructComponent>().CreateConstruct(unit,configId, x, y, id, buildingId);
return construct;
}
public static int GoConstruct(Unit unit, long constructId, List<long> peopleIdList)
{
var construct = unit.GetComponent<ConstructComponent>().GetChild<Construct>(constructId);
if (construct == null)
{
return ErrorCode.ERR_ConstructNotFound;
}
List<People> peopleList = new List<People>();
foreach (var id in peopleIdList)
{
People people = unit.GetComponent<PeopleComponent>().GetChild<People>(id);
if (people == null)
{
return ErrorCode.ERR_PeopleNotFound;
}
var targetId = people.GetTargetId();
if (targetId == constructId)
{
return ErrorCode.ERR_BehaveError;
}
peopleList.Add(people);
}
foreach (var people in peopleList)
{
PeopleOperate.StopBehave(unit, people);
construct.PreparePeopleIdList.Add(people.Id);
people.SetBehaveType(ConstBehaveType.BEHAVE_PREPARE_CONSTRUCT);
people.SetTargetId(construct.Id);
}
return ErrorCode.ERR_Success;
}
public static int StopConstruct(Unit unit, long constructId, long peopleId)
{
People people = unit.GetComponent<PeopleComponent>().GetChild<People>(peopleId);
if (people == null)
{
return ErrorCode.ERR_PeopleNotFound;
}
var construct = unit.GetComponent<ConstructComponent>().GetChild<Construct>(constructId);
if (construct == null)
{
return ErrorCode.ERR_ConstructNotFound;
}
if (construct.PeopleIdList.Remove(peopleId) || construct.PreparePeopleIdList.Remove(peopleId))
{
people.SetTargetId(0);
people.SetBehaveType(ConstBehaveType.BEHAVE_IDLE);
return ErrorCode.ERR_Success;
}
return ErrorCode.ERR_StopConstructError;
}
public static int StartConstruct(Unit unit, long constructId, long peopleId)
{
People people = unit.GetComponent<PeopleComponent>().GetChild<People>(peopleId);
if (people == null)
{
return ErrorCode.ERR_PeopleNotFound;
}
var construct = unit.GetComponent<ConstructComponent>().GetChild<Construct>(constructId);
if (construct == null)
{
return ErrorCode.ERR_ConstructNotFound;
}
if (construct.PreparePeopleIdList.Remove(peopleId))
{
construct.PeopleIdList.Add(peopleId);
if (construct.IsConstructed == 0)
{
construct.IsConstructed = 1;
}
people.SetBehaveType(ConstBehaveType.BEHAVE_CONSTRUCT);
return ErrorCode.ERR_Success;
}
return ErrorCode.ERR_OperateFail;
}
public static int CancelConstruct(Unit unit, long constructId)
{
var construct = unit.GetComponent<ConstructComponent>().GetChild<Construct>(constructId);
if (construct == null)
{
return ErrorCode.ERR_ConstructNotFound;
}
var rate = 100;
if (construct.IsConstructed == 1)
{
rate = WorldParametersConfigCategory.Instance.Get(WorldParam.BuildingReturnRatio).Value[0];
}
for (int i = 0; i < construct.Config.ItemId.Length; i++)
{
unit.GetOrAddComponent<StoreComponent>().Add(construct.Config.ItemId[i], construct.Config.ItemNum[i] * rate / 100);
}
List<long> idList = construct.PeopleIdList.Union(construct.PreparePeopleIdList).ToList<long>();
foreach (var id in idList)
{
People people = unit.GetComponent<PeopleComponent>().GetChild<People>(id);
people.SetBehaveType(ConstBehaveType.BEHAVE_IDLE);
people.SetTargetId(0);
}
construct.Dispose();
return ErrorCode.ERR_Success;
}
public static Building ConstructFinish(Unit unit, long constructId, long buildingId = 0)
{
var construct = unit.GetComponent<ConstructComponent>().GetChild<Construct>(constructId);
Building build;
if (construct == null)
{
return null;
}
if (construct.IsUpgrade)
{
var buildingComp = unit.GetOrAddComponent<BuildingComponent>();
build = buildingComp.GetChild<Building>(construct.BuildingId);
if (build == null)
{
Log.Error($"Can't find building:{construct.BuildingId} for construct:{construct.Id}");
}
build.IsUpgrade = 0;
build.ConfigId = construct.Config.MixtureID;
}
else
{
if (buildingId > 0)
{
build = unit.GetOrAddComponent<BuildingComponent>().AddChildWithId<Building>(buildingId);
}
else
{
build = unit.GetOrAddComponent<BuildingComponent>().AddChild<Building>();
}
build.Position.x = construct.X;
build.Position.y = construct.Y;
build.ConfigId = construct.Config.MixtureID;
//如果是农场
if (build.Config.Special == (int)StructureSpecialEnum.FARMLAND)
{
var farmland = build.AddChild<Farmland>();
farmland.FarmlandState = FarmlandState.FARMLAND_STATE_FREE;
unit.AddGrandChild(farmland);
}
}
StructureConfig structureConfig = StructureConfigCategory.Instance.Get(build.ConfigId);
if (structureConfig != null)
{
build.Durable = structureConfig.Durable;
}
List<long> idList = construct.PeopleIdList.Union(construct.PreparePeopleIdList).ToList<long>();
foreach (var id in idList)
{
People people = unit.GetComponent<PeopleComponent>().GetChild<People>(id);
people.SetBehaveType(ConstBehaveType.BEHAVE_IDLE);
people.SetTargetId(0);
}
construct.Dispose();
return build;
}
public static bool CheckOutBuildingByIds(Unit unit, int[] StructureID)
{
BuildingComponent bc = unit.GetOrAddComponent<BuildingComponent>();
return bc.CheckOutBuildingByIds(StructureID);
}
public static bool CheckOutBuildingBySpecialType(Unit unit, int special)
{
BuildingComponent bc = unit.GetOrAddComponent<BuildingComponent>();
return bc.CheckOutBuildingBySpecialType(special);
}
}
}