示例#1
0
        /// <summary>
        /// Creates the activeplayer object and hooks up the input so that the
        /// player moves
        /// </summary>
        /// <param name="createPacket">The createPacket that holds info
        /// on intitial pos, etc</param>
        /// <returns>the created player</returns>
        private GameObject InitializeUserPlayerAndMovement(
            CreateObjectPacket createPacket
            )
        {
            // Create a new player with the specified packet info.
            ActivePlayer = new PlayerClient(createPacket);

            // Set the active plyer in the graphics manager.
            GraphicsManager.ActivePlayer = ActivePlayer;

            // Add the player to networked game objects.
            NetworkedGameObjects.Add(ActivePlayer.Id, ActivePlayer);

            // Set up the input manager.
            SetupInputManager(ActivePlayer);

            CreateMap();

            return(ActivePlayer);
        }
示例#2
0
        /// <summary>
        /// Creates a new object from a given packet, whether that be a leaf
        /// or a player.
        /// </summary>
        /// <param name="createPacket"></param>
        /// <returns>The object which was created</returns>
        public GameObject CreateObjectFromPacket(CreateObjectPacket createPacket)
        {
            int objId = createPacket.ObjData.IdData.ObjectId;

            // Create a new packet depending on it's type.
            switch (createPacket.ObjectType)
            {
            // Create an active player
            case (ObjectType.ACTIVE_PLAYER):
                var newPlayer = InitializeUserPlayerAndMovement(createPacket) as PlayerClient;
                playerClients.Add(newPlayer);
                newPlayer.Name = GraphicsRenderer.nicknameTextbox.Text;
                return(newPlayer);

            // Create an other player
            case (ObjectType.PLAYER):
                NetworkedGameObjectClient player = new PlayerClient(createPacket);
                playerClients.Add((PlayerClient)player);
                NetworkedGameObjects.Add(objId, player);
                return(player);

            // Create a leaf.
            case (ObjectType.LEAF):
                NetworkedGameObjectClient leaf = new LeafClient(createPacket);
                NetworkedGameObjects.Add(objId, leaf);
                return(leaf);

            case (ObjectType.TREE):
                Transform startTransform = new Transform();
                float     initX          = createPacket.ObjData.PositionX;
                float     initY          = createPacket.ObjData.PositionY;
                float     initZ          = createPacket.ObjData.PositionZ;
                startTransform.Position = new Vector3(initX, initY, initZ);
                NetworkedGameObjectClient tree = new TreeClient(createPacket);
                NetworkedGameObjects.Add(objId, tree);
                return(tree);
            }
            return(null);
        }
示例#3
0
        /// <summary>
        /// Defines the different actions which the client will take upon recieving a packet, and defines
        /// them as a dictionary that hashes by packet type
        /// </summary>
        /// <param name="client">The game client to fire callbacks for</param>
        public ClientPacketHandler(GameClient client)
        {
            this.client = client;

            // What to do during an update
            void UpdateObjectAction(ObjectPacket p)
            {
                NetworkedGameObjectClient packetObject = client.GetObjectFromPacket(p);

                packetObject.UpdateFromPacket(p);
            }

            // what to do during an update to the playerpacket
            void UpdatePlayerAction(PlayerPacket p)
            {
                PlayerClient player = (PlayerClient)client.GetObjectFromPacket(p);

                if (client.GetActivePlayer() == player && p.Dead)
                {
                    client.DoPlayerDeath();
                }
                player.UpdateFromPacket(p);
            }

            // What to do during a destroy
            void DestroyAction(DestroyObjectPacket p)
            {
                NetworkedGameObjectClient packetObject = client.GetObjectFromPacket(p);

                packetObject.Die();
            }

            // What to do when creating an object
            GameObject CreateObjectAction(CreateObjectPacket p)
            {
                return(client.CreateObjectFromPacket(p));
            }

            // What to do when creating a player
            void CreatePlayerAction(CreatePlayerPacket p)
            {
                PlayerClient player = (PlayerClient)CreateObjectAction(p.createPacket);

                player.PlayerTeam = p.team;
            }

            // What to do on game finish
            void GameResultAction(GameResultPacket p)
            {
                client.ResetGameTimer();
                if (client.GetPlayerTeam() == p.winningTeam)
                {
                    GlobalUIManager.GameWinLossState.SetState(UI.UIGameWLState.WinLoseState.Win);
                }
                else
                {
                    GlobalUIManager.GameWinLossState.SetState(UI.UIGameWLState.WinLoseState.Lose);
                }

                // Save the player stats to file when the game ends.
                GameClient.instance.SaveStats(GraphicsManager.ActivePlayer.stats);

                client.WinningTeam         = p.winningTeam;
                client.PendingRematchState = true;
            }

            void GameStartAction(MatchStartPacket p)
            {
                client.StartMatchTimer(p.gameTime);
                GlobalUIManager.GameWinLossState.SetState(UI.UIGameWLState.WinLoseState.None);
                GlobalUIManager.GameWinLossState.SetStats(null);
                client.PendingRematchState = false;
                GlobalUIManager.Countdown.Start();
            }

            void SpectatorAction(SpectatorPacket p)
            {
                GraphicsManager.ActiveCamera.CameraPosition = new SlimDX.Vector3(0, 150, -1);
                GraphicsManager.ActiveCamera.CameraLookAt   = new SlimDX.Vector3(0, 0, 0);
                client.CreateMap();
            }

            void StatReceiveAction(StatResultPacket p)
            {
                if (p.PlayerID == GraphicsManager.ActivePlayer.Id)
                {
                    GraphicsManager.ActivePlayer.stats = p.stats;
                }
            }

            packetHandlers = new Dictionary <PacketType, Action <BasePacket> >()
            {
                { PacketType.CreatePlayerPacket, (p) => CreatePlayerAction((CreatePlayerPacket)p) },
                { PacketType.CreateObjectPacket, (p) => CreateObjectAction((CreateObjectPacket)p) },
                { PacketType.ObjectPacket, (p) => UpdateObjectAction((ObjectPacket)p) },
                { PacketType.PlayerPacket, (p) => UpdatePlayerAction((PlayerPacket)p) },
                { PacketType.DestroyObjectPacket, (p) => DestroyAction((DestroyObjectPacket)p) },
                { PacketType.GameResultPacket, (p) => GameResultAction((GameResultPacket)p) },
                { PacketType.MatchStartPacket, (p) => GameStartAction((MatchStartPacket)p) },
                { PacketType.SpectatorPacket, (p) => SpectatorAction((SpectatorPacket)p) },
                { PacketType.StatResultPacket, (p) => StatReceiveAction((StatResultPacket)p) },
            };
        }