using System.Collections.Generic; using System.Linq; using UnityEngine; namespace ET { [FriendClass(typeof (ResourcePoint))] public static class ResourcePointSystem { public static void FromMessage(this ResourcePoint self, ResourceProto resourceProto) { self.Id = resourceProto.Id; self.ConfigId = resourceProto.ConfigId; self.Position = new Vector2(resourceProto.X, resourceProto.Y); self.MatureState = resourceProto.MatureState; self.MonsterGroupId = resourceProto.MonsterGroupId; self.ResAttriDic = new Dictionary(); foreach (var attri in resourceProto.resAttriList) { var resAttri = new ResourceAttri { ConfigId = attri.ConfigId, GatherAmount = attri.CurrAmount, MaxAmount = attri.MaxAmount }; self.ResAttriDic[resAttri.ConfigId] = resAttri; } } public static ResourceProto ToMessage(this ResourcePoint self) { var proto = new ResourceProto(); proto.Id = self.Id; proto.ConfigId = self.ConfigId; proto.X = self.Position.x; proto.Y = self.Position.y; proto.MatureState = self.MatureState; proto.MonsterGroupId = self.MonsterGroupId; foreach (var attri in self.ResAttriDic.Values) { var attriProto = new ResAttriProto { ConfigId = attri.ConfigId, CurrAmount = attri.GatherAmount, MaxAmount = attri.MaxAmount }; proto.resAttriList.Add(attriProto); } return proto; } public static (bool, int) Product(this ResourcePoint self, int resConfigId, int amount) { var resAttri = self.ResAttriDic[resConfigId]; resAttri.GatherAmount += amount; var result = false; if (resAttri.GatherAmount >= resAttri.MaxAmount) { amount -= resAttri.GatherAmount - resAttri.MaxAmount; resAttri.GatherAmount = resAttri.MaxAmount; result = true; } self.ResAttriDic[resConfigId] = resAttri; return (result, amount); } public static bool HaveResource(this ResourcePoint self, int resConfigId) { if (self.ResAttriDic.Keys.Contains(resConfigId)) { return self.ResAttriDic[resConfigId].MaxAmount > self.ResAttriDic[resConfigId].GatherAmount; } return false; } public static void UpdateResAttri(this ResourcePoint self,int configId, int amount) { if (self.ResAttriDic.Keys.Contains(configId)) { var resAttri = self.ResAttriDic[configId]; resAttri.GatherAmount = amount; self.ResAttriDic[configId] = resAttri; } } } }