Skip to content

Commit

Permalink
Add GetPin helper methods
Browse files Browse the repository at this point in the history
General cleanup
  • Loading branch information
MatLomax committed Aug 21, 2019
1 parent ce78420 commit 5604758
Show file tree
Hide file tree
Showing 38 changed files with 220 additions and 238 deletions.
2 changes: 1 addition & 1 deletion Devices/Area.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions Devices/Comparison/Equals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions Devices/Comparison/GreaterThan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
6 changes: 3 additions & 3 deletions Devices/Comparison/LessThan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
11 changes: 9 additions & 2 deletions Devices/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions Devices/Gates/AndGate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
10 changes: 5 additions & 5 deletions Devices/Gates/If.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions Devices/Gates/NotGate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
6 changes: 3 additions & 3 deletions Devices/Gates/OrGate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
21 changes: 10 additions & 11 deletions Devices/Inputs/AreaInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,28 @@ 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);
}

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();
}
Expand All @@ -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<string> AreaTypes = new List<string>
{
"Square",
Expand Down
6 changes: 1 addition & 5 deletions Devices/Inputs/PointConstant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}";
}
}
4 changes: 2 additions & 2 deletions Devices/Inputs/RandomInt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
6 changes: 3 additions & 3 deletions Devices/Maths/Add.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
6 changes: 3 additions & 3 deletions Devices/Maths/Divide.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions Devices/Maths/Modulo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
6 changes: 3 additions & 3 deletions Devices/Maths/Multiply.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
6 changes: 3 additions & 3 deletions Devices/Maths/Subtract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
17 changes: 7 additions & 10 deletions Devices/Other/Buffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ public Buffer()

this.PinLayout = new List<PinDesign>
{
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"),
};
}

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;

Expand All @@ -50,22 +50,19 @@ 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;
}
}

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;
Expand Down
19 changes: 8 additions & 11 deletions Devices/Other/Repulsor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Entity>();

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();

Expand Down
Loading

0 comments on commit 5604758

Please sign in to comment.