Small optimization: items have a list of components that have to be drawn

This commit is contained in:
Regalis
2016-05-24 21:17:38 +03:00
parent ed529052a2
commit b4af92ace2
13 changed files with 111 additions and 86 deletions

View File

@@ -10,7 +10,7 @@ using Barotrauma.Lights;
namespace Barotrauma.Items.Components
{
class Door : ItemComponent
class Door : ItemComponent, IDrawableComponent
{
private Gap linkedGap;
@@ -293,7 +293,7 @@ namespace Barotrauma.Items.Components
linkedGap.Open = 1.0f;
}
public override void Draw(SpriteBatch spriteBatch, bool editing)
public void Draw(SpriteBatch spriteBatch, bool editing)
{
Color color = (item.IsSelected) ? Color.Green : Color.White;
color = color * (item.Condition / 100.0f);

View File

@@ -7,7 +7,7 @@ using System.Xml.Linq;
namespace Barotrauma.Items.Components
{
class Pickable : ItemComponent
class Pickable : ItemComponent, IDrawableComponent
{
protected Character picker;
@@ -112,6 +112,7 @@ namespace Barotrauma.Items.Components
var leftHand = picker.AnimController.GetLimb(LimbType.LeftHand);
var rightHand = picker.AnimController.GetLimb(LimbType.RightHand);
Drawable = true;
pickTimer = 0.0f;
while (pickTimer < requiredTime)
@@ -150,8 +151,10 @@ namespace Barotrauma.Items.Components
private void StopPicking(Character picker)
{
Drawable = false;
picker.AnimController.Anim = AnimController.Animation.None;
pickTimer = 0.0f;
pickTimer = 0.0f;
}
protected void DropConnectedWires(Character character)
@@ -174,9 +177,13 @@ namespace Barotrauma.Items.Components
}
public override void Draw(SpriteBatch spriteBatch, bool editing = false)
public void Draw(SpriteBatch spriteBatch, bool editing = false)
{
if (pickTimer <= 0.0f) return;
if (pickTimer <= 0.0f)
{
Drawable = false;
return;
}
float progressBarWidth = 100.0f;

View File

@@ -105,8 +105,6 @@ namespace Barotrauma.Items.Components
//if (DoesUseFail(Character)) return false;
IsActive = true;
//targetPosition = targetPosition.X, -targetPosition.Y);
float degreeOfSuccess = DegreeOfSuccess(character)/100.0f;
@@ -195,7 +193,6 @@ namespace Barotrauma.Items.Components
if (character.IsKeyDown(InputType.Aim))
{
targetLimb.character.AddDamage(CauseOfDeath.Damage, -LimbFixAmount * degreeOfSuccess, character);
//isActive = true;
}
}
else if ((targetItem = (targetBody.UserData as Item)) != null)
@@ -203,12 +200,12 @@ namespace Barotrauma.Items.Components
targetItem.IsHighlighted = true;
ApplyStatusEffects(ActionType.OnUse, targetItem.AllPropertyObjects, deltaTime);
}
}
}
GameMain.ParticleManager.CreateParticle(particles, item.WorldPosition + TransformedBarrelPos,
-item.body.Rotation + ((item.body.Dir > 0.0f) ? 0.0f : MathHelper.Pi), ParticleSpeed);
return true;
}
@@ -247,33 +244,6 @@ namespace Barotrauma.Items.Components
Use(deltaTime, character);
return leak.Open <= 0.0f;
}
public override void Draw(SpriteBatch spriteBatch, bool editing)
{
if (!IsActive) return;
//Vector2 particleSpeed = new Vector2(
// (float)Math.Cos(item.body.Rotation),
// (float)Math.Sin(item.body.Rotation)) *item.body.Dir * 0.1f;
if (!string.IsNullOrWhiteSpace(particles))
{
GameMain.ParticleManager.CreateParticle(particles, item.WorldPosition+TransformedBarrelPos,
-item.body.Rotation + ((item.body.Dir>0.0f) ? 0.0f : MathHelper.Pi), ParticleSpeed);
}
//Vector2 startPos = ConvertUnits.ToDisplayUnits(item.body.Position);
//Vector2 endPos = ConvertUnits.ToDisplayUnits(pickedPosition);
//endPos = new Vector2(endPos.X + Game1.localRandom.Next(-2, 2), endPos.Y + Game1.localRandom.Next(-2, 2));
//GUI.DrawLine(spriteBatch, startPos, endPos, Color.Orange, 0.0f);
IsActive = false;
}
}
}

