using System.Collections.Generic; using System.Linq; namespace ET { [FriendClass(typeof (Construct))] [FriendClass(typeof (Building))] [FriendClass(typeof (Farmland))] 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().CreateConstruct(unit,configId, x, y, id, buildingId); return construct; } public static int GoConstruct(Unit unit, long constructId, List peopleIdList) { var construct = unit.GetComponent().GetChild(constructId); if (construct == null) { return ErrorCode.ERR_ConstructNotFound; } List peopleList = new List(); foreach (var id in peopleIdList) { People people = unit.GetComponent().GetChild(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().GetChild(peopleId); if (people == null) { return ErrorCode.ERR_PeopleNotFound; } var construct = unit.GetComponent().GetChild(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().GetChild(peopleId); if (people == null) { return ErrorCode.ERR_PeopleNotFound; } var construct = unit.GetComponent().GetChild(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().GetChild(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().Add(construct.Config.ItemId[i], construct.Config.ItemNum[i] * rate / 100); } List idList = construct.PeopleIdList.Union(construct.PreparePeopleIdList).ToList(); foreach (var id in idList) { People people = unit.GetComponent().GetChild(id); people.SetBehaveType(ConstBehaveType.BEHAVE_IDLE); people.SetTargetId(0); } construct.Dispose(); return ErrorCode.ERR_Success; } public static Building ConstructFinish(Unit unit, long constructId) { var construct = unit.GetComponent().GetChild(constructId); Building build; if (construct == null) { return null; } if (construct.IsUpgrade) { build = unit.GetOrAddComponent().GetChild(construct.BuildingId); build.IsUpgrade = 0; } else { build = unit.GetOrAddComponent().AddChild(); build.Position.x = construct.X; build.Position.y = construct.Y; //如果是农场 if (build.Config.Special == (int)StructureSpecialEnum.FARMLAND) { var farmland = build.AddChild(); farmland.FarmlandState = FarmlandState.FARMLAND_STATE_FREE; } } build.ConfigId = construct.Config.MixtureID; StructureConfig structureConfig = StructureConfigCategory.Instance.Get(build.ConfigId); if (structureConfig != null) { build.Durable = structureConfig.Durable; } List idList = construct.PeopleIdList.Union(construct.PreparePeopleIdList).ToList(); foreach (var id in idList) { People people = unit.GetComponent().GetChild(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(); return bc.CheckOutBuildingByIds(StructureID); } public static bool CheckOutBuildingBySpecialType(Unit unit, int special) { BuildingComponent bc = unit.GetOrAddComponent(); return bc.CheckOutBuildingBySpecialType(special); } } }