コード例 #1
0
        private void OnInstantiate(Socket socket, byte[] data)
        {
            C2S_Instantiate clientCmd = NetworkSerializer.Deserialize <C2S_Instantiate>(data);

            //新增对象数据
            instances[clientCmd.TypeId].Add(instanceId, new Entity(clients[socket], clientCmd.X, clientCmd.Y));

            S2C_Instantiate cmd = new S2C_Instantiate();

            cmd.Frame           = clientCmd.Frame;
            cmd.Instance.TypeId = clientCmd.TypeId;
            cmd.Instance.Id     = instanceId;
            cmd.Instance.X      = clientCmd.X;
            cmd.Instance.Y      = clientCmd.Y;
            //自增
            instanceId++;
            //广播所有人
            foreach (var client in clients.Keys)
            {
                if (client == socket) //调用者拥有IsLocal属性
                {
                    cmd.Instance.IsLocal = true;
                }
                else
                {
                    cmd.Instance.IsLocal = false;
                }
                Send(client, (ushort)NetworkRole.Server, (ushort)GameCmd.Instantiate, cmd);
            }
        }
コード例 #2
0
        private void DestroyCallback(byte[] data)
        {
            S2C_Destroy cmd = NetworkSerializer.Deserialize <S2C_Destroy>(data);
            //获取场景实例
            GameObject instance = instances[cmd.TypeId][cmd.Id];

            //从列表中删除
            instances[cmd.TypeId].Remove(cmd.Id);
            //从场景中删除
            global::Destroy.Object.Destroy(instance);
            //从他人物体中移除
            otherInstances.Remove(cmd.Id);
        }
コード例 #3
0
        private void JoinCallback(byte[] data)
        {
            S2C_Join join = NetworkSerializer.Deserialize <S2C_Join>(data);

            this.join = true;
            frame     = join.Frame;
            id        = join.YourId;
            if (join.Instances == null) //表示当前没有游戏物体
            {
                return;
            }
            //生成所有游戏物体
            foreach (var instance in join.Instances)
            {
                CreatInstance(instance);
            }
        }
コード例 #4
0
        private void OnMove(Socket socket, byte[] data)
        {
            C2S_Move move = NetworkSerializer.Deserialize <C2S_Move>(data);

            if (move.Entities == null) //表示该玩家没有创建移动的游戏物体
            {
                return;
            }
            //玩家的实体
            foreach (var entity in move.Entities)
            {
                //获得场景中所有实例
                foreach (var dict in instances.Values)
                {
                    dict[entity.Id] = entity; //赋值
                }
            }
        }
コード例 #5
0
        private void MoveCallback(byte[] data)
        {
            S2C_Move move = NetworkSerializer.Deserialize <S2C_Move>(data);

            frame = move.Frame;
            if (move.Entities == null) //表示当前没有游戏物体
            {
                return;
            }
            //实现其他物体同步移动
            foreach (var entity in move.Entities)
            {
                //只同步他人
                Vector2Int pos = new Vector2Int(entity.X, entity.Y);
                if (otherInstances.ContainsKey(entity.Id))
                {
                    otherInstances[entity.Id].transform.Position = pos;
                }
            }
        }
コード例 #6
0
        private void OnDestroy(Socket socket, byte[] data)
        {
            C2S_Destroy clientCmd = NetworkSerializer.Deserialize <C2S_Destroy>(data);

            //移除对象数据
            instances[clientCmd.TypeId].Remove(clientCmd.Id);

            S2C_Destroy cmd = new S2C_Destroy();

            cmd.Frame  = clientCmd.Frame;
            cmd.TypeId = clientCmd.TypeId;
            cmd.Id     = clientCmd.Id;
            //广播除了发送者之外的人
            foreach (var client in clients.Keys)
            {
                if (client == socket)
                {
                    continue;
                }
                Send(client, (ushort)NetworkRole.Server, (ushort)GameCmd.Destroy, cmd);
            }
        }
コード例 #7
0
        private void OnInstantiated(byte[] data)
        {
            S2C_Instantiate cmd = NetworkSerializer.Deserialize <S2C_Instantiate>(data);

            CreatInstance(cmd.Instance);
        }