View File

@@ -12,6 +12,11 @@ using System.Globalization;
namespace Barotrauma.Items.Components
{
interface IDrawableComponent
{
void Draw(SpriteBatch spriteBatch, bool editing);
}
class ItemSound
{
public readonly Sound Sound;
@@ -104,6 +109,28 @@ namespace Barotrauma.Items.Components
}
}
private bool drawable = true;
public bool Drawable
{
get { return drawable; }
set
{
if (value == drawable) return;
drawable = value;
if (drawable)
{
if (!item.drawableComponents.Contains(this as IDrawableComponent))
item.drawableComponents.Add(this as IDrawableComponent);
}
else
{
item.drawableComponents.Remove(this as IDrawableComponent);
}
}
}
[HasDefaultValue(false, false)]
public bool CanBePicked
{
@@ -430,7 +457,10 @@ namespace Barotrauma.Items.Components
/// <summary>a Character has dropped the item</summary>
public virtual void Drop(Character dropper) { }
public virtual void Draw(SpriteBatch spriteBatch, bool editing = false) { }
//public virtual void Draw(SpriteBatch spriteBatch, bool editing = false)
//{
// item.drawableComponents = Array.FindAll(item.drawableComponents, i => i != this);
//}
public virtual void DrawHUD(SpriteBatch spriteBatch, Character character) { }

View File

@@ -7,7 +7,7 @@ using Microsoft.Xna.Framework.Graphics;
namespace Barotrauma.Items.Components
{
class ItemContainer : ItemComponent
class ItemContainer : ItemComponent, IDrawableComponent
{
public const int MaxInventoryCount = 4;
@@ -29,7 +29,11 @@ namespace Barotrauma.Items.Components
public bool HideItems
{
get { return hideItems; }
set { hideItems = value; }
set
{
hideItems = value;
Drawable = !hideItems;
}
}
private bool hideItems;
@@ -160,10 +164,8 @@ namespace Barotrauma.Items.Components
}
}
public override void Draw(SpriteBatch spriteBatch, bool editing = false)
public void Draw(SpriteBatch spriteBatch, bool editing = false)
{
base.Draw(spriteBatch, editing);
if (hideItems || (item.body != null && !item.body.Enabled)) return;
Vector2 transformedItemPos = itemPos;

View File

@@ -3,7 +3,7 @@ using Microsoft.Xna.Framework.Graphics;
using System.Xml.Linq;
namespace Barotrauma.Items.Components
{
class ItemLabel : ItemComponent
class ItemLabel : ItemComponent, IDrawableComponent
{
private GUITextBlock textBlock;
@@ -64,10 +64,8 @@ namespace Barotrauma.Items.Components
{
}
public override void Draw(SpriteBatch spriteBatch, bool editing = false)
public void Draw(SpriteBatch spriteBatch, bool editing = false)
{
base.Draw(spriteBatch, editing);
var drawPos = new Vector2(
item.DrawPosition.X - item.Rect.Width/2.0f,
-(item.DrawPosition.Y + item.Rect.Height/2.0f));

View File

@@ -8,7 +8,7 @@ using Barotrauma.Networking;
namespace Barotrauma.Items.Components
{
class Reactor : Powered
class Reactor : Powered, IDrawableComponent
{
const float NetworkUpdateInterval = 3.0f;
@@ -385,10 +385,8 @@ namespace Barotrauma.Items.Components
return picker != null;
}
public override void Draw(SpriteBatch spriteBatch, bool editing = false)
public void Draw(SpriteBatch spriteBatch, bool editing = false)
{
base.Draw(spriteBatch, editing);
GUI.DrawRectangle(spriteBatch,
new Vector2(item.Rect.X + item.Rect.Width / 2 - 6, -item.Rect.Y + 29),
new Vector2(12, 42), Color.Black);

View File

@@ -5,7 +5,7 @@ using Microsoft.Xna.Framework.Graphics;
namespace Barotrauma.Items.Components
{
class PowerContainer : Powered
class PowerContainer : Powered, IDrawableComponent
{
//[power/min]
float capacity;
@@ -224,10 +224,8 @@ namespace Barotrauma.Items.Components
//if (connection.IsPower) voltage = power;
}
public override void Draw(SpriteBatch spriteBatch, bool editing = false)
public void Draw(SpriteBatch spriteBatch, bool editing = false)
{
base.Draw(spriteBatch, editing);
GUI.DrawRectangle(spriteBatch,
new Vector2(item.DrawPosition.X- 4, -item.DrawPosition.Y),
new Vector2(8, 22), Color.Black);

View File

@@ -13,7 +13,7 @@ using Microsoft.Xna.Framework.Graphics;
namespace Barotrauma.Items.Components
{
class Rope : ItemComponent
class Rope : ItemComponent, IDrawableComponent
{
PhysicsBody[] ropeBodies;
RevoluteJoint[] ropeJoints;
@@ -239,10 +239,8 @@ namespace Barotrauma.Items.Components
}
}
public override void Draw(SpriteBatch spriteBatch, bool editing = false)
public void Draw(SpriteBatch spriteBatch, bool editing = false)
{
base.Draw(spriteBatch, editing);
if (!IsActive) return;
RevoluteJoint firstJoint = null;

View File

@@ -5,7 +5,7 @@ using System.Xml.Linq;
namespace Barotrauma.Items.Components
{
class LightComponent : Powered
class LightComponent : Powered, IDrawableComponent
{
private Color lightColor;
@@ -173,7 +173,7 @@ namespace Barotrauma.Items.Components
return true;
}
public override void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch, bool editing = false)
public void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch, bool editing = false)
{
if (light.LightSprite != null && (item.body == null || item.body.Enabled))
{

View File

@@ -8,7 +8,7 @@ using System.Xml.Linq;
namespace Barotrauma.Items.Components
{
class Wire : ItemComponent
class Wire : ItemComponent, IDrawableComponent
{
const float nodeDistance = 32.0f;
const float heightFromFloor = 128.0f;
@@ -129,6 +129,7 @@ namespace Barotrauma.Items.Components
Nodes.Add(newConnection.Item.Position - Submarine.HiddenSubPosition);
}
break;
}
@@ -150,6 +151,8 @@ namespace Barotrauma.Items.Components
CleanNodes();
}
Drawable = Nodes.Any();
if (!loading) Item.NewComponentEvent(this, true, true);
return true;
@@ -160,6 +163,7 @@ namespace Barotrauma.Items.Components
ClearConnections();
IsActive = true;
//Drawable = true;
}
public override void Unequip(Character character)
@@ -174,7 +178,6 @@ namespace Barotrauma.Items.Components
ClearConnections();
IsActive = false;
}
public override void Update(float deltaTime, Camera cam)
@@ -221,6 +224,8 @@ namespace Barotrauma.Items.Components
if (newNodePos!= Vector2.Zero && Nodes.Count>0 && Vector2.Distance(newNodePos, Nodes[Nodes.Count - 1]) > nodeDistance)
{
Nodes.Add(newNodePos);
Drawable = true;
newNodePos = Vector2.Zero;
}
return true;
@@ -231,8 +236,11 @@ namespace Barotrauma.Items.Components
if (Nodes.Count > 1)
{
Nodes.RemoveAt(Nodes.Count - 1);
item.NewComponentEvent(this, true, true);
}
Drawable = Nodes.Any();
}
public override bool Pick(Character picker)
@@ -256,6 +264,8 @@ namespace Barotrauma.Items.Components
connections[i] = null;
}
Drawable = false;
}
private Vector2 RoundNode(Vector2 position, Hull hull)
@@ -317,16 +327,19 @@ namespace Barotrauma.Items.Components
}
public override void Draw(SpriteBatch spriteBatch, bool editing)
public void Draw(SpriteBatch spriteBatch, bool editing)
{
if (Nodes.Count == 0) return;
//for (int i = 0; i < nodes.Count; i++)
//{
// GUI.DrawRectangle(spriteBatch, new Rectangle((int)nodes[i].X, (int)-nodes[i].Y, 5, 5), Color.DarkGray, true, wireSprite.Depth - 0.01f);
//}
if (!Nodes.Any())
{
Drawable = false;
return;
}
if (item.IsHighlighted)
{
for (int i = 1; i < Nodes.Count; i++)
@@ -347,9 +360,9 @@ namespace Barotrauma.Items.Components
//nodes.Add(newNodePos);
}
if (!editing || !PlayerInput.MouseInsideWindow || !GameMain.EditMapScreen.WiringMode ) return;
if (!editing || !PlayerInput.MouseInsideWindow || !GameMain.EditMapScreen.WiringMode) return;
if (Character.Controlled != null && Character.Controlled.SelectedConstruction != null) return;
for (int i = 0; i < Nodes.Count; i++)
{
Vector2 worldPos = Nodes[i];
@@ -396,8 +409,8 @@ namespace Barotrauma.Items.Components
Vector2 nodeWorldPos = GameMain.EditMapScreen.Cam.ScreenToWorld(PlayerInput.MousePosition) - Submarine.HiddenSubPosition - Submarine.Loaded.Position;// Nodes[(int)selectedNodeIndex];
nodeWorldPos.X = MathUtils.Round(nodeWorldPos.X, Submarine.GridSize.X/2.0f);
nodeWorldPos.Y = MathUtils.Round(nodeWorldPos.Y, Submarine.GridSize.Y/2.0f);
nodeWorldPos.X = MathUtils.Round(nodeWorldPos.X, Submarine.GridSize.X / 2.0f);
nodeWorldPos.Y = MathUtils.Round(nodeWorldPos.Y, Submarine.GridSize.Y / 2.0f);
//if (item.Submarine != null) nodeWorldPos += item.Submarine.Position;
@@ -478,6 +491,8 @@ namespace Barotrauma.Items.Components
Nodes.Add(new Vector2(x, y));
}
Drawable = Nodes.Any();
}
protected override void RemoveComponentSpecific()
@@ -513,6 +528,8 @@ namespace Barotrauma.Items.Components
}
Nodes = newNodes;
Drawable = Nodes.Any();
}
}
}

