public void AddServerScript() { if (Network.IsServer || Network.IsClient) { return; } Network.SetupServer(); GameObject serverScriptObject = new GameObject(); serverScript = serverScriptObject.AddComponent <ServerMonkScript>(); }
public ServerMonkScript() { Instance = this; Server.MessageReceivedEvent += Server_MessageReceivedEvent; Parser.AddRule(MessageType.PlayerAnimation, (Message receivedMessage, Received data) => { string[] anim = receivedMessage.Contents.Split('|'); int animation = int.Parse(anim[0]); int frame = int.Parse(anim[1]); ConnectedClients[data.Sender].Animation = (Player.AnimationIndex)animation; typeof(Player).GetProperty("animationFrame").SetValue(ConnectedClients[data.Sender].Player, frame, null); SendMessageExcluding(new Message(MessageType.RemotePlayerAnimation, $"{ConnectedClients[data.Sender].ID}|{animation}"), data.Sender); }); Parser.AddRule(MessageType.PlayerChunkPosition, (Message receivedMessage, Received data) => { string[] pos = receivedMessage.Contents.Split('|', ','); if (!int.TryParse(pos[0], out int chunkIndex)) { RWConsole.LogError("Bad chunkindex"); } if (!float.TryParse(pos[1], out float x)) { RWConsole.LogError("Bad chunkposition x"); } if (!float.TryParse(pos[2], out float y)) { RWConsole.LogError("Bad chunkposition y"); } if (!float.TryParse(pos[3], out float rx)) { RWConsole.LogError("Bad chunkrotation x"); } if (!float.TryParse(pos[4], out float ry)) { RWConsole.LogError("Bad chunkrotation y"); } if (!float.TryParse(pos[5], out float velx)) { RWConsole.LogError("Bad velocity x"); } if (!float.TryParse(pos[6], out float vely)) { RWConsole.LogError("Bad velocity y"); } chunkIndex = int.Parse(pos[0]); Vector2 chunkPosition = new Vector2(x, y); Vector2 chunkRotation = new Vector2(rx, ry); Vector2 chunkVelocity = new Vector2(velx, vely); ConnectedClients[data.Sender].Creature.bodyChunks[chunkIndex].pos = chunkPosition; ConnectedClients[data.Sender].Creature.bodyChunks[chunkIndex].Rotation.Set(rx, ry); ConnectedClients[data.Sender].Creature.bodyChunks[chunkIndex].vel = chunkVelocity; SendMessageExcluding(new Message( MessageType.RemotePlayerChunkPosition, $"{ConnectedClients[data.Sender].ID}|{chunkIndex}|" + $"{chunkPosition.x},{chunkPosition.y},{chunkRotation.x},{chunkRotation.y}" ), data.Sender); }); Parser.AddRule(MessageType.Handshake, (Message receivedMessage, Received data) => { SendMessageTo(new Message(MessageType.HandshakeAck, ""), data.Sender); }); }
public ServerMonkScript() { Instance = this; Server.MessageReceivedEvent += Server_MessageReceivedEvent; }