Exemple #1
0
        public void Use(OpenedPacket packet)
        {
            GameScreen screen = (GameScreen)game.GetScreen();

            switch (packet.PacketType)
            {
            case ServerPackets.WORLDNAME:
                Console.WriteLine(String.Format("Successfully connected to {0} as {1}.", packet.PacketMessage[0], userName));
                break;

            default:
                break;
            }
        }
Exemple #2
0
        public static OpenedPacket Sort(NetIncomingMessage message, Game1 game)
        {
            int           msg = message.ReadByte();
            NetConnection senderConnection = message.SenderConnection;
            GameScreen    screen           = (GameScreen)game.GetScreen();
            int           id;
            int           x;
            int           y;
            string        pClass;
            int           pRole;
            OpenedPacket  packet = new OpenedPacket();

            switch (msg)
            {
            case ServerPackets.WORLDNAME:
                packet.PacketType    = ServerPackets.WORLDNAME;
                packet.PacketMessage = new String[] { message.ReadString() };
                return(packet);

            case ServerPackets.PLAYERINFORMATION:
                string name = message.ReadString();
                x      = message.ReadInt32();
                y      = message.ReadInt32();
                id     = message.ReadInt32();
                pClass = message.ReadString();
                pRole  = message.ReadInt32();
                PlayerPacket pPacket = new PlayerPacket(ServerPackets.PLAYERINFORMATION, x, y, name, id, pClass, pRole);
                return(pPacket);

            case ServerPackets.POSITION:
                id = message.ReadInt32();
                x  = message.ReadInt32();
                y  = message.ReadInt32();
                PositionPacket posPacket = new PositionPacket(ServerPackets.POSITION, id, x, y);
                return(posPacket);

            default:
                break;
            }
            return(null);
        }
Exemple #3
0
        public int Check()
        {
            NetIncomingMessage message;
            OpenedPacket       packet = new OpenedPacket();

            if (client.ConnectionStatus == NetConnectionStatus.Connected && connecting == true)
            {
                connecting = false;
            }
            if (connecting == false)
            {
                if (client.ConnectionStatus == NetConnectionStatus.Disconnected)
                {
                    game.SetScreen(new MenuScreen(game));
                }
            }
            while ((message = client.ReadMessage()) != null)
            {
                switch (message.MessageType)
                {
                case NetIncomingMessageType.Data:
                    int type = message.ReadByte();
                    if (type == ServerPackets.PLAYERINFORMATION)
                    {
                        bool   mine        = false;
                        string name        = message.ReadString();
                        int    x           = message.ReadInt32();
                        int    y           = message.ReadInt32();
                        int    id          = message.ReadInt32();
                        string playerClass = message.ReadString();
                        int    playerRole  = message.ReadInt32();

                        PlayerPacket pPacket = new PlayerPacket(ServerPackets.PLAYERINFORMATION, x, y, name, id, playerClass, playerRole);
                        if (pPacket.name == userName)
                        {
                            mine = true;
                        }
                        else
                        {
                            Console.WriteLine(String.Format("{0} joined the world!", name));
                        }

                        if (playerClass == CharacterSelections.NONE)
                        {
                            if (mine)
                            {
                                return(CheckConstants.CHARACTERSELECT);
                            }
                        }
                        else
                        {
                            gameScreen.AddPlayer(new Player(pPacket.id, pPacket.name, pPacket.x, pPacket.y, mine, this, pPacket.playerClass, pPacket.playerRole, gameScreen));
                        }
                    }
                    else if (type == ServerPackets.POSITION)
                    {
                        int            id      = message.ReadInt32();
                        int            x       = message.ReadInt32();
                        int            y       = message.ReadInt32();
                        PositionPacket pPacket = new PositionPacket(ServerPackets.POSITION, id, x, y);

                        Player posPlayer = gameScreen.GetPlayerByID(pPacket.playerID);
                        if (posPlayer != null)
                        {
                            posPlayer.SetPosition(new Vector2(pPacket.x, pPacket.y));
                        }
                    }
                    else if (type == ServerPackets.LEAVINGPLAYER)
                    {
                        int id = message.ReadInt32();
                        gameScreen.RemovePlayerByID(id);
                    }
                    else if (type == ServerPackets.CHATPACKET)
                    {
                        int    id     = message.ReadInt32();
                        string text   = message.ReadString();
                        Player player = gameScreen.GetPlayerByID(id);
                        gameScreen.AddChat(new ChatHolder(player, text));
                    }
                    else if (type == ServerPackets.ENEMYSPAWN)
                    {
                        int   enemyType       = message.ReadInt32();
                        int   enemyX          = message.ReadInt32();
                        int   enemyY          = message.ReadInt32();
                        bool  enemyAggressive = message.ReadBoolean();
                        int   netID           = message.ReadInt32();
                        Enemy enemy           = new Enemy(new Vector2(enemyX, enemyY), enemyType, enemyAggressive, netID, gameScreen);
                        gameScreen.AddEntity(enemy);
                        gameScreen.AddEnemy(enemy);
                    }
                    else if (type == ServerPackets.ENEMYMOVE)
                    {
                        int   x  = message.ReadInt32();
                        int   y  = message.ReadInt32();
                        int   id = message.ReadInt32();
                        Enemy e  = gameScreen.GetEnemyByID(id);
                        if (e != null)
                        {
                            e.Move(x, y);
                        }
                    }
                    else if (type == ServerPackets.ENEMYDESTINATION)
                    {
                        int   destX = message.ReadInt32();
                        int   destY = message.ReadInt32();
                        int   id    = message.ReadInt32();
                        Enemy e     = gameScreen.GetEnemyByID(id);
                        if (e != null)
                        {
                            e.SetDestination(destX, destY);
                        }
                    }
                    else if (type == ServerPackets.ENEMYSTOP)
                    {
                        int   id = message.ReadInt32();
                        Enemy e  = gameScreen.GetEnemyByID(id);
                        if (e != null)
                        {
                            e.ClearDestination();
                        }
                    }

                    break;

                case NetIncomingMessageType.StatusChanged:
                    //SortConnections(message);
                    break;

                case NetIncomingMessageType.DebugMessage:
                    try
                    {
                        if (ServerInfo.DEBUG)
                        {
                            Console.WriteLine(message.ReadString());
                        }
                    } catch (Exception e) { Console.Write(e); }
                    break;

                case NetIncomingMessageType.ErrorMessage:
                    try
                    {
                        if (ServerInfo.DEBUG)
                        {
                            Console.WriteLine(message.ReadString());
                        }
                    } catch (Exception e) { Console.Write(e); }
                    break;

                default:
                    break;
                }
            }
            if (packet.PacketType >= 0)
            {
                Use(packet);
            }
            return(CheckConstants.FULFILLED);
        }