public void ClientRead(ServerNetObject type, NetBuffer msg, float sendingTime) { foreach (Connection connection in Connections) { connection.ClearConnections(); int wireCount = msg.ReadRangedInteger(0, Connection.MaxLinked); for (int i = 0; i < wireCount; i++) { ushort wireId = msg.ReadUInt16(); Item wireItem = MapEntity.FindEntityByID(wireId) as Item; if (wireItem == null) { continue; } Wire wireComponent = wireItem.GetComponent <Wire>(); if (wireComponent == null) { continue; } connection.Wires[i] = wireComponent; wireComponent.Connect(connection, false); } } }
private void ConnectWireBetweenPorts() { Wire wire = item.GetComponent <Wire>(); if (wire == null) { return; } wire.Hidden = true; wire.Locked = true; if (Item.Connections == null) { return; } var powerConnection = Item.Connections.Find(c => c.IsPower); if (powerConnection == null) { return; } if (DockingTarget == null || DockingTarget.item.Connections == null) { return; } var recipient = DockingTarget.item.Connections.Find(c => c.IsPower); if (recipient == null) { return; } wire.RemoveConnection(item); wire.RemoveConnection(DockingTarget.item); powerConnection.TryAddLink(wire); wire.Connect(powerConnection, false, false); recipient.TryAddLink(wire); wire.Connect(recipient, false, false); }
private void Draw(SpriteBatch spriteBatch, Item item, Vector2 position, Vector2 labelPos, Vector2 wirePosition, bool mouseIn, Wire equippedWire, float wireInterval) { //spriteBatch.DrawString(GUI.SmallFont, Name, new Vector2(labelPos.X, labelPos.Y-10), Color.White); GUI.DrawString(spriteBatch, labelPos, Name, IsPower ? Color.Red : Color.White, Color.Black, 0, GUI.SmallFont); GUI.DrawRectangle(spriteBatch, new Rectangle((int)position.X - 10, (int)position.Y - 10, 20, 20), Color.White); spriteBatch.Draw(panelTexture, position - new Vector2(16.0f, 16.0f), new Rectangle(64, 256, 32, 32), Color.White); for (int i = 0; i < MaxLinked; i++) { if (Wires[i] == null || Wires[i].Hidden || draggingConnected == Wires[i]) { continue; } Connection recipient = Wires[i].OtherConnection(this); DrawWire(spriteBatch, Wires[i], (recipient == null) ? Wires[i].Item : recipient.item, position, wirePosition, mouseIn, equippedWire); wirePosition.Y += wireInterval; } if (draggingConnected != null && Vector2.Distance(position, PlayerInput.MousePosition) < 13.0f) { spriteBatch.Draw(panelTexture, position - new Vector2(21.5f, 21.5f), new Rectangle(106, 250, 43, 43), Color.White); if (!PlayerInput.LeftButtonHeld()) { //find an empty cell for the new connection int index = FindWireIndex(null); if (index > -1 && !Wires.Contains(draggingConnected)) { bool alreadyConnected = draggingConnected.IsConnectedTo(item); draggingConnected.RemoveConnection(item); if (draggingConnected.Connect(this, !alreadyConnected, true)) { Wires[index] = draggingConnected; } } } } int screwIndex = (position.Y % 60 < 30) ? 0 : 1; if (Wires.Any(w => w != null && w != draggingConnected)) { spriteBatch.Draw(panelTexture, position - new Vector2(16.0f, 16.0f), new Rectangle(screwIndex * 32, 256, 32, 32), Color.White); } }
private void Draw(SpriteBatch spriteBatch, ConnectionPanel panel, Vector2 position, Vector2 labelPos, Vector2 wirePosition, bool mouseIn, Wire equippedWire, float wireInterval) { //spriteBatch.DrawString(GUI.SmallFont, Name, new Vector2(labelPos.X, labelPos.Y-10), Color.White); GUI.DrawString(spriteBatch, labelPos, Name, IsPower ? Color.Red : Color.White, Color.Black, 0, GUI.SmallFont); connectionSprite.Draw(spriteBatch, position); for (int i = 0; i < MaxLinked; i++) { if (wires[i] == null || wires[i].Hidden || draggingConnected == wires[i]) { continue; } Connection recipient = wires[i].OtherConnection(this); string label = recipient == null ? "" : wires[i].Locked ? recipient.item.Name + "\n" + TextManager.Get("ConnectionLocked") : recipient.item.Name; DrawWire(spriteBatch, wires[i], (recipient == null) ? wires[i].Item : recipient.item, position, wirePosition, mouseIn, equippedWire, panel, label); wirePosition.Y += wireInterval; } if (draggingConnected != null && Vector2.Distance(position, PlayerInput.MousePosition) < 13.0f) { connectionSpriteHighlight.Draw(spriteBatch, position); if (!PlayerInput.LeftButtonHeld()) { //find an empty cell for the new connection int index = FindEmptyIndex(); if (index > -1 && !Wires.Contains(draggingConnected)) { bool alreadyConnected = draggingConnected.IsConnectedTo(panel.Item); draggingConnected.RemoveConnection(panel.Item); if (draggingConnected.Connect(this, !alreadyConnected, true)) { var otherConnection = draggingConnected.OtherConnection(this); SetWire(index, draggingConnected); } } } } if (Wires.Any(w => w != null && w != draggingConnected)) { int screwIndex = (int)Math.Floor(position.Y / 30.0f) % screwSprites.Count; screwSprites[screwIndex].Draw(spriteBatch, position); } }
public void ClientRead(ServerNetObject type, NetBuffer msg, float sendingTime) { List <Wire> prevWires = Connections.SelectMany(c => Array.FindAll(c.Wires, w => w != null)).ToList(); List <Wire> newWires = new List <Wire>(); foreach (Connection connection in Connections) { connection.ClearConnections(); } foreach (Connection connection in Connections) { int wireCount = msg.ReadRangedInteger(0, Connection.MaxLinked); for (int i = 0; i < wireCount; i++) { ushort wireId = msg.ReadUInt16(); Item wireItem = Entity.FindEntityByID(wireId) as Item; if (wireItem == null) { continue; } Wire wireComponent = wireItem.GetComponent <Wire>(); if (wireComponent == null) { continue; } newWires.Add(wireComponent); connection.Wires[i] = wireComponent; wireComponent.Connect(connection, false); } } foreach (Wire wire in prevWires) { if (wire.Connections[0] == null && wire.Connections[1] == null) { wire.Item.Drop(null); } //wires that are not in anyone's inventory (i.e. not currently being rewired) can never be connected to only one connection // -> someone must have dropped the wire from the connection panel else if (wire.Item.ParentInventory == null && (wire.Connections[0] != null ^ wire.Connections[1] != null)) { wire.Item.Drop(null); } } }
private void ApplyRemoteState(NetBuffer msg) { List <Wire> prevWires = Connections.SelectMany(c => c.Wires.Where(w => w != null)).ToList(); List <Wire> newWires = new List <Wire>(); foreach (Connection connection in Connections) { connection.ClearConnections(); } foreach (Connection connection in Connections) { for (int i = 0; i < Connection.MaxLinked; i++) { ushort wireId = msg.ReadUInt16(); Item wireItem = Entity.FindEntityByID(wireId) as Item; if (wireItem == null) { continue; } Wire wireComponent = wireItem.GetComponent <Wire>(); if (wireComponent == null) { continue; } newWires.Add(wireComponent); connection.SetWire(i, wireComponent); wireComponent.Connect(connection, false); } } foreach (Wire wire in prevWires) { if (wire.Connections[0] == null && wire.Connections[1] == null) { wire.Item.Drop(null); } //wires that are not in anyone's inventory (i.e. not currently being rewired) can never be connected to only one connection // -> someone must have dropped the wire from the connection panel else if (wire.Item.ParentInventory == null && (wire.Connections[0] != null ^ wire.Connections[1] != null)) { wire.Item.Drop(null); } } }
private void Draw(SpriteBatch spriteBatch, ConnectionPanel panel, Vector2 position, Vector2 labelPos, Vector2 wirePosition, bool mouseIn, Wire equippedWire, float wireInterval) { GUI.DrawString(spriteBatch, labelPos, DisplayName, IsPower ? Color.Red : Color.White, Color.Black, 0, GUI.SmallFont); connectionSprite.Draw(spriteBatch, position); for (int i = 0; i < MaxLinked; i++) { if (wires[i] == null || wires[i].Hidden || (draggingConnected == wires[i] && (mouseIn || Screen.Selected == GameMain.SubEditorScreen))) { continue; } Connection recipient = wires[i].OtherConnection(this); string label = recipient == null ? "" : recipient.item.Name + $" ({recipient.DisplayName})"; if (wires[i].Locked) { label += "\n" + TextManager.Get("ConnectionLocked"); } DrawWire(spriteBatch, wires[i], position, wirePosition, equippedWire, panel, label); wirePosition.Y += wireInterval; } if (draggingConnected != null && Vector2.Distance(position, PlayerInput.MousePosition) < (20.0f * GUI.Scale)) { connectionSpriteHighlight.Draw(spriteBatch, position); if (!PlayerInput.LeftButtonHeld()) { //find an empty cell for the new connection int index = FindEmptyIndex(); if (index > -1 && !Wires.Contains(draggingConnected)) { bool alreadyConnected = draggingConnected.IsConnectedTo(panel.Item); draggingConnected.RemoveConnection(panel.Item); if (draggingConnected.Connect(this, !alreadyConnected, true)) { var otherConnection = draggingConnected.OtherConnection(this); SetWire(index, draggingConnected); } } if (GameMain.Client != null) { panel.Item.CreateClientEvent(panel); } draggingConnected = null; } } if (flashTimer > 0.0f) { //the number of flashes depends on the duration, 1 flash per 1 full second int flashCycleCount = (int)Math.Max(flashDuration, 1); float flashCycleDuration = flashDuration / flashCycleCount; //MathHelper.Pi * 0.8f -> the curve goes from 144 deg to 0, //i.e. quickly bumps up from almost full brightness to full and then fades out connectionSpriteHighlight.Draw(spriteBatch, position, flashColor * (float)Math.Sin(flashTimer % flashCycleDuration / flashCycleDuration * MathHelper.Pi * 0.8f)); } if (Wires.Any(w => w != null && w != draggingConnected)) { int screwIndex = (int)Math.Floor(position.Y / 30.0f) % screwSprites.Count; screwSprites[screwIndex].Draw(spriteBatch, position); } }
private void ApplyRemoteState(IReadMessage msg) { List <Wire> prevWires = Connections.SelectMany(c => c.Wires.Where(w => w != null)).ToList(); List <Wire> newWires = new List <Wire>(); ushort userID = msg.ReadUInt16(); if (userID == 0) { user = null; } else { user = Entity.FindEntityByID(userID) as Character; base.IsActive = true; } foreach (Connection connection in Connections) { connection.ClearConnections(); } foreach (Connection connection in Connections) { for (int i = 0; i < connection.MaxWires; i++) { ushort wireId = msg.ReadUInt16(); if (!(Entity.FindEntityByID(wireId) is Item wireItem)) { continue; } Wire wireComponent = wireItem.GetComponent <Wire>(); if (wireComponent == null) { continue; } newWires.Add(wireComponent); connection.SetWire(i, wireComponent); wireComponent.Connect(connection, false); } } List <Wire> previousDisconnectedWires = new List <Wire>(DisconnectedWires); DisconnectedWires.Clear(); ushort disconnectedWireCount = msg.ReadUInt16(); for (int i = 0; i < disconnectedWireCount; i++) { ushort wireId = msg.ReadUInt16(); if (!(Entity.FindEntityByID(wireId) is Item wireItem)) { continue; } Wire wireComponent = wireItem.GetComponent <Wire>(); if (wireComponent == null) { continue; } DisconnectedWires.Add(wireComponent); base.IsActive = true; } foreach (Wire wire in prevWires) { bool connected = wire.Connections[0] != null || wire.Connections[1] != null; if (!connected) { foreach (Item item in Item.ItemList) { var connectionPanel = item.GetComponent <ConnectionPanel>(); if (connectionPanel != null && connectionPanel.DisconnectedWires.Contains(wire)) { connected = true; break; } } } if (wire.Item.ParentInventory == null && !connected) { wire.Item.Drop(null); } } foreach (Wire disconnectedWire in previousDisconnectedWires) { if (disconnectedWire.Connections[0] == null && disconnectedWire.Connections[1] == null && !DisconnectedWires.Contains(disconnectedWire)) { disconnectedWire.Item.Drop(dropper: null); } } }