Fabricator & deconstructor syncing
This commit is contained in:
@@ -1,4 +1,6 @@
|
|||||||
using Microsoft.Xna.Framework;
|
using Barotrauma.Networking;
|
||||||
|
using Lidgren.Network;
|
||||||
|
using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@@ -8,7 +10,7 @@ using System.Xml.Linq;
|
|||||||
|
|
||||||
namespace Barotrauma.Items.Components
|
namespace Barotrauma.Items.Components
|
||||||
{
|
{
|
||||||
class Deconstructor : Powered
|
class Deconstructor : Powered, IServerSerializable, IClientSerializable
|
||||||
{
|
{
|
||||||
GUIProgressBar progressBar;
|
GUIProgressBar progressBar;
|
||||||
GUIButton activateButton;
|
GUIButton activateButton;
|
||||||
@@ -16,9 +18,7 @@ namespace Barotrauma.Items.Components
|
|||||||
float progressTimer;
|
float progressTimer;
|
||||||
|
|
||||||
ItemContainer container;
|
ItemContainer container;
|
||||||
|
|
||||||
private float lastNetworkUpdate;
|
|
||||||
|
|
||||||
public Deconstructor(Item item, XElement element)
|
public Deconstructor(Item item, XElement element)
|
||||||
: base(item, element)
|
: base(item, element)
|
||||||
{
|
{
|
||||||
@@ -100,6 +100,15 @@ namespace Barotrauma.Items.Components
|
|||||||
SetActive(!IsActive);
|
SetActive(!IsActive);
|
||||||
|
|
||||||
currPowerConsumption = IsActive ? powerConsumption : 0.0f;
|
currPowerConsumption = IsActive ? powerConsumption : 0.0f;
|
||||||
|
|
||||||
|
if (GameMain.Server != null)
|
||||||
|
{
|
||||||
|
item.CreateServerEvent<Deconstructor>(this);
|
||||||
|
}
|
||||||
|
else if (GameMain.Client != null)
|
||||||
|
{
|
||||||
|
item.CreateClientEvent<Deconstructor>(this);
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -129,8 +138,35 @@ namespace Barotrauma.Items.Components
|
|||||||
activateButton.Text = "Cancel";
|
activateButton.Text = "Cancel";
|
||||||
}
|
}
|
||||||
|
|
||||||
container.Inventory.Locked = IsActive;
|
container.Inventory.Locked = IsActive;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void ClientWrite(NetBuffer msg, object[] extraData = null)
|
||||||
|
{
|
||||||
|
msg.Write(IsActive);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ServerRead(ClientNetObject type, NetIncomingMessage msg, Client c)
|
||||||
|
{
|
||||||
|
bool active = msg.ReadBoolean();
|
||||||
|
|
||||||
|
item.CreateServerEvent<Deconstructor>(this);
|
||||||
|
|
||||||
|
if (item.CanClientAccess(c))
|
||||||
|
{
|
||||||
|
SetActive(active);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ServerWrite(NetBuffer msg, Client c, object[] extraData = null)
|
||||||
|
{
|
||||||
|
msg.Write(IsActive);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ClientRead(ServerNetObject type, NetIncomingMessage msg, float sendingTime)
|
||||||
|
{
|
||||||
|
SetActive(msg.ReadBoolean());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
using Microsoft.Xna.Framework;
|
using Barotrauma.Networking;
|
||||||
|
using Lidgren.Network;
|
||||||
|
using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@@ -77,7 +79,7 @@ namespace Barotrauma.Items.Components
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Fabricator : Powered
|
class Fabricator : Powered, IServerSerializable, IClientSerializable
|
||||||
{
|
{
|
||||||
private List<FabricableItem> fabricableItems;
|
private List<FabricableItem> fabricableItems;
|
||||||
|
|
||||||
@@ -94,9 +96,7 @@ namespace Barotrauma.Items.Components
|
|||||||
//used for checking if contained items have changed
|
//used for checking if contained items have changed
|
||||||
//(in which case we need to recheck which items can be fabricated)
|
//(in which case we need to recheck which items can be fabricated)
|
||||||
private Item[] prevContainedItems;
|
private Item[] prevContainedItems;
|
||||||
|
|
||||||
private float lastNetworkUpdate;
|
|
||||||
|
|
||||||
public Fabricator(Item item, XElement element)
|
public Fabricator(Item item, XElement element)
|
||||||
: base(item, element)
|
: base(item, element)
|
||||||
{
|
{
|
||||||
@@ -288,6 +288,15 @@ namespace Barotrauma.Items.Components
|
|||||||
{
|
{
|
||||||
CancelFabricating();
|
CancelFabricating();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (GameMain.Server != null)
|
||||||
|
{
|
||||||
|
item.CreateServerEvent<Fabricator>(this);
|
||||||
|
}
|
||||||
|
else if (GameMain.Client != null)
|
||||||
|
{
|
||||||
|
item.CreateClientEvent<Fabricator>(this);
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -437,6 +446,60 @@ namespace Barotrauma.Items.Components
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void ClientWrite(NetBuffer msg, object[] extraData = null)
|
||||||
|
{
|
||||||
|
int itemIndex = fabricatedItem == null ? -1 : fabricableItems.IndexOf(fabricatedItem);
|
||||||
|
msg.WriteRangedInteger(-1, fabricableItems.Count - 1, itemIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ServerRead(ClientNetObject type, NetIncomingMessage msg, Client c)
|
||||||
|
{
|
||||||
|
int itemIndex = msg.ReadRangedInteger(-1, fabricableItems.Count - 1);
|
||||||
|
|
||||||
|
item.CreateServerEvent<Fabricator>(this);
|
||||||
|
|
||||||
|
if (!item.CanClientAccess(c)) return;
|
||||||
|
|
||||||
|
if (itemIndex == -1)
|
||||||
|
{
|
||||||
|
CancelFabricating();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//if already fabricating the selected item, return
|
||||||
|
if (fabricatedItem != null && fabricableItems.IndexOf(fabricatedItem) == itemIndex) return;
|
||||||
|
if (itemIndex < 0 || itemIndex >= fabricableItems.Count) return;
|
||||||
|
|
||||||
|
SelectItem(null, fabricableItems[itemIndex]);
|
||||||
|
StartFabricating(fabricableItems[itemIndex]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ServerWrite(NetBuffer msg, Client c, object[] extraData = null)
|
||||||
|
{
|
||||||
|
int itemIndex = fabricatedItem == null ? -1 : fabricableItems.IndexOf(fabricatedItem);
|
||||||
|
msg.WriteRangedInteger(-1, fabricableItems.Count - 1, itemIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ClientRead(ServerNetObject type, NetIncomingMessage msg, float sendingTime)
|
||||||
|
{
|
||||||
|
int itemIndex = msg.ReadRangedInteger(-1, fabricableItems.Count - 1);
|
||||||
|
|
||||||
|
if (itemIndex == -1)
|
||||||
|
{
|
||||||
|
CancelFabricating();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//if already fabricating the selected item, return
|
||||||
|
if (fabricatedItem != null && fabricableItems.IndexOf(fabricatedItem) == itemIndex) return;
|
||||||
|
if (itemIndex < 0 || itemIndex >= fabricableItems.Count) return;
|
||||||
|
|
||||||
|
SelectItem(null, fabricableItems[itemIndex]);
|
||||||
|
StartFabricating(fabricableItems[itemIndex]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user