Add a subsystem for interaction syncing. Possibly borkd
This commit is contained in:
parent
2854a093fc
commit
bcaf1e3e12
@ -57,6 +57,9 @@ namespace Cyber.Controls {
|
|||||||
Interactable LookingAt = LookedAtObject.GetComponent<Interactable>();
|
Interactable LookingAt = LookedAtObject.GetComponent<Interactable>();
|
||||||
if (LookingAt != null && (LookingAt.transform.position - Character.GetPosition()).magnitude < Character.InteractionDistance) {
|
if (LookingAt != null && (LookingAt.transform.position - Character.GetPosition()).magnitude < Character.InteractionDistance) {
|
||||||
LookingAt.Interact();
|
LookingAt.Interact();
|
||||||
|
if (LookingAt.GetInteractableSyncdata().PublicInteractions) {
|
||||||
|
Client.Send(PktType.InteractPkt, new InteractionPkt(LookingAt.ID));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
26
Assets/Scripts/Entities/InteractableSyncdata.cs
Normal file
26
Assets/Scripts/Entities/InteractableSyncdata.cs
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
|
||||||
|
namespace Cyber.Entities {
|
||||||
|
public struct InteractableSyncdata {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Weather this interactable requires syncing or not.
|
||||||
|
/// </summary>
|
||||||
|
public bool RequiresSyncing;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Weather interacting with this should send a TCP-packet or not.
|
||||||
|
/// </summary>
|
||||||
|
public bool PublicInteractions;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates an InteractibleSyncdata struct.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="RequiresSyncing">Weather this interactible requires syncing (like a door) or not (like a bell).</param>
|
||||||
|
/// <param name="PublicInteractions">Weather interacting with this interactible should send a TCP-packet (like a bell or a door) or not (like opening a screen where you can type).</param>
|
||||||
|
public InteractableSyncdata(bool requiresSyncing, bool publicInteractions) {
|
||||||
|
RequiresSyncing = requiresSyncing;
|
||||||
|
PublicInteractions = publicInteractions;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
12
Assets/Scripts/Entities/InteractableSyncdata.cs.meta
Normal file
12
Assets/Scripts/Entities/InteractableSyncdata.cs.meta
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: b02b5449859d14f4784a47e5663a1d3c
|
||||||
|
timeCreated: 1494455790
|
||||||
|
licenseType: Free
|
||||||
|
MonoImporter:
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
@ -4,6 +4,7 @@ using UnityEngine;
|
|||||||
using UnityEngine.Networking;
|
using UnityEngine.Networking;
|
||||||
using Cyber.Networking;
|
using Cyber.Networking;
|
||||||
using Cyber.Console;
|
using Cyber.Console;
|
||||||
|
using System;
|
||||||
|
|
||||||
namespace Cyber.Entities.SyncBases {
|
namespace Cyber.Entities.SyncBases {
|
||||||
|
|
||||||
@ -44,22 +45,20 @@ namespace Cyber.Entities.SyncBases {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Update the blink time if the server has a newer blinktime
|
/// Does nothing, because it doesn't need to synced.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="reader"></param>
|
/// <param name="reader"></param>
|
||||||
public override void Deserialize(NetworkReader reader) {
|
public override void Deserialize(NetworkReader reader) {}
|
||||||
double ServerBlinkTime = reader.ReadDouble();
|
|
||||||
if (ServerBlinkTime > BlinkTime) {
|
|
||||||
BlinkTime = ServerBlinkTime;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Write the blink time so others will sync if they have older blinks.
|
/// Does nothing, because it doesn't need to synced.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="writer"></param>
|
/// <param name="writer"></param>
|
||||||
public override void Serialize(NetworkWriter writer) {
|
public override void Serialize(NetworkWriter writer) {
|
||||||
writer.Write(BlinkTime);
|
}
|
||||||
|
|
||||||
|
public override InteractableSyncdata GetInteractableSyncdata() {
|
||||||
|
return new InteractableSyncdata(false, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
using System.Collections;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using UnityEngine.Networking;
|
||||||
|
|
||||||
namespace Cyber.Entities.SyncBases {
|
namespace Cyber.Entities.SyncBases {
|
||||||
|
|
||||||
@ -13,6 +15,12 @@ namespace Cyber.Entities.SyncBases {
|
|||||||
/// All interactables should implement their interactions by overriding this.
|
/// All interactables should implement their interactions by overriding this.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public abstract void Interact();
|
public abstract void Interact();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get Interaction information about this interactible.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>The Interaction information.</returns>
|
||||||
|
public abstract InteractableSyncdata GetInteractableSyncdata();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -127,6 +127,7 @@ namespace Cyber.Networking.Clientside {
|
|||||||
NetClient.RegisterHandler(PktType.SpawnEntity, HandlePacket);
|
NetClient.RegisterHandler(PktType.SpawnEntity, HandlePacket);
|
||||||
NetClient.RegisterHandler(PktType.MoveCreature, HandlePacket);
|
NetClient.RegisterHandler(PktType.MoveCreature, HandlePacket);
|
||||||
NetClient.RegisterHandler(PktType.SyncPacket, HandlePacket);
|
NetClient.RegisterHandler(PktType.SyncPacket, HandlePacket);
|
||||||
|
NetClient.RegisterHandler(PktType.InteractPkt, HandlePacket);
|
||||||
|
|
||||||
NetClient.RegisterHandler(MsgType.Connect, OnConnected);
|
NetClient.RegisterHandler(MsgType.Connect, OnConnected);
|
||||||
NetClient.RegisterHandler(MsgType.Disconnect, OnDisconnected);
|
NetClient.RegisterHandler(MsgType.Disconnect, OnDisconnected);
|
||||||
@ -194,6 +195,17 @@ namespace Cyber.Networking.Clientside {
|
|||||||
Term.Println("SyncBase " + MoveCreature.SyncBaseID + " is not a Creature");
|
Term.Println("SyncBase " + MoveCreature.SyncBaseID + " is not a Creature");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
case (PktType.InteractPkt):
|
||||||
|
InteractionPkt Interaction = new InteractionPkt();
|
||||||
|
Interaction.Deserialize(msg.reader);
|
||||||
|
|
||||||
|
SyncBase Target = Spawner.SyncDB.Get(Interaction.SyncBaseID);
|
||||||
|
if (Target != null && Target is Interactable) {
|
||||||
|
((Interactable) Target).Interact();
|
||||||
|
} else {
|
||||||
|
Term.Println("Server has sent an erroneus SyncBase ID!");
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case (PktType.SyncPacket):
|
case (PktType.SyncPacket):
|
||||||
SyncHandler.HandleSyncPkt(msg);
|
SyncHandler.HandleSyncPkt(msg);
|
||||||
|
46
Assets/Scripts/Networking/Messages/InteractionPkt.cs
Normal file
46
Assets/Scripts/Networking/Messages/InteractionPkt.cs
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
|
||||||
|
using UnityEngine.Networking;
|
||||||
|
|
||||||
|
namespace Cyber.Networking.Messages {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Includes information about interacting an interactible. Applicable only for some interactibles.
|
||||||
|
/// </summary>
|
||||||
|
public class InteractionPkt : MessageBase {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// ID of the interactible.
|
||||||
|
/// </summary>
|
||||||
|
public int SyncBaseID;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates an InteraktionPkt, which contains the message "someone interacted".
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="SyncBaseID"></param>
|
||||||
|
public InteractionPkt(int syncBaseID) {
|
||||||
|
SyncBaseID = syncBaseID;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Empty constructor for deserialization.
|
||||||
|
/// </summary>
|
||||||
|
public InteractionPkt() {}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deserializes SyncBaseID for the recipent.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="reader"></param>
|
||||||
|
public override void Deserialize(NetworkReader reader) {
|
||||||
|
SyncBaseID = reader.ReadInt32();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Serializes the SyncBaseID for sending.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="writer"></param>
|
||||||
|
public override void Serialize(NetworkWriter writer) {
|
||||||
|
writer.Write(SyncBaseID);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
12
Assets/Scripts/Networking/Messages/InteractionPkt.cs.meta
Normal file
12
Assets/Scripts/Networking/Messages/InteractionPkt.cs.meta
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: ee7d568a49719d248910f19e26d82051
|
||||||
|
timeCreated: 1494453893
|
||||||
|
licenseType: Free
|
||||||
|
MonoImporter:
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
@ -38,5 +38,10 @@ namespace Cyber.Networking {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public const short SyncPacket = 205;
|
public const short SyncPacket = 205;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Packet telling that someone has made an interactive remark.
|
||||||
|
/// </summary>
|
||||||
|
public const short InteractPkt = 206;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -20,6 +20,11 @@ namespace Cyber.Networking.Serverside {
|
|||||||
|
|
||||||
private Spawner Spawner;
|
private Spawner Spawner;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The Syncer which syncs. <see cref="Syncer"/>
|
||||||
|
/// </summary>
|
||||||
|
public Syncer Syncer;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates the server-component, and sets the singleton as itself.
|
/// Creates the server-component, and sets the singleton as itself.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -118,6 +123,7 @@ namespace Cyber.Networking.Serverside {
|
|||||||
|
|
||||||
NetworkServer.RegisterHandler(PktType.TextMessage, HandlePacket);
|
NetworkServer.RegisterHandler(PktType.TextMessage, HandlePacket);
|
||||||
NetworkServer.RegisterHandler(PktType.MoveCreature, HandlePacket);
|
NetworkServer.RegisterHandler(PktType.MoveCreature, HandlePacket);
|
||||||
|
NetworkServer.RegisterHandler(PktType.InteractPkt, HandlePacket);
|
||||||
|
|
||||||
NetworkServer.RegisterHandler(MsgType.Connect, OnConnected);
|
NetworkServer.RegisterHandler(MsgType.Connect, OnConnected);
|
||||||
NetworkServer.RegisterHandler(MsgType.Disconnect, OnDisconnected);
|
NetworkServer.RegisterHandler(MsgType.Disconnect, OnDisconnected);
|
||||||
@ -139,35 +145,58 @@ namespace Cyber.Networking.Serverside {
|
|||||||
private void HandlePacket(NetworkMessage msg) {
|
private void HandlePacket(NetworkMessage msg) {
|
||||||
|
|
||||||
switch (msg.msgType) {
|
switch (msg.msgType) {
|
||||||
case PktType.TextMessage:
|
case PktType.TextMessage:
|
||||||
TextMessagePkt TextMsg = new TextMessagePkt();
|
TextMessagePkt TextMsg = new TextMessagePkt();
|
||||||
TextMsg.Deserialize(msg.reader);
|
TextMsg.Deserialize(msg.reader);
|
||||||
Term.Println(TextMsg.Message);
|
Term.Println(TextMsg.Message);
|
||||||
|
break;
|
||||||
|
case PktType.MoveCreature:
|
||||||
|
MoveCreaturePkt MoveCreature = new MoveCreaturePkt();
|
||||||
|
MoveCreature.Deserialize(msg.reader);
|
||||||
|
|
||||||
|
// Check if the player is allowed to move this character
|
||||||
|
Character Controlled = Players[msg.conn.connectionId].Character.GetComponent<Character>();
|
||||||
|
if (Controlled.ID != MoveCreature.SyncBaseID) {
|
||||||
break;
|
break;
|
||||||
case PktType.MoveCreature:
|
}
|
||||||
MoveCreaturePkt MoveCreature = new MoveCreaturePkt();
|
|
||||||
MoveCreature.Deserialize(msg.reader);
|
|
||||||
|
|
||||||
// Check if the player is allowed to move this character
|
Controlled.Move(MoveCreature.Direction);
|
||||||
Character Controlled = Players[msg.conn.connectionId].Character.GetComponent<Character>();
|
|
||||||
if (Controlled.ID != MoveCreature.SyncBaseID) {
|
foreach (var Player in Players) {
|
||||||
break;
|
if (Player.Value.ConnectionID == msg.conn.connectionId) {
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
MoveCreature.Timestamp = NetworkHelper.GetCurrentSystemTime();
|
||||||
|
NetworkServer.SendToClient(Player.Value.ConnectionID, PktType.MoveCreature, MoveCreature);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case PktType.InteractPkt:
|
||||||
|
InteractionPkt Interaction = new InteractionPkt();
|
||||||
|
Interaction.Deserialize(msg.reader);
|
||||||
|
|
||||||
Controlled.Move(MoveCreature.Direction);
|
Character Sender = Players[msg.conn.connectionId].Character;
|
||||||
|
SyncBase Target = Spawner.SyncDB.Get(Interaction.SyncBaseID);
|
||||||
|
|
||||||
foreach (var Player in Players) {
|
if (Target != null && Target is Interactable) {
|
||||||
if (Player.Value.ConnectionID == msg.conn.connectionId) {
|
Interactable Interacted = (Interactable) Target;
|
||||||
continue;
|
Vector3 Delta = Interacted.gameObject.transform.position - Sender.gameObject.transform.position;
|
||||||
|
float ServerInteractionDistance = Sender.InteractionDistance + Sender.MovementSpeed * 0.5f;
|
||||||
|
if (Delta.magnitude <= ServerInteractionDistance) {
|
||||||
|
Interacted.Interact();
|
||||||
|
NetworkServer.SendToAll(PktType.InteractPkt, Interaction);
|
||||||
|
if (Interacted.GetInteractableSyncdata().RequiresSyncing) {
|
||||||
|
Syncer.DirtSyncBase(Interacted.ID);
|
||||||
}
|
}
|
||||||
MoveCreature.Timestamp = NetworkHelper.GetCurrentSystemTime();
|
|
||||||
NetworkServer.SendToClient(Player.Value.ConnectionID, PktType.MoveCreature, MoveCreature);
|
|
||||||
}
|
}
|
||||||
break;
|
} else {
|
||||||
default:
|
Term.Println("Client has reported an erronous SyncBase ID!");
|
||||||
Debug.LogError("Received an unknown packet, id: " + msg.msgType);
|
}
|
||||||
Term.Println("Received an unknown packet, id: " + msg.msgType);
|
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
Debug.LogError("Received an unknown packet, id: " + msg.msgType);
|
||||||
|
Term.Println("Received an unknown packet, id: " + msg.msgType);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user