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.
80 lines
2.4 KiB
80 lines
2.4 KiB
using UnityEngine; |
|
|
|
namespace ET |
|
{ |
|
[FriendClass(typeof(Building))] |
|
public static class BuildingSystem |
|
{ |
|
public static void FromMessage(this Building self, BuildingProto buildingProto) |
|
{ |
|
self.Id = buildingProto.Id; |
|
self.Position = new Vector2(buildingProto.X, buildingProto.Y); |
|
self.ConfigId = buildingProto.ConfigId; |
|
self.Durable = buildingProto.Durable; |
|
self.IsUpgrade = buildingProto.IsUpgrade; |
|
if (buildingProto.Farmland != null) |
|
{ |
|
var farmland = self.AddChildWithId<Farmland>(buildingProto.Farmland.Id); |
|
farmland.FromMessage(buildingProto.Farmland); |
|
} |
|
|
|
if (buildingProto.Cabin != null) |
|
{ |
|
var cabin = self.AddChildWithId<Cabin>(buildingProto.Cabin.Id); |
|
cabin.FromMessage(buildingProto.Cabin); |
|
} |
|
} |
|
|
|
public static BuildingProto ToMessage(this Building self) |
|
{ |
|
var proto= new BuildingProto() { Id = self.Id, |
|
ConfigId = self.ConfigId, |
|
X = self.Position.x, |
|
Y = self.Position.y, |
|
Durable = self.Durable, |
|
IsUpgrade = self.IsUpgrade |
|
}; |
|
if (self.Config.Special == (int) StructureSpecialEnum.FARMLAND) |
|
{ |
|
foreach (var v in self.Children.Values) |
|
{ |
|
if (v.GetType() == typeof (Farmland)) |
|
{ |
|
proto.Farmland = ((Farmland) v).ToMessage(); |
|
} |
|
} |
|
} |
|
|
|
if (self.Config.Special == (int) StructureSpecialEnum.FARM_CABIN) |
|
{ |
|
foreach (var v in self.Children.Values) |
|
{ |
|
if (v.GetType() == typeof (Cabin)) |
|
{ |
|
proto.Cabin = ((Cabin) v).ToMessage(); |
|
} |
|
} |
|
} |
|
|
|
return proto; |
|
} |
|
|
|
public static void DurableReduce(this Building self) |
|
{ |
|
if (self.Durable == 0) |
|
{ |
|
return; |
|
} |
|
self.Durable -= self.Config.DurableCoefficient; |
|
if (self.Durable < 0) |
|
{ |
|
self.Durable = 0; |
|
} |
|
} |
|
|
|
public static void Repair(this Building self) |
|
{ |
|
self.Durable = self.Config.Durable; |
|
} |
|
} |
|
} |