27 changed files with 593 additions and 51 deletions
Binary file not shown.
@ -0,0 +1,99 @@
|
||||
namespace ET |
||||
{ |
||||
public class FarmlandDestroySystem : DestroySystem<Farmland> |
||||
{ |
||||
public override void Destroy(Farmland self) |
||||
{ |
||||
self.Parent.Parent.GetParent<Unit>().RemoveFarmland(self); |
||||
} |
||||
} |
||||
|
||||
public class FarmlandAwakeSystem : AwakeSystem<Farmland> |
||||
{ |
||||
public override void Awake(Farmland self) |
||||
{ |
||||
self.Parent.Parent.GetParent<Unit>().AddFarmland(self); |
||||
} |
||||
} |
||||
|
||||
public class FarmlandUpdateSystem: UpdateSystem<Farmland> |
||||
{ |
||||
public override void Update(Farmland self) |
||||
{ |
||||
|
||||
} |
||||
} |
||||
[FriendClass(typeof(Farmland))] |
||||
public static class FarmlandSystem |
||||
{ |
||||
public static void FromMessage(this Farmland self, FarmlandProto farmlandProto) |
||||
{ |
||||
self.Id = farmlandProto.id; |
||||
self.SeedCfgId = farmlandProto.SeedCfgId; |
||||
self.Duration = farmlandProto.duration; |
||||
self.ExProduct = farmlandProto.exProduct; |
||||
self.PlanSeedCfgId = farmlandProto.plantCfgId; |
||||
self.FarmlandState = farmlandProto.farmlandState; |
||||
} |
||||
|
||||
public static FarmlandProto ToMessage(this Farmland self) |
||||
{ |
||||
FarmlandProto proto = new FarmlandProto(); |
||||
proto.id = self.Id; |
||||
proto.SeedCfgId = self.SeedCfgId; |
||||
proto.duration = self.Duration; |
||||
proto.exProduct = self.ExProduct; |
||||
proto.plantCfgId = self.PlanSeedCfgId; |
||||
proto.farmlandState = self.FarmlandState; |
||||
return proto; |
||||
} |
||||
|
||||
public static int Plant(this Farmland self, int cropId,int seedId) |
||||
{ |
||||
if (self.FarmlandState != FarmlandState.FARMLAND_STATE_FREE) |
||||
{ |
||||
return ErrorCode.ERR_FarmlandNotFree; |
||||
} |
||||
|
||||
self.ConfigId = cropId; |
||||
self.Duration = 0; |
||||
self.SeedCfgId = seedId; |
||||
self.PlanSeedCfgId = seedId; |
||||
self.FarmlandState = FarmlandState.FARMLAND_STATE_GROW; |
||||
self.ExProduct = 0; |
||||
return ErrorCode.ERR_Success; |
||||
|
||||
} |
||||
|
||||
public static void Update(this Farmland self) |
||||
{ |
||||
if (self.FarmlandState != FarmlandState.FARMLAND_STATE_GROW) |
||||
{ |
||||
return; |
||||
} |
||||
self.Duration += 1; |
||||
if (self.Duration >= self.Config.GrowthCycle) |
||||
{ |
||||
self.FarmlandState = FarmlandState.FARMLAND_STATE_RIPE; |
||||
} |
||||
} |
||||
|
||||
public static int Harvest(this Farmland self) |
||||
{ |
||||
if (self.FarmlandState != FarmlandState.FARMLAND_STATE_RIPE) |
||||
{ |
||||
return ErrorCode.ERR_PlantNotRipe; |
||||
} |
||||
self.Duration = 0; |
||||
self.SeedCfgId = 0; |
||||
self.PlanSeedCfgId = 0; |
||||
self.ExProduct = 0; |
||||
self.FarmlandState = FarmlandState.FARMLAND_STATE_FREE; |
||||
return ErrorCode.ERR_Success; |
||||
|
||||
} |
||||
|
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,55 @@
|
||||
using System; |
||||
|
||||
namespace ET |
||||
{ |
||||
public static class PlantHelper |
||||
{ |
||||
public static async ETTask<int> FarmlandPlant(Unit unit, long farmlandId, int cropId) |
||||
{ |
||||
try |
||||
{ |
||||
C2M_FarmlandPlant msg = new C2M_FarmlandPlant() { FarmlandId = farmlandId, CropCfgId = cropId }; |
||||
M2C_FarmlandPlant resp = await unit.ZoneScene().GetComponent<SessionComponent>().Session.Call(msg) as M2C_FarmlandPlant; |
||||
if (resp.Error == ErrorCode.ERR_Success) |
||||
{ |
||||
FarmlandOperate.Plant(unit, farmlandId, cropId); |
||||
} |
||||
else |
||||
{ |
||||
Log.Error(resp.Error.ToString()); |
||||
} |
||||
|
||||
return resp.Error; |
||||
} |
||||
catch (Exception e) |
||||
{ |
||||
Log.Error(e.ToString()); |
||||
throw; |
||||
} |
||||
} |
||||
|
||||
public static async ETTask<int> FarmlandHarvest(Unit unit, long farmlandId) |
||||
{ |
||||
try |
||||
{ |
||||
C2M_FarmlandHarvest msg = new C2M_FarmlandHarvest() { FarmlandId = farmlandId }; |
||||
M2C_FarmlandPlant resp = await unit.ZoneScene().GetComponent<SessionComponent>().Session.Call(msg) as M2C_FarmlandPlant; |
||||
if (resp.Error == ErrorCode.ERR_Success) |
||||
{ |
||||
FarmlandOperate.Harvest(unit, farmlandId); |
||||
} |
||||
else |
||||
{ |
||||
Log.Error(resp.Error.ToString()); |
||||
} |
||||
|
||||
return resp.Error; |
||||
} |
||||
catch (Exception e) |
||||
{ |
||||
Log.Error(e.ToString()); |
||||
throw; |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,50 @@
|
||||
namespace ET |
||||
{ |
||||
[FriendClass(typeof(Unit))] |
||||
[FriendClass(typeof(Farmland))] |
||||
public static class FarmlandOperate |
||||
{ |
||||
public static int Plant(Unit unit,long farmlandId, int cropId) |
||||
{ |
||||
if (!unit.FarmlandDic.ContainsKey(farmlandId)) |
||||
{ |
||||
return ErrorCode.ERR_FarmlandNotFound; |
||||
} |
||||
|
||||
var farmland = unit.FarmlandDic[farmlandId]; |
||||
if (farmland.FarmlandState != FarmlandState.FARMLAND_STATE_FREE) |
||||
{ |
||||
return ErrorCode.ERR_FarmlandNotFree; |
||||
} |
||||
//检查种子数量 |
||||
var cropConfig = CropConfigCategory.Instance.Get(cropId); |
||||
if (cropConfig == null) |
||||
{ |
||||
return ErrorCode.ERR_CropConfigNotFound; |
||||
} |
||||
|
||||
if (!unit.GetComponent<StoreComponent>().Remove(cropConfig.SeedNeed, cropConfig.SeedNum)) |
||||
{ |
||||
return ErrorCode.ERR_SeedNotEnough; |
||||
} |
||||
|
||||
farmland.Plant(cropId,cropConfig.SeedNeed); |
||||
|
||||
return ErrorCode.ERR_Success; |
||||
} |
||||
|
||||
public static int Harvest(Unit unit, long farmlandId) |
||||
{ |
||||
var farmland = unit.FarmlandDic[farmlandId]; |
||||
var storeNc = unit.GetComponent<StoreComponent>(); |
||||
storeNc.Add(farmland.Config.ProductID, farmland.Config.BasicProduction); |
||||
if (farmland.Config.ByProduct > 0) |
||||
{ |
||||
storeNc.Add(farmland.Config.ByProduct,farmland.Config.ByProductNum); |
||||
} |
||||
storeNc.Add(farmland.SeedCfgId,farmland.Config.SeedProduce); |
||||
farmland.Harvest(); |
||||
return ErrorCode.ERR_Success; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,20 @@
|
||||
using System; |
||||
using MongoDB.Bson.Serialization.Attributes; |
||||
|
||||
namespace ET |
||||
{ |
||||
public class Farmland: Entity, IAwake,IUpdate, IDestroy,ISerializeToEntity |
||||
{ |
||||
public int SeedCfgId; //农作物configid |
||||
public int Duration; //种植时间 |
||||
public int ExProduct; //额外产量 |
||||
public int FarmlandState; //农田状态 |
||||
public int PlanSeedCfgId; //计划种植的东西 |
||||
public int ConfigId; |
||||
|
||||
[BsonIgnore] |
||||
public CropConfig Config =>CropConfigCategory.Instance.Get(this.ConfigId); |
||||
|
||||
|
||||
} |
||||
} |
Loading…
Reference in new issue