public MultiplayerClientGameplay(string server, int port) :
            base("multiplayerClient")
        {
            useEnqueueTileUpdates = false;

            stats = MultiplayerStats.Singleton;
            stats.Reset();
            stats.connected = true;

            client = new MultiplayerClient(server, port, this);
        }
Пример #2
0
        public void Update()
        {
            try
            {
                if (serverSocket.Pending())
                {
                    MultiplayerClient client = new MultiplayerClient(serverSocket.AcceptTcpClient(), this, clientListener);
                    clients.Add(client);
                }
            }
            catch (Exception ex)
            {
                CWConsole.LogError(ex.ToString());
            }

            List<MultiplayerClient> clientsToRemove = new List<MultiplayerClient>();

            foreach (MultiplayerClient client in clients)
            {
                try
                {
                    if (client.Update(999) == false)
                        clientsToRemove.Add(client);
                }
                catch (Exception ex)
                {
                    clientsToRemove.Add(client);
                    CWConsole.LogError(ex.ToString());
                }
            }

            foreach (MultiplayerClient client in clientsToRemove)
            {
                try
                {
                    client.Clear();
                }
                catch (Exception ex)
                {
                    CWConsole.LogError(ex.ToString());
                }
                clients.Remove(client);

                if (client.sendingPolicy == false)
                    serverListener.ClientDisconnected(client);
            }
        }
        public void ClientActionReceived(MultiplayerClient client, MultiplayerAction action)
        {
            switch (action.action)
            {
                case MultiplayerAction.Action.AVATAR_MOVE:
                {
                    int objectId = Int32.Parse(action.GetParameter(0));
                    Vector3 pos = new Vector3(
                        Single.Parse(action.GetParameter(1)),
                        Single.Parse(action.GetParameter(2)),
                        Single.Parse(action.GetParameter(3)));
                    Vector3 rot = new Vector3(
                        Single.Parse(action.GetParameter(4)),
                        Single.Parse(action.GetParameter(5)),
                        Single.Parse(action.GetParameter(6)));

                    if (objectId == client.id)
                    {
                        Avatar avatar = world.avatarManager.GetAvatarByObjectId(objectId);

                        avatar.position = pos;
                        avatar.rotation = rot;
                        
                        server.SendToEveryone(action, client.id);
                    }
                    break;
                }

                case MultiplayerAction.Action.TILE_CREATE:
                {
                    byte tileType = byte.Parse(action.GetParameter(0));
                    TilePosition pos = new TilePosition(
                        Int32.Parse(action.GetParameter(1)),
                        Int32.Parse(action.GetParameter(2)),
                        Int32.Parse(action.GetParameter(3)));

                    CreateTile(pos, tileType);
                    break;
                }

                case MultiplayerAction.Action.TILE_CLICKED:
                {
                    TilePosition pos = new TilePosition(
                        Int32.Parse(action.GetParameter(0)),
                        Int32.Parse(action.GetParameter(1)),
                        Int32.Parse(action.GetParameter(2)));
                    this.TileClicked(pos);
                    break;
                }

                case MultiplayerAction.Action.TILE_HIT:
                {
                    string itemId = action.GetParameter(0);

                    TilePosition pos = new TilePosition(
                        Int32.Parse(action.GetParameter(1)),
                        Int32.Parse(action.GetParameter(2)),
                        Int32.Parse(action.GetParameter(3)));

                    this.TileHit(pos, world.itemManager.GetItemDefinitionById(itemId));
                    break;
                }
            }
        }
        public void ClientDisconnected(MultiplayerClient client)
        {
            stats.multiplayerConnectedClients--;

            DestroyAvatar(world.avatarManager.GetAvatarByObjectId(client.id));
        }
 private byte[] GetInitialData(MultiplayerClient client, Player player)
 {
     return world.SaveMultiplayer(player);
 }
        public void ClientConnected(MultiplayerClient client)
        {
            Player player = (Player) CreateAvatar("player_remote", Graphics.TilePositionToVector3(GetPlayerResetPosition()));
            player.resetPosition = GetPlayerResetPosition();
            FillPlayerInventory(player.inventory);

            client.id = player.objectId;

            MultiplayerAction initialDataAction = new MultiplayerAction(MultiplayerAction.Action.INITIAL_DATA, GetInitialData(client, player));
            client.AddAction(initialDataAction);

            //Send others avatars
            foreach (Avatar avatar in world.avatarManager.Avatars)
            {
                if (avatar.objectId != player.objectId)
                {
                    client.AddAction(new MultiplayerAction(
                        MultiplayerAction.Action.AVATAR_CREATE, 
                        new String[] {
                            avatar.objectId.ToString(),
                            "player_remote",
                            avatar.position.x.ToString(),
                            avatar.position.y.ToString(),
                            avatar.position.z.ToString(),
                            avatar.rotation.x.ToString(),
                            avatar.rotation.y.ToString(),
                            avatar.rotation.z.ToString()
                        }));
                }
            }

            stats.multiplayerConnectedClients++;
        }
