Exemplo n.º 1
0
        public void BroadCastEntityUpdate(ObjectMessageType objectMessageType, Entity entity)
        {
            NetOutgoingMessage om = s_client.CreateMessage();

            om.Write((byte)ServerInfo.Broadcast);
            om.Write((byte)MessageType.ObjectUpdate);
            om.Write((byte)objectMessageType);
            om.Write(entity.objectID);
            switch (objectMessageType)
            {
            case ObjectMessageType.PlayerSpawn:
                Player player = (Player)entity;
                om.Write(player.onlinePlayer.clientID);
                XNAExtensions.Write(om, entity.Hitbox.Center);

                om.Write((byte)player.carType);
                om.Write((byte)player.carFrontType);
                om.Write((byte)player.abilityType);
                break;

            case ObjectMessageType.Spawn:
                om.Write((byte)entity.entityType);
                XNAExtensions.Write(om, entity.Hitbox.Center);
                break;

            case ObjectMessageType.UpdatePosition:
                XNAExtensions.Write(om, entity.Hitbox.Center);
                om.Write(entity.Hitbox.RotationRad);
                XNAExtensions.Write(om, entity.Velocity);
                break;

            case ObjectMessageType.Despawn:
                //doesnt need more information
                break;

            case ObjectMessageType.StateChange:
                break;

            case ObjectMessageType.UpdateHitpoints:
                om.Write(entity.hitpoints);
                break;
            }


            s_client.SendMessage(om, NetDeliveryMethod.ReliableOrdered);
            s_client.FlushSendQueue();
        }
Exemplo n.º 2
0
        public void ParseMessage(NetIncomingMessage im)
        {
            //Parse and Apply all Changes
            var del  = im.ReadByte();
            var type = im.ReadByte();

            CarGoServer.ServerData serverData = new CarGoServer.ServerData();
            switch ((MessageType)type)
            {
            case MessageType.GameState:
                GameState newState = (GameState)im.ReadByte();
                StateMachine.Instance.RemoteChangeState(newState);
                //Update GameState

                break;

            case MessageType.ObjectUpdate:
                var     objectMessageType = (ObjectMessageType)im.ReadByte();
                var     objectID          = im.ReadInt32();
                Vector2 center;
                int     clientID;
                switch (objectMessageType)
                {
                case ObjectMessageType.PlayerSpawn:
                    clientID = im.ReadInt32();
                    center   = XNAExtensions.ReadVector2(im);
                    CarType      carType      = (CarType)im.ReadByte();
                    CarFrontType carFrontType = (CarFrontType)im.ReadByte();
                    AbilityType  abilityType  = (AbilityType)im.ReadByte();
                    scene.RemoteAddPlayer(clientID, center, objectID, carType, carFrontType, abilityType, lobbyOnline.GetOnlinePlayer(clientID));
                    break;

                case ObjectMessageType.Spawn:
                    EntityType entityType = (EntityType)im.ReadByte();
                    center = XNAExtensions.ReadVector2(im);
                    scene.RemoteAddEntity(entityType, center, objectID);
                    break;

                case ObjectMessageType.Despawn:
                    scene.RemoteRemoveEntity(objectID);
                    break;

                case ObjectMessageType.UpdatePosition:
                    center = XNAExtensions.ReadVector2(im);
                    float   rotation = im.ReadFloat();
                    Vector2 velocity = XNAExtensions.ReadVector2(im);
                    scene.RemoteUpdatePosition(objectID, center, rotation, velocity);
                    break;

                case ObjectMessageType.StateChange:
                    break;

                case ObjectMessageType.UpdateHitpoints:
                    int newHitpoints = im.ReadInt32();
                    scene.RemoteUpdateHitpoints(objectID, newHitpoints);
                    break;
                }
                break;

            case MessageType.ReceiveClientNumber:
                clientID = im.ReadInt32();
                ID_Manager.Instance.ClientNumber = clientID;
                lobbyOnline.AddOnlinePlayer(Settings.Instance.PlayerName, clientID, PreferredInput.Instance.GetPreferredInput);
                break;

            case MessageType.IntroduceClient:
                clientID = im.ReadInt32();
                var clientName = im.ReadString();
                lobbyOnline.AddOnlinePlayer(clientName, clientID, InputController.Remote);
                //create new onlinePlayer for Lobby
                break;

            case MessageType.MenuInput:
                Menu currentmenu = game.GetCurrentMenu();
                clientID = im.ReadInt32();
                currentmenu.RemoteInput((InputType)im.ReadByte(), clientID);
                break;

            case MessageType.ReceiveServerInfo:
                im.ReadAllFields(serverData);
                //string serverName = im.ReadString();
                //string address = im.ReadString() + ":" + networkThread.port;//im.SenderEndPoint.ToString();
                lobbyOnline.serverData = serverData;
                break;

            case MessageType.PlayerReady:
                clientID = im.ReadInt32();
                lobbyOnline.SetPlayerReady(clientID);

                break;

            case MessageType.Chat:
                string message = im.ReadString();
                game.menuPause.AddChatMessage(message);
                break;

            case MessageType.ServerInList:
                long key = im.ReadInt64();
                serverData = new CarGoServer.ServerData();
                im.ReadAllFields(serverData);
                game.lobbySearch.AddServerData(serverData);
                break;

            case MessageType.ReceiveServerAddress:
                break;
            }
        }