From d37bad2d441a45cf103e7292eb018661c0309e81 Mon Sep 17 00:00:00 2001 From: Regalis Date: Wed, 16 Nov 2016 18:32:18 +0200 Subject: [PATCH] Wire sections are updated after flipping, made the wire node list private and added public methods which ensure that sections are updated when modifying nodes --- Subsurface/Source/DebugConsole.cs | 2 +- .../Source/Items/Components/Signal/Wire.cs | 113 ++++++++++-------- Subsurface/Source/Map/MapEntity.cs | 3 +- Subsurface/Source/Map/Submarine.cs | 5 +- 4 files changed, 67 insertions(+), 56 deletions(-) diff --git a/Subsurface/Source/DebugConsole.cs b/Subsurface/Source/DebugConsole.cs index 63f4263ce..d18889d4e 100644 --- a/Subsurface/Source/DebugConsole.cs +++ b/Subsurface/Source/DebugConsole.cs @@ -627,7 +627,7 @@ namespace Barotrauma var wire = item.GetComponent(); if (wire == null) continue; - if (wire.Nodes.Any() && !wire.Connections.Any(c => c != null)) + if (wire.GetNodes().Count > 0 && !wire.Connections.Any(c => c != null)) { wire.Item.Drop(null); DebugConsole.NewMessage("Dropped wire (ID: "+wire.Item.ID+") - attached on wall but no connections found", Color.Orange); diff --git a/Subsurface/Source/Items/Components/Signal/Wire.cs b/Subsurface/Source/Items/Components/Signal/Wire.cs index b505011ca..412d16919 100644 --- a/Subsurface/Source/Items/Components/Signal/Wire.cs +++ b/Subsurface/Source/Items/Components/Signal/Wire.cs @@ -56,8 +56,7 @@ namespace Barotrauma.Items.Components static Sprite wireSprite; - public List Nodes; - + private List nodes; private List sections; Connection[] connections; @@ -83,7 +82,7 @@ namespace Barotrauma.Items.Components wireSprite.Depth = 0.85f; } - Nodes = new List(); + nodes = new List(); sections = new List(); connections = new Connection[2]; @@ -159,17 +158,17 @@ namespace Barotrauma.Items.Components if (newConnection.Item.Submarine == null) continue; - if (Nodes.Count > 0 && Nodes[0] == newConnection.Item.Position - newConnection.Item.Submarine.HiddenSubPosition) break; - if (Nodes.Count > 1 && Nodes[Nodes.Count-1] == newConnection.Item.Position - newConnection.Item.Submarine.HiddenSubPosition) break; + if (nodes.Count > 0 && nodes[0] == newConnection.Item.Position - newConnection.Item.Submarine.HiddenSubPosition) break; + if (nodes.Count > 1 && nodes[nodes.Count-1] == newConnection.Item.Position - newConnection.Item.Submarine.HiddenSubPosition) break; if (i == 0) { - Nodes.Insert(0, newConnection.Item.Position - newConnection.Item.Submarine.HiddenSubPosition); + nodes.Insert(0, newConnection.Item.Position - newConnection.Item.Submarine.HiddenSubPosition); } else { - Nodes.Add(newConnection.Item.Position - newConnection.Item.Submarine.HiddenSubPosition); + nodes.Add(newConnection.Item.Position - newConnection.Item.Submarine.HiddenSubPosition); } @@ -192,7 +191,7 @@ namespace Barotrauma.Items.Components CleanNodes(); } - Drawable = Nodes.Any(); + Drawable = nodes.Any(); if (!loading) Item.NewComponentEvent(this, true, true); @@ -225,7 +224,7 @@ namespace Barotrauma.Items.Components public override void Update(float deltaTime, Camera cam) { - if (Nodes.Count == 0) return; + if (nodes.Count == 0) return; Submarine sub = null; if (connections[0] != null && connections[0].Item.Submarine != null) sub = connections[0].Item.Submarine; @@ -234,7 +233,6 @@ namespace Barotrauma.Items.Components if (item.Submarine != sub && Screen.Selected != GameMain.EditMapScreen) { ClearConnections(); - Nodes.Clear(); return; } @@ -245,9 +243,9 @@ namespace Barotrauma.Items.Components { if (character == Character.Controlled && character.SelectedConstruction != null) return false; - if (newNodePos!= Vector2.Zero && Nodes.Count>0 && Vector2.Distance(newNodePos, Nodes[Nodes.Count - 1]) > nodeDistance) + if (newNodePos!= Vector2.Zero && nodes.Count>0 && Vector2.Distance(newNodePos, nodes[nodes.Count - 1]) > nodeDistance) { - Nodes.Add(newNodePos); + nodes.Add(newNodePos); UpdateSections(); Drawable = true; @@ -259,9 +257,9 @@ namespace Barotrauma.Items.Components public override void SecondaryUse(float deltaTime, Character character = null) { - if (Nodes.Count > 1) + if (nodes.Count > 1) { - Nodes.RemoveAt(Nodes.Count - 1); + nodes.RemoveAt(nodes.Count - 1); UpdateSections(); item.NewComponentEvent(this, true, true); @@ -282,11 +280,22 @@ namespace Barotrauma.Items.Components if (item.IsSelected) MoveNodes(amount); } + public List GetNodes() + { + return new List(nodes); + } + + public void SetNodes(List nodes) + { + this.nodes = new List(nodes); + UpdateSections(); + } + public void MoveNodes(Vector2 amount) { - for (int i = 0; i < Nodes.Count; i++) + for (int i = 0; i < nodes.Count; i++) { - Nodes[i] += amount; + nodes[i] += amount; } UpdateSections(); } @@ -295,16 +304,16 @@ namespace Barotrauma.Items.Components { sections.Clear(); - for (int i = 0; i < Nodes.Count-1; i++) + for (int i = 0; i < nodes.Count-1; i++) { - sections.Add(new WireSection(Nodes[i], Nodes[i + 1])); + sections.Add(new WireSection(nodes[i], nodes[i + 1])); } Drawable = sections.Count > 0; } private void ClearConnections() { - Nodes.Clear(); + nodes.Clear(); sections.Clear(); for (int i = 0; i < 2; i++) @@ -348,14 +357,14 @@ namespace Barotrauma.Items.Components private void CleanNodes() { - for (int i = Nodes.Count - 2; i > 0; i--) + 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 ((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])) + if (Vector2.Distance(nodes[i - 1], nodes[i]) == Vector2.Distance(nodes[i + 1], nodes[i])) { - Nodes.RemoveAt(i); + nodes.RemoveAt(i); } } } @@ -364,12 +373,12 @@ namespace Barotrauma.Items.Components do { removed = false; - for (int i = Nodes.Count - 2; i > 0; i--) + 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)) + 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); + nodes.RemoveAt(i); removed = true; } } @@ -414,11 +423,11 @@ namespace Barotrauma.Items.Components section.Draw(spriteBatch, item.Color, drawOffset, depth, 0.3f); } - if (IsActive && Vector2.Distance(newNodePos, Nodes[Nodes.Count - 1]) > nodeDistance) + if (IsActive && Vector2.Distance(newNodePos, nodes[nodes.Count - 1]) > nodeDistance) { WireSection.Draw( spriteBatch, - new Vector2(Nodes[Nodes.Count - 1].X, Nodes[Nodes.Count - 1].Y) + drawOffset, + new Vector2(nodes[nodes.Count - 1].X, nodes[nodes.Count - 1].Y) + drawOffset, new Vector2(newNodePos.X, newNodePos.Y) + drawOffset, item.Color * 0.5f, depth, @@ -428,9 +437,9 @@ namespace Barotrauma.Items.Components if (!editing || !PlayerInput.MouseInsideWindow || !GameMain.EditMapScreen.WiringMode) return; if (Character.Controlled != null && Character.Controlled.SelectedConstruction != null) return; - for (int i = 0; i < Nodes.Count; i++) + for (int i = 0; i < nodes.Count; i++) { - Vector2 worldPos = Nodes[i]; + Vector2 worldPos = nodes[i]; if (item.Submarine != null) worldPos += item.Submarine.Position + item.Submarine.HiddenSubPosition; worldPos.Y = -worldPos.Y; @@ -458,7 +467,7 @@ namespace Barotrauma.Items.Components } else if (PlayerInput.RightButtonClicked()) { - Nodes.RemoveAt(i); + nodes.RemoveAt(i); break; } } @@ -483,7 +492,7 @@ namespace Barotrauma.Items.Components //if (item.Submarine != null) nodeWorldPos += item.Submarine.Position; - Nodes[(int)selectedNodeIndex] = nodeWorldPos; + nodes[(int)selectedNodeIndex] = nodeWorldPos; UpdateSections(); MapEntity.SelectEntity(item); @@ -498,23 +507,24 @@ namespace Barotrauma.Items.Components public override void FlipX() { - for (int i = 0; i < Nodes.Count; i++) + for (int i = 0; i < nodes.Count; i++) { - Nodes[i] = new Vector2(-Nodes[i].X, Nodes[i].Y); - } + nodes[i] = new Vector2(-nodes[i].X, nodes[i].Y); + } + UpdateSections(); } public override XElement Save(XElement parentElement) { XElement componentElement = base.Save(parentElement); - if (Nodes == null || Nodes.Count == 0) return componentElement; + if (nodes == null || nodes.Count == 0) return componentElement; - string[] nodeCoords = new string[Nodes.Count * 2]; - for (int i = 0; i < Nodes.Count; i++) + 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); + 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))); @@ -546,10 +556,10 @@ namespace Barotrauma.Items.Components } catch { y = 0.0f; } - Nodes.Add(new Vector2(x, y)); + nodes.Add(new Vector2(x, y)); } - Drawable = Nodes.Any(); + Drawable = nodes.Any(); } @@ -562,11 +572,11 @@ namespace Barotrauma.Items.Components public override bool FillNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetBuffer message) { - message.Write((byte)Math.Min(Nodes.Count, 10)); - for (int i = 0; i < Math.Min(Nodes.Count,10); i++) + message.Write((byte)Math.Min(nodes.Count, 10)); + for (int i = 0; i < Math.Min(nodes.Count,10); i++) { - message.Write(Nodes[i].X); - message.Write(Nodes[i].Y); + message.Write(nodes[i].X); + message.Write(nodes[i].Y); } return true; @@ -574,7 +584,7 @@ namespace Barotrauma.Items.Components public override void ReadNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetIncomingMessage message, float sendingTime) { - Nodes.Clear(); + nodes.Clear(); List newNodes = new List(); int nodeCount = message.ReadByte(); @@ -585,9 +595,8 @@ namespace Barotrauma.Items.Components newNodes.Add(newNode); } - Nodes = newNodes; - - Drawable = Nodes.Any(); + SetNodes(newNodes); + Drawable = nodes.Any(); } } } diff --git a/Subsurface/Source/Map/MapEntity.cs b/Subsurface/Source/Map/MapEntity.cs index 9ae6dea14..2b0a27b98 100644 --- a/Subsurface/Source/Map/MapEntity.cs +++ b/Subsurface/Source/Map/MapEntity.cs @@ -251,8 +251,7 @@ namespace Barotrauma var originalWire = ((Item)entitiesToClone[i]).GetComponent(); - cloneWire.Nodes = new List(originalWire.Nodes); - cloneWire.UpdateSections(); + cloneWire.SetNodes(originalWire.GetNodes()); for (int n = 0; n < 2; n++) { diff --git a/Subsurface/Source/Map/Submarine.cs b/Subsurface/Source/Map/Submarine.cs index 76f0db368..9afaa82c8 100644 --- a/Subsurface/Source/Map/Submarine.cs +++ b/Subsurface/Source/Map/Submarine.cs @@ -1080,7 +1080,10 @@ namespace Barotrauma if (item.Submarine != this) continue; var wire = item.GetComponent(); - if (wire != null) wire.MoveNodes(-center); + if (wire != null) + { + wire.MoveNodes(-center); + } } for (int i = 0; i < MapEntity.mapEntityList.Count; i++)