Exemplo n.º 1
0
        // Methods
        /// <summary>
        /// Loads all maps, weapons and enemies from the resource file
        /// </summary>
        public void LoadResources()
        {
            // Temporary Fields
            string currentEnemyName;

            int[]    weaponFieldsParsed;
            int[]    currentEnemyFields;
            string[] currentWeaponFields;
            int[,] tiles;
            BinaryReader reader;
            FileStream   resources;

            // Initialize Temporary Fields
            tiles = new int[16, 16];

            // Initialize Class Fields
            tileSize        = 68;
            key             = new Item(new Rectangle(500, 500, (int)(.75 * tileSize), (int)(.75 * tileSize)), keyTexture, false, true);
            spear           = new MeleeWeapon(new Point((int)(1.2 * tileSize), (int)(.6 * tileSize)), swordTexture, 8, 20);
            sword           = new MeleeWeapon(new Point((int)(.6 * tileSize), (int)(.6 * tileSize)), swordTexture, 5, 90, 5);
            player          = new Player(new Rectangle(0, 0, (int)(.75 * tileSize), (int)(.75 * tileSize)), playerTexture, 300, sword);
            arrow           = new Projectile(new Point((int)(.3 * tileSize), (int)(.08 * tileSize)), arrowTexture, 20);
            spell           = new Projectile(new Point((int)(.3 * tileSize), (int)(.3 * tileSize)), spellTexture, 12);
            bow             = new RangedWeapon(new Point((int)(.6 * tileSize), (int)(.6 * tileSize)), bowTexture, 2, arrow);
            wand            = new RangedWeapon(new Point((int)(.75 * tileSize), (int)(.75 * tileSize)), wandTexture, 1, spell);
            maps            = new List <Map>();
            resourceEnemies = new List <Enemy>();
            resourceWeapons = new List <Weapon>();

            // Creates the enemyWeapons List
            enemyWeapons = new List <Weapon> {
                sword, spear, wand, bow
            };

            // Loads Enemy Textures
            List <Texture2D> enemySprites = new List <Texture2D>
            {
                Content.Load <Texture2D>("enemies/archer"),
                Content.Load <Texture2D>("enemies/berserker"),
                Content.Load <Texture2D>("enemies/bug"),
                Content.Load <Texture2D>("enemies/sentry"),
                Content.Load <Texture2D>("enemies/knight"),
                Content.Load <Texture2D>("enemies/Wizard"),
                Content.Load <Texture2D>("enemies/tank"),
                Content.Load <Texture2D>("enemies/sentry"),
                Content.Load <Texture2D>("enemies/viking"),
                Content.Load <Texture2D>("enemies/wanderer")
            };

            // Open FileStream and BinaryReader
            resources = File.OpenRead("../../../../../Resources/master.rsrc");
            reader    = new BinaryReader(resources);

            // While there is unread data, continue looping
            while (reader.PeekChar() != -1)
            {
                // Read a string that says "Map," "Enemy," or "Weapon"
                switch (reader.ReadString())
                {
                case "Map":
                    // Skip file name
                    reader.ReadString();

                    // Assign all tiles to list
                    for (int i = 0; i < tiles.GetLength(0); i++)
                    {
                        for (int j = 0; j < tiles.GetLength(1); j++)
                        {
                            tiles[i, j] = reader.ReadInt32();
                        }
                    }

                    // Add the Map to the List
                    maps.Add(new Map(tileSize, tiles, floorTexture, wallTexture));
                    break;

                case "Enemy":
                    // Enemy File Format (8 parameters from .enemy file): health, damage, attackSpeed, speed, xSize, ySize, movementType, weaponType
                    //    -  All items are read in as strings but parsed to: (int, int, int, int, int, int, int, int)

                    // Stores name of the Enemy file
                    currentEnemyName = reader.ReadString();

                    // Initialize Enemy Fields
                    currentEnemyFields = new int[8];

                    // Read in parameters
                    for (int i = 0; i < currentEnemyFields.Length; i++)
                    {
                        currentEnemyFields[i] = int.Parse(reader.ReadString());
                    }

                    // Sets the appropriate movement type based on the integer from the file
                    EnemyMovementType movementType = EnemyMovementType.Chase;     // default type
                    if (currentEnemyFields[6] == 0)
                    {
                        movementType = EnemyMovementType.Chase;
                    }
                    else if (currentEnemyFields[6] == 1)
                    {
                        movementType = EnemyMovementType.LeftRight;
                    }
                    else if (currentEnemyFields[6] == 2)
                    {
                        movementType = EnemyMovementType.Random;
                    }
                    else if (currentEnemyFields[6] == 3)
                    {
                        movementType = EnemyMovementType.Rectangle;
                    }
                    else if (currentEnemyFields[6] == 4)
                    {
                        movementType = EnemyMovementType.UpDown;
                    }

                    // Add the enemy to the list of imported resource enemies
                    resourceEnemies.Add(new Enemy(new Rectangle(0, 0, (int)(currentEnemyFields[4] / 10.0 * tileSize), (int)(currentEnemyFields[5] / 10.0 * tileSize)), // Enemy Size (position should be overridden in NextLevel()
                                                  squareTexture,                                                                                                       // Enemy Texture
                                                  currentEnemyFields[0],                                                                                               // Health
                                                  null,                                                                                                                // Weapon
                                                  currentEnemyFields[2],                                                                                               // Attack Interval
                                                  currentEnemyFields[1],                                                                                               // Body Damage
                                                  150,                                                                                                                 // Default Max Travel Width
                                                  150,                                                                                                                 // Default Max Travel Height
                                                  currentEnemyFields[3],                                                                                               // Travel Speed
                                                  currentEnemyName,                                                                                                    // Enemy Name
                                                  movementType,                                                                                                        // Enemy Movement Type
                                                  player,                                                                                                              // The Player
                                                  random));                                                                                                            // Random Number Generator

                    // Assigns Texture if applicable
                    if (resourceEnemies.Count <= enemySprites.Count)
                    {
                        resourceEnemies[resourceEnemies.Count - 1].Texture = enemySprites[resourceEnemies.Count - 1];
                    }

                    //Adds the weapon to the enemy
                    if (currentEnemyFields[7] < 4 && currentEnemyFields[7] > -1)
                    {
                        // Adds the Weapon to the Enemy
                        // If the field is a valid Weapon type
                        if (currentEnemyFields[7] < 4 && currentEnemyFields[7] > -1)
                        {
                            // If the Weapon is a MeleeWeapon or RangedWeapon, set appropriately
                            if (enemyWeapons[currentEnemyFields[7]] is MeleeWeapon)
                            {
                                resourceEnemies[resourceEnemies.Count - 1].Weapon = new MeleeWeapon((MeleeWeapon)enemyWeapons[currentEnemyFields[7]]);
                            }
                            else if (enemyWeapons[currentEnemyFields[7]] is RangedWeapon)
                            {
                                resourceEnemies[resourceEnemies.Count - 1].Weapon = new RangedWeapon((RangedWeapon)enemyWeapons[currentEnemyFields[7]]);
                            }
                        }
                    }
                    break;

                //####### TO-DO: Integrate this list of enemies into the game.. some helpful info: ######################################################
                // - The enemies brought in do not have sizes, textures, set movement patterns, or max travelling dimensions
                // - Note that the current texture used is a square test texture
                // - I restate, in the .rsrc file, those ints are stored as strings.  They are strings since they come out of the Enemy external tool.
                //   They are then parsed to an array of ints here, and act as ints. No need to TryParse, the external tool only allows integers > 0 to be used.

                case "Weapon":
                    // Weapon File Format (four parameters from .weapon file): name, damage, size, type
                    //    -  All items are read in as strings, but the ints are parsed: (string, int, int, int)

                    // Skips file name
                    reader.ReadString();

                    // Intialize Fields
                    currentWeaponFields = new string[4]; // holds the original four parameters in string form
                    weaponFieldsParsed  = new int[3];    // takes parameters 2 through 4 and converts them to ints

                    // Read in four parameters
                    for (int i = 0; i < 4; i++)
                    {
                        currentWeaponFields[i] = reader.ReadString();
                    }

                    // Parse the last three parameters to integers, and store them in a separate array
                    weaponFieldsParsed[0] = int.Parse(currentWeaponFields[1]);
                    weaponFieldsParsed[1] = int.Parse(currentWeaponFields[2]);
                    weaponFieldsParsed[2] = int.Parse(currentWeaponFields[3]);

                    // Add the weapon to the list of imported resource weapons
                    // based on its type: 0 - Sword, 1 - Spear, 2 - Wand, 3 - Bow

                    // The weapon is a sword
                    if (weaponFieldsParsed[2] == 0)
                    {
                        resourceWeapons.Add(new MeleeWeapon(new Point((int)(weaponFieldsParsed[1] * tileSize / 10), (int)(weaponFieldsParsed[1] * tileSize / 10)), swordTexture, weaponFieldsParsed[0], 90, 5));
                    }

                    // The weapon is a spear
                    else if (weaponFieldsParsed[2] == 1)
                    {
                        resourceWeapons.Add(new MeleeWeapon(new Point((int)(weaponFieldsParsed[1] * tileSize / 5), (int)(weaponFieldsParsed[1] * tileSize / 10)), swordTexture, weaponFieldsParsed[0], 20));
                    }

                    // The weapon is a wand
                    else if (weaponFieldsParsed[2] == 2)
                    {
                        resourceWeapons.Add(new RangedWeapon(new Point((int)(weaponFieldsParsed[1] * tileSize / 10), (int)(weaponFieldsParsed[1] * tileSize / 10)), wandTexture, weaponFieldsParsed[0], spell));
                    }

                    // The weapon is a bow
                    else
                    {
                        resourceWeapons.Add(new RangedWeapon(new Point((int)(weaponFieldsParsed[1] * tileSize / 10), (int)(weaponFieldsParsed[1] * tileSize / 10)), bowTexture, weaponFieldsParsed[0], arrow));
                    }

                    // End weapon case
                    break;

                    //####### TO-DO: Integrate this list of weapons into the game.. some helpful info: ######################################################
                    // - The weapons brought in do not have textures, sizes, projectiles, spin speeds, or spin angles.
                    // - The weapons brought in have durability and name fields that are not utilized by the constructors,
                    //   but are there if you want to use them.
                }
            }

            // Close the binary reader.
            reader.Close();
        }