Пример #7
0
 public void ClientAccepted(MultiplayerClient client, bool isPolicy)
 {
     if (isPolicy == false)
         serverListener.ClientConnected(client);
 }
        public void ClientActionReceived(MultiplayerClient client, MultiplayerAction action)
        {
            switch (action.action)
            {
                case MultiplayerAction.Action.INITIAL_DATA:
                    initializationDataReceived = true;
                    initializationData = action.extraData;
                    break;

                case MultiplayerAction.Action.AVATAR_MOVE:
                {
                    int objectId = Int32.Parse(action.GetParameter(0));
                    Vector3 pos = new Vector3(
                        Single.Parse(action.GetParameter(1)),
                        Single.Parse(action.GetParameter(2)),
                        Single.Parse(action.GetParameter(3)));
                    Vector3 rot = new Vector3(
                        Single.Parse(action.GetParameter(4)),
                        Single.Parse(action.GetParameter(5)),
                        Single.Parse(action.GetParameter(6)));

                    Avatar avatar = world.avatarManager.GetAvatarByObjectId(objectId);
                    avatar.position = pos;
                    avatar.rotation = rot;
                    break;
                }

                case MultiplayerAction.Action.AVATAR_CREATE:
                {
                    int objectId = Int32.Parse(action.GetParameter(0));
                    string avatarDefinitionId = action.GetParameter(1);
                    Vector3 pos = new Vector3(
                        Single.Parse(action.GetParameter(2)),
                        Single.Parse(action.GetParameter(3)),
                        Single.Parse(action.GetParameter(4)));
                    Vector3 rot = new Vector3(
                        Single.Parse(action.GetParameter(5)),
                        Single.Parse(action.GetParameter(6)),
                        Single.Parse(action.GetParameter(7)));

                    Avatar avatar = world.avatarManager.CreateAvatar(world.avatarManager.GetAvatarDefinitionById(avatarDefinitionId), objectId, pos, true);
                    avatar.rotation = rot;
                    break;
                }

                case MultiplayerAction.Action.AVATAR_DESTROY:
                {
                    int objectId = Int32.Parse(action.GetParameter(0));
                    world.avatarManager.DestroyAvatar(world.avatarManager.GetAvatarByObjectId(objectId));
                    break;
                }

                case MultiplayerAction.Action.TILE_INVALIDATED:
                {
                    MemoryStream ms = new MemoryStream(action.extraData);
                    BinaryReader br = new BinaryReader(ms);

                    int n = br.ReadInt32();
                    Tile tile = new Tile();

                    for (int i = 0; i < n; i++)
                    {
                        TilePosition pos = SerializationUtils.ReadTilePosition(br);
                        tile.Deserialize(br.ReadUInt32());

                        if (tile.tileType != world.tileManager.GetTileType(pos))
                            world.tileManager.SetTileType(pos, tile.tileType);
                        if (tile.OnFire != world.tileManager.GetTileOnFire(pos))
                            world.tileManager.SetTileOnFire(pos, tile.OnFire);
                        if (tile.ExtraData != world.tileManager.GetTileExtraData(pos))
                            world.tileManager.SetTileExtraData(pos, tile.ExtraData);
                        if (tile.Energy != world.tileManager.GetTileEnergy(pos))
                            world.tileManager.SetTileEnergy(pos, tile.Energy);
                    }
                    break;
                }
            }
        }