mirror of
https://github.com/misternebula/quantum-space-buddies.git
synced 2025-01-17 01:13:05 +00:00
Using messages to udpate sectors
This commit is contained in:
parent
c6dcac1236
commit
f1d8beed02
@ -9,10 +9,24 @@ namespace QSB {
|
||||
float _smoothSpeed = 10f;
|
||||
public static NetworkPlayer localInstance { get; private set; }
|
||||
|
||||
[SyncVar(hook = "OnChangeSector")]
|
||||
public Sector.Name _sector;
|
||||
Transform _sectorTransform;
|
||||
|
||||
public class SectorMessage: MessageBase {
|
||||
public int sector;
|
||||
public int id;
|
||||
|
||||
public override void Deserialize (NetworkReader reader) {
|
||||
sector = reader.ReadInt32();
|
||||
id = reader.ReadInt32();
|
||||
}
|
||||
|
||||
public override void Serialize (NetworkWriter writer) {
|
||||
writer.Write(sector);
|
||||
writer.Write(id);
|
||||
}
|
||||
}
|
||||
|
||||
void Start () {
|
||||
QSB.LogToScreen("Started network player");
|
||||
|
||||
@ -30,6 +44,45 @@ namespace QSB {
|
||||
_body.localPosition = Vector3.zero;
|
||||
_body.localRotation = Quaternion.identity;
|
||||
}
|
||||
|
||||
if (isServer) {
|
||||
NetworkServer.RegisterHandler(MsgType.Highest + 1, OnReceiveMessage);
|
||||
} else {
|
||||
NetworkManager.singleton.client.RegisterHandler(MsgType.Highest + 1, OnReceiveMessage);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void DoThisThing () {
|
||||
_sector = Sector.Name.Comet;
|
||||
}
|
||||
|
||||
private void OnReceiveMessage (NetworkMessage netMsg) {
|
||||
QSB.LogToScreen("Messager Receive");
|
||||
SectorMessage msg = netMsg.ReadMessage<SectorMessage>();
|
||||
|
||||
if (isServer) {
|
||||
if (msg.id == connectionToClient.connectionId) {
|
||||
NetworkServer.SendToAll(MsgType.Highest + 1, msg);
|
||||
SetSectorById(msg.sector);
|
||||
}
|
||||
} else {
|
||||
if (msg.id == connectionToServer.connectionId) {
|
||||
SetSectorById(msg.sector);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SetSectorById (int sectorId) {
|
||||
QSB.LogToScreen("Set sector by ID");
|
||||
var sectorName = (Sector.Name) sectorId;
|
||||
var sectors = GameObject.FindObjectsOfType<Sector>();
|
||||
foreach (var sector in sectors) {
|
||||
if (sectorName == sector.GetName()) {
|
||||
_sectorTransform = sector.transform;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void EnterSector (Sector sector) {
|
||||
@ -39,29 +92,13 @@ namespace QSB {
|
||||
var name = sector.GetName();
|
||||
if (name != Sector.Name.Unnamed) {
|
||||
QSB.LogToScreen("Client entered sector", name.ToString());
|
||||
CmdSetSector(name);
|
||||
}
|
||||
}
|
||||
|
||||
[Command]
|
||||
void CmdSetSector (Sector.Name name) {
|
||||
if (!isServer) {
|
||||
QSB.LogToScreen("This is not the server, so skipping CmdSetSector");
|
||||
return;
|
||||
}
|
||||
QSB.LogToScreen("This is iserver, so setting client sector to", name.ToString());
|
||||
_sector = name;
|
||||
}
|
||||
|
||||
void OnChangeSector (Sector.Name name) {
|
||||
QSB.LogToScreen("Client received onChange from server, to sector", name.ToString());
|
||||
_sector = name;
|
||||
|
||||
var sectors = GameObject.FindObjectsOfType<Sector>();
|
||||
foreach (var sector in sectors) {
|
||||
if (name == sector.GetName()) {
|
||||
_sectorTransform = sector.transform;
|
||||
return;
|
||||
SectorMessage msg = new SectorMessage();
|
||||
msg.sector = (int) sector.GetName();
|
||||
msg.id = connectionToServer.connectionId;
|
||||
if (isServer) {
|
||||
NetworkServer.SendToAll(MsgType.Highest + 1, msg);
|
||||
} else {
|
||||
connectionToServer.Send(MsgType.Highest + 1, msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user