Exemple #1
0
        /// <summary>
        /// Initialize the world with the data from the WorldSetup packet.
        /// </summary>
        /// <param name="packetReader">The packet reader with the world data.</param>
        public void Initialize()
        {
            // reset the game status
            gameWon = false;
            winnerIndex = -1;
            gameExited = false;

            // initialize the ships with the data from the packet
            for (int i = 0; i < MaximumPlayers; i++)
            {
                // read each of the positions
                Vector2 position = packetReader.ReadVector2();
                // use the position value if we know of that many players
                if (i < networkSession.AllGamers.Count)
                {
                    PlayerData playerData = networkSession.AllGamers[i].Tag 
                        as PlayerData;
                    if ((playerData != null) && (playerData.Ship != null))
                    {
                        // initialize the ship with the provided position
                        playerData.Ship.Position = position;
                        playerData.Ship.Score = 0;
                        playerData.Ship.Initialize();
                    }
                }
            }

            // initialize the ships with the data from the packet
            for (int i = 0; i < asteroids.Length; i++)
            {
                float radius = packetReader.ReadSingle();
                if (asteroids[i] == null)
                {
                    asteroids[i] = new Asteroid(radius);
                }
                asteroids[i].Variation = packetReader.ReadInt32();
                asteroids[i].Position = packetReader.ReadVector2();
                asteroids[i].Initialize();
                asteroids[i].Velocity = packetReader.ReadVector2();
            }

            // set the initialized state
            initialized = true;
        }
Exemple #2
0
        /// <summary>
        /// Generate the initial state of the game, and send it to everyone.
        /// </summary>
        public void GenerateWorld()
        {
            if ((networkSession != null) && (networkSession.LocalGamers.Count > 0))
            {
                // write the identification value
                packetWriter.Write((int)PacketTypes.WorldSetup);

                // place the ships
                // -- we always write the maximum number of players, making the packet
                //    predictable, in case the player count changes on the client before 
                //    this packet is received
                for (int i = 0; i < MaximumPlayers; i++)
                {
                    Vector2 position = Vector2.Zero;
                    if (i < networkSession.AllGamers.Count)
                    {
                        PlayerData playerData = networkSession.AllGamers[i].Tag
                            as PlayerData;
                        if ((playerData != null) && (playerData.Ship != null))
                        {
                            playerData.Ship.Initialize();
                            position = playerData.Ship.Position =
                                CollisionManager.FindSpawnPoint(playerData.Ship,
                                playerData.Ship.Radius * 5f);
                            playerData.Ship.Score = 0;
                        }
                    }
                    // write the ship position
                    packetWriter.Write(position);
                }

                // place the asteroids
                // -- for simplicity, the same number of asteroids is always the same
                for (int i = 0; i < asteroids.Length; i++)
                {
                    // choose one of three radii
                    float radius = 32f;
                    switch (RandomMath.Random.Next(3))
                    {
                        case 0:
                            radius = 32f;
                            break;
                        case 1:
                            radius = 60f;
                            break;
                        case 2:
                            radius = 96f;
                            break;
                    }
                    // create the asteroid
                    asteroids[i] = new Asteroid(radius);
                    // write the radius
                    packetWriter.Write(asteroids[i].Radius);
                    // choose a variation
                    asteroids[i].Variation = i % Asteroid.Variations;
                    // write the variation
                    packetWriter.Write(asteroids[i].Variation);
                    // initialize the asteroid and it's starting position
                    asteroids[i].Initialize();
                    asteroids[i].Position =
                        CollisionManager.FindSpawnPoint(asteroids[i], 
                        asteroids[i].Radius);
                    // write the starting position and velocity
                    packetWriter.Write(asteroids[i].Position);
                    packetWriter.Write(asteroids[i].Velocity);
                }

                // send the packet to everyone
                networkSession.LocalGamers[0].SendData(packetWriter,
                    SendDataOptions.ReliableInOrder);
            }
        }