diff --git a/Devices/Area.cs b/Devices/Area.cs index 40767b0..6893d0e 100644 --- a/Devices/Area.cs +++ b/Devices/Area.cs @@ -22,7 +22,7 @@ public class RectArea : Area public override void Draw(SpriteBatch spriteBatch, Color color) { - if (!WireMod.Instance.GetScreenRect().Intersects(this.GetRect())) return; + if (!Helpers.GetScreenRect().Intersects(this.GetRect())) return; var size = this.GetRect().Width; var rect = Helpers.CreateRectangle(size, size); diff --git a/Devices/Comparison/Equals.cs b/Devices/Comparison/Equals.cs index d5adaf6..6ecdf75 100644 --- a/Devices/Comparison/Equals.cs +++ b/Devices/Comparison/Equals.cs @@ -27,18 +27,18 @@ public Equals() private int GetOutput() { - if (!this.Pins["In"][0].IsConnected() || !this.Pins["In"][1].IsConnected()) return -1; - if (this.Pins["In"][0].DataType != this.Pins["In"][1].DataType) return -1; + if (!this.GetPinIn(0).IsConnected() || !this.GetPinIn(1).IsConnected()) return -1; + if (this.GetPinIn(0).DataType != this.GetPinIn(1).DataType) return -1; - switch (this.Pins["In"][0].DataType) + switch (this.GetPinIn(0).DataType) { case "int": - if (!int.TryParse(this.Pins["In"][0].GetValue(), out var in0)) return -2; - if (!int.TryParse(this.Pins["In"][1].GetValue(), out var in1)) return -2; + if (!int.TryParse(this.GetPinIn(0).GetValue(), out var in0)) return -2; + if (!int.TryParse(this.GetPinIn(1).GetValue(), out var in1)) return -2; return in0 == in1 ? 1 : 0; case "string": default: - return this.Pins["In"][0].GetValue() == this.Pins["In"][1].GetValue() ? 1 : 0; + return GetPinIn(0).GetValue() == this.GetPinIn(1).GetValue() ? 1 : 0; } } } diff --git a/Devices/Comparison/GreaterThan.cs b/Devices/Comparison/GreaterThan.cs index 861df41..0cff13c 100644 --- a/Devices/Comparison/GreaterThan.cs +++ b/Devices/Comparison/GreaterThan.cs @@ -24,9 +24,9 @@ public GreaterThan() private int GetOutput() { - if (!this.Pins["In"][0].IsConnected() || !this.Pins["In"][1].IsConnected()) return -1; - if (!int.TryParse(this.Pins["In"][0].GetValue(), out var in0)) return -1; - if (!int.TryParse(this.Pins["In"][1].GetValue(), out var in1)) return -1; + if (!this.GetPinIn(0).IsConnected() || !this.Pins["In"][1].IsConnected()) return -1; + if (!int.TryParse(this.GetPinIn(0).GetValue(), out var in0)) return -1; + if (!int.TryParse(this.GetPinIn(1).GetValue(), out var in1)) return -1; return in0 > in1 ? 1 : 0; } } diff --git a/Devices/Comparison/LessThan.cs b/Devices/Comparison/LessThan.cs index dba5f14..a073ad7 100644 --- a/Devices/Comparison/LessThan.cs +++ b/Devices/Comparison/LessThan.cs @@ -24,9 +24,9 @@ public LessThan() private int GetOutput() { - if (!this.Pins["In"][0].IsConnected() || !this.Pins["In"][1].IsConnected()) return -1; - if (!int.TryParse(this.Pins["In"][0].GetValue(), out var in0)) return -1; - if (!int.TryParse(this.Pins["In"][1].GetValue(), out var in1)) return -1; + if (!this.GetPinIn(0).IsConnected() || !this.GetPinIn(1).IsConnected()) return -1; + if (!int.TryParse(this.GetPinIn(0).GetValue(), out var in0)) return -1; + if (!int.TryParse(this.GetPinIn(1).GetValue(), out var in1)) return -1; return in0 < in1 ? 1 : 0; } } diff --git a/Devices/Device.cs b/Devices/Device.cs index 25e28c2..825747a 100644 --- a/Devices/Device.cs +++ b/Devices/Device.cs @@ -50,14 +50,15 @@ public Rectangle LocationScreenRect { get { - var screenRect = WireMod.Instance.GetScreenRect(); + var screenRect = Helpers.GetScreenRect(); var worldRect = this.LocationWorldRect; return !worldRect.Intersects(screenRect) ? default(Rectangle) : new Rectangle(worldRect.X - screenRect.X, worldRect.Y - screenRect.Y, worldRect.Width, worldRect.Height); } } - public Vector2 LocationOriginWorld => (this.LocationTile + this.Origin).ToWorldCoordinates(); + public Point16 LocationOriginTile => this.LocationTile + this.Origin; + public Vector2 LocationOriginWorld => this.LocationOriginTile.ToWorldCoordinates(); public Vector2 LocationOriginScreen => this.LocationOriginWorld - Main.screenPosition; public string DetectType() @@ -89,6 +90,12 @@ public void SetAutoType(string dataType = null) } } + public Pin GetPin(string name) => this.Pins["In"].Values.FirstOrDefault(p => p.Name == name) ?? this.Pins["Out"].Values.FirstOrDefault(p => p.Name == name); + public PinIn GetPinIn(string name) => (PinIn)this.Pins["In"].Values.FirstOrDefault(p => p.Name == name); + public PinIn GetPinIn(int index) => (PinIn)this.Pins["In"][index]; + public PinOut GetPinOut(string name) => (PinOut)this.Pins["Out"].Values.FirstOrDefault(p => p.Name == name); + public PinOut GetPinOut(int index) => (PinOut)this.Pins["Out"][index]; + public virtual void OnRightClick(Pin pin = null) { } public virtual void OnKill() diff --git a/Devices/Gates/AndGate.cs b/Devices/Gates/AndGate.cs index deb2d2c..4f27ac4 100644 --- a/Devices/Gates/AndGate.cs +++ b/Devices/Gates/AndGate.cs @@ -24,9 +24,9 @@ public AndGate() private int GetOutput() { - if (!this.Pins["In"][0].IsConnected() || !this.Pins["In"][1].IsConnected()) return -1; - if (!int.TryParse(this.Pins["In"][0].GetValue(), out var in0)) return -1; - if (!int.TryParse(this.Pins["In"][1].GetValue(), out var in1)) return -1; + if (!this.GetPinIn(0).IsConnected() || !this.Pins["In"][1].IsConnected()) return -1; + if (!int.TryParse(this.GetPinIn(0).GetValue(), out var in0)) return -1; + if (!int.TryParse(this.GetPinIn(1).GetValue(), out var in1)) return -1; return in0 == 1 && in1 == 1 ? 1 : 0; } } diff --git a/Devices/Gates/If.cs b/Devices/Gates/If.cs index 1e74bd4..d5565af 100644 --- a/Devices/Gates/If.cs +++ b/Devices/Gates/If.cs @@ -25,19 +25,19 @@ public If() public string Output(Pin pin = null) { - if (!this.Pins["In"][0].IsConnected()) return "-1"; - if (!int.TryParse(this.Pins["In"][0].GetValue(), out var condition)) return "-2"; + if (!this.GetPin("Condition").IsConnected()) return "-1"; + if (!int.TryParse(this.GetPin("Condition").GetValue(), out var condition)) return "-2"; switch (this.DetectType()) { case "auto": return "-1"; case "bool": case "int": - if (!int.TryParse(this.Pins["In"][1].GetValue(), out var trueInput)) trueInput = 0; - if (!int.TryParse(this.Pins["In"][2].GetValue(), out var falseInput)) falseInput = 0; + if (!int.TryParse(this.GetPin("TrueValue").GetValue(), out var trueInput)) trueInput = 0; + if (!int.TryParse(this.GetPin("FalseValue").GetValue(), out var falseInput)) falseInput = 0; return condition == 1 ? trueInput.ToString() : falseInput.ToString(); case "string": - return condition == 1 ? this.Pins["In"][1].GetValue() : this.Pins["In"][2].GetValue(); + return condition == 1 ? this.GetPin("TrueValue").GetValue() : this.GetPin("FalseValue").GetValue(); // TODO: Add other data types } diff --git a/Devices/Gates/NotGate.cs b/Devices/Gates/NotGate.cs index fe7b68d..0b9cb80 100644 --- a/Devices/Gates/NotGate.cs +++ b/Devices/Gates/NotGate.cs @@ -23,8 +23,8 @@ public NotGate() private int GetOutput() { - if (!this.Pins["In"][0].IsConnected()) return -1; - if (!int.TryParse(this.Pins["In"][0].GetValue(), out var in0)) return -1; + if (!this.GetPinIn(0).IsConnected()) return -1; + if (!int.TryParse(this.GetPinIn(0).GetValue(), out var in0)) return -1; return in0 == 1 ? 0 : 1; } } diff --git a/Devices/Gates/OrGate.cs b/Devices/Gates/OrGate.cs index 202ce0c..08f5303 100644 --- a/Devices/Gates/OrGate.cs +++ b/Devices/Gates/OrGate.cs @@ -24,9 +24,9 @@ public OrGate() private int GetOutput() { - if (!this.Pins["In"][0].IsConnected() || !this.Pins["In"][1].IsConnected()) return -1; - if (!int.TryParse(this.Pins["In"][0].GetValue(), out var in0)) return -1; - if (!int.TryParse(this.Pins["In"][1].GetValue(), out var in1)) return -1; + if (!this.GetPinIn(0).IsConnected() || !this.Pins["In"][1].IsConnected()) return -1; + if (!int.TryParse(this.GetPinIn(0).GetValue(), out var in0)) return -1; + if (!int.TryParse(this.GetPinIn(1).GetValue(), out var in1)) return -1; return in0 == 1 || in1 == 1 ? 1 : 0; } } diff --git a/Devices/Inputs/AreaInput.cs b/Devices/Inputs/AreaInput.cs index 0ede22a..525ed9d 100644 --- a/Devices/Inputs/AreaInput.cs +++ b/Devices/Inputs/AreaInput.cs @@ -30,20 +30,14 @@ public AreaInput() public string Output(Pin pin = null) { - if (!this.Pins["In"][0].IsConnected() || !int.TryParse(this.Pins["In"][0].GetValue(), out var radius)) return ""; + if (!this.GetPin("Distance").IsConnected() || !int.TryParse(this.GetPin("Distance").GetValue(), out var distance)) return ""; - return $"{this.Settings["AreaType"]}:{radius}"; - } - - public override void OnRightClick(Pin pin = null) - { - this.Settings["AreaType"] = AreaTypes[(AreaTypes.IndexOf(this.Settings["AreaType"]) + 1) % AreaTypes.Count]; + return $"{this.Settings["AreaType"]}:{distance}"; } public override void Draw(SpriteBatch spriteBatch) { - if (this.LocationRect == default(Rectangle)) return; - if (!this.Pins["In"][0].IsConnected()) return; + if (!this.GetPin("Distance").IsConnected()) return; this.GetArea(this).Draw(spriteBatch, Color.LightGreen); } @@ -51,13 +45,13 @@ public override void Draw(SpriteBatch spriteBatch) public Area GetArea(Device device) { var radius = 0; - if (this.Pins["In"][0].IsConnected() && int.TryParse(this.Pins["In"][0].GetValue(), out var dist)) + if (this.GetPin("Distance").IsConnected() && int.TryParse(this.GetPin("Distance").GetValue(), out var dist)) { radius = dist; } var pos = device.LocationOriginWorld; - if (this.Pins["In"][1].IsConnected() && Helpers.TryParsePoint(this.Pins["In"][1].GetValue(), out var point) && point.HasValue) + if (this.GetPin("Point").IsConnected() && Helpers.TryParsePoint(this.GetPin("Point").GetValue(), out var point) && point.HasValue) { pos = point.Value.ToWorldCoordinates(); } @@ -78,6 +72,11 @@ public Area GetArea(Device device) }; } + public override void OnRightClick(Pin pin = null) + { + this.Settings["AreaType"] = AreaTypes[(AreaTypes.IndexOf(this.Settings["AreaType"]) + 1) % AreaTypes.Count]; + } + private static readonly List AreaTypes = new List { "Square", diff --git a/Devices/Inputs/PointConstant.cs b/Devices/Inputs/PointConstant.cs index 7eda1b3..3643cb4 100644 --- a/Devices/Inputs/PointConstant.cs +++ b/Devices/Inputs/PointConstant.cs @@ -18,10 +18,6 @@ public PointConstant() }; } - public string Output(Pin pin = null) - { - var pos = this.LocationTile + this.Origin; - return $"{pos.X},{pos.Y}"; - } + public string Output(Pin pin = null) => $"{this.LocationOriginTile.X},{this.LocationOriginTile.Y}"; } } diff --git a/Devices/Inputs/RandomInt.cs b/Devices/Inputs/RandomInt.cs index 3aa06f2..ff41b57 100644 --- a/Devices/Inputs/RandomInt.cs +++ b/Devices/Inputs/RandomInt.cs @@ -32,8 +32,8 @@ public RandomInt() public void Trigger(Pin pin = null) { - if (!int.TryParse(this.Pins["In"][1].GetValue(), out var min)) min = 0; - if (!int.TryParse(this.Pins["In"][2].GetValue(), out var max)) max = 100; + if (!int.TryParse(this.GetPin("Min").GetValue(), out var min)) min = 0; + if (!int.TryParse(this.GetPin("Max").GetValue(), out var max)) max = 100; this.Settings["Value"] = WorldGen.genRand.Next(min, max).ToString(); } diff --git a/Devices/Maths/Add.cs b/Devices/Maths/Add.cs index acb37a0..9beed02 100644 --- a/Devices/Maths/Add.cs +++ b/Devices/Maths/Add.cs @@ -24,9 +24,9 @@ public Add() private int GetOutput() { - if (!this.Pins["In"][0].IsConnected() || !this.Pins["In"][1].IsConnected()) return -1; - if (!int.TryParse(this.Pins["In"][0].GetValue(), out var in0)) return -1; - if (!int.TryParse(this.Pins["In"][1].GetValue(), out var in1)) return -1; + if (!this.GetPinIn(0).IsConnected() || !this.GetPinIn(1).IsConnected()) return -1; + if (!int.TryParse(this.GetPinIn(0).GetValue(), out var in0)) return -1; + if (!int.TryParse(this.GetPinIn(1).GetValue(), out var in1)) return -1; return in0 + in1; } } diff --git a/Devices/Maths/Divide.cs b/Devices/Maths/Divide.cs index 3428afd..e5fcb8b 100644 --- a/Devices/Maths/Divide.cs +++ b/Devices/Maths/Divide.cs @@ -24,9 +24,9 @@ public Divide() private int GetOutput() { - if (!this.Pins["In"][0].IsConnected() || !this.Pins["In"][1].IsConnected()) return -1; - if (!int.TryParse(this.Pins["In"][0].GetValue(), out var in0)) return -1; - if (!int.TryParse(this.Pins["In"][1].GetValue(), out var in1)) return -1; + if (!this.GetPinIn(0).IsConnected() || !this.GetPinIn(1).IsConnected()) return -1; + if (!int.TryParse(this.GetPinIn(0).GetValue(), out var in0)) return -1; + if (!int.TryParse(this.GetPinIn(1).GetValue(), out var in1)) return -1; if (in1 == 0) return 0; // Divide by zero protection return in0 / in1; } diff --git a/Devices/Maths/Modulo.cs b/Devices/Maths/Modulo.cs index 7ffd720..014684b 100644 --- a/Devices/Maths/Modulo.cs +++ b/Devices/Maths/Modulo.cs @@ -24,10 +24,10 @@ public Modulo() private int GetOutput() { - if (!this.Pins["In"][0].IsConnected() || !this.Pins["In"][1].IsConnected()) return -1; - if (!int.TryParse(this.Pins["In"][0].GetValue(), out var in0)) return -1; - if (!int.TryParse(this.Pins["In"][1].GetValue(), out var in1)) return -1; - if (in1 == 0) return 0; + if (!this.GetPinIn(0).IsConnected() || !this.GetPinIn(1).IsConnected()) return -1; + if (!int.TryParse(this.GetPinIn(0).GetValue(), out var in0)) return -1; + if (!int.TryParse(this.GetPinIn(1).GetValue(), out var in1)) return -1; + if (in1 == 0) return 0; // Mod by zero protection return in0 % in1; } } diff --git a/Devices/Maths/Multiply.cs b/Devices/Maths/Multiply.cs index b6384e3..9531d20 100644 --- a/Devices/Maths/Multiply.cs +++ b/Devices/Maths/Multiply.cs @@ -24,9 +24,9 @@ public Multiply() private int GetOutput() { - if (!this.Pins["In"][0].IsConnected() || !this.Pins["In"][1].IsConnected()) return -1; - if (!int.TryParse(this.Pins["In"][0].GetValue(), out var in0)) return -1; - if (!int.TryParse(this.Pins["In"][1].GetValue(), out var in1)) return -1; + if (!this.GetPinIn(0).IsConnected() || !this.GetPinIn(1).IsConnected()) return -1; + if (!int.TryParse(this.GetPinIn(0).GetValue(), out var in0)) return -1; + if (!int.TryParse(this.GetPinIn(1).GetValue(), out var in1)) return -1; return in0 * in1; } } diff --git a/Devices/Maths/Subtract.cs b/Devices/Maths/Subtract.cs index 2770dfd..24023d6 100644 --- a/Devices/Maths/Subtract.cs +++ b/Devices/Maths/Subtract.cs @@ -24,9 +24,9 @@ public Subtract() private int GetOutput() { - if (!this.Pins["In"][0].IsConnected() || !this.Pins["In"][1].IsConnected()) return -1; - if (!int.TryParse(this.Pins["In"][0].GetValue(), out var in0)) return -1; - if (!int.TryParse(this.Pins["In"][1].GetValue(), out var in1)) return -1; + if (!this.GetPinIn(0).IsConnected() || !this.GetPinIn(1).IsConnected()) return -1; + if (!int.TryParse(this.GetPinIn(0).GetValue(), out var in0)) return -1; + if (!int.TryParse(this.GetPinIn(1).GetValue(), out var in1)) return -1; return in0 - in1; } } diff --git a/Devices/Other/Buffer.cs b/Devices/Other/Buffer.cs index 65af04c..97b68b2 100644 --- a/Devices/Other/Buffer.cs +++ b/Devices/Other/Buffer.cs @@ -22,7 +22,7 @@ public Buffer() this.PinLayout = new List { - new PinDesign("In", 0, new Point16(1, 0), "int", "Buff ID"), + new PinDesign("In", 0, new Point16(1, 0), "int", "BuffID"), new PinDesign("In", 1, new Point16(0, 1), "int", "Duration"), new PinDesign("In", 2, new Point16(2, 1), "int", "Distance"), }; @@ -30,13 +30,13 @@ public Buffer() public override void Update(GameTime gameTime) { - if (!this.Pins["In"][0].IsConnected()) return; - if (!int.TryParse(this.Pins["In"][0].GetValue(), out var buffId)) return; + if (!this.GetPin("BuffID").IsConnected()) return; + if (!int.TryParse(this.GetPin("BuffID").GetValue(), out var buffId)) return; if (buffId == 0) return; - if (!int.TryParse(this.Pins["In"][2].GetValue(), out var maxDistance)) return; + if (!int.TryParse(this.GetPin("Distance").GetValue(), out var maxDistance)) return; - if (!int.TryParse(this.Pins["In"][1].GetValue(), out var duration)) duration = 10; + if (!int.TryParse(this.GetPin("Duration").GetValue(), out var duration)) duration = 10; duration *= 100; @@ -50,11 +50,9 @@ public override void Update(GameTime gameTime) case "NPCs" when !npcs.Any(): return; case "NPCs": - //Main.NewText($"NPC Buff: {buffId} ({duration} seconds)"); npcs.ForEach(n => n.AddBuff(buffId, duration)); break; case "Players": - //Main.NewText($"Player Buff: {buffId} ({duration} seconds)"); players.ForEach(p => p.AddBuff(buffId, duration)); break; } @@ -62,10 +60,9 @@ public override void Update(GameTime gameTime) public override void Draw(SpriteBatch spriteBatch) { - if (this.LocationRect == default(Rectangle)) return; - if (!this.Pins["In"][2].IsConnected() || !int.TryParse(this.Pins["In"][2].GetValue(), out var maxDistance)) return; + if (!this.GetPin("Distance").IsConnected() || !int.TryParse(this.GetPin("Distance").GetValue(), out var maxDistance)) return; if (maxDistance == 0) return; - if (!int.TryParse(this.Pins["In"][0].GetValue(), out var buffId)) return; + if (!int.TryParse(this.GetPin("BuffID").GetValue(), out var buffId)) return; if (buffId == 0) return; var pixels = 16; diff --git a/Devices/Other/Repulsor.cs b/Devices/Other/Repulsor.cs index 35076d2..5aad512 100644 --- a/Devices/Other/Repulsor.cs +++ b/Devices/Other/Repulsor.cs @@ -31,10 +31,9 @@ public Repulsor() public override void Draw(SpriteBatch spriteBatch) { - if (this.LocationRect == default(Rectangle)) return; - if (!this.Pins["In"][2].IsConnected() || !int.TryParse(this.Pins["In"][2].GetValue(), out var maxDistance)) return; + if (!this.GetPin("Distance").IsConnected() || !int.TryParse(this.GetPin("Distance").GetValue(), out var maxDistance)) return; if (maxDistance == 0) return; - if (!int.TryParse(this.Pins["In"][0].GetValue(), out var armed)) return; + if (!int.TryParse(this.GetPin("Armed").GetValue(), out var armed)) return; if (armed == 0) return; var deviceScreenRect = this.LocationScreenRect; @@ -48,33 +47,31 @@ public override void Draw(SpriteBatch spriteBatch) public override void Update(GameTime gameTime) { - if (!this.Pins["In"][0].IsConnected()) return; + if (!this.GetPin("Armed").IsConnected()) return; if (!int.TryParse(this.Pins["In"][0].GetValue(), out var armed)) return; if (armed == 0) return; - if (!this.Pins["In"][1].IsConnected() || !int.TryParse(this.Pins["In"][1].GetValue(), out var maxVelocity)) return; - if (!this.Pins["In"][2].IsConnected() || !int.TryParse(this.Pins["In"][2].GetValue(), out var maxDistance)) return; + if (!this.GetPin("Velocity").IsConnected() || !int.TryParse(this.GetPin("Velocity").GetValue(), out var maxVelocity)) return; + if (!this.GetPin("Distance").IsConnected() || !int.TryParse(this.GetPin("Distance").GetValue(), out var maxDistance)) return; var entities = new List(); if (this.Settings["TargetType"] == "All" || this.Settings["TargetType"] == "Players") { - entities.AddRange(Main.player.Select(p => new { player = p, distance = (p.position - this.LocationRect.Location.ToWorldCoordinates()).Length() }).OrderBy(p => p.distance).Select(p => p.player)); + entities.AddRange(Main.player.Select(p => new { player = p, distance = (this.LocationOriginWorld - p.position).Length() }).OrderBy(p => p.distance).Select(p => p.player)); } if (this.Settings["TargetType"] == "All" || this.Settings["TargetType"] == "NPCs") { - entities.AddRange(Main.npc.Select(p => new { player = p, distance = (p.position - this.LocationRect.Location.ToWorldCoordinates()).Length() }).OrderBy(p => p.distance).Select(p => p.player)); + entities.AddRange(Main.npc.Select(p => new { player = p, distance = (this.LocationOriginWorld - p.position).Length() }).OrderBy(p => p.distance).Select(p => p.player)); } if (!entities.Any()) return; foreach (var entity in entities) { - var direction = entity.position - (this.LocationWorld + this.Origin.ToWorldCoordinates()); - //var pLeft = entity.velocity.X < 0; - //var pUp = entity.velocity.Y < 0; + var direction = this.LocationOriginWorld - entity.position; var distance = direction.Length(); diff --git a/Devices/Other/Spawner.cs b/Devices/Other/Spawner.cs index d300e7f..1441058 100644 --- a/Devices/Other/Spawner.cs +++ b/Devices/Other/Spawner.cs @@ -25,13 +25,13 @@ public Spawner() this.PinLayout = new List { new PinDesign("In", 0, new Point16(1, 0), "bool", "Trigger"), - new PinDesign("In", 1, new Point16(0, 1), "int", "NPC ID"), + new PinDesign("In", 1, new Point16(0, 1), "int", "NPCID"), }; } public void Trigger(Pin pin = null) { - if (!int.TryParse(this.Pins["In"][1].GetValue(), out var id)) return; + if (!int.TryParse(this.GetPin("NPCID").GetValue(), out var id)) return; if (this._blacklist.Contains(id)) { @@ -40,7 +40,7 @@ public void Trigger(Pin pin = null) } // TODO: Figure out what the 'Start' argument actually does - var npcId = NPC.NewNPC((int)this.LocationWorld.X + (this.Origin.X * 16) + 8, (int)this.LocationWorld.Y + (this.Origin.Y * 16) + 8, id, 1); + var npcId = NPC.NewNPC((int)this.LocationOriginWorld.X, (int)this.LocationOriginWorld.Y, id, 1); var npc = Main.npc[npcId]; // Do something with NPC? diff --git a/Devices/Outputs/FlipFlop.cs b/Devices/Outputs/FlipFlop.cs index c773e26..898d825 100644 --- a/Devices/Outputs/FlipFlop.cs +++ b/Devices/Outputs/FlipFlop.cs @@ -32,8 +32,8 @@ public FlipFlop() private int GetOutput() { if (!int.TryParse(this.Settings["Value"], out var val)) return -1; - if (!this.Pins["In"][0].IsConnected()) return -1; - if (!int.TryParse(this.Pins["In"][0].GetValue(), out var in0)) return -1; + if (!this.GetPinIn(0).IsConnected()) return -1; + if (!int.TryParse(this.GetPinIn(0).GetValue(), out var in0)) return -1; if ((in0 <= 0 && val > 0) || (in0 > 0 && val <= 0)) { @@ -45,12 +45,12 @@ private int GetOutput() if (Main.netMode != NetmodeID.MultiplayerClient) { - Wiring.TripWire(this.Pins["Out"][0].Location.X, this.Pins["Out"][0].Location.Y, 1, 1); + Wiring.TripWire(this.GetPinOut(0).Location.X, this.GetPinOut(0).Location.Y, 1, 1); } else { - Wiring.TripWire(this.Pins["Out"][0].Location.X, this.Pins["Out"][0].Location.Y, 1, 1); - WireMod.PacketHandler.SendTripWire(256, Main.myPlayer, this.Pins["Out"][0].Location.X, this.Pins["Out"][0].Location.Y); + Wiring.TripWire(this.GetPinOut(0).Location.X, this.GetPinOut(0).Location.Y, 1, 1); + WireMod.PacketHandler.SendTripWire(256, Main.myPlayer, this.GetPinOut(0).Location.X, this.GetPinOut(0).Location.Y); } } } diff --git a/Devices/Outputs/OutputLamp.cs b/Devices/Outputs/OutputLamp.cs index 698cee8..0afbae4 100644 --- a/Devices/Outputs/OutputLamp.cs +++ b/Devices/Outputs/OutputLamp.cs @@ -23,7 +23,7 @@ public override Rectangle GetSourceRect(int style = -1) { if (style == -1) { - if (!int.TryParse(this.Pins["In"][0].GetValue(), out var input)) return base.GetSourceRect(style); + if (!int.TryParse(this.GetPinIn(0).GetValue(), out var input)) return base.GetSourceRect(style); style = input + 1; } diff --git a/Devices/Outputs/OutputSign.cs b/Devices/Outputs/OutputSign.cs deleted file mode 100644 index e452d64..0000000 --- a/Devices/Outputs/OutputSign.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System.Collections.Generic; -using Terraria.DataStructures; - -namespace WireMod.Devices -{ - internal class OutputSign : Device - { - public OutputSign() - { - this.Name = "Output Sign"; - this.Width = 1; - this.Height = 2; - this.Origin = new Point16(0, 1); - - this.PinLayout = new List - { - new PinDesign("In", 0, new Point16(0, 0), "string"), - }; - } - } -} diff --git a/Devices/Outputs/Trigger.cs b/Devices/Outputs/Trigger.cs index faa8fbd..91e116f 100644 --- a/Devices/Outputs/Trigger.cs +++ b/Devices/Outputs/Trigger.cs @@ -39,7 +39,7 @@ private int GetOutput() if (!this.Pins["In"][0].IsConnected()) return -1; if (!int.TryParse(this.Pins["In"][0].GetValue(), out var trigger)) return -1; - if (this.Pins["In"][1].IsConnected() && int.TryParse(this.Pins["In"][1].GetValue(), out var reset)) + if (this.GetPin("Trigger").IsConnected() && int.TryParse(this.GetPin("Trigger").GetValue(), out var reset)) { if (reset == 1) this._reset = true; } @@ -48,14 +48,15 @@ private int GetOutput() { this._reset = false; - if (!this.Pins["Out"][0].IsConnected()) + if (!this.GetPinOut(0).IsConnected()) { this.Settings["Value"] = trigger.ToString(); return 0; } - var pins = ((PinOut)this.Pins["Out"][0]).ConnectedPins.Where(p => p.Device is ITriggered).ToList(); + var pins = this.GetPinOut(0).ConnectedPins.Where(p => p.Device is ITriggered).ToList(); if (!int.TryParse(this.Settings["TriggerTarget"], out var target)) return 0; + if (target >= pins.Count) target = pins.Count - 1; // Trigger connected devices if (this.Settings["TriggerType"] == "All") @@ -77,7 +78,7 @@ private int GetOutput() return 1; } - if (trigger <= 0 && val > 0 && !this.Pins["In"][1].IsConnected()) + if (trigger <= 0 && val > 0 && !this.GetPinIn(1).IsConnected()) { this._reset = true; } @@ -89,7 +90,9 @@ private int GetOutput() public override List<(string Line, Color Color, float Size)> Debug(Pin pin = null) { var debug = base.Debug(pin); + debug.Add(($"Reset: {(this._reset ? "True" : "False")}", Color.Black, WireMod.SmallText)); + return debug; } diff --git a/Devices/Pin.cs b/Devices/Pin.cs index d81b0f9..28594f9 100644 --- a/Devices/Pin.cs +++ b/Devices/Pin.cs @@ -50,7 +50,7 @@ public int GetInt() public Wire GetWire(Pin connectedPin) => this.Wires.FirstOrDefault(w => w.StartPin == this && w.EndPin == connectedPin || w.StartPin == connectedPin && w.EndPin == this); } - internal class PinIn : Pin + public class PinIn : Pin { public Pin ConnectedPin { get; set; } private string _value => this.ConnectedPin?.GetValue(); @@ -94,7 +94,7 @@ public override List GetDebug() } } - internal class PinOut : Pin + public class PinOut : Pin { public List ConnectedPins { get; set; } = new List(); private string _value => (this.Device is IOutput ? ((IOutput)this.Device).Output(this) : ""); diff --git a/Devices/Sensors/NPCCounter.cs b/Devices/Sensors/NPCCounter.cs index 1dfe3e2..c125310 100644 --- a/Devices/Sensors/NPCCounter.cs +++ b/Devices/Sensors/NPCCounter.cs @@ -18,8 +18,8 @@ public NPCCounter() this.PinLayout = new List { new PinDesign("In", 0, new Point16(1, 0), "area", "Area"), - new PinDesign("In", 1, new Point16(0, 1), "bool", "Hostile Filter"), - new PinDesign("In", 2, new Point16(2, 1), "bool", "TownNPC Filter"), + new PinDesign("In", 1, new Point16(0, 1), "bool", "IsHostile"), + new PinDesign("In", 2, new Point16(2, 1), "bool", "IsTownNPC"), new PinDesign("Out", 0, new Point16(1, 2), "int", "Count"), }; } @@ -28,32 +28,31 @@ public NPCCounter() private int GetOutput() { - if (!this.Pins["In"][0].IsConnected()) return -1; + if (!this.GetPin("Area").IsConnected()) return -1; return this.GetNPCs().Count(); } private IEnumerable GetNPCs() { - var npc = Main.npc.Select(n => n).Where(n => n.life > 0 && !n.dontCountMe); - var pos = this.LocationWorld + new Vector2(8, 8); + if (!this.GetPin("Area").IsConnected()) return new List(); - if (this.Pins["In"][1].IsConnected() && int.TryParse(this.Pins["In"][1].GetValue(), out var hostile)) + var input = this.GetPinIn("Area").ConnectedPin.Device; + if (!(input is AreaInput areaInput)) return new List(); + + var npc = Main.npc.Select(n => n).Where(n => !n.dontCountMe); + + if (this.GetPin("IsHostile").IsConnected() && int.TryParse(this.GetPin("IsHostile").GetValue(), out var hostile)) { npc = npc.Where(n => n.friendly == (hostile == 0)); } - if (this.Pins["In"][2].IsConnected() && int.TryParse(this.Pins["In"][2].GetValue(), out var character)) + if (this.GetPin("IsTownNPC").IsConnected() && int.TryParse(this.GetPin("IsTownNPC").GetValue(), out var townNPC)) { - npc = npc.Where(n => n.townNPC == (character == 1)); + npc = npc.Where(n => n.townNPC == (townNPC == 1)); } - - if (!this.Pins["In"][0].IsConnected()) return new List(); - - var input = ((PinIn)this.Pins["In"][0]).ConnectedPin.Device; - if (!(input is AreaInput)) return new List(); - - var area = ((AreaInput)input).GetArea(this); + + var area = areaInput.GetArea(this); return npc.Where(p => area.Contains(p.position)); } @@ -62,11 +61,8 @@ private IEnumerable GetNPCs() { var debug = base.Debug(pin); - if (pin == null) - { - debug.Add(("----------------", Color.Black, WireMod.SmallText)); - debug.AddRange(this.GetNPCs().Select(npc => ($"NPC: {npc.FullName}", Color.Red, WireMod.SmallText))); - } + debug.Add(("----------------", Color.Black, WireMod.SmallText)); + debug.AddRange(this.GetNPCs().Select(npc => ($"NPC: {npc.FullName}", Color.Red, WireMod.SmallText))); return debug; } diff --git a/Devices/Sensors/NPCDistanceSensor.cs b/Devices/Sensors/NPCDistanceSensor.cs index ce751ba..8c16781 100644 --- a/Devices/Sensors/NPCDistanceSensor.cs +++ b/Devices/Sensors/NPCDistanceSensor.cs @@ -16,8 +16,8 @@ public NPCDistanceSensor() this.PinLayout = new List { - new PinDesign("In", 0, new Point16(0, 1), "bool", "Hostile Filter"), - new PinDesign("In", 1, new Point16(1, 0), "bool", "TownNPC Filter"), + new PinDesign("In", 0, new Point16(0, 1), "bool", "IsHostile"), + new PinDesign("In", 1, new Point16(1, 0), "bool", "IsTownNPC"), new PinDesign("Out", 0, new Point16(1, 2), "int", "Distance"), new PinDesign("Out", 1, new Point16(2, 1), "string", "Name"), }; @@ -25,25 +25,51 @@ public NPCDistanceSensor() public string Output(Pin pin = null) { - var npc = this.GetNearestNPC(); - return pin == this.Pins["Out"][0] ? ((int)(this.LocationRect.Location.ToWorldCoordinates() - npc.position).Length()).ToString() : npc.FullName; + switch (pin?.Name) + { + case "Distance": return this.GetOutputDistance(pin).ToString(); + case "Name": return this.GetOutputName(pin); + default: return ""; + } + } + + private int GetOutputDistance(Pin pin) + { + if (pin == null) return -2; + if (Main.npc.Length == 0) return -1; + + var nearest = this.GetNearestNPC(); + + if (nearest == null) return -1; + + return (int)(this.LocationOriginWorld - nearest.position).Length(); + } + + private string GetOutputName(Pin pin) + { + if (pin == null) return ""; + if (Main.npc.Length == 0) return ""; + + var nearest = this.GetNearestNPC(); + + return nearest?.FullName ?? ""; } private NPC GetNearestNPC() { var npc = Main.npc.Select(n => n); - if (this.Pins["In"][0].IsConnected() && int.TryParse(this.Pins["In"][0].GetValue(), out var hostile)) + if (this.GetPin("IsHostile").IsConnected() && int.TryParse(this.GetPin("IsHostile").GetValue(), out var hostile)) { npc = npc.Where(n => n.friendly == (hostile == 0)); } - if (this.Pins["In"][1].IsConnected() && int.TryParse(this.Pins["In"][1].GetValue(), out var character)) + if (this.GetPin("IsTownNPC").IsConnected() && int.TryParse(this.GetPin("IsTownNPC").GetValue(), out var townNPC)) { - npc = npc.Where(n => n.townNPC == (character == 1)); + npc = npc.Where(n => n.townNPC == (townNPC == 1)); } - return npc.OrderBy(n => (this.LocationRect.Location.ToWorldCoordinates() - n.position).Length()).FirstOrDefault(); + return npc.OrderBy(n => (this.LocationOriginWorld - n.position).Length()).FirstOrDefault(); } } } diff --git a/Devices/Sensors/PlayerCounter.cs b/Devices/Sensors/PlayerCounter.cs index bebfc1e..4027345 100644 --- a/Devices/Sensors/PlayerCounter.cs +++ b/Devices/Sensors/PlayerCounter.cs @@ -18,7 +18,7 @@ public PlayerCounter() this.PinLayout = new List { new PinDesign("In", 0, new Point16(1, 0), "area", "Area"), - new PinDesign("In", 1, new Point16(0, 1), "teamColor", "Team Color Filter"), + new PinDesign("In", 1, new Point16(0, 1), "teamColor", "TeamColorFilter"), new PinDesign("Out", 0, new Point16(1, 2), "int", "Count"), }; } @@ -27,31 +27,31 @@ public PlayerCounter() private int GetOutput() { - if (!this.Pins["In"][0].IsConnected()) return -1; + if (!this.GetPin("Area").IsConnected()) return -1; return this.GetPlayers().Count(); } private IEnumerable GetPlayers() { + if (!this.GetPin("Area").IsConnected()) return new List(); + + var input = this.GetPinIn("Area").ConnectedPin.Device; + if (!(input is AreaInput areaInput)) return new List(); + var players = Main.player.Select(p => p); - if (this.Pins["In"][1].IsConnected()) + if (this.GetPin("TeamColorFilter").IsConnected()) { var team = TeamColor.White; - if (int.TryParse(this.Pins["In"][1].GetValue(), out var tc)) + if (int.TryParse(this.GetPin("TeamColorFilter").GetValue(), out var tc)) { team = (TeamColor)tc; } players = players.Where(p => p.team == (int)team); } - - if (!this.Pins["In"][0].IsConnected()) return new List(); - - var input = ((PinIn)this.Pins["In"][0]).ConnectedPin.Device; - if (!(input is AreaInput)) return new List(); - - var area = ((AreaInput)input).GetArea(this); + + var area = areaInput.GetArea(this); return players.Where(p => area.Contains(p.position)); } @@ -60,11 +60,8 @@ private IEnumerable GetPlayers() { var debug = base.Debug(pin); - if (pin == null) - { - debug.Add(("----------------", Color.Black, WireMod.SmallText)); - debug.AddRange(this.GetPlayers().Select(player => ($"Player: {player.name}", Color.Red, WireMod.SmallText))); - } + debug.Add(("----------------", Color.Black, WireMod.SmallText)); + debug.AddRange(this.GetPlayers().Select(player => ($"Player: {player.name}", Color.Red, WireMod.SmallText))); return debug; } diff --git a/Devices/Sensors/PlayerDistanceSensor.cs b/Devices/Sensors/PlayerDistanceSensor.cs index d87a8ae..3e59c9b 100644 --- a/Devices/Sensors/PlayerDistanceSensor.cs +++ b/Devices/Sensors/PlayerDistanceSensor.cs @@ -16,24 +16,32 @@ public PlayerDistanceSensor() this.PinLayout = new List { - new PinDesign("In", 0, new Point16(0, 0), "teamColor", "TeamColor Filter"), + new PinDesign("In", 0, new Point16(0, 0), "teamColor", "TeamColorFilter"), new PinDesign("Out", 0, new Point16(0, 2), "int", "Distance"), new PinDesign("Out", 1, new Point16(1, 1), "string", "Name"), }; } - public string Output(Pin pin = null) => pin != null && pin.DataType == "string" ? this.GetOutputName(pin) : this.GetOutput(pin).ToString(); + public string Output(Pin pin = null) + { + switch (pin?.Name) + { + case "Distance": return this.GetOutputDistance(pin).ToString(); + case "Name": return this.GetOutputName(pin); + default: return ""; + } + } - private int GetOutput(Pin pin) + private int GetOutputDistance(Pin pin) { - if (pin == null) return -1; + if (pin == null) return -2; if (Main.PlayerList.Count == 0) return -1; var nearest = this.GetNearestPlayer(); if (nearest == null) return -1; - return (int)(this.LocationRect.Location.ToWorldCoordinates() - nearest.position).Length(); + return (int)(this.LocationOriginWorld - nearest.position).Length(); } private string GetOutputName(Pin pin) @@ -43,32 +51,22 @@ private string GetOutputName(Pin pin) var nearest = this.GetNearestPlayer(); - if (nearest == null) return ""; - - return nearest.name; + return nearest?.name ?? ""; } private Player GetNearestPlayer() { - var player = Main.player.OrderBy(p => (this.LocationRect.Location.ToWorldCoordinates() - p.position).Length()); - Player nearest; + var player = Main.player.OrderBy(p => (this.LocationOriginWorld - p.position).Length()); - if (this.Pins["In"][0].IsConnected()) - { - var team = TeamColor.White; - if (int.TryParse(this.Pins["In"][0].GetValue(), out var tc)) - { - team = (TeamColor)tc; - } + if (!this.GetPin("TeamColorFilter").IsConnected()) return player.FirstOrDefault(); - nearest = player.FirstOrDefault(p => p.team == (int)team); - } - else + var team = TeamColor.White; + if (int.TryParse(this.GetPin("TeamColorFilter").GetValue(), out var tc)) { - nearest = player.FirstOrDefault(); + team = (TeamColor)tc; } - return nearest; + return player.FirstOrDefault(p => p.team == (int)team); } } } diff --git a/Devices/Sensors/TileCounter.cs b/Devices/Sensors/TileCounter.cs index e64bded..ded82c6 100644 --- a/Devices/Sensors/TileCounter.cs +++ b/Devices/Sensors/TileCounter.cs @@ -17,7 +17,7 @@ public TileCounter() this.PinLayout = new List { new PinDesign("In", 0, new Point16(1, 0), "area", "Area"), - new PinDesign("In", 1, new Point16(0, 1), "int", "Tile ID"), + new PinDesign("In", 1, new Point16(0, 1), "int", "TileID"), new PinDesign("Out", 0, new Point16(1, 2), "int", "Count"), }; } @@ -26,13 +26,13 @@ public TileCounter() private int GetOutput() { - if (!this.Pins["In"][0].IsConnected()) return -1; - if (!Helpers.TryParseArea(this.Pins["In"][0].GetValue(), out var area)) return -2; + if (!this.GetPin("Area").IsConnected()) return -1; + if (!Helpers.TryParseArea(this.GetPin("Area").GetValue(), out var area)) return -2; if (!area.HasValue) return -1; if (area.Value.AreaType == "Circle") return -2; - if (!this.Pins["In"][1].IsConnected()) return -1; - if (!int.TryParse(this.Pins["In"][1].GetValue(), out var _)) return -2; + if (!this.GetPin("TileID").IsConnected()) return -1; + if (!int.TryParse(this.GetPin("TileID").GetValue(), out var _)) return -2; return this.GetTiles(area.Value.Radius).Count(); } @@ -43,18 +43,18 @@ private IEnumerable GetTiles(int distance) var tiles = new List(); - if (!this.Pins["In"][1].IsConnected() || !int.TryParse(this.Pins["In"][1].GetValue(), out var id)) return tiles; + if (!this.GetPin("TileID").IsConnected() || !int.TryParse(this.GetPin("TileID").GetValue(), out var id)) return tiles; Point16 pos; - var connDev = ((PinIn)this.Pins["In"][0]).ConnectedPin.Device; - if (connDev.Pins["In"][1].IsConnected() && Helpers.TryParsePoint(connDev.Pins["In"][1].GetValue(), out var point) && point.HasValue) + var connDev = this.GetPinIn("Area").ConnectedPin.Device; + if (connDev.GetPin("Point").IsConnected() && Helpers.TryParsePoint(connDev.GetPin("Point").GetValue(), out var point) && point.HasValue) { pos = point.Value; } else { - pos = this.LocationTile + this.Origin; + pos = this.LocationOriginTile; } for (var y = pos.Y - distance; y <= pos.Y + distance; y++) diff --git a/Devices/Sensors/TimeSensor.cs b/Devices/Sensors/TimeSensor.cs index e766339..4b54f39 100644 --- a/Devices/Sensors/TimeSensor.cs +++ b/Devices/Sensors/TimeSensor.cs @@ -21,9 +21,7 @@ public TimeSensor() }; } - public string Output(Pin pin = null) => this.GetOutput(pin); - - private string GetOutput(Pin pin = null) + public string Output(Pin pin = null) { var time = Main.time; @@ -44,7 +42,7 @@ private string GetOutput(Pin pin = null) return seconds.ToString(); } } - + return $"{hours}:{minutes}:{seconds}"; } } diff --git a/Devices/Storage/Variable.cs b/Devices/Storage/Variable.cs index ad09705..fb600ee 100644 --- a/Devices/Storage/Variable.cs +++ b/Devices/Storage/Variable.cs @@ -28,11 +28,11 @@ public Variable() public override void Update(GameTime gameTime) { - if (!this.Pins["In"][1].IsConnected() || !int.TryParse(this.Pins["In"][1].GetValue(), out var write)) write = 0; + if (!this.GetPin("Write").IsConnected() || !int.TryParse(this.GetPin("Write").GetValue(), out var write)) write = 0; - if (this.Pins["In"][0].IsConnected() && write == 1) + if (this.GetPinIn(0).IsConnected() && write == 1) { - this.Settings["Value"] = this.Pins["In"][0].GetValue(); + this.Settings["Value"] = this.GetPinIn(0).GetValue(); } } diff --git a/Devices/Strings/Concat.cs b/Devices/Strings/Concat.cs index 7b179c9..d7c6f19 100644 --- a/Devices/Strings/Concat.cs +++ b/Devices/Strings/Concat.cs @@ -20,6 +20,6 @@ public Concat() }; } - public string Output(Pin pin = null) => this.Pins["In"][0].GetValue() + this.Pins["In"][1].GetValue(); + public string Output(Pin pin = null) => this.GetPinIn(0).GetValue() + this.GetPinIn(1).GetValue(); } } diff --git a/Devices/Strings/TextTileController.cs b/Devices/Strings/TextTileController.cs index d86df13..ae4d261 100644 --- a/Devices/Strings/TextTileController.cs +++ b/Devices/Strings/TextTileController.cs @@ -27,8 +27,8 @@ public override void Update(GameTime gameTime) { var tiles = this.GetTiles(); - var input = this.Pins["In"][0].IsConnected() ? this.Pins["In"][0].GetValue() : ""; - if (this.Pins["In"][1].IsConnected() && (!int.TryParse(this.Pins["In"][1].GetValue(), out var active) || active == 0)) input = ""; + var input = this.GetPinIn(0).IsConnected() ? this.GetPinIn(0).GetValue() : ""; + if (this.GetPin("Activated").IsConnected() && (!int.TryParse(this.GetPin("Activated").GetValue(), out var active) || active == 0)) input = ""; input = input.PadRight(tiles.Count, ' '); @@ -43,7 +43,7 @@ private List GetTiles() { var tiles = new List(); - var pos = this.LocationTile + this.Origin; + var pos = this.LocationOriginTile; var x = pos.X; for (var i = 0; i < 255; i++) @@ -57,27 +57,24 @@ private List GetTiles() return tiles; } - - private static readonly List InputChars = new List - { - " ", - "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z", - "0","1","2","3","4","5","6","7","8","9" - }; - - private static int GetStyle(string inputChar) => InputChars.Contains(inputChar.ToUpper()) ? InputChars.IndexOf(inputChar.ToUpper()) : 0; - + public override List<(string Line, Color Color, float Size)> Debug(Pin pin = null) { var debug = base.Debug(pin); - if (pin == null) - { - debug.Add(("----------------", Color.Black, WireMod.SmallText)); - debug.Add(($"Found {this.GetTiles().Count} tiles", Color.Red, WireMod.SmallText)); - } + debug.Add(("----------------", Color.Black, WireMod.SmallText)); + debug.Add(($"Found {this.GetTiles().Count} tiles", Color.Red, WireMod.SmallText)); return debug; } + + private static int GetStyle(string inputChar) => InputChars.Contains(inputChar.ToUpper()) ? InputChars.IndexOf(inputChar.ToUpper()) : 0; + + private static readonly List InputChars = new List + { + " ", + "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z", + "0","1","2","3","4","5","6","7","8","9" + }; } } diff --git a/Helpers.cs b/Helpers.cs index 2df5336..7e06b7e 100644 --- a/Helpers.cs +++ b/Helpers.cs @@ -7,6 +7,8 @@ namespace WireMod { public class Helpers { + public static Rectangle GetScreenRect() => new Rectangle((int)Main.screenPosition.X, (int)Main.screenPosition.Y, Main.screenWidth, Main.screenHeight); + public static Texture2D CreateCircle(int diameter) { var texture = new Texture2D(Main.graphics.GraphicsDevice, diameter, diameter); diff --git a/Items/ElectronicsManual.cs b/Items/ElectronicsManual.cs index c490448..1da82dc 100644 --- a/Items/ElectronicsManual.cs +++ b/Items/ElectronicsManual.cs @@ -57,11 +57,12 @@ public override void HoldItem(Player player) } else { - var point = WireMod.Instance.GetMouseTilePosition(); - var dev = WireMod.GetDevice(point); + (var x, var y) = WireMod.Instance.GetMouseTilePosition(); + + var dev = WireMod.GetDevice(x, y); if (dev == null) return; - var pin = WireMod.GetDevicePin(point.X, point.Y); + var pin = WireMod.GetDevicePin(x, y); WireMod.Instance.DebuggerHoverUserInterface.SetState(new HoverDebuggerUI(dev, pin)); HoverDebuggerUI.Visible = true; diff --git a/UI/ElectronicsVisionUI.cs b/UI/ElectronicsVisionUI.cs index 219fed4..4a1fb2d 100644 --- a/UI/ElectronicsVisionUI.cs +++ b/UI/ElectronicsVisionUI.cs @@ -108,7 +108,7 @@ private static void DrawWireDot(SpriteBatch spriteBatch, Vector2 position, int s private static void DrawWire(SpriteBatch spriteBatch, Wire wire) { - var screenRect = WireMod.Instance.GetScreenRect(); + var screenRect = Helpers.GetScreenRect(); var points = wire.GetPoints(); for (var i = 0; i < points.Count - 1; i++) diff --git a/WireMod.cs b/WireMod.cs index 53d3acb..e75e8a5 100644 --- a/WireMod.cs +++ b/WireMod.cs @@ -167,18 +167,11 @@ public override void ModifyInterfaceLayers(List layers) (int)(y / 16) ); } - - public Rectangle GetScreenRect() => new Rectangle((int)Main.screenPosition.X, (int)Main.screenPosition.Y, Main.screenWidth, Main.screenHeight); - - - // For lack of a better place to put all this... - + public static List Devices = new List(); public static List Pins = new List(); #region Device Functions - public static Device GetDevice(Point16 location) => GetDevice(location.X, location.Y); - public static Device GetDevice((int X, int Y) location) => GetDevice(location.X, location.Y); public static Device GetDevice(int x, int y) { return Devices.FirstOrDefault(d => d.LocationRect.Intersects(new Rectangle(x, y, 1, 1))); @@ -190,7 +183,6 @@ public static Pin GetDevicePin(int x, int y) return Pins.FirstOrDefault(p => p.Location.X == x && p.Location.Y == y); } - public static bool CanPlace(Device device, Point16 location) => CanPlace(device, location.X, location.Y); public static bool CanPlace(Device device, int x, int y) { // Check for devices and pins in the new device destination area @@ -202,7 +194,6 @@ public static bool CanPlace(Device device, int x, int y) ))); } - public static void PlaceDevice(Device device, Point16 location) => PlaceDevice(device, location.X, location.Y); public static void PlaceDevice(Device device, int x, int y) { // Check if the target area is clear of other devices @@ -223,11 +214,9 @@ public static void PlaceDevice(Device device, int x, int y) device.OnPlace(); } - public static void RemoveDevice(Point16 location) => RemoveDevice(location.X, location.Y); - public static void RemoveDevice(int x, int y) => RemoveDevice(new Rectangle(x, y, 1, 1)); - public static void RemoveDevice(Rectangle rect) + public static void RemoveDevice(int x, int y) { - var device = Devices.FirstOrDefault(d => d.LocationRect.Intersects(rect)); + var device = Devices.FirstOrDefault(d => d.LocationRect.Contains(x, y)); if (device == null) return;