Fabricator & deconstructor syncing

This commit is contained in:
Regalis
2017-01-28 12:40:43 +02:00
parent 0570956e24
commit 6bbd11630d
2 changed files with 111 additions and 12 deletions
@@ -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;
@@ -17,8 +19,6 @@ namespace Barotrauma.Items.Components
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)
{ {
@@ -101,6 +101,15 @@ namespace Barotrauma.Items.Components
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;
} }
@@ -130,7 +139,34 @@ namespace Barotrauma.Items.Components
} }
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;
@@ -95,8 +97,6 @@ namespace Barotrauma.Items.Components
//(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)
{ {
@@ -289,6 +289,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;
} }
@@ -438,5 +447,59 @@ 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]);
}
}
} }
} }