diff --git a/Assets/Scripts/Controls/PlayerController.cs b/Assets/Scripts/Controls/PlayerController.cs
index ec991c5..1c733dc 100644
--- a/Assets/Scripts/Controls/PlayerController.cs
+++ b/Assets/Scripts/Controls/PlayerController.cs
@@ -3,6 +3,9 @@ using System.Collections.Generic;
using UnityEngine;
using Cyber.Entities;
using Cyber.Console;
+using Cyber.Networking.Clientside;
+using Cyber.Networking;
+using Cyber.Networking.Messages;
namespace Cyber.Controls {
@@ -23,8 +26,12 @@ namespace Cyber.Controls {
Vector3 Move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
if (Move.sqrMagnitude != 0) {
Character.Move(transform.TransformDirection(Move));
+
+ Client.Send(PktType.MoveCreature, new MoveCreaturePkt(transform.TransformDirection(Move), Character.ID));
} else if (Character.Moving()) {
Character.Stop();
+
+ Client.Send(PktType.MoveCreature, new MoveCreaturePkt(new Vector3(), Character.ID));
}
} else if (Character.Moving()) {
// The debug console is open, stop the player.
diff --git a/Assets/Scripts/Entities/SyncDB.cs b/Assets/Scripts/Entities/SyncDB.cs
index 5fc2d7e..2866fc7 100644
--- a/Assets/Scripts/Entities/SyncDB.cs
+++ b/Assets/Scripts/Entities/SyncDB.cs
@@ -77,6 +77,9 @@ namespace Cyber.Entities {
///
/// The ID.
public SyncBase Get(int id) {
+ if (!Database.ContainsKey(id)) {
+ return null;
+ }
return Database[id];
}
diff --git a/Assets/Scripts/Networking/Clientside/CConnectedPlayer.cs b/Assets/Scripts/Networking/Clientside/CConnectedPlayer.cs
index 37154b4..1c598b9 100644
--- a/Assets/Scripts/Networking/Clientside/CConnectedPlayer.cs
+++ b/Assets/Scripts/Networking/Clientside/CConnectedPlayer.cs
@@ -18,6 +18,10 @@ namespace Cyber.Networking.Clientside {
///
public Character Character;
+ ///
+ /// Create a connected player on the Clientside and give it it's ID.
+ ///
+ /// Connection ID of the player.
public CConnectedPlayer(int connectionID) {
ConnectionID = connectionID;
}
diff --git a/Assets/Scripts/Networking/Clientside/Client.cs b/Assets/Scripts/Networking/Clientside/Client.cs
index ef2af51..7b5db31 100644
--- a/Assets/Scripts/Networking/Clientside/Client.cs
+++ b/Assets/Scripts/Networking/Clientside/Client.cs
@@ -113,6 +113,7 @@ namespace Cyber.Networking.Clientside {
NetClient.RegisterHandler(PktType.Identity, HandlePacket);
NetClient.RegisterHandler(PktType.MassIdentity, HandlePacket);
NetClient.RegisterHandler(PktType.SpawnEntity, HandlePacket);
+ NetClient.RegisterHandler(PktType.MoveCreature, HandlePacket);
NetClient.RegisterHandler(MsgType.Connect, OnConnected);
NetClient.RegisterHandler(MsgType.Disconnect, OnDisconnected);
@@ -163,6 +164,20 @@ namespace Cyber.Networking.Clientside {
EntityType = EntityType.PC;
}
Spawner.Spawn(EntityType, SpawnPkt.Position, SpawnPkt.SyncBaseIDList);
+ break;
+ case (PktType.MoveCreature):
+ MoveCreaturePkt MoveCreature = new MoveCreaturePkt();
+ MoveCreature.Deserialize(msg.reader);
+
+ SyncBase SyncBase = Spawner.SyncDB.Get(MoveCreature.SyncBaseID);
+ if (SyncBase != null || SyncBase is Character ) {
+ Character Character = (Character) SyncBase;
+ Character.Move(MoveCreature.Direction);
+ } else {
+ Debug.LogError("SyncBase " + MoveCreature.SyncBaseID + " is not a Creature");
+ Term.Println("SyncBase " + MoveCreature.SyncBaseID + " is not a Creature");
+ }
+
break;
default:
Debug.LogError("Received an unknown packet, id: " + msg.msgType);
diff --git a/Assets/Scripts/Networking/Messages/MoveCreaturePkt.cs b/Assets/Scripts/Networking/Messages/MoveCreaturePkt.cs
new file mode 100644
index 0000000..cc17820
--- /dev/null
+++ b/Assets/Scripts/Networking/Messages/MoveCreaturePkt.cs
@@ -0,0 +1,58 @@
+using UnityEngine;
+using UnityEngine.Networking;
+
+namespace Cyber.Networking.Messages {
+
+ ///
+ /// Packet informing the client to move a creature, or the server to request change in movement.
+ /// More information at
+ ///
+ public class MoveCreaturePkt : MessageBase {
+
+ ///
+ /// Direction of the new movement (or (0, 0, 0) for stopping)
+ ///
+ public Vector3 Direction;
+
+ ///
+ /// SyncBase ID of the Creature to be moved.
+ ///
+ public int SyncBaseID;
+
+ ///
+ /// Creates a MoveCreaturePkt which contains the direction of desired movement (or (0, 0, 0) for stopping)
+ ///
+ /// Direction of movement
+ /// SyncBase ID of the Creature.
+ public MoveCreaturePkt(Vector3 direction, int syncBaseId) {
+ Direction = direction;
+ SyncBaseID = syncBaseId;
+ }
+
+ ///
+ /// Parameter-less constructor using when deserializing the message.
+ ///
+ public MoveCreaturePkt() {
+
+ }
+
+ ///
+ /// Used to deserialize a message received via networking.
+ ///
+ ///
+ public override void Deserialize(NetworkReader reader) {
+ Direction = reader.ReadVector3();
+ SyncBaseID = reader.ReadInt32();
+ }
+
+ ///
+ /// Used to serialize the message before it is sent.
+ ///
+ ///
+ public override void Serialize(NetworkWriter writer) {
+ writer.Write(Direction);
+ writer.Write(SyncBaseID);
+ }
+
+ }
+}
diff --git a/Assets/Scripts/Networking/Messages/MoveCreaturePkt.cs.meta b/Assets/Scripts/Networking/Messages/MoveCreaturePkt.cs.meta
new file mode 100644
index 0000000..f47baea
--- /dev/null
+++ b/Assets/Scripts/Networking/Messages/MoveCreaturePkt.cs.meta
@@ -0,0 +1,12 @@
+fileFormatVersion: 2
+guid: 5f58037fe80279d46b570c4653c2e05e
+timeCreated: 1494301859
+licenseType: Free
+MonoImporter:
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/Assets/Scripts/Networking/PktType.cs b/Assets/Scripts/Networking/PktType.cs
index 1c897f8..2bd1db5 100644
--- a/Assets/Scripts/Networking/PktType.cs
+++ b/Assets/Scripts/Networking/PktType.cs
@@ -25,5 +25,12 @@ namespace Cyber.Networking {
/// Packet containing necessary information about spawning an entity.
///
public const short SpawnEntity = 203;
+
+ ///
+ /// Packet informing a creature must be moved or stopped.
+ /// This Packet can be used by the client to inform a request to move or the server
+ /// to inform actual movement.
+ ///
+ public const short MoveCreature = 204;
}
}
\ No newline at end of file
diff --git a/Assets/Scripts/Networking/Serverside/Server.cs b/Assets/Scripts/Networking/Serverside/Server.cs
index 582e66a..e63444e 100644
--- a/Assets/Scripts/Networking/Serverside/Server.cs
+++ b/Assets/Scripts/Networking/Serverside/Server.cs
@@ -98,6 +98,7 @@ namespace Cyber.Networking.Serverside {
NetworkServer.Listen(port);
NetworkServer.RegisterHandler(PktType.TextMessage, HandlePacket);
+ NetworkServer.RegisterHandler(PktType.MoveCreature, HandlePacket);
NetworkServer.RegisterHandler(MsgType.Connect, OnConnected);
NetworkServer.RegisterHandler(MsgType.Disconnect, OnDisconnected);
@@ -122,6 +123,27 @@ namespace Cyber.Networking.Serverside {
TextMsg.Deserialize(msg.reader);
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();
+ Debug.Log(Controlled.ID);
+ Debug.Log(MoveCreature.SyncBaseID);
+ if (Controlled.ID != MoveCreature.SyncBaseID) {
+ break;
+ }
+
+ Controlled.Move(MoveCreature.Direction);
+
+ foreach (var Player in Players) {
+ if (Player.Value.ConnectionID == msg.conn.connectionId) {
+ continue;
+ }
+ NetworkServer.SendToClient(Player.Value.ConnectionID, PktType.MoveCreature, MoveCreature);
+ }
+ break;
default:
Debug.LogError("Received an unknown packet, id: " + msg.msgType);
Term.Println("Received an unknown packet, id: " + msg.msgType);
@@ -172,7 +194,7 @@ namespace Cyber.Networking.Serverside {
}
Character Char = Players[Entry.Key].Character;
GameObject CurrObj = Char.gameObject;
- int[] CurrEntityIdList = Spawner.SyncDB.GetNewEntityIDs(CurrObj);
+ int[] CurrEntityIdList = Spawner.SyncDB.GetEntityIDs(CurrObj);
NetworkServer.SendToClient(Id, PktType.SpawnEntity,
new SpawnEntityPkt(EntityType.NPC, CurrObj.transform.position, CurrEntityIdList, Entry.Key));
}