First commit
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace Subsurface.Items.Components
|
||||
{
|
||||
class AndComponent : ItemComponent
|
||||
{
|
||||
protected string output;
|
||||
|
||||
//an array to keep track of how long ago a non-zero signal was received on both inputs
|
||||
protected float[] timeSinceReceived;
|
||||
|
||||
//the output is sent if both inputs have received a signal within the timeframe
|
||||
protected float timeFrame;
|
||||
|
||||
[InGameEditable, HasDefaultValue(0.1f, true)]
|
||||
public float TimeFrame
|
||||
{
|
||||
get { return timeFrame; }
|
||||
set
|
||||
{
|
||||
timeFrame = Math.Max(0.0f, value);
|
||||
}
|
||||
}
|
||||
|
||||
[InGameEditable, HasDefaultValue("1", true)]
|
||||
public string Output
|
||||
{
|
||||
get { return output; }
|
||||
set { output = value; }
|
||||
}
|
||||
|
||||
public AndComponent(Item item, XElement element)
|
||||
: base (item, element)
|
||||
{
|
||||
timeSinceReceived = new float[] { timeFrame*2.0f, timeFrame*2.0f};
|
||||
|
||||
//output = "1";
|
||||
}
|
||||
|
||||
public override void Update(float deltaTime, Camera cam)
|
||||
{
|
||||
bool sendOutput = true;
|
||||
for (int i = 0; i<timeSinceReceived.Length; i++)
|
||||
{
|
||||
if (timeSinceReceived[i] > timeFrame) sendOutput = false;
|
||||
timeSinceReceived[i] += deltaTime;
|
||||
}
|
||||
|
||||
if (sendOutput)
|
||||
{
|
||||
item.SendSignal(output, "signal_out");
|
||||
}
|
||||
}
|
||||
|
||||
public override void ReceiveSignal(string signal, Connection connection, Item sender)
|
||||
{
|
||||
switch (connection.name)
|
||||
{
|
||||
case "signal_in1":
|
||||
if (signal == "0") return;
|
||||
timeSinceReceived[0] = 0.0f;
|
||||
isActive = true;
|
||||
break;
|
||||
case "signal_in2":
|
||||
if (signal == "0") return;
|
||||
timeSinceReceived[1] = 0.0f;
|
||||
isActive = true;
|
||||
break;
|
||||
case "set_output":
|
||||
output = signal;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,434 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace Subsurface.Items.Components
|
||||
{
|
||||
|
||||
class Connection
|
||||
{
|
||||
private static Sprite connector;
|
||||
private static Sprite wireCorner, wireVertical, wireHorizontal;
|
||||
|
||||
//how many wires can be linked to a single connector
|
||||
private const int MaxLinked = 5;
|
||||
|
||||
public readonly string name;
|
||||
|
||||
public Wire[] wires;
|
||||
|
||||
private Item item;
|
||||
|
||||
public readonly bool isOutput;
|
||||
|
||||
private static Item draggingConnected;
|
||||
|
||||
int[] wireId;
|
||||
|
||||
public List<Connection> Recipients
|
||||
{
|
||||
get
|
||||
{
|
||||
List<Connection> recipients = new List<Connection>();
|
||||
for (int i = 0; i<MaxLinked; i++)
|
||||
{
|
||||
if (wires[i] == null) continue;
|
||||
Connection recipient = wires[i].OtherConnection(this);
|
||||
if (recipient != null) recipients.Add(recipient);
|
||||
}
|
||||
return recipients;
|
||||
}
|
||||
}
|
||||
|
||||
public Item Item
|
||||
{
|
||||
get { return item; }
|
||||
}
|
||||
|
||||
public Connection(XElement element, Item item)
|
||||
{
|
||||
|
||||
if (connector == null)
|
||||
{
|
||||
connector = new Sprite("Content/Items/connector.png", new Vector2(0.5f, 0.5f));
|
||||
wireCorner = new Sprite("Content/Items/wireCorner.png", new Vector2(0.5f, 0.1f));
|
||||
wireVertical = new Sprite("Content/Items/wireVertical.png", new Vector2(0.5f, 0.5f));
|
||||
wireHorizontal = new Sprite("Content/Items/wireHorizontal.png", new Vector2(0.5f, 0.5f));
|
||||
}
|
||||
|
||||
this.item = item;
|
||||
|
||||
//recipient = new Connection[MaxLinked];
|
||||
wires = new Wire[MaxLinked];
|
||||
|
||||
isOutput = (element.Name.ToString() == "output");
|
||||
name = ToolBox.GetAttributeString(element, "name", (isOutput) ? "output" : "input");
|
||||
|
||||
wireId = new int[MaxLinked];
|
||||
|
||||
foreach (XElement subElement in element.Elements())
|
||||
{
|
||||
int index = -1;
|
||||
|
||||
for (int i = 0; i < MaxLinked; i++)
|
||||
{
|
||||
if (wireId[i]<1) index = i;
|
||||
}
|
||||
if (index == -1) break;
|
||||
|
||||
wireId[index] = ToolBox.GetAttributeInt(subElement, "w", -1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public int FindEmptyIndex()
|
||||
{
|
||||
for (int i = 0; i < MaxLinked; i++)
|
||||
{
|
||||
if (wires[i]==null) return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
//public int FindLinkIndex(Item item)
|
||||
//{
|
||||
// for (int i = 0; i < MaxLinked; i++)
|
||||
// {
|
||||
// if (item == null && recipient[i] == null) return i;
|
||||
// if (recipient[i]!=null && recipient[i].item == item) return i;
|
||||
// }
|
||||
// return -1;
|
||||
//}
|
||||
|
||||
public int FindWireIndex(Item wireItem)
|
||||
{
|
||||
for (int i = 0; i < MaxLinked; i++)
|
||||
{
|
||||
if (wires[i] == null && wireItem == null) return i;
|
||||
if (wires[i] != null && wires[i].Item == wireItem) return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public void AddLink(int index, Wire wire)
|
||||
{
|
||||
//linked[index] = connectedItem;
|
||||
//recipient[index] = otherConnection;
|
||||
wires[index] = wire;
|
||||
}
|
||||
|
||||
//public bool AddLink(Item connectedItem, Connection otherConnection)
|
||||
//{
|
||||
// if (linked.Contains(connectedItem)) return false;
|
||||
|
||||
// for (int i = 0; i<MaxLinked; i++)
|
||||
// {
|
||||
// if (linked[i]!=null) continue;
|
||||
|
||||
// linked[i] = connectedItem;
|
||||
// return true;
|
||||
// }
|
||||
|
||||
// return false;
|
||||
//}
|
||||
|
||||
public void SendSignal(string signal, Item sender)
|
||||
{
|
||||
for (int i = 0; i<MaxLinked; i++)
|
||||
{
|
||||
if (wires[i]==null) continue;
|
||||
|
||||
Connection recipient = wires[i].OtherConnection(this);
|
||||
if (recipient == null) continue;
|
||||
|
||||
foreach (ItemComponent ic in recipient.item.components)
|
||||
{
|
||||
ic.ReceiveSignal(signal, recipient, sender);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void ClearConnections()
|
||||
{
|
||||
for (int i = 0; i<MaxLinked; i++)
|
||||
{
|
||||
if (wires[i] == null) continue;
|
||||
|
||||
wires[i].RemoveConnection(this);
|
||||
wires[i] = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void DrawConnections(SpriteBatch spriteBatch, ConnectionPanel panel, Character character)
|
||||
{
|
||||
|
||||
int width = 400, height = 200;
|
||||
int x = Game1.GraphicsWidth/2 - width/2, y = Game1.GraphicsHeight - height;
|
||||
|
||||
Rectangle panelRect = new Rectangle(x, y, width, height);
|
||||
|
||||
GUI.DrawRectangle(spriteBatch, panelRect, Color.Black, true);
|
||||
|
||||
bool mouseInRect = panelRect.Contains(PlayerInput.MousePosition);
|
||||
|
||||
Vector2 rightPos = new Vector2(x + width - 110, y + 20);
|
||||
Vector2 leftPos = new Vector2(x + 110, y + 20);
|
||||
|
||||
float wireInterval = 10.0f;
|
||||
|
||||
float rightWireX = x+width / 2 + wireInterval;
|
||||
float leftWireX = x + width / 2 - wireInterval;
|
||||
foreach (Connection c in panel.connections)
|
||||
{
|
||||
//if dragging a wire, let the Inventory know so that the wire can be
|
||||
//dropped or dragged from the panel to the players inventory
|
||||
if (draggingConnected != null)
|
||||
{
|
||||
int linkIndex = c.FindWireIndex(draggingConnected);
|
||||
if (linkIndex>-1)
|
||||
{
|
||||
Inventory.draggingItem = c.wires[linkIndex].Item;
|
||||
}
|
||||
}
|
||||
|
||||
//outputs are drawn at the right side of the panel, inputs at the left
|
||||
if (c.isOutput)
|
||||
{
|
||||
c.Draw(spriteBatch, panel.Item, rightPos,
|
||||
new Vector2(rightPos.X + 20, rightPos.Y),
|
||||
new Vector2(rightWireX, y + height), mouseInRect);
|
||||
|
||||
rightPos.Y += 30;
|
||||
rightWireX += wireInterval;
|
||||
}
|
||||
else
|
||||
{
|
||||
c.Draw(spriteBatch, panel.Item, leftPos,
|
||||
new Vector2(leftPos.X - 100, leftPos.Y),
|
||||
new Vector2(leftWireX, y + height), mouseInRect);
|
||||
|
||||
leftPos.Y += 30;
|
||||
leftWireX -= wireInterval;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//draw a wire for all the items that are linked to this item, but not to any of the signal connections
|
||||
//foreach (MapEntity entity in panel.Item.linkedTo)
|
||||
//{
|
||||
// Item linked = entity as Item;
|
||||
// if (linked == null) continue;
|
||||
|
||||
// //if the item is already connected, don't draw it again
|
||||
// if (panel.connections.Find(c => c.linked.Contains()) != null) continue;
|
||||
|
||||
// DrawWire(spriteBatch, false, linked,
|
||||
// new Vector2(leftPos.X + (leftPos.Y - y), y + height- 50),
|
||||
// new Vector2(leftPos.X + (leftPos.Y - y), y + height), mouseInRect);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// leftPos.Y += 30.0f;
|
||||
//}
|
||||
|
||||
//if the character using the panel has a wire item equipped
|
||||
//and the wire hasn't been connected yet, draw it on the panel
|
||||
|
||||
|
||||
for (int i = 0; i < character.SelectedItems.Length; i++ )
|
||||
{
|
||||
Item selectedItem = character.SelectedItems[i];
|
||||
|
||||
if (selectedItem == null) continue;
|
||||
|
||||
Wire wireComponent = selectedItem.GetComponent<Wire>();
|
||||
|
||||
if (wireComponent != null &&
|
||||
panel.connections.Find(c => c.wires.Contains(wireComponent)) == null)
|
||||
{
|
||||
DrawWire(spriteBatch, selectedItem, selectedItem,
|
||||
new Vector2(x + width / 2, y + height - 100),
|
||||
new Vector2(x + width / 2, y + height), mouseInRect);
|
||||
|
||||
if (draggingConnected == selectedItem) Inventory.draggingItem = selectedItem;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//stop dragging a wire item if cursor is outside the panel
|
||||
if (mouseInRect) Inventory.draggingItem = null;
|
||||
|
||||
if (draggingConnected != null)
|
||||
{
|
||||
if (!PlayerInput.LeftButtonDown())
|
||||
{
|
||||
panel.Item.NewComponentEvent(panel, true);
|
||||
draggingConnected = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void Draw(SpriteBatch spriteBatch, Item item, Vector2 position, Vector2 labelPos, Vector2 wirePosition, bool mouseIn)
|
||||
{
|
||||
|
||||
spriteBatch.DrawString(GUI.font, name, new Vector2(labelPos.X, labelPos.Y-10), Color.White);
|
||||
|
||||
GUI.DrawRectangle(spriteBatch, new Rectangle((int)position.X-10, (int)position.Y-10, 20, 20), Color.White);
|
||||
|
||||
|
||||
for (int i = 0; i<MaxLinked; i++)
|
||||
{
|
||||
if (wires[i]==null) continue;
|
||||
|
||||
Connection recipient = wires[i].OtherConnection(this);
|
||||
|
||||
DrawWire(spriteBatch, wires[i].Item, (recipient == null) ? wires[i].Item : recipient.item, position, wirePosition, mouseIn);
|
||||
wirePosition.X += (isOutput) ? -20 : 20;
|
||||
}
|
||||
|
||||
//dragging a wire and released the mouse -> see if the wire can be connected to this connection
|
||||
if (draggingConnected != null
|
||||
&& !PlayerInput.LeftButtonDown())
|
||||
{
|
||||
//close enough to the connector -> make a new connection
|
||||
if (Vector2.Distance(position, PlayerInput.MousePosition) < 10.0f)
|
||||
{
|
||||
//find an empty cell for the new connection
|
||||
int index = FindWireIndex(null);
|
||||
|
||||
Wire wireComponent = draggingConnected.GetComponent<Wire>();
|
||||
|
||||
if (index>-1 && wireComponent!=null && !wires.Contains(wireComponent))
|
||||
{
|
||||
wires[index] = wireComponent;
|
||||
wireComponent.Connect(this);
|
||||
}
|
||||
}
|
||||
//far away -> disconnect if the wire is linked to this connector
|
||||
else
|
||||
{
|
||||
int index = FindWireIndex(draggingConnected);
|
||||
if (index>-1)
|
||||
{
|
||||
wires[index].RemoveConnection(this);
|
||||
wires[index] = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private static void DrawWire(SpriteBatch spriteBatch, Item wireItem, Item item, Vector2 end, Vector2 start, bool mouseIn)
|
||||
{
|
||||
if (draggingConnected == wireItem)
|
||||
{
|
||||
if (!mouseIn) return;
|
||||
end = PlayerInput.MousePosition;
|
||||
}
|
||||
else if (draggingConnected == null)
|
||||
{
|
||||
if (Vector2.Distance(end, PlayerInput.MousePosition)<20.0f)
|
||||
{
|
||||
item.IsHighlighted = true;
|
||||
//start dragging the wire
|
||||
if (PlayerInput.LeftButtonDown()) draggingConnected = wireItem;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int textX = (int)start.X;
|
||||
float connLength = 10.0f;
|
||||
|
||||
if (Math.Abs(end.X-start.X)<connLength*6.0f)
|
||||
{
|
||||
wireVertical.DrawTiled(spriteBatch,
|
||||
new Vector2(end.X - wireVertical.size.X / 2, end.Y + connLength),
|
||||
new Vector2(wireVertical.size.X, (float)Math.Abs(end.Y - start.Y)), Color.White);
|
||||
textX = (int)end.X;
|
||||
connector.Draw(spriteBatch, end);
|
||||
}
|
||||
else
|
||||
{
|
||||
wireVertical.DrawTiled(spriteBatch,
|
||||
new Vector2(start.X, end.Y + wireCorner.size.Y) - wireVertical.size / 2,
|
||||
new Vector2(wireVertical.size.X, (float)Math.Abs((end.Y + wireCorner.size.Y) - start.Y)), Color.White);
|
||||
|
||||
float dir = (end.X > start.X) ? -1.0f : 1.0f;
|
||||
|
||||
wireCorner.Draw(spriteBatch,
|
||||
new Vector2(start.X, end.Y), 0.0f, 1.0f,
|
||||
(end.X > start.X) ? SpriteEffects.None : SpriteEffects.FlipHorizontally);
|
||||
|
||||
float wireStartX = start.X - wireCorner.size.X / 2 * dir;
|
||||
float wireEndX = end.X + connLength * dir;
|
||||
|
||||
wireHorizontal.DrawTiled(spriteBatch, new Vector2(Math.Min(wireStartX,wireEndX), end.Y - wireVertical.size.Y / 2),
|
||||
new Vector2(Math.Abs(wireStartX - wireEndX), wireHorizontal.size.Y), Color.White);
|
||||
|
||||
|
||||
connector.Draw(spriteBatch, end, -MathHelper.PiOver2*dir);
|
||||
}
|
||||
|
||||
spriteBatch.DrawString(GUI.font, item.Name,
|
||||
new Vector2(textX, start.Y-30),
|
||||
Color.White,
|
||||
MathHelper.PiOver2,
|
||||
GUI.font.MeasureString(item.Name)*0.5f,
|
||||
1.0f, SpriteEffects.None, 0.0f);
|
||||
}
|
||||
|
||||
public void Save(XElement parentElement)
|
||||
{
|
||||
XElement newElement = new XElement(isOutput ? "output" : "input", new XAttribute("name", name));
|
||||
|
||||
for (int i = 0; i < MaxLinked; i++ )
|
||||
{
|
||||
if (wires[i] == null) continue;
|
||||
|
||||
Connection recipient = wires[i].OtherConnection(this);
|
||||
|
||||
//int connectionIndex = recipient.item.Connections.FindIndex(x => x == recipient);
|
||||
newElement.Add(new XElement("link",
|
||||
new XAttribute("w", (wires[i] == null) ? "-1" : wires[i].Item.ID.ToString())));
|
||||
}
|
||||
|
||||
parentElement.Add(newElement);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void ConnectLinked()
|
||||
{
|
||||
if (wireId == null) return;
|
||||
|
||||
for (int i = 0; i < MaxLinked; i++)
|
||||
{
|
||||
if (wireId[i] == -1) continue;
|
||||
|
||||
Item wireItem = MapEntity.FindEntityByID(wireId[i]) as Item;
|
||||
|
||||
if (wireItem == null) continue;
|
||||
|
||||
wires[i] = wireItem.GetComponent<Wire>();
|
||||
|
||||
if (wires[i]!=null)
|
||||
{
|
||||
wires[i].Item.body.Enabled = false;
|
||||
wires[i].Connect(this, false);
|
||||
}
|
||||
}
|
||||
|
||||
wireId = null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace Subsurface.Items.Components
|
||||
{
|
||||
class ConnectionPanel : ItemComponent
|
||||
{
|
||||
|
||||
public List<Connection> connections;
|
||||
|
||||
Character user;
|
||||
|
||||
public ConnectionPanel(Item item, XElement element)
|
||||
: base(item, element)
|
||||
{
|
||||
connections = new List<Connection>();
|
||||
|
||||
foreach (XElement subElement in element.Elements())
|
||||
{
|
||||
switch (subElement.Name.ToString())
|
||||
{
|
||||
case "input":
|
||||
connections.Add(new Connection(subElement, item));
|
||||
break;
|
||||
case "output":
|
||||
connections.Add(new Connection(subElement, item));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void DrawHUD(Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch, Character character)
|
||||
{
|
||||
if (user!=character) return;
|
||||
Connection.DrawConnections(spriteBatch, this, character);
|
||||
}
|
||||
|
||||
public override XElement Save(XElement parentElement)
|
||||
{
|
||||
XElement componentElement = base.Save(parentElement);
|
||||
|
||||
foreach (Connection c in connections)
|
||||
{
|
||||
//XElement newElement = new XElement(c.isOutput ? "output" : "input", new XAttribute("name", c.name));
|
||||
|
||||
c.Save(componentElement);
|
||||
}
|
||||
|
||||
return componentElement;
|
||||
}
|
||||
|
||||
public override void OnMapLoaded()
|
||||
{
|
||||
foreach (Connection c in connections)
|
||||
{
|
||||
c.ConnectLinked();
|
||||
}
|
||||
}
|
||||
|
||||
public override void Update(float deltaTime, Camera cam)
|
||||
{
|
||||
if (user != null && user.SelectedConstruction != item) user = null;
|
||||
}
|
||||
|
||||
public override bool Pick(Character picker)
|
||||
{
|
||||
user = picker;
|
||||
isActive = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
public override void Load(XElement element)
|
||||
{
|
||||
base.Load(element);
|
||||
|
||||
connections.Clear();
|
||||
|
||||
foreach (XElement subElement in element.Elements())
|
||||
{
|
||||
switch (subElement.Name.ToString())
|
||||
{
|
||||
case "input":
|
||||
connections.Add(new Connection(subElement, item));
|
||||
break;
|
||||
case "output":
|
||||
connections.Add(new Connection(subElement, item));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void FillNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetOutgoingMessage message)
|
||||
{
|
||||
foreach (Connection c in connections)
|
||||
{
|
||||
int wireCount = c.wires.Length;
|
||||
for (int i = 0 ; i < wireCount; i++)
|
||||
{
|
||||
message.Write(c.wires[i]==null ? -1 : c.wires[i].Item.ID);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void ReadNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetIncomingMessage message)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("connectionpanel update");
|
||||
foreach (Connection c in connections)
|
||||
{
|
||||
int wireCount = c.wires.Length;
|
||||
c.ClearConnections();
|
||||
|
||||
for (int i = 0; i < wireCount; i++)
|
||||
{
|
||||
int wireId = message.ReadInt32();
|
||||
if (wireId == -1) continue;
|
||||
|
||||
Item wireItem = MapEntity.FindEntityByID(wireId) as Item;
|
||||
if (wireItem == null) continue;
|
||||
|
||||
Wire wireComponent = wireItem.GetComponent<Wire>();
|
||||
if (wireComponent == null) continue;
|
||||
|
||||
c.wires[i] = wireComponent;
|
||||
wireComponent.Connect(c, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace Subsurface.Items.Components
|
||||
{
|
||||
class LightComponent : ItemComponent
|
||||
{
|
||||
private Color lightColor;
|
||||
|
||||
private Sprite sprite;
|
||||
|
||||
[InGameEditable, HasDefaultValue("1.0,1.0,1.0,1.0", true)]
|
||||
public string LightColor
|
||||
{
|
||||
get { return ToolBox.Vector4ToString(lightColor.ToVector4()); }
|
||||
set
|
||||
{
|
||||
lightColor = new Color(ToolBox.ParseToVector4(value));
|
||||
}
|
||||
}
|
||||
|
||||
public LightComponent(Item item, XElement element)
|
||||
: base (item, element)
|
||||
{
|
||||
foreach (XElement subElement in element.Elements())
|
||||
{
|
||||
if (subElement.Name.ToString().ToLower() != "sprite") continue;
|
||||
sprite = new Sprite(subElement);
|
||||
break;
|
||||
}
|
||||
|
||||
//lightColor = new Color(ToolBox.GetAttributeVector4(element, "color", Vector4.One));
|
||||
}
|
||||
|
||||
public override void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch)
|
||||
{
|
||||
if (!isActive || sprite==null) return;
|
||||
sprite.Draw(spriteBatch, new Vector2(item.Position.X, -item.Position.Y), 0.0f, 1.0f, Microsoft.Xna.Framework.Graphics.SpriteEffects.None);
|
||||
}
|
||||
|
||||
public override void ReceiveSignal(string signal, Connection connection, Item sender)
|
||||
{
|
||||
switch (connection.name)
|
||||
{
|
||||
case "toggle":
|
||||
isActive = !isActive;
|
||||
break;
|
||||
case "set_state":
|
||||
isActive = (signal == "0") ? false : true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace Subsurface.Items.Components
|
||||
{
|
||||
class NotComponent : ItemComponent
|
||||
{
|
||||
public NotComponent(Item item, XElement element)
|
||||
: base (item, element)
|
||||
{
|
||||
}
|
||||
|
||||
public override void ReceiveSignal(string signal, Connection connection, Item sender)
|
||||
{
|
||||
if (connection.name != "signal_in") return;
|
||||
|
||||
item.SendSignal(signal=="0" ? "1" : "0", "signal_out", item);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace Subsurface.Items.Components
|
||||
{
|
||||
class OrComponent : AndComponent
|
||||
{
|
||||
public OrComponent(Item item, XElement element)
|
||||
: base (item, element)
|
||||
{
|
||||
}
|
||||
|
||||
public override void Update(float deltaTime, Camera cam)
|
||||
{
|
||||
bool sendOutput = true;
|
||||
for (int i = 0; i<timeSinceReceived.Length; i++)
|
||||
{
|
||||
if (timeSinceReceived[i] > timeFrame) sendOutput = false;
|
||||
timeSinceReceived[i] += deltaTime;
|
||||
}
|
||||
|
||||
if (sendOutput)
|
||||
{
|
||||
item.SendSignal(output, "signal_out");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace Subsurface.Items.Components
|
||||
{
|
||||
class OxygenDetector : ItemComponent
|
||||
{
|
||||
private Hull hull;
|
||||
|
||||
public OxygenDetector(Item item, XElement element)
|
||||
: base (item, element)
|
||||
{
|
||||
hull = Hull.FindHull(item.Position);
|
||||
|
||||
isActive = true;
|
||||
}
|
||||
|
||||
public override void OnMapLoaded()
|
||||
{
|
||||
hull = Hull.FindHull(item.Position);
|
||||
}
|
||||
|
||||
public override void Move(Microsoft.Xna.Framework.Vector2 amount)
|
||||
{
|
||||
hull = Hull.FindHull(item.Position);
|
||||
}
|
||||
|
||||
public override void Update(float deltaTime, Camera cam)
|
||||
{
|
||||
if (hull == null) return;
|
||||
|
||||
item.SendSignal(((int)hull.OxygenPercentage).ToString(), "signal_out");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
using System.Xml.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Subsurface.Items.Components
|
||||
{
|
||||
class RegExFindComponent : ItemComponent
|
||||
{
|
||||
private string output;
|
||||
|
||||
private string expression;
|
||||
|
||||
[InGameEditable, HasDefaultValue("1", true)]
|
||||
public string Output
|
||||
{
|
||||
get { return output; }
|
||||
set { output = value; }
|
||||
}
|
||||
|
||||
[InGameEditable, HasDefaultValue("", true)]
|
||||
public string Expression
|
||||
{
|
||||
get { return expression; }
|
||||
set { expression = value; }
|
||||
}
|
||||
|
||||
public RegExFindComponent(Item item, XElement element)
|
||||
: base(item, element)
|
||||
{
|
||||
}
|
||||
|
||||
public override void ReceiveSignal(string signal, Connection connection, Item sender)
|
||||
{
|
||||
switch (connection.name)
|
||||
{
|
||||
case "signal_in":
|
||||
if (string.IsNullOrWhiteSpace(expression)) return;
|
||||
|
||||
bool success = false;
|
||||
try
|
||||
{
|
||||
Regex regex = new Regex(@expression);
|
||||
Match match = regex.Match(signal);
|
||||
success = match.Success;
|
||||
}
|
||||
catch
|
||||
{
|
||||
item.SendSignal("ERROR", "signal_out", item);
|
||||
return;
|
||||
}
|
||||
|
||||
if (success)
|
||||
{
|
||||
item.SendSignal(output, "signal_out", item);
|
||||
}
|
||||
else
|
||||
{
|
||||
item.SendSignal("0", "signal_out", item);
|
||||
}
|
||||
|
||||
break;
|
||||
case "set_output":
|
||||
output = signal;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,294 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace Subsurface.Items.Components
|
||||
{
|
||||
class Wire : ItemComponent
|
||||
{
|
||||
const float nodeDistance = 128.0f;
|
||||
|
||||
static Sprite wireSprite;
|
||||
|
||||
List<Vector2> nodes;
|
||||
|
||||
Connection[] connections;
|
||||
|
||||
public Wire(Item item, XElement element)
|
||||
: base(item, element)
|
||||
{
|
||||
if (wireSprite==null)
|
||||
{
|
||||
wireSprite = new Sprite("Content/Items/wireHorizontal.png",new Vector2(0.5f,0.5f));
|
||||
wireSprite.Depth = 0.85f;
|
||||
}
|
||||
|
||||
nodes = new List<Vector2>();
|
||||
|
||||
connections = new Connection[2];
|
||||
}
|
||||
|
||||
public Connection OtherConnection(Connection connection)
|
||||
{
|
||||
if (connection==null) return null;
|
||||
if (connection==connections[0]) return connections[1];
|
||||
if (connection==connections[1]) return connections[0];
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void RemoveConnection(Connection connection)
|
||||
{
|
||||
if (connection == connections[0]) connections[0] = null;
|
||||
if (connection == connections[1]) connections[1] = null;
|
||||
}
|
||||
|
||||
public void Connect(Connection newConnection, bool addNode = true)
|
||||
{
|
||||
for (int i = 0; i < 2; i++)
|
||||
{
|
||||
if (connections[i] == newConnection) return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 2; i++)
|
||||
{
|
||||
if (connections[i] != null) continue;
|
||||
|
||||
connections[i] = newConnection;
|
||||
|
||||
if (!addNode) break;
|
||||
|
||||
if (i==0)
|
||||
{
|
||||
nodes.Insert(0, newConnection.Item.Position);
|
||||
}
|
||||
else
|
||||
{
|
||||
nodes.Add(newConnection.Item.Position);
|
||||
}
|
||||
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (connections[0]!=null && connections[1]!=null)
|
||||
{
|
||||
item.Drop(null, false);
|
||||
item.body.Enabled = false;
|
||||
|
||||
CleanNodes();
|
||||
}
|
||||
|
||||
//new Networking.NetworkEvent(item.ID, true);
|
||||
|
||||
}
|
||||
|
||||
public override void Equip(Character character)
|
||||
{
|
||||
ClearConnections();
|
||||
|
||||
isActive = true;
|
||||
}
|
||||
|
||||
public override void Unequip(Character character)
|
||||
{
|
||||
ClearConnections();
|
||||
}
|
||||
|
||||
public override void Update(float deltaTime, Camera cam)
|
||||
{
|
||||
if (nodes.Count == 0) return;
|
||||
|
||||
if (Math.Abs(item.Position.X-nodes[nodes.Count-1].X)>nodeDistance)
|
||||
{
|
||||
nodes.Add(new Vector2(
|
||||
ToolBox.Round(item.Position.X, Map.gridSize.X),
|
||||
nodes[nodes.Count - 1].Y));
|
||||
|
||||
item.NewComponentEvent(this, true);
|
||||
}
|
||||
else if (Math.Abs(item.Position.Y-nodes[nodes.Count-1].Y)>nodeDistance)
|
||||
{
|
||||
nodes.Add(new Vector2(nodes[nodes.Count - 1].X,
|
||||
ToolBox.Round(item.Position.Y, Map.gridSize.Y)));
|
||||
|
||||
item.NewComponentEvent(this, true);
|
||||
}
|
||||
}
|
||||
|
||||
//public override bool Use(Character character = null)
|
||||
//{
|
||||
// Vector2 nodePos = item.Position;
|
||||
// ToolBox.Round(nodePos.X, Map.gridSize.X);
|
||||
// ToolBox.Round(nodePos.Y, Map.gridSize.Y);
|
||||
|
||||
// nodes.Add(nodePos);
|
||||
|
||||
// return true;
|
||||
//}
|
||||
|
||||
public override void SecondaryUse(Character character = null)
|
||||
{
|
||||
if (nodes.Count > 0)
|
||||
{
|
||||
nodes.RemoveAt(nodes.Count - 1);
|
||||
item.NewComponentEvent(this, true);
|
||||
}
|
||||
}
|
||||
|
||||
public override bool Pick(Character picker)
|
||||
{
|
||||
ClearConnections();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void ClearConnections()
|
||||
{
|
||||
nodes.Clear();
|
||||
|
||||
for (int i = 0; i < 2; i++ )
|
||||
{
|
||||
if (connections[i] == null) continue;
|
||||
int wireIndex = connections[i].FindWireIndex(item);
|
||||
|
||||
if (wireIndex == -1) continue;
|
||||
connections[i].AddLink(wireIndex, null);
|
||||
|
||||
connections[i] = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void CleanNodes()
|
||||
{
|
||||
for (int i = nodes.Count - 2; i > 0; i--)
|
||||
{
|
||||
if ((nodes[i-1].X == nodes[i].X || nodes[i-1].Y == nodes[i].Y) &&
|
||||
(nodes[i+1].X == nodes[i].X || nodes[i+1].Y == nodes[i].Y))
|
||||
{
|
||||
if (Vector2.Distance(nodes[i - 1], nodes[i]) == Vector2.Distance(nodes[i + 1], nodes[i]))
|
||||
{
|
||||
nodes.RemoveAt(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool removed;
|
||||
do
|
||||
{
|
||||
removed = false;
|
||||
for (int i = nodes.Count - 2; i > 0; i--)
|
||||
{
|
||||
if ((nodes[i - 1].X == nodes[i].X && nodes[i + 1].X == nodes[i].X)
|
||||
|| (nodes[i - 1].Y == nodes[i].Y && nodes[i + 1].Y == nodes[i].Y))
|
||||
{
|
||||
nodes.RemoveAt(i);
|
||||
removed = true;
|
||||
}
|
||||
}
|
||||
|
||||
} while (removed);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public override void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
for (int i = 1; i<nodes.Count; i++)
|
||||
{
|
||||
DrawSection(spriteBatch, nodes[i], nodes[i - 1], i);
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawSection(SpriteBatch spriteBatch, Vector2 start, Vector2 end, int i)
|
||||
{
|
||||
start.Y = -start.Y;
|
||||
end.Y = -end.Y;
|
||||
|
||||
spriteBatch.Draw(wireSprite.Texture,
|
||||
start, null, Color.White,
|
||||
ToolBox.VectorToAngle(end - start),
|
||||
new Vector2(0.0f, wireSprite.size.Y / 2.0f),
|
||||
new Vector2((Vector2.Distance(start, end)) / wireSprite.Texture.Width, 0.3f),
|
||||
SpriteEffects.None,
|
||||
wireSprite.Depth +0.1f + i * 0.00001f);
|
||||
}
|
||||
|
||||
public override XElement Save(XElement parentElement)
|
||||
{
|
||||
XElement componentElement = base.Save(parentElement);
|
||||
|
||||
if (nodes == null || nodes.Count == 0) return componentElement;
|
||||
|
||||
string[] nodeCoords = new string[nodes.Count()*2];
|
||||
for (int i = 0; i < nodes.Count(); i++)
|
||||
{
|
||||
nodeCoords[i * 2] = nodes[i].X.ToString(CultureInfo.InvariantCulture);
|
||||
nodeCoords[i * 2 + 1] = nodes[i].Y.ToString(CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
componentElement.Add(new XAttribute("nodes", string.Join(";", nodeCoords)));
|
||||
|
||||
return componentElement;
|
||||
}
|
||||
|
||||
public override void Load(XElement componentElement)
|
||||
{
|
||||
base.Load(componentElement);
|
||||
|
||||
string nodeString = ToolBox.GetAttributeString(componentElement, "nodes", "");
|
||||
if (nodeString == "") return;
|
||||
|
||||
string[] nodeCoords = nodeString.Split(';');
|
||||
for (int i = 0; i<nodeCoords.Length/2; i++)
|
||||
{
|
||||
float x = 0.0f, y = 0.0f;
|
||||
|
||||
try
|
||||
{
|
||||
x = float.Parse(nodeCoords[i * 2], CultureInfo.InvariantCulture);
|
||||
}
|
||||
catch { x = 0.0f; }
|
||||
|
||||
try
|
||||
{
|
||||
y = float.Parse(nodeCoords[i * 2 + 1], CultureInfo.InvariantCulture);
|
||||
}
|
||||
catch { y = 0.0f; }
|
||||
|
||||
nodes.Add(new Vector2(x, y));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public override void FillNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetOutgoingMessage message)
|
||||
{
|
||||
message.Write(nodes.Count);
|
||||
for (int i = 0; i < nodes.Count; i++)
|
||||
{
|
||||
message.Write(nodes[i].X);
|
||||
message.Write(nodes[i].Y);
|
||||
}
|
||||
}
|
||||
|
||||
public override void ReadNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetIncomingMessage message)
|
||||
{
|
||||
nodes.Clear();
|
||||
int nodeCount = message.ReadInt32();
|
||||
for (int i = 0; i < nodeCount; i++)
|
||||
{
|
||||
nodes.Add(new Vector2(message.ReadFloat(), message.ReadFloat()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user