Пример #1
0
        public static string Spawn(string name, Vector2 pos)
        {
            var obj = Get <GameObject>(name);

            if (obj == null)
            {
                return($"{name} could not be found or is not a GameObject, so cannot be spawned into world.");
            }

            var pool = obj.GetComponent <PoolObject>();

            if (pool == null)
            {
                var spawned = Instantiate(obj);
                spawned.transform.position = pos;

                var net = spawned.GetComponent <NetObject>();
                if (net != null)
                {
                    JNet.Spawn(net);
                    return("Spawned as networked object.");
                }
                else
                {
                    return("Spawned as client-only prefab.");
                }
            }
            else
            {
                var spawned = PoolObject.Spawn(pool);
                spawned.transform.position = pos;
                return("Spawed as pooled object.");
            }
        }
Пример #2
0
        private void StartServer()
        {
            JNet.StartServer("My Server Name", 7777, 4);
            JNet.GetServer().UponConnection = (client) =>
            {
                // Create a player object.
                string playerName = $"Player #{Player.AllPlayers.Count}";
                Player player     = Instantiate(Prefab);
                player.gameObject.name = playerName;
                player.Name            = playerName;
                player.Money           = 1000;

                // Assign the player object reference to the remote data.
                client.Data = player;

                // Spawn with local client authority.
                JNet.Spawn(player, client.Connection);
            };
            JNet.GetServer().UponDisconnection = (client, reason) =>
            {
                // Remove the player object from existence.
                Player player = client.GetData <Player>();
                if (player != null)
                {
                    Destroy(player.gameObject);
                }
            };
        }
Пример #3
0
        public void StopPlayback()
        {
            if (!IsInPlayback)
                return;

            IsInPlayback = false;

            JNet.Log("Stopped playback.");
        }
Пример #4
0
        private void Start()
        {
            JNet.Init("Project B");

            // TODO move to somewhere more sensible, such as 'game manager'
            Spawnables.NetRegisterAll();

            UI.AddDrawer(DrawUI);
        }
Пример #5
0
        private void PostData(DataChunk c)
        {
            NetIncomingMessage msg = new NetIncomingMessage(NetIncomingMessageType.Data);

            msg.LengthBits = c.BitLength;
            msg.LengthBytes = (int)Math.Ceiling((double)c.BitLength / 8);
            msg.Data = c.Data;

            JNet.GetClient().InjectDataMessage(msg);
        }
Пример #6
0
        public void StartPlayback(string filePath)
        {
            if (IsRecording)
                return;
            if (IsInPlayback)
                return;

            if (!File.Exists(filePath))
            {
                JNet.Error($"File {filePath} does not exist.");
                return;
            }

            var bytes = File.ReadAllBytes(filePath);
            int index = 0;
            while (true)
            {
                // Read chunk
                float time = BitConverter.ToSingle(bytes, index);
                index += 4;
                int bits = BitConverter.ToInt32(bytes, index);
                index += 4;

                // Calculate number of bytes from bits.
                int byteCount = (int)Math.Ceiling((double)bits / 8);

                // Copy bytes.
                byte[] data = new byte[byteCount];
                Array.Copy(bytes, index, data, 0, byteCount);
                index += byteCount;


                // Make data chunk.
                var chunk = new DataChunk();
                chunk.BitLength = bits;
                chunk.Time = time;
                chunk.Data = data;

                PendingData.Enqueue(chunk);

                if (index >= bytes.Length)
                    break;
            }

            Debug.Log($"Loaded {PendingData.Count} data chunks.");

            IsInPlayback = true;
            Time = 0f;

            JNet.Log("Started playback.");
        }
Пример #7
0
 private void NetRegister()
 {
     foreach (var item in Objects)
     {
         if (item != null && item is GameObject)
         {
             var no = (item as GameObject).GetComponent <NetObject>();
             if (no != null)
             {
                 JNet.RegisterPrefab(no);
                 Debug.Log($"Registered net: {item}");
             }
         }
     }
 }
Пример #8
0
        public void NetSpawn()
        {
            if (!JNet.IsServer)
            {
                Debug.LogError("Cannot network spawn auto destroy effect when not on server.");
                return;
            }

            ushort  id  = NetSpawnID;
            Vector2 pos = transform.position;

            var msg = JNet.CreateCustomMessage(true, CustomMsg.AUTO_DESTROY_SPAWN, 14);

            msg.Write(id);
            msg.Write(pos);
            JNet.SendCustomMessageToAll(JNet.GetServer().LocalClientConnection, msg, Lidgren.Network.NetDeliveryMethod.Unreliable, 0);
        }
