Browse Source

修改unit的管理器,去掉farmlanddic

master
wserver/wangdisen 3 years ago
parent
commit
1243345eaf
  1. 4
      Server/Hotfix/Demo/Farmland/Handler/C2M_FarmlandHarvestHandler.cs
  2. 4
      Server/Hotfix/Demo/Farmland/Handler/C2M_FarmlandPlantHandler.cs
  3. 4
      Server/Hotfix/Demo/Farmland/Handler/C2M_GoFarmlandHarvestHandler.cs
  4. 4
      Server/Hotfix/Demo/Farmland/Handler/C2M_GoFarmlandPlantHandler.cs
  5. 2
      Unity/Codes/Hotfix/Demo/Farmland/FarmlandSystem.cs
  6. 2
      Unity/Codes/Hotfix/Demo/Operate/ConstructOperate.cs
  7. 65
      Unity/Codes/Hotfix/Demo/Operate/FarmlandOperate.cs
  8. 78
      Unity/Codes/Hotfix/Demo/Unit/UnitSystem.cs
  9. 2
      Unity/Codes/Model/Demo/Unit/Unit.cs

4
Server/Hotfix/Demo/Farmland/Handler/C2M_FarmlandHarvestHandler.cs

@ -11,8 +11,8 @@ namespace ET
try try
{ {
People people; People people;
Farmland farmland; Farmland farmland = unit.GetGrandChild<Farmland>(request.FarmlandId);
if (unit.FarmlandDic.TryGetValue(request.FarmlandId, out farmland)) if (farmland!=null)
{ {
people = unit.GetComponent<PeopleComponent>().GetChild<People>(request.PeopleId); people = unit.GetComponent<PeopleComponent>().GetChild<People>(request.PeopleId);
if (people != null) if (people != null)

4
Server/Hotfix/Demo/Farmland/Handler/C2M_FarmlandPlantHandler.cs

@ -10,8 +10,8 @@ namespace ET
try try
{ {
People people; People people;
Farmland farmland; Farmland farmland = unit.GetGrandChild<Farmland>(request.FarmlandId);
if (unit.FarmlandDic.TryGetValue(request.FarmlandId, out farmland)) if (farmland!=null)
{ {
people = unit.GetComponent<PeopleComponent>().GetChild<People>(request.PeopleId); people = unit.GetComponent<PeopleComponent>().GetChild<People>(request.PeopleId);
if (people != null) if (people != null)

4
Server/Hotfix/Demo/Farmland/Handler/C2M_GoFarmlandHarvestHandler.cs

@ -18,8 +18,8 @@ namespace ET
} }
else else
{ {
Farmland farmland; Farmland farmland = unit.GetGrandChild<Farmland>(request.FarmlandId);
if (unit.FarmlandDic.TryGetValue(request.FarmlandId, out farmland)) if (farmland!=null)
{ {
response.Error=FarmlandOperate.GoHarvest(unit,farmland, people); response.Error=FarmlandOperate.GoHarvest(unit,farmland, people);
} }

4
Server/Hotfix/Demo/Farmland/Handler/C2M_GoFarmlandPlantHandler.cs

@ -18,8 +18,8 @@ namespace ET
} }
else else
{ {
Farmland farmland; Farmland farmland = unit.GetGrandChild<Farmland>(request.FarmlandId);
if (unit.FarmlandDic.TryGetValue(request.FarmlandId, out farmland)) if (farmland!=null)
{ {
response.Error = FarmlandOperate.GoPlant(unit, farmland, people); response.Error = FarmlandOperate.GoPlant(unit, farmland, people);
} }

2
Unity/Codes/Hotfix/Demo/Farmland/FarmlandSystem.cs

@ -4,7 +4,7 @@
{ {
public override void Destroy(Farmland self) public override void Destroy(Farmland self)
{ {
self.Parent.Parent.GetParent<Unit>().RemoveFarmland(self); self.Parent.Parent.GetParent<Unit>().RemoveGrandChild(self.Id);
} }
} }

2
Unity/Codes/Hotfix/Demo/Operate/ConstructOperate.cs

@ -179,7 +179,7 @@ namespace ET
{ {
var farmland = build.AddChild<Farmland>(); var farmland = build.AddChild<Farmland>();
farmland.FarmlandState = FarmlandState.FARMLAND_STATE_FREE; farmland.FarmlandState = FarmlandState.FARMLAND_STATE_FREE;
unit.FarmlandDic[farmland.Id] = farmland; unit.AddGrandChild(farmland);
} }
} }

65
Unity/Codes/Hotfix/Demo/Operate/FarmlandOperate.cs

@ -19,19 +19,20 @@ namespace ET
farmland.PeopleId = people.Id; farmland.PeopleId = people.Id;
return ErrorCode.ERR_Success; return ErrorCode.ERR_Success;
} }
public static int Plant(Unit unit, Farmland farmland,People people)
public static int Plant(Unit unit, Farmland farmland, People people)
{ {
if (farmland.FarmlandState != FarmlandState.FARMLAND_STATE_SEED) if (farmland.FarmlandState != FarmlandState.FARMLAND_STATE_SEED)
{ {
return ErrorCode.ERR_FarmlandNotFree; return ErrorCode.ERR_FarmlandNotFree;
} }
farmland.Plant(); farmland.Plant();
PeopleOperate.ChangeBehave(unit,people, farmland.Id, ConstBehaveType.BEHAVE_IDLE); PeopleOperate.ChangeBehave(unit, people, farmland.Id, ConstBehaveType.BEHAVE_IDLE);
return ErrorCode.ERR_Success; return ErrorCode.ERR_Success;
} }
public static int GoHarvest(Unit unit, Farmland farmland, People people) public static int GoHarvest(Unit unit, Farmland farmland, People people)
{ {
if (farmland.FarmlandState != FarmlandState.FARMLAND_STATE_SEED) if (farmland.FarmlandState != FarmlandState.FARMLAND_STATE_SEED)
@ -45,26 +46,26 @@ namespace ET
farmland.PeopleId = people.Id; farmland.PeopleId = people.Id;
return ErrorCode.ERR_Success; return ErrorCode.ERR_Success;
} }
public static int Harvest(Unit unit, Farmland farmland,People people)
public static int Harvest(Unit unit, Farmland farmland, People people)
{ {
if (farmland.FarmlandState == FarmlandState.FARMLAND_STATE_RIPE)
if (farmland.FarmlandState == FarmlandState.FARMLAND_STATE_RIPE) {
var storeNc = unit.GetComponent<StoreComponent>();
storeNc.Add(farmland.Config.ProductID, farmland.Config.BasicProduction);
if (farmland.Config.ByProduct > 0)
{ {
var storeNc = unit.GetComponent<StoreComponent>(); storeNc.Add(farmland.Config.ByProduct, farmland.Config.ByProductNum);
storeNc.Add(farmland.Config.ProductID, farmland.Config.BasicProduction);
if (farmland.Config.ByProduct > 0)
{
storeNc.Add(farmland.Config.ByProduct, farmland.Config.ByProductNum);
}
storeNc.Add(farmland.CropCfgId, farmland.Config.SeedProduce);
farmland.Harvest();
return ErrorCode.ERR_Success;
} }
PeopleOperate.ChangeBehave(unit,people, farmland.Id, ConstBehaveType.BEHAVE_IDLE);
return ErrorCode.ERR_FarmlandNotRipe; storeNc.Add(farmland.CropCfgId, farmland.Config.SeedProduce);
farmland.Harvest();
return ErrorCode.ERR_Success;
}
PeopleOperate.ChangeBehave(unit, people, farmland.Id, ConstBehaveType.BEHAVE_IDLE);
return ErrorCode.ERR_FarmlandNotRipe;
} }
public static (List<long>, List<int>) FarmlandSeed(Unit unit, List<long> farmlandIds, List<int> CropCfgIds) public static (List<long>, List<int>) FarmlandSeed(Unit unit, List<long> farmlandIds, List<int> CropCfgIds)
@ -73,19 +74,21 @@ namespace ET
List<int> sucCropList = new List<int>(); List<int> sucCropList = new List<int>();
for (int i = 0; i < farmlandIds.Count; i++) for (int i = 0; i < farmlandIds.Count; i++)
{ {
Farmland farmland; Farmland farmland = unit.GetGrandChild<Farmland>(farmlandIds[i]);
if (unit.FarmlandDic.TryGetValue(farmlandIds[i], out farmland)) if (farmland == null)
{
continue;
}
if (farmland.FarmlandState == FarmlandState.FARMLAND_STATE_FREE)
{ {
if (farmland.FarmlandState == FarmlandState.FARMLAND_STATE_FREE) var cropCfg = CropConfigCategory.Instance.Get(CropCfgIds[i]);
var sc = unit.GetComponent<StoreComponent>();
if (sc.Remove(cropCfg.SeedNeed, cropCfg.SeedNum))
{ {
var cropCfg = CropConfigCategory.Instance.Get(CropCfgIds[i]); farmland.Seed(cropCfg.SeedNeed);
var sc = unit.GetComponent<StoreComponent>(); sucCropList.Add(cropCfg.SeedNeed);
if (sc.Remove(cropCfg.SeedNeed, cropCfg.SeedNum)) sucFarmlandList.Add(farmlandIds[i]);
{
farmland.Seed(cropCfg.SeedNeed);
sucCropList.Add(cropCfg.SeedNeed);
sucFarmlandList.Add(farmlandIds[i]);
}
} }
} }
} }

78
Unity/Codes/Hotfix/Demo/Unit/UnitSystem.cs

@ -7,7 +7,6 @@ namespace ET
{ {
public override void Awake(Unit self) public override void Awake(Unit self)
{ {
} }
} }
@ -26,7 +25,7 @@ namespace ET
if (dt > timeRate) if (dt > timeRate)
{ {
int tick = Convert.ToInt32(dt / timeRate); int tick = Convert.ToInt32(dt / timeRate);
self.UpdateTime +=tick* timeRate; self.UpdateTime += tick* timeRate;
UnitSystem.AddTime(self, tick); UnitSystem.AddTime(self, tick);
} }
UnitSystem.Update(self,now); UnitSystem.Update(self,now);
@ -45,7 +44,7 @@ namespace ET
} }
#endif #endif
[FriendClass(typeof(Unit))] [FriendClass(typeof (Unit))]
public static class UnitSystem public static class UnitSystem
{ {
public static void AddTime(this Unit self, int tick) public static void AddTime(this Unit self, int tick)
@ -72,10 +71,9 @@ namespace ET
#endif #endif
} }
public static void Update(this Unit self,long now) public static void Update(this Unit self, long now)
{ {
#if SERVER #if SERVER
var synthesisComponent = self.GetComponent<SynthesisComponent>(); var synthesisComponent = self.GetComponent<SynthesisComponent>();
if (synthesisComponent != null) if (synthesisComponent != null)
{ {
@ -97,7 +95,7 @@ namespace ET
m = m % 60; m = m % 60;
var time = h * 100 + m; var time = h * 100 + m;
// todo 月圆之夜 入夜时间加长 // todo 月圆之夜 入夜时间加长
var nightTime = self.GetNightTime(); var nightTime = self.GetNightTime();
if (time >= nightTime) if (time >= nightTime)
{ {
Log.Info($"----------------day: {self.Day}, nightTime: {nightTime}"); Log.Info($"----------------day: {self.Day}, nightTime: {nightTime}");
@ -107,9 +105,9 @@ namespace ET
self.EventSeed = 1; self.EventSeed = 1;
UnitOperate.NightEvent(self, nightTime, time); UnitOperate.NightEvent(self, nightTime, time);
} }
self.GameTime = time; self.GameTime = time;
if (gameTime>self.GameTime) if (gameTime > self.GameTime)
{ {
//每天长大一岁 //每天长大一岁
self.AddAge(); self.AddAge();
@ -141,7 +139,7 @@ namespace ET
PeopleComponent pc = self.GetComponent<PeopleComponent>(); PeopleComponent pc = self.GetComponent<PeopleComponent>();
foreach (var v in pc.Children.Values) foreach (var v in pc.Children.Values)
{ {
((People)v).InitNumericComponent(); ((People) v).InitNumericComponent();
} }
} }
@ -161,11 +159,11 @@ namespace ET
{ {
return; return;
} }
self.Season = self.SeasonConfig.Id; self.Season = self.SeasonConfig.Id;
#if SERVER #if SERVER
UnitHelper.NofityUpdateValley(self, new List<int> { NumericType.Season }, new List<long> { self.Season }); UnitHelper.NofityUpdateValley(self, new List<int> { NumericType.Season }, new List<long> { self.Season });
#endif #endif
} }
public static void SetEmbattle(this Unit self, Dictionary<int, long> FighterDic) public static void SetEmbattle(this Unit self, Dictionary<int, long> FighterDic)
@ -175,7 +173,6 @@ namespace ET
public static void EnterBattle(this Unit self, long enemyUnitId) public static void EnterBattle(this Unit self, long enemyUnitId)
{ {
} }
public static bool ExitBattle(this Unit self) public static bool ExitBattle(this Unit self)
@ -194,9 +191,10 @@ namespace ET
public static bool CanBorn(this Unit self, int resConfigId) public static bool CanBorn(this Unit self, int resConfigId)
{ {
var resConfig = ResourcesConfigCategory.Instance.Get(resConfigId); var resConfig = ResourcesConfigCategory.Instance.Get(resConfigId);
for(int i=0;i<resConfig.RebornCondition.Length;i++) for (int i = 0; i < resConfig.RebornCondition.Length; i++)
{ {
if (resConfig.RebornCondition[i] == (int)ResourcesRebornConditionEnum.SEASON &&resConfig.RebornConditionParameters[i]!=self.Season) if (resConfig.RebornCondition[i] == (int) ResourcesRebornConditionEnum.SEASON &&
resConfig.RebornConditionParameters[i] != self.Season)
{ {
return false; return false;
} }
@ -208,9 +206,10 @@ namespace ET
public static bool CanReborn(this Unit self, int resConfigId) public static bool CanReborn(this Unit self, int resConfigId)
{ {
var resConfig = ResourcesConfigCategory.Instance.Get(resConfigId); var resConfig = ResourcesConfigCategory.Instance.Get(resConfigId);
for(int i=0;i<resConfig.RebornCondition.Length;i++) for (int i = 0; i < resConfig.RebornCondition.Length; i++)
{ {
if (resConfig.RebornCondition[i] == (int)ResourcesRebornConditionEnum.SEASON &&resConfig.RebornConditionParameters[i]!=self.Season) if (resConfig.RebornCondition[i] == (int) ResourcesRebornConditionEnum.SEASON &&
resConfig.RebornConditionParameters[i] != self.Season)
{ {
return false; return false;
} }
@ -225,21 +224,18 @@ namespace ET
self.GetComponent<BuildingComponent>()?.DurableReduce(); self.GetComponent<BuildingComponent>()?.DurableReduce();
} }
public static void AddFarmland(this Unit self,Farmland farmland)
{
self.FarmlandDic[farmland.Id] = farmland;
}
public static void RemoveFarmland(this Unit self, Farmland farmland)
{
self.FarmlandDic.Remove(farmland.Id);
}
public static void UpdateGameTime(this Unit self) public static void UpdateGameTime(this Unit self)
{ {
foreach (var v in self.FarmlandDic.Values) //update farmland
foreach (var v in self.GrandChildren.Values)
{ {
((Farmland)v).Update(); if (v.GetType() == typeof (Farmland))
{
((Farmland) v).Update();
}
} }
} }
@ -258,17 +254,35 @@ namespace ET
{ {
return; return;
} }
foreach (var v in self.GetComponent<BuildingComponent>().Children.Values) foreach (var v in self.GetComponent<BuildingComponent>().Children.Values)
{ {
foreach (var c in v.Children.Values) foreach (var c in v.Children.Values)
{ {
if (c.GetType() == typeof (Farmland)) self.AddGrandChild(c);
{
self.FarmlandDic[c.Id] = (Farmland) c;
}
} }
} }
} }
public static T GetGrandChild<T>(this Unit self, long id) where T : Entity
{
Entity child;
if (self.GrandChildren.TryGetValue(id, out child))
{
return (T) child;
}
return null;
}
public static void AddGrandChild<T>(this Unit self, T entity) where T : Entity
{
self.GrandChildren[entity.Id] = entity;
}
public static void RemoveGrandChild(this Unit self,long id)
{
self.GrandChildren.Remove(id);
}
} }
} }

2
Unity/Codes/Model/Demo/Unit/Unit.cs

@ -69,7 +69,7 @@ namespace ET
public long BattleId; public long BattleId;
[BsonIgnore] [BsonIgnore]
public Dictionary<long, Farmland> FarmlandDic = new Dictionary<long, Farmland>(); public Dictionary<long, Entity> GrandChildren = new Dictionary<long, Entity>();

Loading…
Cancel
Save