MapEntityPrefabs are deserialized using SerializableProperty

This commit is contained in:
Joonas Rikkonen
2017-11-14 23:12:55 +02:00
parent 04e3710a65
commit 08cf902cb8
6 changed files with 94 additions and 115 deletions
@@ -23,28 +23,15 @@ namespace Barotrauma
{ {
//static string contentFolder = "Content/Items/"; //static string contentFolder = "Content/Items/";
string configFile; private readonly string configFile;
//should the camera focus on the construction when selected
protected bool focusOnSelected;
//the amount of "camera offset" when selecting the construction
protected float offsetOnSelected;
//default size //default size
protected Vector2 size; protected Vector2 size;
//how close the Character has to be to the item to pick it up
private float interactDistance;
// this can be used to allow items which are behind other items tp
private float interactPriority;
private bool interactThroughWalls;
//an area next to the construction //an area next to the construction
//the construction can be Activated() by a Character inside the area //the construction can be Activated() by a Character inside the area
public List<Rectangle> Triggers; public List<Rectangle> Triggers;
public readonly bool FireProof;
private float impactTolerance; private float impactTolerance;
public string ConfigFile public string ConfigFile
@@ -72,54 +59,75 @@ namespace Barotrauma
private set; private set;
} }
//how close the Character has to be to the item to pick it up
[Serialize(120.0f, false)]
public float InteractDistance public float InteractDistance
{ {
get { return interactDistance; } get;
private set;
} }
// this can be used to allow items which are behind other items tp
[Serialize(0.0f, false)]
public float InteractPriority public float InteractPriority
{ {
get { return interactPriority; } get;
private set;
} }
[Serialize(false, false)]
public bool InteractThroughWalls public bool InteractThroughWalls
{ {
get { return interactThroughWalls; } get;
private set;
} }
public override bool IsLinkable
{
get { return isLinkable; }
}
//should the camera focus on the item when selected
[Serialize(false, false)]
public bool FocusOnSelected public bool FocusOnSelected
{ {
get { return focusOnSelected; } get;
private set;
} }
//the amount of "camera offset" when selecting the construction
[Serialize(0.0f, false)]
public float OffsetOnSelected public float OffsetOnSelected
{ {
get { return offsetOnSelected; } get;
private set;
} }
[Serialize(100.0f, false)]
public float Health public float Health
{ {
get; get;
private set; private set;
} }
[Serialize(false, false)]
public bool Indestructible public bool Indestructible
{ {
get; get;
private set; private set;
} }
[Serialize(false, false)]
public bool FireProof
{
get;
private set;
}
[Serialize(0.0f, false)]
public float ImpactTolerance public float ImpactTolerance
{ {
get { return impactTolerance; } get { return impactTolerance; }
set { impactTolerance = Math.Max(value, 0.0f); } set { impactTolerance = Math.Max(value, 0.0f); }
} }
[Serialize(false, false)]
public bool CanUseOnSelf public bool CanUseOnSelf
{ {
get; get;
@@ -231,7 +239,7 @@ namespace Barotrauma
} }
} }
public ItemPrefab (XElement element, string filePath) public ItemPrefab(XElement element, string filePath)
{ {
configFile = filePath; configFile = filePath;
ConfigElement = element; ConfigElement = element;
@@ -241,28 +249,6 @@ namespace Barotrauma
DebugConsole.Log(" " + name); DebugConsole.Log(" " + name);
Description = element.GetAttributeString("description", "");
interactThroughWalls = element.GetAttributeBool("interactthroughwalls", false);
interactDistance = element.GetAttributeFloat("interactdistance", 120.0f); // Default to 120 as the new item picking method is tuned to this number
interactPriority = element.GetAttributeFloat("interactpriority", 0.0f);
isLinkable = element.GetAttributeBool("linkable", false);
ResizeHorizontal = element.GetAttributeBool("resizehorizontal", false);
ResizeVertical = element.GetAttributeBool("resizevertical", false);
focusOnSelected = element.GetAttributeBool("focusonselected", false);
offsetOnSelected = element.GetAttributeFloat("offsetonselected", 0.0f);
CanUseOnSelf = element.GetAttributeBool("canuseonself", false);
Health = element.GetAttributeFloat("health", 100.0f);
Indestructible = element.GetAttributeBool("indestructible", false);
FireProof = element.GetAttributeBool("fireproof", false);
ImpactTolerance = element.GetAttributeFloat("impacttolerance", 0.0f);
string aliases = element.GetAttributeString("aliases", ""); string aliases = element.GetAttributeString("aliases", "");
if (!string.IsNullOrWhiteSpace(aliases)) if (!string.IsNullOrWhiteSpace(aliases))
{ {
@@ -270,26 +256,21 @@ namespace Barotrauma
} }
MapEntityCategory category; MapEntityCategory category;
if (!Enum.TryParse(element.GetAttributeString("category", "Misc"), true, out category)) if (!Enum.TryParse(element.GetAttributeString("category", "Misc"), true, out category))
{ {
category = MapEntityCategory.Misc; category = MapEntityCategory.Misc;
} }
Category = category; Category = category;
SpriteColor = element.GetAttributeColor("spritecolor", Color.White);
price = element.GetAttributeInt("price", 0);
Triggers = new List<Rectangle>(); Triggers = new List<Rectangle>();
DeconstructItems = new List<DeconstructItem>(); DeconstructItems = new List<DeconstructItem>();
DeconstructTime = 1.0f; DeconstructTime = 1.0f;
Tags = new List<string>(); Tags = new List<string>();
Tags.AddRange(element.GetAttributeString("tags", "").Split(',')); Tags.AddRange(element.GetAttributeString("tags", "").Split(','));
SerializableProperty.DeserializeProperties(this, element);
foreach (XElement subElement in element.Elements()) foreach (XElement subElement in element.Elements())
{ {
switch (subElement.Name.ToString().ToLowerInvariant()) switch (subElement.Name.ToString().ToLowerInvariant())
@@ -105,7 +105,7 @@ namespace Barotrauma
List<Vector2> points = new List<Vector2>(); List<Vector2> points = new List<Vector2>();
var wallPrefabs = var wallPrefabs =
MapEntityPrefab.List.FindAll(mp => (mp is StructurePrefab) && ((StructurePrefab)mp).HasBody); MapEntityPrefab.List.FindAll(mp => (mp is StructurePrefab) && ((StructurePrefab)mp).Body);
foreach (XElement element in rootElement.Elements()) foreach (XElement element in rootElement.Elements())
{ {
@@ -17,15 +17,7 @@ namespace Barotrauma
public readonly static List<MapEntityPrefab> List = new List<MapEntityPrefab>(); public readonly static List<MapEntityPrefab> List = new List<MapEntityPrefab>();
protected string name; protected string name;
public List<string> Tags
{
get;
protected set;
}
protected bool isLinkable;
public Sprite sprite; public Sprite sprite;
//the position where the structure is being placed (needed when stretching the structure) //the position where the structure is being placed (needed when stretching the structure)
@@ -34,35 +26,45 @@ namespace Barotrauma
protected ConstructorInfo constructor; protected ConstructorInfo constructor;
//is it possible to stretch the entity horizontally/vertically //is it possible to stretch the entity horizontally/vertically
[Serialize(false, false)]
public bool ResizeHorizontal { get; protected set; } public bool ResizeHorizontal { get; protected set; }
[Serialize(false, false)]
public bool ResizeVertical { get; protected set; } public bool ResizeVertical { get; protected set; }
//which prefab has been selected for placing //which prefab has been selected for placing
protected static MapEntityPrefab selected; protected static MapEntityPrefab selected;
protected int price; private int price;
public string Name public string Name
{ {
get { return name; } get { return name; }
} }
public List<string> Tags
{
get;
protected set;
}
public static MapEntityPrefab Selected public static MapEntityPrefab Selected
{ {
get { return selected; } get { return selected; }
set { selected = value; } set { selected = value; }
} }
[Serialize("", false)]
public string Description public string Description
{ {
get; get;
protected set; protected set;
} }
[Serialize(false, false)]
public virtual bool IsLinkable public virtual bool IsLinkable
{ {
get { return isLinkable; } get;
private set;
} }
public MapEntityCategory Category public MapEntityCategory Category
@@ -71,15 +73,18 @@ namespace Barotrauma
protected set; protected set;
} }
[Serialize("1.0,1.0,1.0,1.0", false)]
public Color SpriteColor public Color SpriteColor
{ {
get; get;
protected set; protected set;
} }
[Serialize(0, false)]
public int Price public int Price
{ {
get { return price; } get { return price; }
protected set { price = Math.Max(value, 0); }
} }
//If a matching prefab is not found when loading a sub, the game will attempt to find a prefab with a matching alias. //If a matching prefab is not found when loading a sub, the game will attempt to find a prefab with a matching alias.
@@ -70,7 +70,7 @@ namespace Barotrauma
public bool IsPlatform public bool IsPlatform
{ {
get { return prefab.IsPlatform; } get { return prefab.Platform; }
} }
public Direction StairDirection public Direction StairDirection
@@ -86,7 +86,7 @@ namespace Barotrauma
public bool HasBody public bool HasBody
{ {
get { return prefab.HasBody; } get { return prefab.Body; }
} }
public bool CastShadow public bool CastShadow
@@ -129,7 +129,7 @@ namespace Barotrauma
{ {
get get
{ {
return prefab.HasBody; return prefab.Body;
} }
} }
@@ -156,7 +156,7 @@ namespace Barotrauma
{ {
Rectangle oldRect = Rect; Rectangle oldRect = Rect;
base.Rect = value; base.Rect = value;
if (prefab.HasBody) CreateSections(); if (prefab.Body) CreateSections();
else else
{ {
foreach (WallSection sec in sections) foreach (WallSection sec in sections)
@@ -227,7 +227,7 @@ namespace Barotrauma
SerializableProperties = SerializableProperty.GetProperties(this); SerializableProperties = SerializableProperty.GetProperties(this);
if (prefab.HasBody) if (prefab.Body)
{ {
bodies = new List<Body>(); bodies = new List<Body>();
//gaps = new List<Gap>(); //gaps = new List<Gap>();
@@ -241,7 +241,7 @@ namespace Barotrauma
newBody.Friction = 0.5f; newBody.Friction = 0.5f;
newBody.OnCollision += OnWallCollision; newBody.OnCollision += OnWallCollision;
newBody.UserData = this; newBody.UserData = this;
newBody.CollisionCategories = (prefab.IsPlatform) ? Physics.CollisionPlatform : Physics.CollisionWall; newBody.CollisionCategories = (prefab.Platform) ? Physics.CollisionPlatform : Physics.CollisionWall;
bodies.Add(newBody); bodies.Add(newBody);
@@ -480,7 +480,7 @@ namespace Barotrauma
private bool OnWallCollision(Fixture f1, Fixture f2, Contact contact) private bool OnWallCollision(Fixture f1, Fixture f2, Contact contact)
{ {
if (prefab.IsPlatform) if (prefab.Platform)
{ {
Limb limb; Limb limb;
if ((limb = f2.Body.UserData as Limb) != null) if ((limb = f2.Body.UserData as Limb) != null)
@@ -495,7 +495,7 @@ namespace Barotrauma
if (character.DisableImpactDamageTimer > 0.0f || ((Limb)f2.Body.UserData).Mass < 100.0f) return true; if (character.DisableImpactDamageTimer > 0.0f || ((Limb)f2.Body.UserData).Mass < 100.0f) return true;
} }
if (!prefab.IsPlatform && prefab.StairDirection == Direction.None) if (!prefab.Platform && prefab.StairDirection == Direction.None)
{ {
Vector2 pos = ConvertUnits.ToDisplayUnits(f2.Body.Position); Vector2 pos = ConvertUnits.ToDisplayUnits(f2.Body.Position);
@@ -557,7 +557,7 @@ namespace Barotrauma
public void AddDamage(int sectionIndex, float damage) public void AddDamage(int sectionIndex, float damage)
{ {
if (!prefab.HasBody || prefab.IsPlatform) return; if (!prefab.Body || prefab.Platform) return;
if (sectionIndex < 0 || sectionIndex > sections.Length - 1) return; if (sectionIndex < 0 || sectionIndex > sections.Length - 1) return;
@@ -625,7 +625,7 @@ namespace Barotrauma
public AttackResult AddDamage(IDamageable attacker, Vector2 worldPosition, Attack attack, float deltaTime, bool playSound = false) public AttackResult AddDamage(IDamageable attacker, Vector2 worldPosition, Attack attack, float deltaTime, bool playSound = false)
{ {
if (Submarine != null && Submarine.GodMode) return new AttackResult(0.0f, 0.0f); if (Submarine != null && Submarine.GodMode) return new AttackResult(0.0f, 0.0f);
if (!prefab.HasBody || prefab.IsPlatform) return new AttackResult(0.0f, 0.0f); if (!prefab.Body || prefab.Platform) return new AttackResult(0.0f, 0.0f);
Vector2 transformedPos = worldPosition; Vector2 transformedPos = worldPosition;
if (Submarine != null) transformedPos -= Submarine.Position; if (Submarine != null) transformedPos -= Submarine.Position;
@@ -653,7 +653,7 @@ namespace Barotrauma
private void SetDamage(int sectionIndex, float damage) private void SetDamage(int sectionIndex, float damage)
{ {
if (Submarine != null && Submarine.GodMode) return; if (Submarine != null && Submarine.GodMode) return;
if (!prefab.HasBody) return; if (!prefab.Body) return;
if (!MathUtils.IsValid(damage)) return; if (!MathUtils.IsValid(damage)) return;
@@ -8,13 +8,6 @@ namespace Barotrauma
{ {
partial class StructurePrefab : MapEntityPrefab partial class StructurePrefab : MapEntityPrefab
{ {
//does the structure have a physics body
private bool hasBody;
private bool castShadow;
private bool isPlatform;
private Direction stairDirection;
private bool canSpriteFlipX; private bool canSpriteFlipX;
private float maxHealth; private float maxHealth;
@@ -22,29 +15,40 @@ namespace Barotrauma
//default size //default size
private Vector2 size; private Vector2 size;
public bool HasBody //does the structure have a physics body
[Serialize(false, false)]
public bool Body
{ {
get { return hasBody; } get;
private set;
} }
public bool IsPlatform [Serialize(false, false)]
public bool Platform
{ {
get { return isPlatform; } get;
private set;
} }
[Serialize(100.0f, false)]
public float MaxHealth public float MaxHealth
{ {
get { return maxHealth; } get { return maxHealth; }
set { maxHealth = Math.Max(value, 0.0f); }
} }
[Serialize(false, false)]
public bool CastShadow public bool CastShadow
{ {
get { return castShadow; } get;
private set;
} }
[Serialize(Direction.None, false)]
public Direction StairDirection public Direction StairDirection
{ {
get { return stairDirection; } get;
private set;
} }
public bool CanSpriteFlipX public bool CanSpriteFlipX
@@ -52,9 +56,11 @@ namespace Barotrauma
get { return canSpriteFlipX; } get { return canSpriteFlipX; }
} }
[Serialize("0,0", true)]
public Vector2 Size public Vector2 Size
{ {
get { return size; } get { return size; }
private set { size = value; }
} }
public Sprite BackgroundSprite public Sprite BackgroundSprite
@@ -115,41 +121,28 @@ namespace Barotrauma
} }
MapEntityCategory category; MapEntityCategory category;
if (!Enum.TryParse(element.GetAttributeString("category", "Structure"), true, out category)) if (!Enum.TryParse(element.GetAttributeString("category", "Structure"), true, out category))
{ {
category = MapEntityCategory.Structure; category = MapEntityCategory.Structure;
} }
sp.Category = category; sp.Category = category;
sp.Description = element.GetAttributeString("description", "");
string aliases = element.GetAttributeString("aliases", ""); string aliases = element.GetAttributeString("aliases", "");
if (!string.IsNullOrWhiteSpace(aliases)) if (!string.IsNullOrWhiteSpace(aliases))
{ {
sp.Aliases = aliases.Split(','); sp.Aliases = aliases.Split(',');
} }
sp.size = Vector2.Zero;
sp.size.X = element.GetAttributeFloat("width", 0.0f);
sp.size.Y = element.GetAttributeFloat("height", 0.0f);
string spriteColorStr = element.GetAttributeString("spritecolor", "1.0,1.0,1.0,1.0"); SerializableProperty.DeserializeProperties(sp, element);
sp.SpriteColor = new Color(XMLExtensions.ParseVector4(spriteColorStr));
sp.maxHealth = element.GetAttributeFloat("health", 100.0f); //backwards compatibility
if (element.Attribute("size") == null)
{
sp.size = Vector2.Zero;
sp.size.X = element.GetAttributeFloat("width", 0.0f);
sp.size.Y = element.GetAttributeFloat("height", 0.0f);
}
sp.ResizeHorizontal = element.GetAttributeBool("resizehorizontal", false);
sp.ResizeVertical = element.GetAttributeBool("resizevertical", false);
sp.isPlatform = element.GetAttributeBool("platform", false);
sp.stairDirection = (Direction)Enum.Parse(typeof(Direction), element.GetAttributeString("stairdirection", "None"), true);
sp.castShadow = element.GetAttributeBool("castshadow", false);
sp.hasBody = element.GetAttributeBool("body", false);
return sp; return sp;
} }
@@ -338,8 +338,8 @@ namespace Barotrauma
return dictionary; return dictionary;
} }
public static Dictionary<string, SerializableProperty> DeserializeProperties(ISerializableEntity obj, XElement element) public static Dictionary<string, SerializableProperty> DeserializeProperties(object obj, XElement element)
{ {
var properties = TypeDescriptor.GetProperties(obj.GetType()).Cast<PropertyDescriptor>(); var properties = TypeDescriptor.GetProperties(obj.GetType()).Cast<PropertyDescriptor>();