示例#1
0
    void Update()
    {
        if (service == null)
        {
            return;
        }
        if (!initialized)
        {
            return;
        }

        ExEntityLink link = GetComponent <ExEntityLink>();

        timer += Time.unscaledDeltaTime;
        if (timer >= service.server.tickRate / 1000.0f)
        {
            timer -= service.server.tickRate / 1000.0f;

            Vector3    pos = transform.position;
            Quaternion rot = transform.rotation;


            if (link != null && ((pos - lastPos).magnitude > distanceThreshold) || (Quaternion.Angle(lastRot, rot) > angleThreshold))
            {
                service.server.localClient.Call(mservice.RequestMove, Pack.Base64(link.id), Pack.Base64(pos), Pack.Base64(rot.eulerAngles));
                lastRot = transform.rotation;
                lastPos = pos;
            }
        }
    }
示例#2
0
        // Server-side logic
#if !UNITY
        /// <summary> Server Command, used when transferring a client into a map. </summary>
        /// <param name="client"> Client connection object </param>
        /// <param name="mapId"> ID to put client into </param>
        public void EnterMap(Client client, string mapId, Vector3 position, Vector3 rotation, int?mapInstanceIndex = null)
        {
            Log.Info($"\\jClient {client.identity} entering map {mapId} ");
            var map = GetMap(mapId, mapInstanceIndex);

            Log.Info($"\\jGot map { map.identity }");
            map.EnterMap(client);

            map.Move(client.id, position, rotation, true);

            // Call RPC for initializing clients
            if (mapInstanceIndex != null)
            {
                client.Call(SetMap, mapId, Pack.Base64(position), Pack.Base64(rotation), mapInstanceIndex.Value);
            }
            else
            {
                client.Call(SetMap, mapId, Pack.Base64(position), Pack.Base64(rotation));
            }
        }
示例#3
0
        /// <summary> Formats a message into a string intended to be sent over the network. </summary>
        /// <param name="stuff"> Array of parameters to format. </param>
        /// <returns> String of all objects in <paramref name="stuff"/> formatted to be sent over the network. </returns>
        public static string FormatCall(RPCMessage.Handler callback, params System.Object[] stuff)
        {
            string time       = Pack.Base64(DateTime.UtcNow);
            string methodName = callback.Method.Name;
            string typeName   = callback.Method.DeclaringType.ShortName();
            string msg;

            if (stuff.Length > 0)
            {
                string[] strs = new string[stuff.Length];
                for (int i = 0; i < strs.Length; i++)
                {
                    strs[i] = stuff[i].ToString();
                }
                string rest = String.Join("" + RPCMessage.SEPARATOR, strs);
                msg = String.Join("" + RPCMessage.SEPARATOR, typeName, methodName, time, rest);
            }
            else
            {
                msg = String.Join("" + RPCMessage.SEPARATOR, typeName, methodName, time);
            }
            return(msg);
        }
示例#4
0
        private void UpdateEntities()
        {
            EntityMoveRequest        move;
            List <EntityMoveRequest> retry = null;

            while (toMove.TryDequeue(out move))
            {
                if (entities.ContainsKey(move.id))
                {
                    var e = entities[move.id];
                    if (e == null)
                    {
                        Log.Warning($"No entity {move.id} exists!");
                        continue;
                    }

                    var trs    = e.RequireComponent <TRS>();
                    var client = service.server.GetClient(move.id);

                    // move.oldPos is populated with the TRS position at the time of creating the move request.
                    // move.newPos is the desired destination
                    // Delta only matters if the client is sending the move.
                    Vector3 oldPos = trs.position;
                    Vector3 oldRot = trs.rotation;

                    var  delta    = oldPos - move.newPos;
                    bool posInMap = Contains(info.bounds, info.boundsShape, move.newPos);

                    if (posInMap && (move.serverMove || delta.magnitude < speedCap))
                    {
                        // Log.Debug($"Moving {move.id}\n\tposition {move.oldPos} => {move.newPos}\n\trotation {move.oldRot} => {move.newRot}");

                        trs.position = move.newPos;
                        trs.rotation = move.newRot;
                        trs.Send();

                        var oldCellPos = CellPositionFor(oldPos);
                        var newCellPos = CellPositionFor(move.newPos);

                        if (oldCellPos != newCellPos)
                        {
                            Cell oldCell = GetCell(oldCellPos);
                            Cell newCell = RequireCell(newCellPos);

                            Log.Info($"Entity {move.id} moving from {oldCellPos} => {newCellPos}.");
                            oldCell.TransferEntity(newCell, move.id);
                        }
                    }
                    else
                    {
                        if (posInMap)
                        {
                            Log.Warning($"Entity {move.id} Tried to move too far! {delta.magnitude} vs {speedCap}");
                        }
                        else
                        {
                            Log.Warning($"Entity {move.id} Tried to move outside the map! {move.newPos} vs {info.bounds} / {info.boundsShape}");
                        }

                        if (client != null)
                        {
                            client.Call(service.Rubberband, Pack.Base64(move.id), Pack.Base64(oldPos), Pack.Base64(oldRot));
                        }
                    }
                }
                else
                {
                    if (toSpawn.Contains(move.id))
                    {
                        Log.Debug($"Map {identity} trying to move entity {move.id}, but it has not been spawned yet. Adding back to queue.");

                        (retry == null ? (retry = new List <EntityMoveRequest>()) : retry).Add(move);
                    }
                    else
                    {
                        Log.Warning($"Map {identity} trying to move entity {move.id}, but it does not exist on this map.");
                    }
                }
            }

            if (retry != null)
            {
                foreach (var m in retry)
                {
                    toMove.Enqueue(m);
                }
            }
        }