Lights, colored items (only used for wires atm)

This commit is contained in:
Regalis
2015-07-22 20:22:50 +03:00
parent 9a7eae8ace
commit 4820e55e05
51 changed files with 726 additions and 191 deletions
+1 -1
View File
@@ -141,7 +141,7 @@ namespace Subsurface.Items.Components
}
public override void Draw(SpriteBatch spriteBatch)
public override void Draw(SpriteBatch spriteBatch, bool editing)
{
base.Draw(spriteBatch);
+2 -2
View File
@@ -250,8 +250,8 @@ namespace Subsurface.Items.Components
linkedGap.Open = 1.0f;
if (convexHull2 != null) convexHull2.Enabled = false;
}
public override void Draw(SpriteBatch spriteBatch)
public override void Draw(SpriteBatch spriteBatch, bool editing)
{
Color color = (item.IsSelected) ? Color.Green : Color.White;
color = color * (item.Condition / 100.0f);
@@ -81,6 +81,8 @@ namespace Subsurface.Items.Components
handlePos[i - 1] = ConvertUnits.ToSimUnits(handlePos[i - 1]);
}
canBePicked = true;
//holdAngle = ToolBox.GetAttributeFloat(element, "holdangle", 0.0f);
//holdAngle = MathHelper.ToRadians(holdAngle);
}
@@ -160,6 +162,8 @@ namespace Subsurface.Items.Components
// return false;
//}
if (attached) return false;
item.body = body;
item.body.Enabled = true;
@@ -173,8 +173,8 @@ namespace Subsurface.Items.Components
//isActive = true;
}
public override void Draw(SpriteBatch spriteBatch)
public override void Draw(SpriteBatch spriteBatch, bool editing)
{
if (!isActive) return;
+1 -1
View File
@@ -293,7 +293,7 @@ namespace Subsurface
/// <summary>a character has dropped the item</summary>
public virtual void Drop(Character dropper) { }
public virtual void Draw(SpriteBatch spriteBatch) { }
public virtual void Draw(SpriteBatch spriteBatch, bool editing = false) { }
public virtual void DrawHUD(SpriteBatch spriteBatch, Character character) { }
@@ -281,7 +281,7 @@ namespace Subsurface.Items.Components
return true;
}
public override void Draw(SpriteBatch spriteBatch)
public override void Draw(SpriteBatch spriteBatch, bool editing)
{
base.Draw(spriteBatch);
@@ -173,7 +173,7 @@ namespace Subsurface.Items.Components
voltage = 0.0f;
}
public override void Draw(SpriteBatch spriteBatch)
public override void Draw(SpriteBatch spriteBatch, bool editing)
{
base.Draw(spriteBatch);
@@ -53,11 +53,6 @@ namespace Subsurface.Items.Components
"power", fullPower / Math.Max(fullLoad, 1.0f));
if (-pt.currPowerConsumption > pt.powerLoad * 2.0f) pt.item.Condition = 0.0f;
}
else
{
//p.Power = fullPower;
}
}
+2 -1
View File
@@ -61,7 +61,8 @@ namespace Subsurface.Items.Components
public override void ReceiveSignal(string signal, Connection connection, Item sender, float power)
{
if (connection.Name=="power_in") voltage = power;
if (currPowerConsumption == 0.0f) voltage = 0.0f;
if (connection.Name == "power_in" || connection.Name == "power") voltage = power;
}
public override void Update(float deltaTime, Camera cam)
+1 -1
View File
@@ -239,7 +239,7 @@ namespace Subsurface.Items.Components
}
}
public override void Draw(SpriteBatch spriteBatch)
public override void Draw(SpriteBatch spriteBatch, bool editing)
{
base.Draw(spriteBatch);
@@ -339,29 +339,31 @@ namespace Subsurface.Items.Components
int textX = (int)start.X;
float connLength = 10.0f;
Color color = (wireEquipped) ? Color.Gray : Color.White;
float alpha = wireEquipped ? 0.5f : 1.0f;
//Color color = (wireEquipped) ? wireItem.Color * 0.5f : wireItem.Color;
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);
new Vector2(end.X - wireVertical.size.X / 2, end.Y + connLength),
new Vector2(wireVertical.size.X, (float)Math.Abs(end.Y - start.Y)), wireItem.Color * alpha);
textX = (int)end.X;
connector.Draw(spriteBatch, end, color);
connector.Draw(spriteBatch, end, Color.White*alpha);
}
else
{
Vector2 pos = new Vector2(start.X, end.Y + wireCorner.size.Y) - wireVertical.size / 2;
Vector2 size = new Vector2(wireVertical.size.X, (float)Math.Abs((end.Y + wireCorner.size.Y) - start.Y));
wireVertical.DrawTiled(spriteBatch, pos, size, color);
wireVertical.DrawTiled(spriteBatch, pos, size, wireItem.Color * alpha);
Rectangle rect = new Rectangle((int)pos.X, (int)pos.Y, (int)size.X, (int)size.Y);
if (!wireEquipped && rect.Contains(PlayerInput.MousePosition)) mouseOn = true;
float dir = (end.X > start.X) ? -1.0f : 1.0f;
wireCorner.Draw(spriteBatch,
new Vector2(start.X, end.Y), color, 0.0f, 1.0f,
wireCorner.Draw(spriteBatch,
new Vector2(start.X, end.Y), wireItem.Color * alpha, 0.0f, 1.0f,
(end.X > start.X) ? SpriteEffects.None : SpriteEffects.FlipHorizontally);
float wireStartX = start.X - wireCorner.size.X / 2 * dir;
@@ -370,11 +372,11 @@ namespace Subsurface.Items.Components
pos = new Vector2(Math.Min(wireStartX,wireEndX), end.Y - wireVertical.size.Y / 2);
size = new Vector2(Math.Abs(wireStartX - wireEndX), wireHorizontal.size.Y);
wireHorizontal.DrawTiled(spriteBatch, pos, size, color);
wireHorizontal.DrawTiled(spriteBatch, pos, size, wireItem.Color * alpha);
rect = new Rectangle((int)pos.X, (int)pos.Y, (int)size.X, (int)size.Y);
if (!wireEquipped && rect.Contains(PlayerInput.MousePosition)) mouseOn = true;
connector.Draw(spriteBatch, end, color, -MathHelper.PiOver2*dir);
connector.Draw(spriteBatch, end, Color.White*alpha, -MathHelper.PiOver2 * dir);
}
if (draggingConnected == null && !wireEquipped)
@@ -1,13 +1,33 @@
using Microsoft.Xna.Framework;
using FarseerPhysics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Subsurface.Lights;
using System;
using System.Xml.Linq;
namespace Subsurface.Items.Components
{
class LightComponent : ItemComponent
class LightComponent : Powered
{
private Color lightColor;
private Sprite sprite;
//private Sprite sprite;
LightSource light;
float range;
float lightBrightness;
[HasDefaultValue(100.0f, true)]
public float Range
{
get { return range; }
set
{
range = MathHelper.Clamp(value, 0.0f, 2048.0f);
}
}
[InGameEditable, HasDefaultValue("1.0,1.0,1.0,1.0", true)]
public string LightColor
@@ -19,27 +39,74 @@ namespace Subsurface.Items.Components
}
}
public override void Move(Vector2 amount)
{
light.Position += amount;
}
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;
}
//foreach (XElement subElement in element.Elements())
//{
// if (subElement.Name.ToString().ToLower() != "sprite") continue;
// sprite = new Sprite(subElement);
// break;
//}
light = new LightSource(item.Position, 100.0f, Color.White);
isActive = true;
//lightColor = new Color(ToolBox.GetAttributeVector4(element, "color", Vector4.One));
}
public override void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch)
public override void Update(float deltaTime, Camera cam)
{
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);
base.Update(deltaTime, cam);
if (item.body != null)
{
light.Position = ConvertUnits.ToDisplayUnits(item.body.Position);
}
if (powerConsumption == 0.0f)
{
voltage = 1.0f;
}
else
{
currPowerConsumption = powerConsumption;
}
if (voltage < Rand.Range(0.0f, minVoltage))
{
lightBrightness = 0.0f;
}
else
{
lightBrightness = MathHelper.Lerp(lightBrightness, Math.Min(voltage, 1.0f), 0.1f);
}
light.Color = lightColor * lightBrightness;
light.Range = range * (float)Math.Sqrt(lightBrightness);
voltage = 0.0f;
}
public override void Draw(SpriteBatch spriteBatch, bool editing)
{
if (!isActive)
{
light.Color = Color.Transparent;
}
}
public override void ReceiveSignal(string signal, Connection connection, Item sender, float power=0.0f)
{
base.ReceiveSignal(signal, connection, sender, power);
switch (connection.Name)
{
case "toggle":
+79 -13
View File
@@ -20,7 +20,9 @@ namespace Subsurface.Items.Components
Connection[] connections;
private Vector2 newNodePos;
private static int? selectedNodeIndex;
public Wire(Item item, XElement element)
: base(item, element)
{
@@ -34,14 +36,14 @@ namespace Subsurface.Items.Components
connections = new Connection[2];
}
public override void Move(Vector2 amount)
{
amount = FarseerPhysics.ConvertUnits.ToDisplayUnits(amount);
for (int i = 0; i<Nodes.Count; i++)
{
Nodes[i] += amount;
}
//for (int i = 0; i<Nodes.Count; i++)
//{
// Nodes[i] += amount;
//}
}
public Connection OtherConnection(Connection connection)
@@ -151,7 +153,7 @@ namespace Subsurface.Items.Components
position.Y += item.CurrentHull.Rect.Y - item.CurrentHull.Rect.Height;
}
newNodePos = position;
newNodePos = RoundNode(item.Position, item.CurrentHull);
//if (Vector2.Distance(position, nodes[nodes.Count - 1]) > nodeDistance*10)
//{
@@ -170,14 +172,13 @@ namespace Subsurface.Items.Components
public override bool Use(float deltaTime, Character character = null)
{
if (character == Character.Controlled && character.SelectedConstruction != null) return false;
if (newNodePos!= Vector2.Zero && Nodes.Count>0 && Vector2.Distance(newNodePos, Nodes[Nodes.Count - 1]) > nodeDistance)
{
Nodes.Add(newNodePos);
newNodePos = Vector2.Zero;
}
return true;
}
@@ -213,6 +214,23 @@ namespace Subsurface.Items.Components
}
}
private Vector2 RoundNode(Vector2 position, Hull hull)
{
position.X = MathUtils.Round(position.X, nodeDistance);
if (hull == null)
{
position.Y = MathUtils.Round(position.Y, nodeDistance);
}
else
{
position.Y -= hull.Rect.Y - hull.Rect.Height;
position.Y = Math.Max(MathUtils.Round(position.Y, nodeDistance), heightFromFloor);
position.Y += hull.Rect.Y -hull.Rect.Height;
}
return position;
}
private void CleanNodes()
{
for (int i = Nodes.Count - 2; i > 0; i--)
@@ -246,7 +264,7 @@ namespace Subsurface.Items.Components
}
public override void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch)
public override void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch, bool editing)
{
if (Nodes.Count == 0) return;
@@ -257,15 +275,63 @@ namespace Subsurface.Items.Components
for (int i = 1; i < Nodes.Count; i++)
{
DrawSection(spriteBatch, Nodes[i], Nodes[i - 1], i, Color.White);
DrawSection(spriteBatch, Nodes[i], Nodes[i - 1], i, item.Color);
}
if (isActive && Vector2.Distance(newNodePos, Nodes[Nodes.Count - 1]) > nodeDistance)
{
DrawSection(spriteBatch, Nodes[Nodes.Count - 1], newNodePos, Nodes.Count, Color.White * 0.5f);
DrawSection(spriteBatch, Nodes[Nodes.Count - 1], newNodePos, Nodes.Count, item.Color * 0.5f);
//nodes.Add(newNodePos);
}
if (!editing) return;
for (int i = 1; i < Nodes.Count; i++)
{
GUI.DrawRectangle(spriteBatch, new Rectangle((int)Nodes[i].X - 3, (int)-Nodes[i].Y -3, 6, 6), Color.Red, true, 0.0f);
if (Vector2.Distance(Game1.EditMapScreen.Cam.ScreenToWorld(PlayerInput.MousePosition), Nodes[i]) < 20.0f)
{
GUI.DrawRectangle(spriteBatch, new Rectangle((int)Nodes[i].X - 10, (int)-Nodes[i].Y - 10, 20, 20), Color.Red, false, 0.0f);
if (selectedNodeIndex==null && selectedNodeIndex>0 && selectedNodeIndex<Nodes.Count-1)
{
if ( PlayerInput.LeftButtonDown())
{
MapEntity.SelectEntity(item);
selectedNodeIndex = i;
}
else
{
Nodes.RemoveAt(i);
break;
}
}
}
}
if (PlayerInput.LeftButtonDown())
{
if (selectedNodeIndex!=null && item.IsSelected)
{
MapEntity.DisableSelect = true;
Nodes[(int)selectedNodeIndex] = Game1.EditMapScreen.Cam.ScreenToWorld(PlayerInput.MousePosition);
Vector2 pos = Nodes[(int)selectedNodeIndex];
Nodes[(int)selectedNodeIndex] = RoundNode(Nodes[(int)selectedNodeIndex], Hull.FindHull(Nodes[(int)selectedNodeIndex]));
MapEntity.SelectEntity(item);
}
}
else
{
//if (selectedNodeIndex != null) MapEntity.SelectEntity(item); ;
selectedNodeIndex = null;
}
}
private void DrawSection(SpriteBatch spriteBatch, Vector2 start, Vector2 end, int i, Color color)
+1 -1
View File
@@ -79,7 +79,7 @@ namespace Subsurface.Items.Components
ToolBox.GetAttributeVector2(element, "origin", Vector2.Zero));
}
public override void Draw(SpriteBatch spriteBatch)
public override void Draw(SpriteBatch spriteBatch, bool editing)
{
barrelSprite.Draw(spriteBatch, new Vector2(item.Rect.X, -item.Rect.Y) + barrelPos, rotation + MathHelper.PiOver2, 1.0f);
}