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.

89 lines
2.9 KiB

using System.Collections.Generic;
namespace ET
{
[FriendClass(typeof(Synthesis))]
public static class SynthesisOperate
{
public static int StartSynthesis(Unit unit, int configId, long maxAmount)
{
if (maxAmount <= 0)
{
return ErrorCode.ERR_OperateFail;
}
var config = SynthesisConfigCategory.Instance.Get(configId);
if (config == null)
{
return ErrorCode.ERR_ConfigError;
}
if (config.MixtureTpye != Constant.ConstSynthesisMixtureType.MATERIAL)
{
return ErrorCode.ERR_OperateFail;
}
if (!unit.GetComponent<BuildingComponent>().CheckOutBuildingByIds(config.StructureID))
{
return ErrorCode.ERR_NotProductionBuilding;
}
var store = unit.GetComponent<StoreComponent>();
for (var i = 0; i < config.ItemId.Length; i++)
{
long num = store.GetItemNum(config.ItemId[i]);
if (num < config.ItemNum[i] * maxAmount)
{
return ErrorCode.ERR_MaterialNotEnough;
}
}
var synthesis = unit.GetOrAddComponent<SynthesisComponent>().StartSynthesis(configId, maxAmount);
if (synthesis == null)
{
return ErrorCode.ERR_SynthesisError;
}
for (var i = 0; i < config.ItemId.Length; i++)
{
store.Remove(config.ItemId[i], (int) (config.ItemNum[i] * maxAmount));
}
return ErrorCode.ERR_Success;
}
public static int EndSynthesis(Unit unit, int configId)
{
Synthesis synthesis = unit.GetComponent<SynthesisComponent>().GetChild<Synthesis>(configId);
if (synthesis == null)
{
return ErrorCode.ERR_SynthesisNotFound;
}
SynthesisConfig config = synthesis.Config;
long leftAmount = synthesis.MaxAmount - synthesis.CurrAmount;
synthesis.Dispose();
if (leftAmount > 0)
{
StoreComponent storeComponent = unit.GetComponent<StoreComponent>();
for (int i = 0; i < config.ItemId.Length; i++)
{
storeComponent.Add(config.ItemId[i], config.ItemNum[i] * leftAmount);
}
}
return ErrorCode.ERR_Success;
}
public static bool CheckOutBuildingByIds(Unit unit, int[] StructureID)
{
BuildingComponent bc = unit.GetComponent<BuildingComponent>();
return bc.CheckOutBuildingByIds(StructureID);
}
public static void AddItem(Unit unit, SynthesisConfig config)
{
StoreComponent sc = unit.GetComponent<StoreComponent>();
sc.Add(config.MixtureID, config.Copies);
}
}
}