Merge branch 'master' of github.com:Saltosion/Cyber
This commit is contained in:
commit
6e5c2e1f63
@ -57,6 +57,9 @@ namespace Cyber.Controls {
|
||||
Interactable LookingAt = LookedAtObject.GetComponent<Interactable>();
|
||||
if (LookingAt != null && (LookingAt.transform.position - Character.GetPosition()).magnitude < Character.InteractionDistance) {
|
||||
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 Cyber.Networking;
|
||||
using Cyber.Console;
|
||||
using System;
|
||||
|
||||
namespace Cyber.Entities.SyncBases {
|
||||
|
||||
@ -55,19 +56,22 @@ namespace Cyber.Entities.SyncBases {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Buttons only act as triggers, so only interact is sent to the server.
|
||||
/// Does nothing, because it doesn't need to synced.
|
||||
/// </summary>
|
||||
/// <param name="reader"></param>
|
||||
public override void Deserialize(NetworkReader reader) {
|
||||
}
|
||||
public override void Deserialize(NetworkReader reader) {}
|
||||
|
||||
/// <summary>
|
||||
/// Buttons only act as triggers, so only interact is sent to the server.
|
||||
/// Does nothing, because it doesn't need to synced.
|
||||
/// </summary>
|
||||
/// <param name="writer"></param>
|
||||
public override void Serialize(NetworkWriter writer) {
|
||||
}
|
||||
|
||||
public override InteractableSyncdata GetInteractableSyncdata() {
|
||||
return new InteractableSyncdata(false, true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Buttons only act as triggers, so only interact is sent to the server.
|
||||
/// </summary>
|
||||
|
@ -51,6 +51,10 @@ namespace Cyber.Entities.SyncBases {
|
||||
return new SyncHandletype(false, 10);
|
||||
}
|
||||
|
||||
public override InteractableSyncdata GetInteractableSyncdata() {
|
||||
return new InteractableSyncdata(true, true);
|
||||
}
|
||||
|
||||
private void Update() {
|
||||
float DoorScale = IsOpen ? 0.01f : 1;
|
||||
if (DoorRoot.localScale.x != DoorScale) {
|
||||
|
@ -1,6 +1,8 @@
|
||||
using System.Collections;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace Cyber.Entities.SyncBases {
|
||||
|
||||
@ -14,5 +16,11 @@ namespace Cyber.Entities.SyncBases {
|
||||
/// </summary>
|
||||
public abstract void Interact();
|
||||
|
||||
/// <summary>
|
||||
/// Get Interaction information about this interactible.
|
||||
/// </summary>
|
||||
/// <returns>The Interaction information.</returns>
|
||||
public abstract InteractableSyncdata GetInteractableSyncdata();
|
||||
|
||||
}
|
||||
}
|
@ -13,7 +13,8 @@ namespace Cyber.Entities {
|
||||
public class SyncDB : MonoBehaviour {
|
||||
|
||||
private static readonly Type[] SyncableClasses = new Type[] {
|
||||
typeof(Character)
|
||||
typeof(Character),
|
||||
typeof(Button)
|
||||
};
|
||||
|
||||
private int IDCounter = 0;
|
||||
@ -21,6 +22,8 @@ namespace Cyber.Entities {
|
||||
private Dictionary<Type, List<int>> CategorizedDatabase = new Dictionary<Type, List<int>>();
|
||||
private Dictionary<Type, SyncHandletype> SyncHandletypes = new Dictionary<Type, SyncHandletype>();
|
||||
|
||||
private List<int> StaticSyncBaseIDList = new List<int>();
|
||||
|
||||
/// <summary>
|
||||
/// Add an entity to the database with the given IDs.
|
||||
/// </summary>
|
||||
@ -32,16 +35,8 @@ namespace Cyber.Entities {
|
||||
for (int i = 0; i < SyncableClasses.Length; i++) {
|
||||
SyncBase Syncable = (SyncBase)gameObject.GetComponent(SyncableClasses[i]);
|
||||
if (Syncable != null) {
|
||||
Syncable.ID = ids[Index];
|
||||
Database[ids[Index++]] = Syncable;
|
||||
if (Server.IsRunning()) {
|
||||
Type Type = Syncable.GetType();
|
||||
if (!CategorizedDatabase.ContainsKey(Type)) {
|
||||
CategorizedDatabase.Add(Type, new List<int>());
|
||||
SyncHandletypes.Add(Type, Syncable.GetSyncHandletype());
|
||||
}
|
||||
CategorizedDatabase[Type].Add(Syncable.ID);
|
||||
}
|
||||
Syncable.ID = ids[Index++];
|
||||
AddSyncBaseToDatabase(Syncable);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -134,5 +129,49 @@ namespace Cyber.Entities {
|
||||
}
|
||||
return ID;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets static objects for all objects in the world. This method should be called once per game launch ever.
|
||||
/// </summary>
|
||||
/// <param name="idList">The list of id's to be set. If null, will create new ids.</param>
|
||||
public void SetStaticObjectsIDs(int[] idList = null) {
|
||||
SyncBase[] SyncBases = FindObjectsOfType<SyncBase>();
|
||||
Array.Sort(SyncBases, (a, b) => {
|
||||
Vector3 APos = a.gameObject.transform.position;
|
||||
float AComparison = APos.x + APos.y + APos.z + Array.IndexOf(SyncableClasses, a);
|
||||
Vector3 BPos = b.gameObject.transform.position;
|
||||
float BComparison = BPos.x + BPos.y + BPos.z + Array.IndexOf(SyncableClasses, b);
|
||||
|
||||
return AComparison.CompareTo(BComparison);
|
||||
});
|
||||
if (idList == null) {
|
||||
foreach (SyncBase SyncBase in SyncBases) {
|
||||
SyncBase.ID = CreateID();
|
||||
AddSyncBaseToDatabase(SyncBase);
|
||||
StaticSyncBaseIDList.Add(SyncBase.ID);
|
||||
}
|
||||
} else {
|
||||
for (int i = 0; i < Math.Min(SyncBases.Length, idList.Length); i++) {
|
||||
SyncBases[i].ID = idList[i];
|
||||
AddSyncBaseToDatabase(SyncBases[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int[] GetStaticSyncBaseIDList() {
|
||||
return StaticSyncBaseIDList.ToArray();
|
||||
}
|
||||
|
||||
private void AddSyncBaseToDatabase(SyncBase syncBase) {
|
||||
Database[syncBase.ID] = syncBase;
|
||||
if (Server.IsRunning()) {
|
||||
Type Type = syncBase.GetType();
|
||||
if (!CategorizedDatabase.ContainsKey(Type)) {
|
||||
CategorizedDatabase.Add(Type, new List<int>());
|
||||
SyncHandletypes.Add(Type, syncBase.GetSyncHandletype());
|
||||
}
|
||||
CategorizedDatabase[Type].Add(syncBase.ID);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -127,6 +127,8 @@ namespace Cyber.Networking.Clientside {
|
||||
NetClient.RegisterHandler(PktType.SpawnEntity, HandlePacket);
|
||||
NetClient.RegisterHandler(PktType.MoveCreature, HandlePacket);
|
||||
NetClient.RegisterHandler(PktType.SyncPacket, HandlePacket);
|
||||
NetClient.RegisterHandler(PktType.InteractPkt, HandlePacket);
|
||||
NetClient.RegisterHandler(PktType.StaticObjectIdsPkt, HandlePacket);
|
||||
|
||||
NetClient.RegisterHandler(MsgType.Connect, OnConnected);
|
||||
NetClient.RegisterHandler(MsgType.Disconnect, OnDisconnected);
|
||||
@ -160,7 +162,7 @@ namespace Cyber.Networking.Clientside {
|
||||
Players.Add(Conn.ConnectionID, Conn);
|
||||
break;
|
||||
case (PktType.MassIdentity):
|
||||
MassIdentityPkt Identities = new MassIdentityPkt();
|
||||
IntListPkt Identities = new IntListPkt();
|
||||
Identities.Deserialize(msg.reader);
|
||||
foreach (int currId in Identities.IdList) {
|
||||
Players.Add(currId, new CConnectedPlayer(currId));
|
||||
@ -194,10 +196,29 @@ namespace Cyber.Networking.Clientside {
|
||||
Term.Println("SyncBase " + MoveCreature.SyncBaseID + " is not a Creature");
|
||||
}
|
||||
|
||||
break;
|
||||
case (PktType.InteractPkt):
|
||||
InteractionPkt Interaction = new InteractionPkt();
|
||||
Interaction.Deserialize(msg.reader);
|
||||
if (Interaction.OwnerSyncBaseID == Player.Character.ID) {
|
||||
break;
|
||||
}
|
||||
|
||||
SyncBase Target = Spawner.SyncDB.Get(Interaction.InteractSyncBaseID);
|
||||
if (Target != null && Target is Interactable) {
|
||||
((Interactable) Target).Interact();
|
||||
} else {
|
||||
Term.Println("Server has sent an erroneus SyncBase ID!");
|
||||
}
|
||||
break;
|
||||
case (PktType.SyncPacket):
|
||||
SyncHandler.HandleSyncPkt(msg);
|
||||
break;
|
||||
case (PktType.StaticObjectIdsPkt):
|
||||
IntListPkt StaticIds = new IntListPkt();
|
||||
StaticIds.Deserialize(msg.reader);
|
||||
Spawner.SyncDB.SetStaticObjectsIDs(StaticIds.IdList);
|
||||
break;
|
||||
default:
|
||||
Debug.LogError("Received an unknown packet, id: " + msg.msgType);
|
||||
Term.Println("Received an unknown packet, id: " + msg.msgType);
|
||||
|
@ -4,27 +4,27 @@ using UnityEngine.Networking;
|
||||
namespace Cyber.Networking.Messages {
|
||||
|
||||
/// <summary>
|
||||
/// Packet containing a list of ID's of players currently connected.
|
||||
/// Packet containing integers, used in many packet types, such as <see cref="PktType.MassIdentity"/> and <see cref="PktType.StaticObjectIdsPkt"/>.
|
||||
/// </summary>
|
||||
public class MassIdentityPkt : MessageBase {
|
||||
public class IntListPkt : MessageBase {
|
||||
|
||||
/// <summary>
|
||||
/// List of Connection ID's to send
|
||||
/// List of Integers.
|
||||
/// </summary>
|
||||
public int[] IdList;
|
||||
|
||||
/// <summary>
|
||||
/// Create a Mass Identity packet used to send a list of currently online connections.
|
||||
/// Create a packet containing integers.
|
||||
/// </summary>
|
||||
/// <param name="idList"></param>
|
||||
public MassIdentityPkt(int[] idList) {
|
||||
public IntListPkt(int[] idList) {
|
||||
IdList = idList;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parameter-less constructor using when deserializing the message.
|
||||
/// </summary>
|
||||
public MassIdentityPkt() {
|
||||
public IntListPkt() {
|
||||
|
||||
}
|
||||
|
53
Assets/Scripts/Networking/Messages/InteractionPkt.cs
Normal file
53
Assets/Scripts/Networking/Messages/InteractionPkt.cs
Normal file
@ -0,0 +1,53 @@
|
||||
|
||||
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 InteractSyncBaseID;
|
||||
|
||||
/// <summary>
|
||||
/// Id of the interactor.
|
||||
/// </summary>
|
||||
public int OwnerSyncBaseID;
|
||||
|
||||
/// <summary>
|
||||
/// Creates an InteraktionPkt, which contains the message "someone interacted".
|
||||
/// </summary>
|
||||
/// <param name="SyncBaseID"></param>
|
||||
public InteractionPkt(int syncBaseID) {
|
||||
InteractSyncBaseID = 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) {
|
||||
InteractSyncBaseID = reader.ReadInt32();
|
||||
OwnerSyncBaseID = reader.ReadInt32();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serializes the SyncBaseID for sending.
|
||||
/// </summary>
|
||||
/// <param name="writer"></param>
|
||||
public override void Serialize(NetworkWriter writer) {
|
||||
writer.Write(InteractSyncBaseID);
|
||||
writer.Write(OwnerSyncBaseID);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
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:
|
@ -18,6 +18,7 @@ namespace Cyber.Networking {
|
||||
|
||||
/// <summary>
|
||||
/// Packet containing the identification details about everyone on the server before the client connected.
|
||||
/// This packet contains an <see cref="IntListPkt"/>.
|
||||
/// </summary>
|
||||
public const short MassIdentity = 202;
|
||||
|
||||
@ -38,5 +39,16 @@ namespace Cyber.Networking {
|
||||
/// </summary>
|
||||
public const short SyncPacket = 205;
|
||||
|
||||
/// <summary>
|
||||
/// Packet telling that someone has made an interactive remark.
|
||||
/// </summary>
|
||||
public const short InteractPkt = 206;
|
||||
|
||||
/// <summary>
|
||||
/// Packet containing an id list of static objects existing in the ready game.
|
||||
/// This packet contains an <see cref="IntListPkt"/>.
|
||||
/// </summary>
|
||||
public const short StaticObjectIdsPkt = 207;
|
||||
|
||||
}
|
||||
}
|
@ -20,6 +20,11 @@ namespace Cyber.Networking.Serverside {
|
||||
|
||||
private Spawner Spawner;
|
||||
|
||||
/// <summary>
|
||||
/// The Syncer which syncs. <see cref="Syncer"/>
|
||||
/// </summary>
|
||||
public Syncer Syncer;
|
||||
|
||||
/// <summary>
|
||||
/// Creates the server-component, and sets the singleton as itself.
|
||||
/// </summary>
|
||||
@ -108,6 +113,8 @@ namespace Cyber.Networking.Serverside {
|
||||
|
||||
Spawner = GetComponent<Spawner>();
|
||||
|
||||
Spawner.SyncDB.SetStaticObjectsIDs();
|
||||
|
||||
ConnectionConfig Config = new ConnectionConfig();
|
||||
NetworkChannelID.ReliableSequenced = Config.AddChannel(QosType.ReliableSequenced);
|
||||
NetworkChannelID.UnreliableSequenced = Config.AddChannel(QosType.UnreliableSequenced);
|
||||
@ -118,6 +125,7 @@ namespace Cyber.Networking.Serverside {
|
||||
|
||||
NetworkServer.RegisterHandler(PktType.TextMessage, HandlePacket);
|
||||
NetworkServer.RegisterHandler(PktType.MoveCreature, HandlePacket);
|
||||
NetworkServer.RegisterHandler(PktType.InteractPkt, HandlePacket);
|
||||
|
||||
NetworkServer.RegisterHandler(MsgType.Connect, OnConnected);
|
||||
NetworkServer.RegisterHandler(MsgType.Disconnect, OnDisconnected);
|
||||
@ -163,6 +171,31 @@ namespace Cyber.Networking.Serverside {
|
||||
MoveCreature.Timestamp = NetworkHelper.GetCurrentSystemTime();
|
||||
NetworkServer.SendToClient(Player.Value.ConnectionID, PktType.MoveCreature, MoveCreature);
|
||||
}
|
||||
break;
|
||||
case PktType.InteractPkt:
|
||||
InteractionPkt Interaction = new InteractionPkt();
|
||||
Interaction.Deserialize(msg.reader);
|
||||
|
||||
Character Sender = Players[msg.conn.connectionId].Character;
|
||||
SyncBase Target = Spawner.SyncDB.Get(Interaction.InteractSyncBaseID);
|
||||
|
||||
Interaction.OwnerSyncBaseID = Sender.ID;
|
||||
|
||||
if (Target != null && Target is Interactable) {
|
||||
Interactable Interacted = (Interactable) Target;
|
||||
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);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Term.Println("Client has reported an erronous SyncBase ID!");
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
Debug.LogError("Received an unknown packet, id: " + msg.msgType);
|
||||
@ -189,7 +222,7 @@ namespace Cyber.Networking.Serverside {
|
||||
}
|
||||
|
||||
// Then send the client a list of all other clients
|
||||
NetworkServer.SendToClient(Id, PktType.MassIdentity, new MassIdentityPkt(IdList));
|
||||
NetworkServer.SendToClient(Id, PktType.MassIdentity, new IntListPkt(IdList));
|
||||
|
||||
// Add the player to the list
|
||||
SConnectedPlayer Player = new SConnectedPlayer(msg.conn.connectionId);
|
||||
@ -199,7 +232,7 @@ namespace Cyber.Networking.Serverside {
|
||||
NetworkServer.SendToClient(msg.conn.connectionId,
|
||||
PktType.Identity, new IdentityPkt(msg.conn.connectionId, true));
|
||||
|
||||
// Spawn the player and collet it's IDs
|
||||
// Spawn the player and collect it's IDs
|
||||
Vector3 Position = new Vector3(0, 0, 0);
|
||||
GameObject Obj = Spawner.Spawn(EntityType.NPC, Position);
|
||||
int[] EntityIdList = Spawner.SyncDB.GetEntityIDs(Obj);
|
||||
@ -207,6 +240,9 @@ namespace Cyber.Networking.Serverside {
|
||||
|
||||
NetworkServer.SendToAll(PktType.SpawnEntity, new SpawnEntityPkt(EntityType.NPC, Position, EntityIdList, Id));
|
||||
|
||||
// Send ID's of every existing static SyncBase object in the world.
|
||||
NetworkServer.SendToClient(Id, PktType.StaticObjectIdsPkt, new IntListPkt(Spawner.SyncDB.GetStaticSyncBaseIDList()));
|
||||
|
||||
// Send every entity to the player who just connected.
|
||||
foreach (var Entry in Players) {
|
||||
if (Entry.Key == Id) {
|
||||
|
@ -36,7 +36,6 @@ namespace Cyber.Util {
|
||||
/// </summary>
|
||||
public float Brightness = 1f;
|
||||
|
||||
private TextTextureProperties LastText = new TextTextureProperties("");
|
||||
private Material Material;
|
||||
private bool Dirty = true;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user