Make moving possible between clients
This commit is contained in:
parent
eb23f474c5
commit
897e797195
@ -3,6 +3,9 @@ using System.Collections.Generic;
|
|||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using Cyber.Entities;
|
using Cyber.Entities;
|
||||||
using Cyber.Console;
|
using Cyber.Console;
|
||||||
|
using Cyber.Networking.Clientside;
|
||||||
|
using Cyber.Networking;
|
||||||
|
using Cyber.Networking.Messages;
|
||||||
|
|
||||||
namespace Cyber.Controls {
|
namespace Cyber.Controls {
|
||||||
|
|
||||||
@ -23,8 +26,12 @@ namespace Cyber.Controls {
|
|||||||
Vector3 Move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
|
Vector3 Move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
|
||||||
if (Move.sqrMagnitude != 0) {
|
if (Move.sqrMagnitude != 0) {
|
||||||
Character.Move(transform.TransformDirection(Move));
|
Character.Move(transform.TransformDirection(Move));
|
||||||
|
|
||||||
|
Client.Send(PktType.MoveCreature, new MoveCreaturePkt(transform.TransformDirection(Move), Character.ID));
|
||||||
} else if (Character.Moving()) {
|
} else if (Character.Moving()) {
|
||||||
Character.Stop();
|
Character.Stop();
|
||||||
|
|
||||||
|
Client.Send(PktType.MoveCreature, new MoveCreaturePkt(new Vector3(), Character.ID));
|
||||||
}
|
}
|
||||||
} else if (Character.Moving()) {
|
} else if (Character.Moving()) {
|
||||||
// The debug console is open, stop the player.
|
// The debug console is open, stop the player.
|
||||||
|
@ -77,6 +77,9 @@ namespace Cyber.Entities {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="id">The ID.</param>
|
/// <param name="id">The ID.</param>
|
||||||
public SyncBase Get(int id) {
|
public SyncBase Get(int id) {
|
||||||
|
if (!Database.ContainsKey(id)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
return Database[id];
|
return Database[id];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,6 +18,10 @@ namespace Cyber.Networking.Clientside {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public Character Character;
|
public Character Character;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Create a connected player on the Clientside and give it it's ID.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="connectionID">Connection ID of the player.</param>
|
||||||
public CConnectedPlayer(int connectionID) {
|
public CConnectedPlayer(int connectionID) {
|
||||||
ConnectionID = connectionID;
|
ConnectionID = connectionID;
|
||||||
}
|
}
|
||||||
|
@ -113,6 +113,7 @@ namespace Cyber.Networking.Clientside {
|
|||||||
NetClient.RegisterHandler(PktType.Identity, HandlePacket);
|
NetClient.RegisterHandler(PktType.Identity, HandlePacket);
|
||||||
NetClient.RegisterHandler(PktType.MassIdentity, HandlePacket);
|
NetClient.RegisterHandler(PktType.MassIdentity, HandlePacket);
|
||||||
NetClient.RegisterHandler(PktType.SpawnEntity, HandlePacket);
|
NetClient.RegisterHandler(PktType.SpawnEntity, HandlePacket);
|
||||||
|
NetClient.RegisterHandler(PktType.MoveCreature, HandlePacket);
|
||||||
|
|
||||||
NetClient.RegisterHandler(MsgType.Connect, OnConnected);
|
NetClient.RegisterHandler(MsgType.Connect, OnConnected);
|
||||||
NetClient.RegisterHandler(MsgType.Disconnect, OnDisconnected);
|
NetClient.RegisterHandler(MsgType.Disconnect, OnDisconnected);
|
||||||
@ -163,6 +164,20 @@ namespace Cyber.Networking.Clientside {
|
|||||||
EntityType = EntityType.PC;
|
EntityType = EntityType.PC;
|
||||||
}
|
}
|
||||||
Spawner.Spawn(EntityType, SpawnPkt.Position, SpawnPkt.SyncBaseIDList);
|
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;
|
break;
|
||||||
default:
|
default:
|
||||||
Debug.LogError("Received an unknown packet, id: " + msg.msgType);
|
Debug.LogError("Received an unknown packet, id: " + msg.msgType);
|
||||||
|
58
Assets/Scripts/Networking/Messages/MoveCreaturePkt.cs
Normal file
58
Assets/Scripts/Networking/Messages/MoveCreaturePkt.cs
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
using UnityEngine;
|
||||||
|
using UnityEngine.Networking;
|
||||||
|
|
||||||
|
namespace Cyber.Networking.Messages {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Packet informing the client to move a creature, or the server to request change in movement.
|
||||||
|
/// More information at <see cref="PktType.MoveCreature"/>
|
||||||
|
/// </summary>
|
||||||
|
public class MoveCreaturePkt : MessageBase {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Direction of the new movement (or (0, 0, 0) for stopping)
|
||||||
|
/// </summary>
|
||||||
|
public Vector3 Direction;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// SyncBase ID of the Creature to be moved.
|
||||||
|
/// </summary>
|
||||||
|
public int SyncBaseID;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a MoveCreaturePkt which contains the direction of desired movement (or (0, 0, 0) for stopping)
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="direction">Direction of movement</param>
|
||||||
|
/// <param name="syncBaseId">SyncBase ID of the Creature.</param>
|
||||||
|
public MoveCreaturePkt(Vector3 direction, int syncBaseId) {
|
||||||
|
Direction = direction;
|
||||||
|
SyncBaseID = syncBaseId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Parameter-less constructor using when deserializing the message.
|
||||||
|
/// </summary>
|
||||||
|
public MoveCreaturePkt() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Used to deserialize a message received via networking.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="reader"></param>
|
||||||
|
public override void Deserialize(NetworkReader reader) {
|
||||||
|
Direction = reader.ReadVector3();
|
||||||
|
SyncBaseID = reader.ReadInt32();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Used to serialize the message before it is sent.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="writer"></param>
|
||||||
|
public override void Serialize(NetworkWriter writer) {
|
||||||
|
writer.Write(Direction);
|
||||||
|
writer.Write(SyncBaseID);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
12
Assets/Scripts/Networking/Messages/MoveCreaturePkt.cs.meta
Normal file
12
Assets/Scripts/Networking/Messages/MoveCreaturePkt.cs.meta
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 5f58037fe80279d46b570c4653c2e05e
|
||||||
|
timeCreated: 1494301859
|
||||||
|
licenseType: Free
|
||||||
|
MonoImporter:
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
@ -25,5 +25,12 @@ namespace Cyber.Networking {
|
|||||||
/// Packet containing necessary information about spawning an entity.
|
/// Packet containing necessary information about spawning an entity.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public const short SpawnEntity = 203;
|
public const short SpawnEntity = 203;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 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.
|
||||||
|
/// </summary>
|
||||||
|
public const short MoveCreature = 204;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -98,6 +98,7 @@ namespace Cyber.Networking.Serverside {
|
|||||||
NetworkServer.Listen(port);
|
NetworkServer.Listen(port);
|
||||||
|
|
||||||
NetworkServer.RegisterHandler(PktType.TextMessage, HandlePacket);
|
NetworkServer.RegisterHandler(PktType.TextMessage, HandlePacket);
|
||||||
|
NetworkServer.RegisterHandler(PktType.MoveCreature, HandlePacket);
|
||||||
|
|
||||||
NetworkServer.RegisterHandler(MsgType.Connect, OnConnected);
|
NetworkServer.RegisterHandler(MsgType.Connect, OnConnected);
|
||||||
NetworkServer.RegisterHandler(MsgType.Disconnect, OnDisconnected);
|
NetworkServer.RegisterHandler(MsgType.Disconnect, OnDisconnected);
|
||||||
@ -122,6 +123,27 @@ namespace Cyber.Networking.Serverside {
|
|||||||
TextMsg.Deserialize(msg.reader);
|
TextMsg.Deserialize(msg.reader);
|
||||||
Term.Println(TextMsg.Message);
|
Term.Println(TextMsg.Message);
|
||||||
break;
|
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>();
|
||||||
|
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:
|
default:
|
||||||
Debug.LogError("Received an unknown packet, id: " + msg.msgType);
|
Debug.LogError("Received an unknown packet, id: " + msg.msgType);
|
||||||
Term.Println("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;
|
Character Char = Players[Entry.Key].Character;
|
||||||
GameObject CurrObj = Char.gameObject;
|
GameObject CurrObj = Char.gameObject;
|
||||||
int[] CurrEntityIdList = Spawner.SyncDB.GetNewEntityIDs(CurrObj);
|
int[] CurrEntityIdList = Spawner.SyncDB.GetEntityIDs(CurrObj);
|
||||||
NetworkServer.SendToClient(Id, PktType.SpawnEntity,
|
NetworkServer.SendToClient(Id, PktType.SpawnEntity,
|
||||||
new SpawnEntityPkt(EntityType.NPC, CurrObj.transform.position, CurrEntityIdList, Entry.Key));
|
new SpawnEntityPkt(EntityType.NPC, CurrObj.transform.position, CurrEntityIdList, Entry.Key));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user