Motion sensor component, ruins have doors and hatches with randomly placed motion sensors that close them automatically

This commit is contained in:
Regalis
2016-05-07 09:09:09 +03:00
parent 03626c997b
commit ef05650c01
11 changed files with 207 additions and 11 deletions
+5 -3
View File
@@ -127,13 +127,14 @@ namespace Barotrauma.Items.Components
// isOpen = false;
foreach (XElement subElement in element.Elements())
{
string texturePath = ToolBox.GetAttributeString(subElement, "texture", "");
switch (subElement.Name.ToString().ToLowerInvariant())
{
case "sprite":
doorSprite = new Sprite(subElement, Path.GetDirectoryName(item.Prefab.ConfigFile));
doorSprite = new Sprite(subElement, texturePath.Contains("/") ? "" : Path.GetDirectoryName(item.Prefab.ConfigFile));
break;
case "weldedsprite":
weldedSprite = new Sprite(subElement, Path.GetDirectoryName(item.Prefab.ConfigFile));
weldedSprite = new Sprite(subElement, texturePath.Contains("/") ? "" : Path.GetDirectoryName(item.Prefab.ConfigFile));
break;
}
}
@@ -336,7 +337,7 @@ namespace Barotrauma.Items.Components
pos.Y = -pos.Y;
spriteBatch.Draw(doorSprite.Texture, pos,
new Rectangle(doorSprite.SourceRect.X, (int)(doorSprite.size.Y * openState),
new Rectangle(doorSprite.SourceRect.X, (int)(doorSprite.SourceRect.Y + doorSprite.size.Y * openState),
(int)doorSprite.size.X, (int)(doorSprite.size.Y * (1.0f - openState))),
color, 0.0f, doorSprite.Origin, 1.0f, SpriteEffects.None, doorSprite.Depth);
}
@@ -346,6 +347,7 @@ namespace Barotrauma.Items.Components
public override void OnMapLoaded()
{
LinkedGap.ConnectedDoor = this;
LinkedGap.Open = openState;
Vector2[] corners = GetConvexHullCorners(doorRect);
@@ -0,0 +1,91 @@
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace Barotrauma.Items.Components
{
class MotionSensor : ItemComponent
{
private const float UpdateInterval = 0.1f;
private string output, falseOutput;
private bool motionDetected;
private float range;
private float updateTimer;
[InGameEditable, HasDefaultValue(0.0f, true)]
public float Range
{
get { return range; }
set
{
range = MathHelper.Clamp(value, 0.0f, 500.0f);
}
}
[InGameEditable, HasDefaultValue("1", true)]
public string Output
{
get { return output; }
set { output = value; }
}
[InGameEditable, HasDefaultValue("", true)]
public string FalseOutput
{
get { return falseOutput; }
set { falseOutput = value; }
}
public MotionSensor(Item item, XElement element)
: base (item, element)
{
IsActive = true;
}
public override void Update(float deltaTime, Camera cam)
{
if (motionDetected)
{
item.SendSignal(1, output, "state_out");
}
else if (!string.IsNullOrWhiteSpace(falseOutput))
{
item.SendSignal(1, falseOutput, "state_out");
}
updateTimer -= deltaTime;
if (updateTimer>0.0f) return;
motionDetected = false;
updateTimer = UpdateInterval;
if (item.body != null && item.body.Enabled)
{
if (Math.Abs(item.body.LinearVelocity.X) > 0.01f || Math.Abs(item.body.LinearVelocity.Y) > 0.1f)
{
motionDetected = true;
}
}
foreach (Character c in Character.CharacterList)
{
if (Math.Abs(c.WorldPosition.X - item.WorldPosition.X) < range &&
Math.Abs(c.WorldPosition.Y - item.WorldPosition.Y) < range)
{
if (!c.AnimController.Limbs.Any(l => l.body.FarseerBody.Awake)) continue;
motionDetected = true;
break;
}
}
}
}
}