View File

@@ -8,7 +8,7 @@ using FarseerPhysics;
namespace Barotrauma.Items.Components
{
class Turret : Powered
class Turret : Powered, IDrawableComponent
{
Sprite barrelSprite;
@@ -90,7 +90,7 @@ namespace Barotrauma.Items.Components
}
}
public override void Draw(SpriteBatch spriteBatch, bool editing = false)
public void Draw(SpriteBatch spriteBatch, bool editing = false)
{
Vector2 drawPos = new Vector2(item.Rect.X, item.Rect.Y);
if (item.Submarine != null) drawPos += item.Submarine.DrawPosition;

View File

@@ -42,6 +42,7 @@ namespace Barotrauma
//components that determine the functionality of the item
public List<ItemComponent> components;
public List<IDrawableComponent> drawableComponents;
public PhysicsBody body;
@@ -98,7 +99,7 @@ namespace Barotrauma
{
get
{
return ParentInventory == null && (body == null || body.Enabled);
return parentInventory == null && (body == null || body.Enabled);
}
}
@@ -312,10 +313,11 @@ namespace Barotrauma
spriteColor = prefab.SpriteColor;
linkedTo = new ObservableCollection<MapEntity>();
components = new List<ItemComponent>();
FixRequirements = new List<FixRequirement>();
tags = new List<string>();
linkedTo = new ObservableCollection<MapEntity>();
components = new List<ItemComponent>();
drawableComponents = new List<IDrawableComponent>();
FixRequirements = new List<FixRequirement>();
tags = new List<string>();
rect = newRect;
@@ -339,8 +341,6 @@ namespace Barotrauma
properties = ObjectProperty.InitProperties(this, element);
foreach (XElement subElement in element.Elements())
{
switch (subElement.Name.ToString().ToLowerInvariant())
@@ -366,6 +366,8 @@ namespace Barotrauma
components.Add(ic);
if (ic is IDrawableComponent && ic.Drawable) drawableComponents.Add(ic as IDrawableComponent);
if (ic.statusEffectLists == null) continue;
if (statusEffectLists == null)
@@ -394,7 +396,7 @@ namespace Barotrauma
break;
}
}
//containers need to handle collision events to notify items inside them about the impact
if (ImpactTolerance > 0.0f || GetComponent<ItemContainer>() != null)
{
@@ -846,8 +848,13 @@ namespace Barotrauma
}
}
}
foreach (ItemComponent component in components) component.Draw(spriteBatch, editing);
for (int i = 0; i < drawableComponents.Count; i++ )
{
drawableComponents[i].Draw(spriteBatch, editing);
}
//foreach (ItemComponent component in components) component.Draw(spriteBatch, editing);
if (GameMain.DebugDraw && aiTarget!=null) aiTarget.Draw(spriteBatch);