Пример #9
0
        public void LogIncoming(NetIncomingMessage msg)
        {
            if (!IsRecording)
                return;
            if (IsInPlayback)
                return;

            int bits = msg.LengthBits;
            int bytes = msg.LengthBytes;
            byte[] data = msg.Data;

            JNet.Assert(msg.LengthBytes == (int)Math.Ceiling((double)bits / 8), "Unexpected data length.");

            Writer.Write(Time);
            Writer.Write(bits);
            Writer.Write(data, 0, bytes);
        }
Пример #10
0
        private void StartClient()
        {
            JNet.StartClient();

            JNet.GetClient().UponConnect = () =>
            {
                Debug.Log($"Client connected.");
            };
            JNet.GetClient().UponDisconnect = (reason) =>
            {
                Debug.Log($"Client disconnected. ({reason})");
            };
            JNet.GetClient().UponCustomData = (id, msg) =>
            {
                switch (id)
                {
                case CustomMsg.PROJECTILE_SPAWN:
                    Projectile.ProcessMessage(msg);
                    break;

                case CustomMsg.AUTO_DESTROY_SPAWN:
                    AutoDestroy.ProcessMessage(msg);
                    break;

                default:
                    Debug.LogError($"Unhandled custom data id: {id}");
                    break;
                }
            };

            if (record)
            {
                StartRecording();
            }

            if (JNet.IsServer)
            {
                JNet.ConnectClientToHost(null);
            }
            else
            {
                JNet.ConnectClientToRemote("127.0.0.1", 7777);
            }
        }
Пример #11
0
        // SPAWNING & NETWORKING

        public static Projectile Spawn(Vector2 position, Vector2 direction, float speed)
        {
            if (!JNet.IsServer)
            {
                Debug.LogError("Cannot spawn projectile when not on server.");
                return(null);
            }

            int seed    = Random.Range(0, int.MaxValue);
            var spawned = SpawnLocal(position, direction, speed, seed);

            var msg = JNet.CreateCustomMessage(true, CustomMsg.PROJECTILE_SPAWN, 32);

            msg.Write(position);
            msg.Write(direction);
            msg.Write(speed);
            msg.Write(spawned.Seed);
            JNet.SendCustomMessageToAll(JNet.GetServer().LocalClientConnection, msg, Lidgren.Network.NetDeliveryMethod.ReliableUnordered, 0);

            return(spawned);
        }
Пример #12
0
        /// <summary>
        /// Sets the mounted weapon to the mounted weapon with the specified prefab name. Only works when called from the server.
        /// If the weapon is null, the current weapon is removed instantly.
        /// </summary>
        /// <param name="spot">The spot. Should be on this vehicle, and not null!</param>
        /// <param name="mountedWeaponName">The name of the prefab.</param>
        public void SetWeapon(MountedWeaponSpot spot, string mountedWeaponName)
        {
            if (string.IsNullOrWhiteSpace(mountedWeaponName))
            {
                Debug.LogError("Null or whitespace name. Cannot set mounted weapon!");
                return;
            }

            var spawnable = Spawnables.Get <MountedWeapon>(mountedWeaponName);

            if (spawnable != null)
            {
                var spawned = Instantiate(spawnable);
                SetWeapon(spot, spawned);
                JNet.Spawn(spawned.gameObject);
            }
            else
            {
                Debug.LogError($"Failed to spawn mounted weapon from name {mountedWeaponName}.");
            }
        }
Пример #13
0
        public void StartRecording(string filePath, bool deleteExisting = false)
        {
            if (IsRecording)
                return;
            if (IsInPlayback)
                return;

            if (File.Exists(filePath))
            {
                if (deleteExisting)
                {
                    File.Delete(filePath);
                }
                else
                {
                    JNet.Error($"File {filePath} already exists.");
                    return;
                }                
            }

            try
            {
                Writer = new BinaryWriter(new FileStream(filePath, FileMode.CreateNew, FileAccess.Write, FileShare.None));
            }
            catch(Exception e)
            {
                JNet.Error(e.Message);
                if(Writer != null)
                {
                    Writer.Dispose();
                }
                return;
            }

            Time = 0f;
            IsRecording = true;
        }
Пример #14
0
 private void Update()
 {
     JNet.Update();
 }