示例#1
0
        private void RecieveServerCommand()
        {
            byte[] bytes      = ReadData(sizeof(int), _networkStream);
            int    packetSize = BitConverter.ToInt32(bytes, 0);

            bytes = ReadData(packetSize, _networkStream);
            ServerCommand command = ServerCommand.FromBytes(bytes);

            int             eventID;
            int             enemyID;
            int             enemyIndex;
            int             mapID;
            int             mapX;
            int             mapY;
            float           realX;
            float           realY;
            FacingDirection direction;
            MapData         map;
            int             projectileID;
            int             playerID;
            string          playerName;
            int             itemIndex;
            int             itemID;
            int             count;

            switch (command.GetCommandType())
            {
            case ServerCommand.CommandType.Disconnect:
                this.Disconnect();
                Console.WriteLine("Server Disconnected");
                break;

            case ServerCommand.CommandType.ShowMessage:

                if (_messageBox == null)
                {
                    _messageBox = new MessageBox((string)command.GetParameter("Message"), _gameState, false);
                    _gameState.AddControl(_messageBox);
                }

                break;

            case ServerCommand.CommandType.ShowOptions:

                if (_messageBox == null)
                {
                    string message = (string)command.GetParameter("Message");
                    _messageBox = new MessageBox(message, _gameState, false);
                    string[] options = ((string)command.GetParameter("Options")).Split(',');
                    for (int i = 0; i < options.Length; i++)
                    {
                        _messageBox.AddOption(options[i]);
                    }
                    _messageBox.SetSelectedOption(0);
                    _gameState.AddControl(_messageBox);
                }

                break;

            case ServerCommand.CommandType.AddMapEnemy:

                enemyID = (int)command.GetParameter("EnemyID");
                mapID   = (int)command.GetParameter("MapID");
                if (_gameState.MapEntity.FindComponent <MapComponent>().GetMapInstance().GetMapPacket().MapID == mapID)
                {
                    mapX = (int)command.GetParameter("MapX");
                    mapY = (int)command.GetParameter("MapY");
                    bool     onBridge = (bool)command.GetParameter("OnBridge");
                    MapEnemy mapEnemy = new MapEnemy(enemyID, mapX, mapY, onBridge);
                    MapComponent.Instance.AddMapEnemy(mapEnemy);
                }

                break;

            case ServerCommand.CommandType.UpdateMapEnemy:

                enemyIndex = (int)command.GetParameter("EnemyIndex");
                mapID      = (int)command.GetParameter("MapID");
                if ((_gameState.MapEntity.FindComponent <MapComponent>()).GetMapInstance().GetMapPacket().MapID == mapID)
                {
                    int HP = (int)command.GetParameter("HP");
                    mapX      = (int)command.GetParameter("MapX");
                    mapY      = (int)command.GetParameter("MapY");
                    realX     = (float)command.GetParameter("RealX");
                    realY     = (float)command.GetParameter("RealY");
                    direction = (FacingDirection)command.GetParameter("Direction");
                    bool onBridge = (bool)command.GetParameter("OnBridge");
                    bool dead     = (bool)command.GetParameter("Dead");
                    MapEnemyComponent enemyComponent = MapComponent.Instance.EnemyEntites[enemyIndex].FindComponent <MapEnemyComponent>();
                    enemyComponent.UpdateMapEnemy(HP, mapX, mapY, realX, realY, direction, onBridge, dead);

                    if (dead)
                    {
                        MapComponent.Instance.EnemyEntites[enemyIndex].Destroy();
                        MapComponent.Instance.EnemyEntites.RemoveAt(enemyIndex);
                        MapComponent.Instance.GetMapInstance().RemoveMapEnemy(enemyIndex);
                    }
                }

                break;

            case ServerCommand.CommandType.UpdateMapEvent:

                eventID = (int)command.GetParameter("EventID");
                mapID   = (int)command.GetParameter("MapID");
                if (_gameState.MapEntity.FindComponent <MapComponent>().GetMapInstance().GetMapPacket().MapID == mapID)
                {
                    mapX      = (int)command.GetParameter("MapX");
                    mapY      = (int)command.GetParameter("MapY");
                    realX     = (float)command.GetParameter("RealX");
                    realY     = (float)command.GetParameter("RealY");
                    direction = (FacingDirection)command.GetParameter("Direction");
                    bool onBridge = (bool)command.GetParameter("OnBridge");

                    map = _gameState.MapEntity.FindComponent <MapComponent>().GetMapInstance().GetMapData();
                    if (map != null)
                    {
                        map.GetMapEvent(eventID).MapX           = mapX;
                        map.GetMapEvent(eventID).MapY           = mapY;
                        map.GetMapEvent(eventID).RealX          = realX;
                        map.GetMapEvent(eventID).RealY          = realY;
                        map.GetMapEvent(eventID).EventDirection = direction;
                        map.GetMapEvent(eventID).OnBridge       = onBridge;
                    }
                }

                break;

            case ServerCommand.CommandType.ChangeMapEventDirection:

                eventID = (int)command.GetParameter("EventID");
                mapID   = (int)command.GetParameter("MapID");
                if (_gameState.MapEntity.FindComponent <MapComponent>().GetMapInstance().GetMapPacket().MapID == mapID)
                {
                    direction = (FacingDirection)command.GetParameter("Direction");

                    map = _gameState.MapEntity.FindComponent <MapComponent>().GetMapInstance().GetMapData();
                    if (map != null)
                    {
                        map.GetMapEvent(eventID).EventDirection = direction;
                    }
                }

                break;

            case ServerCommand.CommandType.ChangeMapEventSprite:

                mapID = (int)command.GetParameter("MapID");
                if (_gameState.MapEntity.FindComponent <MapComponent>().GetMapInstance().GetMapPacket().MapID == mapID)
                {
                    eventID = (int)command.GetParameter("EventID");
                    int spriteID = (int)command.GetParameter("SpriteID");
                    _gameState.MapEntity.FindComponent <MapComponent>().ChangeMapEventSprite(eventID, spriteID);
                }

                break;

            case ServerCommand.CommandType.ChangeMapEventRenderPriority:

                mapID = (int)command.GetParameter("MapID");
                if (_gameState.MapEntity.FindComponent <MapComponent>().GetMapInstance().GetMapPacket().MapID == mapID)
                {
                    eventID = (int)command.GetParameter("EventID");
                    RenderPriority priority = (RenderPriority)command.GetParameter("RenderPriority");
                    _gameState.MapEntity.FindComponent <MapComponent>().ChangeMapEventRenderPriority(eventID, priority);
                }

                break;

            case ServerCommand.CommandType.ChangeMapEventEnabled:

                mapID = (int)command.GetParameter("MapID");
                if (_gameState.MapEntity.FindComponent <MapComponent>().GetMapInstance().GetMapPacket().MapID == mapID)
                {
                    eventID = (int)command.GetParameter("EventID");
                    bool enabled = (bool)command.GetParameter("Enabled");
                    _gameState.MapEntity.FindComponent <MapComponent>().ChangeMapEventEnabled(eventID, enabled);
                }

                break;

            case ServerCommand.CommandType.AddProjectile:

                mapID = (int)command.GetParameter("MapID");
                if (_gameState.MapEntity.FindComponent <MapComponent>().GetMapInstance().GetMapPacket().MapID == mapID)
                {
                    {
                        int dataID = (int)command.GetParameter("DataID");
                        realX     = (float)command.GetParameter("RealX");
                        realY     = (float)command.GetParameter("RealY");
                        direction = (FacingDirection)command.GetParameter("Direction");
                        bool onBridge = (bool)command.GetParameter("OnBridge");

                        MapProjectile projectile = new MapProjectile(dataID, CharacterType.Player, -1, new Vector2(realX, realY), direction);
                        projectile.OnBridge = onBridge;
                        MapComponent.Instance.AddMapProjectile(projectile);
                    }
                }

                break;

            case ServerCommand.CommandType.UpdateProjectile:

                mapID = (int)command.GetParameter("MapID");
                if (_gameState.MapEntity.FindComponent <MapComponent>().GetMapInstance().GetMapPacket().MapID == mapID)
                {
                    projectileID = (int)command.GetParameter("ProjectileID");
                    if (projectileID < MapComponent.Instance.ProjectileEntites.Count)
                    {
                        realX = (float)command.GetParameter("RealX");
                        realY = (float)command.GetParameter("RealY");
                        bool onBridge  = (bool)command.GetParameter("OnBridge");
                        bool destroyed = (bool)command.GetParameter("Destroyed");
                        ProjectileComponent component = MapComponent.Instance.ProjectileEntites[projectileID].FindComponent <ProjectileComponent>();
                        component.SetRealPosition(realX, realY);
                        component.SetOnBridge(onBridge);
                        if (destroyed)
                        {
                            MapComponent.Instance.ProjectileEntites[projectileID].Destroy();
                            MapComponent.Instance.ProjectileEntites.RemoveAt(projectileID);
                            MapComponent.Instance.GetMapInstance().RemoveMapProjectile(projectileID);
                        }
                    }
                }

                break;

            case ServerCommand.CommandType.AddMapItem:

                mapID = (int)command.GetParameter("MapID");
                MapComponent mapComponent = _gameState.MapEntity.FindComponent <MapComponent>();
                if (mapComponent.GetMapInstance().GetMapPacket().MapID == mapID)
                {
                    itemID   = (int)command.GetParameter("ItemID");
                    count    = (int)command.GetParameter("Count");
                    mapX     = (int)command.GetParameter("MapX");
                    mapY     = (int)command.GetParameter("MapY");
                    playerID = (int)command.GetParameter("PlayerID");
                    bool    onBridge = (bool)command.GetParameter("OnBridge");
                    MapItem mapItem  = new MapItem(itemID, count, mapX, mapY, playerID, onBridge);
                    MapComponent.Instance.AddMapItem(mapItem);
                }

                break;

            case ServerCommand.CommandType.RemoveMapItem:

                mapID = (int)command.GetParameter("MapID");
                if (_gameState.MapEntity.FindComponent <MapComponent>().GetMapInstance().GetMapPacket().MapID == mapID)
                {
                    itemIndex = (int)command.GetParameter("ItemIndex");
                    if (itemIndex < MapComponent.Instance.MapItemEntities.Count)
                    {
                        MapComponent.Instance.MapItemEntities[itemIndex].Destroy();
                        MapComponent.Instance.MapItemEntities.RemoveAt(itemIndex);
                        MapComponent.Instance.GetMapInstance().RemoveMapItem(itemIndex);
                    }
                }

                break;

            case ServerCommand.CommandType.UpdateMapItem:

                mapID = (int)command.GetParameter("MapID");
                if (_gameState.MapEntity.FindComponent <MapComponent>().GetMapInstance().GetMapPacket().MapID == mapID)
                {
                    itemIndex = (int)command.GetParameter("ItemIndex");
                    if (itemIndex < MapComponent.Instance.MapItemEntities.Count)
                    {
                        playerID = (int)command.GetParameter("PlayerID");
                        count    = (int)command.GetParameter("Count");
                        MapComponent.Instance.MapItemEntities[itemIndex].FindComponent <MapItemComponent>().GetMapItem().PlayerID = playerID;
                        MapComponent.Instance.MapItemEntities[itemIndex].FindComponent <MapItemComponent>().GetMapItem().Count    = count;
                    }
                }

                break;

            case ServerCommand.CommandType.ShowShop:

                int      shopID = (int)command.GetParameter("ShopID");
                ShopData data   = ShopData.GetData(shopID);
                if (data != null)
                {
                    if (InventoryPanel.Instance == null)
                    {
                        GameState.Instance.ToggleInventory();
                    }
                    _gameState.AddControl(new GUI.ShopPanel(_gameState, data));
                }
                else
                {
                    AddClientCommand(new ClientCommand(ClientCommand.CommandType.CloseShop));
                }

                break;

            case ServerCommand.CommandType.TradeRequest:
                playerID   = (int)command.GetParameter("PlayerID");
                playerName = (string)command.GetParameter("PlayerName");
                MessagePanel.Instance.AddMessage(playerName + " would like to trade.");

                break;

            case ServerCommand.CommandType.StartTrade:
                playerID   = (int)command.GetParameter("PlayerID");
                playerName = (string)command.GetParameter("PlayerName");

                if (InventoryPanel.Instance == null)
                {
                    GameState.Instance.ToggleInventory();
                }

                _gameState.AddControl(new GUI.TradePanel(_gameState, playerID, playerName));
                break;

            case ServerCommand.CommandType.AcceptTrade:
                if (TradePanel.Instance != null)
                {
                    TradePanel.Instance.TradeRequest.TradeOffer2.Accepted = true;
                }
                break;

            case ServerCommand.CommandType.EndTrade:
                if (TradePanel.Instance != null)
                {
                    TradePanel.Instance.Close();
                }
                break;

            case ServerCommand.CommandType.AddTradeItem:
                itemID = (int)command.GetParameter("ItemID");
                count  = (int)command.GetParameter("Count");

                if (TradePanel.Instance != null)
                {
                    TradePanel.Instance.TradeRequest.TradeOffer2.AddItem(itemID, count);
                    TradePanel.Instance.TradeRequest.TradeOffer1.Accepted = false;
                    TradePanel.Instance.TradeRequest.TradeOffer2.Accepted = false;
                }

                break;

            case ServerCommand.CommandType.RemoveTradeItem:
                itemIndex = (int)command.GetParameter("ItemIndex");
                count     = (int)command.GetParameter("Count");

                if (TradePanel.Instance != null)
                {
                    TradePanel.Instance.TradeRequest.TradeOffer2.RemoveItem(itemIndex, count);
                    TradePanel.Instance.TradeRequest.TradeOffer1.Accepted = false;
                    TradePanel.Instance.TradeRequest.TradeOffer2.Accepted = false;
                }

                break;

            case ServerCommand.CommandType.CantTrade:
                if (TradePanel.Instance != null)
                {
                    TradePanel.Instance.TradeRequest.TradeOffer1.Accepted = false;
                    TradePanel.Instance.TradeRequest.TradeOffer2.Accepted = false;
                }
                break;

            case ServerCommand.CommandType.OpenBank:
                if (InventoryPanel.Instance == null)
                {
                    GameState.Instance.ToggleInventory();
                }

                _gameState.AddControl(new BankPanel(_gameState));

                break;

            case ServerCommand.CommandType.ShowWorkbench:
                int workbenchID = (int)command.GetParameter("WorkbenchID");

                if (InventoryPanel.Instance == null)
                {
                    GameState.Instance.ToggleInventory();
                }

                _gameState.AddControl(new GUI.WorkbenchPanel(_gameState, workbenchID));

                break;
            }
        }