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

View File

@@ -23,28 +23,15 @@ namespace Barotrauma
{
//static string contentFolder = "Content/Items/";
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;
private readonly string configFile;
//default 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
//the construction can be Activated() by a Character inside the area
public List<Rectangle> Triggers;
public readonly bool FireProof;
private float impactTolerance;
public string ConfigFile
@@ -72,54 +59,75 @@ namespace Barotrauma
private set;
}
//how close the Character has to be to the item to pick it up
[Serialize(120.0f, false)]
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
{
get { return interactPriority; }
get;
private set;
}
[Serialize(false, false)]
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
{
get { return focusOnSelected; }
get;
private set;
}
//the amount of "camera offset" when selecting the construction
[Serialize(0.0f, false)]
public float OffsetOnSelected
{
get { return offsetOnSelected; }
get;
private set;
}
[Serialize(100.0f, false)]
public float Health
{
get;
private set;
}
[Serialize(false, false)]
public bool Indestructible
{
get;
private set;
}
[Serialize(false, false)]
public bool FireProof
{
get;
private set;
}
[Serialize(0.0f, false)]
public float ImpactTolerance
{
get { return impactTolerance; }
set { impactTolerance = Math.Max(value, 0.0f); }
}
[Serialize(false, false)]
public bool CanUseOnSelf
{
get;
@@ -231,7 +239,7 @@ namespace Barotrauma
}
}
public ItemPrefab (XElement element, string filePath)
public ItemPrefab(XElement element, string filePath)
{
configFile = filePath;
ConfigElement = element;
@@ -241,28 +249,6 @@ namespace Barotrauma
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", "");
if (!string.IsNullOrWhiteSpace(aliases))
{
@@ -270,26 +256,21 @@ namespace Barotrauma
}
MapEntityCategory category;
if (!Enum.TryParse(element.GetAttributeString("category", "Misc"), true, out category))
{
category = MapEntityCategory.Misc;
}
Category = category;
SpriteColor = element.GetAttributeColor("spritecolor", Color.White);
price = element.GetAttributeInt("price", 0);
Triggers = new List<Rectangle>();
DeconstructItems = new List<DeconstructItem>();
DeconstructTime = 1.0f;
Tags = new List<string>();
Tags.AddRange(element.GetAttributeString("tags", "").Split(','));
SerializableProperty.DeserializeProperties(this, element);
foreach (XElement subElement in element.Elements())
{
switch (subElement.Name.ToString().ToLowerInvariant())

View File

@@ -105,7 +105,7 @@ namespace Barotrauma
List<Vector2> points = new List<Vector2>();
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())
{

View File

@@ -17,15 +17,7 @@ namespace Barotrauma
public readonly static List<MapEntityPrefab> List = new List<MapEntityPrefab>();
protected string name;
public List<string> Tags
{
get;
protected set;
}
protected bool isLinkable;
public Sprite sprite;
//the position where the structure is being placed (needed when stretching the structure)
@@ -34,35 +26,45 @@ namespace Barotrauma
protected ConstructorInfo constructor;
//is it possible to stretch the entity horizontally/vertically
[Serialize(false, false)]
public bool ResizeHorizontal { get; protected set; }
[Serialize(false, false)]
public bool ResizeVertical { get; protected set; }
//which prefab has been selected for placing
protected static MapEntityPrefab selected;
protected int price;
private int price;
public string Name
{
get { return name; }
}
public List<string> Tags
{
get;
protected set;
}
public static MapEntityPrefab Selected
{
get { return selected; }
set { selected = value; }
}
[Serialize("", false)]
public string Description
{
get;
protected set;
}
[Serialize(false, false)]
public virtual bool IsLinkable
{
get { return isLinkable; }
get;
private set;
}
public MapEntityCategory Category
@@ -71,15 +73,18 @@ namespace Barotrauma
protected set;
}
[Serialize("1.0,1.0,1.0,1.0", false)]
public Color SpriteColor
{
get;
protected set;
}
[Serialize(0, false)]
public int 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.

View File

@@ -70,7 +70,7 @@ namespace Barotrauma
public bool IsPlatform
{
get { return prefab.IsPlatform; }
get { return prefab.Platform; }
}
public Direction StairDirection
@@ -86,7 +86,7 @@ namespace Barotrauma
public bool HasBody
{
get { return prefab.HasBody; }
get { return prefab.Body; }
}
public bool CastShadow
@@ -129,7 +129,7 @@ namespace Barotrauma
{
get
{
return prefab.HasBody;
return prefab.Body;
}
}
@@ -156,7 +156,7 @@ namespace Barotrauma
{
Rectangle oldRect = Rect;
base.Rect = value;
if (prefab.HasBody) CreateSections();
if (prefab.Body) CreateSections();
else
{
foreach (WallSection sec in sections)
@@ -227,7 +227,7 @@ namespace Barotrauma
SerializableProperties = SerializableProperty.GetProperties(this);
if (prefab.HasBody)
if (prefab.Body)
{
bodies = new List<Body>();
//gaps = new List<Gap>();
@@ -241,7 +241,7 @@ namespace Barotrauma
newBody.Friction = 0.5f;
newBody.OnCollision += OnWallCollision;
newBody.UserData = this;
newBody.CollisionCategories = (prefab.IsPlatform) ? Physics.CollisionPlatform : Physics.CollisionWall;
newBody.CollisionCategories = (prefab.Platform) ? Physics.CollisionPlatform : Physics.CollisionWall;
bodies.Add(newBody);
@@ -480,7 +480,7 @@ namespace Barotrauma
private bool OnWallCollision(Fixture f1, Fixture f2, Contact contact)
{
if (prefab.IsPlatform)
if (prefab.Platform)
{
Limb limb;
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 (!prefab.IsPlatform && prefab.StairDirection == Direction.None)
if (!prefab.Platform && prefab.StairDirection == Direction.None)
{
Vector2 pos = ConvertUnits.ToDisplayUnits(f2.Body.Position);
@@ -557,7 +557,7 @@ namespace Barotrauma
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;
@@ -625,7 +625,7 @@ namespace Barotrauma
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 (!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;
if (Submarine != null) transformedPos -= Submarine.Position;
@@ -653,7 +653,7 @@ namespace Barotrauma
private void SetDamage(int sectionIndex, float damage)
{
if (Submarine != null && Submarine.GodMode) return;
if (!prefab.HasBody) return;
if (!prefab.Body) return;
if (!MathUtils.IsValid(damage)) return;

View File

@@ -8,13 +8,6 @@ namespace Barotrauma
{
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 float maxHealth;
@@ -22,29 +15,40 @@ namespace Barotrauma
//default 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
{
get { return maxHealth; }
set { maxHealth = Math.Max(value, 0.0f); }
}
[Serialize(false, false)]
public bool CastShadow
{
get { return castShadow; }
get;
private set;
}
[Serialize(Direction.None, false)]
public Direction StairDirection
{
get { return stairDirection; }
get;
private set;
}
public bool CanSpriteFlipX
@@ -52,9 +56,11 @@ namespace Barotrauma
get { return canSpriteFlipX; }
}
[Serialize("0,0", true)]
public Vector2 Size
{
get { return size; }
private set { size = value; }
}
public Sprite BackgroundSprite
@@ -115,41 +121,28 @@ namespace Barotrauma
}
MapEntityCategory category;
if (!Enum.TryParse(element.GetAttributeString("category", "Structure"), true, out category))
{
category = MapEntityCategory.Structure;
}
sp.Category = category;
sp.Description = element.GetAttributeString("description", "");
string aliases = element.GetAttributeString("aliases", "");
if (!string.IsNullOrWhiteSpace(aliases))
{
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");
sp.SpriteColor = new Color(XMLExtensions.ParseVector4(spriteColorStr));
SerializableProperty.DeserializeProperties(sp, element);
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;
}

View File

@@ -338,8 +338,8 @@ namespace Barotrauma
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>();