From 1243345eafa58fa6afa0e78d30731268db1caba9 Mon Sep 17 00:00:00 2001 From: wserver/wangdisen <1065498738@qq.com> Date: Thu, 18 Aug 2022 10:43:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9unit=E7=9A=84=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E5=99=A8=EF=BC=8C=E5=8E=BB=E6=8E=89farmlanddic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Handler/C2M_FarmlandHarvestHandler.cs | 4 +- .../Handler/C2M_FarmlandPlantHandler.cs | 4 +- .../Handler/C2M_GoFarmlandHarvestHandler.cs | 4 +- .../Handler/C2M_GoFarmlandPlantHandler.cs | 4 +- .../Hotfix/Demo/Farmland/FarmlandSystem.cs | 2 +- .../Hotfix/Demo/Operate/ConstructOperate.cs | 2 +- .../Hotfix/Demo/Operate/FarmlandOperate.cs | 65 ++++++++-------- Unity/Codes/Hotfix/Demo/Unit/UnitSystem.cs | 78 +++++++++++-------- Unity/Codes/Model/Demo/Unit/Unit.cs | 2 +- 9 files changed, 91 insertions(+), 74 deletions(-) diff --git a/Server/Hotfix/Demo/Farmland/Handler/C2M_FarmlandHarvestHandler.cs b/Server/Hotfix/Demo/Farmland/Handler/C2M_FarmlandHarvestHandler.cs index 52c75f2..80cbf3a 100644 --- a/Server/Hotfix/Demo/Farmland/Handler/C2M_FarmlandHarvestHandler.cs +++ b/Server/Hotfix/Demo/Farmland/Handler/C2M_FarmlandHarvestHandler.cs @@ -11,8 +11,8 @@ namespace ET try { People people; - Farmland farmland; - if (unit.FarmlandDic.TryGetValue(request.FarmlandId, out farmland)) + Farmland farmland = unit.GetGrandChild(request.FarmlandId); + if (farmland!=null) { people = unit.GetComponent().GetChild(request.PeopleId); if (people != null) diff --git a/Server/Hotfix/Demo/Farmland/Handler/C2M_FarmlandPlantHandler.cs b/Server/Hotfix/Demo/Farmland/Handler/C2M_FarmlandPlantHandler.cs index 8aeb717..9c7cec1 100644 --- a/Server/Hotfix/Demo/Farmland/Handler/C2M_FarmlandPlantHandler.cs +++ b/Server/Hotfix/Demo/Farmland/Handler/C2M_FarmlandPlantHandler.cs @@ -10,8 +10,8 @@ namespace ET try { People people; - Farmland farmland; - if (unit.FarmlandDic.TryGetValue(request.FarmlandId, out farmland)) + Farmland farmland = unit.GetGrandChild(request.FarmlandId); + if (farmland!=null) { people = unit.GetComponent().GetChild(request.PeopleId); if (people != null) diff --git a/Server/Hotfix/Demo/Farmland/Handler/C2M_GoFarmlandHarvestHandler.cs b/Server/Hotfix/Demo/Farmland/Handler/C2M_GoFarmlandHarvestHandler.cs index 702a8fd..5aae91b 100644 --- a/Server/Hotfix/Demo/Farmland/Handler/C2M_GoFarmlandHarvestHandler.cs +++ b/Server/Hotfix/Demo/Farmland/Handler/C2M_GoFarmlandHarvestHandler.cs @@ -18,8 +18,8 @@ namespace ET } else { - Farmland farmland; - if (unit.FarmlandDic.TryGetValue(request.FarmlandId, out farmland)) + Farmland farmland = unit.GetGrandChild(request.FarmlandId); + if (farmland!=null) { response.Error=FarmlandOperate.GoHarvest(unit,farmland, people); } diff --git a/Server/Hotfix/Demo/Farmland/Handler/C2M_GoFarmlandPlantHandler.cs b/Server/Hotfix/Demo/Farmland/Handler/C2M_GoFarmlandPlantHandler.cs index 382dabb..8af0f90 100644 --- a/Server/Hotfix/Demo/Farmland/Handler/C2M_GoFarmlandPlantHandler.cs +++ b/Server/Hotfix/Demo/Farmland/Handler/C2M_GoFarmlandPlantHandler.cs @@ -18,8 +18,8 @@ namespace ET } else { - Farmland farmland; - if (unit.FarmlandDic.TryGetValue(request.FarmlandId, out farmland)) + Farmland farmland = unit.GetGrandChild(request.FarmlandId); + if (farmland!=null) { response.Error = FarmlandOperate.GoPlant(unit, farmland, people); } diff --git a/Unity/Codes/Hotfix/Demo/Farmland/FarmlandSystem.cs b/Unity/Codes/Hotfix/Demo/Farmland/FarmlandSystem.cs index 543c945..506e4be 100644 --- a/Unity/Codes/Hotfix/Demo/Farmland/FarmlandSystem.cs +++ b/Unity/Codes/Hotfix/Demo/Farmland/FarmlandSystem.cs @@ -4,7 +4,7 @@ { public override void Destroy(Farmland self) { - self.Parent.Parent.GetParent().RemoveFarmland(self); + self.Parent.Parent.GetParent().RemoveGrandChild(self.Id); } } diff --git a/Unity/Codes/Hotfix/Demo/Operate/ConstructOperate.cs b/Unity/Codes/Hotfix/Demo/Operate/ConstructOperate.cs index 5382180..8cd34e6 100644 --- a/Unity/Codes/Hotfix/Demo/Operate/ConstructOperate.cs +++ b/Unity/Codes/Hotfix/Demo/Operate/ConstructOperate.cs @@ -179,7 +179,7 @@ namespace ET { var farmland = build.AddChild(); farmland.FarmlandState = FarmlandState.FARMLAND_STATE_FREE; - unit.FarmlandDic[farmland.Id] = farmland; + unit.AddGrandChild(farmland); } } diff --git a/Unity/Codes/Hotfix/Demo/Operate/FarmlandOperate.cs b/Unity/Codes/Hotfix/Demo/Operate/FarmlandOperate.cs index 6919ae6..d953fd1 100644 --- a/Unity/Codes/Hotfix/Demo/Operate/FarmlandOperate.cs +++ b/Unity/Codes/Hotfix/Demo/Operate/FarmlandOperate.cs @@ -19,19 +19,20 @@ namespace ET farmland.PeopleId = people.Id; 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) { return ErrorCode.ERR_FarmlandNotFree; } + farmland.Plant(); - PeopleOperate.ChangeBehave(unit,people, farmland.Id, ConstBehaveType.BEHAVE_IDLE); + PeopleOperate.ChangeBehave(unit, people, farmland.Id, ConstBehaveType.BEHAVE_IDLE); return ErrorCode.ERR_Success; } - + public static int GoHarvest(Unit unit, Farmland farmland, People people) { if (farmland.FarmlandState != FarmlandState.FARMLAND_STATE_SEED) @@ -45,26 +46,26 @@ namespace ET farmland.PeopleId = people.Id; 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(); + storeNc.Add(farmland.Config.ProductID, farmland.Config.BasicProduction); + if (farmland.Config.ByProduct > 0) { - var storeNc = unit.GetComponent(); - 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; + storeNc.Add(farmland.Config.ByProduct, farmland.Config.ByProductNum); } - 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, List) FarmlandSeed(Unit unit, List farmlandIds, List CropCfgIds) @@ -73,19 +74,21 @@ namespace ET List sucCropList = new List(); for (int i = 0; i < farmlandIds.Count; i++) { - Farmland farmland; - if (unit.FarmlandDic.TryGetValue(farmlandIds[i], out farmland)) + Farmland farmland = unit.GetGrandChild(farmlandIds[i]); + 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(); + if (sc.Remove(cropCfg.SeedNeed, cropCfg.SeedNum)) { - var cropCfg = CropConfigCategory.Instance.Get(CropCfgIds[i]); - var sc = unit.GetComponent(); - if (sc.Remove(cropCfg.SeedNeed, cropCfg.SeedNum)) - { - farmland.Seed(cropCfg.SeedNeed); - sucCropList.Add(cropCfg.SeedNeed); - sucFarmlandList.Add(farmlandIds[i]); - } + farmland.Seed(cropCfg.SeedNeed); + sucCropList.Add(cropCfg.SeedNeed); + sucFarmlandList.Add(farmlandIds[i]); } } } diff --git a/Unity/Codes/Hotfix/Demo/Unit/UnitSystem.cs b/Unity/Codes/Hotfix/Demo/Unit/UnitSystem.cs index af7f662..a1a514d 100644 --- a/Unity/Codes/Hotfix/Demo/Unit/UnitSystem.cs +++ b/Unity/Codes/Hotfix/Demo/Unit/UnitSystem.cs @@ -7,7 +7,6 @@ namespace ET { public override void Awake(Unit self) { - } } @@ -26,7 +25,7 @@ namespace ET if (dt > timeRate) { int tick = Convert.ToInt32(dt / timeRate); - self.UpdateTime +=tick* timeRate; + self.UpdateTime += tick* timeRate; UnitSystem.AddTime(self, tick); } UnitSystem.Update(self,now); @@ -45,7 +44,7 @@ namespace ET } #endif - [FriendClass(typeof(Unit))] + [FriendClass(typeof (Unit))] public static class UnitSystem { public static void AddTime(this Unit self, int tick) @@ -72,10 +71,9 @@ namespace ET #endif } - public static void Update(this Unit self,long now) + public static void Update(this Unit self, long now) { #if SERVER - var synthesisComponent = self.GetComponent(); if (synthesisComponent != null) { @@ -97,7 +95,7 @@ namespace ET m = m % 60; var time = h * 100 + m; // todo 月圆之夜 入夜时间加长 - var nightTime = self.GetNightTime(); + var nightTime = self.GetNightTime(); if (time >= nightTime) { Log.Info($"----------------day: {self.Day}, nightTime: {nightTime}"); @@ -107,9 +105,9 @@ namespace ET self.EventSeed = 1; UnitOperate.NightEvent(self, nightTime, time); } - + self.GameTime = time; - if (gameTime>self.GameTime) + if (gameTime > self.GameTime) { //每天长大一岁 self.AddAge(); @@ -141,7 +139,7 @@ namespace ET PeopleComponent pc = self.GetComponent(); foreach (var v in pc.Children.Values) { - ((People)v).InitNumericComponent(); + ((People) v).InitNumericComponent(); } } @@ -161,11 +159,11 @@ namespace ET { return; } + self.Season = self.SeasonConfig.Id; #if SERVER - UnitHelper.NofityUpdateValley(self, new List { NumericType.Season }, new List { self.Season }); + UnitHelper.NofityUpdateValley(self, new List { NumericType.Season }, new List { self.Season }); #endif - } public static void SetEmbattle(this Unit self, Dictionary FighterDic) @@ -175,7 +173,6 @@ namespace ET public static void EnterBattle(this Unit self, long enemyUnitId) { - } public static bool ExitBattle(this Unit self) @@ -194,9 +191,10 @@ namespace ET public static bool CanBorn(this Unit self, int resConfigId) { var resConfig = ResourcesConfigCategory.Instance.Get(resConfigId); - for(int i=0;i()?.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) { - 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; } + foreach (var v in self.GetComponent().Children.Values) { foreach (var c in v.Children.Values) { - if (c.GetType() == typeof (Farmland)) - { - self.FarmlandDic[c.Id] = (Farmland) c; - } + self.AddGrandChild(c); } } } + + public static T GetGrandChild(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(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); + } } - } \ No newline at end of file diff --git a/Unity/Codes/Model/Demo/Unit/Unit.cs b/Unity/Codes/Model/Demo/Unit/Unit.cs index ef9322b..567cd20 100644 --- a/Unity/Codes/Model/Demo/Unit/Unit.cs +++ b/Unity/Codes/Model/Demo/Unit/Unit.cs @@ -69,7 +69,7 @@ namespace ET public long BattleId; [BsonIgnore] - public Dictionary FarmlandDic = new Dictionary(); + public Dictionary GrandChildren = new Dictionary();