Example #1
0
        public void Render(SpriteBatch spriteBatch, EnemySpawner enemies)
        {
            SmoothScroll();

            int xLoop = (int)(drawArea.Width / tileSize.X);
            if (xLoop >= tiles.Length) xLoop = tiles.Length;

            for (int x = 0; x < xLoop; x++)
            {
                int yLoop = (int)(drawArea.Height / tileSize.Y);
                if (yLoop >= tiles[x].Length) yLoop = tiles[x].Length;

                for (int y = 0; y < yLoop; y++)
                {
                    float xpos = ((x * tileSize.X) + drawOffset.X) + drawArea.X;
                    float ypos = ((y * tileSize.Y) + drawOffset.Y) + drawArea.Y;

                    if (tileOffset.X < 0) tileOffset.X = 0;
                    if (tileOffset.Y < 0) tileOffset.Y = 0;
                    if ((tileOffset.Y + y) >= tiles[x].Length) tileOffset.Y = MaxOffset('Y', x);
                    if ((tileOffset.X + x) >= tiles.Length) tileOffset.X = MaxOffset('X', 0);

                    tiles[x + tileOffset.X][y + tileOffset.Y].Prepare(new Vector2(xpos, ypos), false);
                    tiles[x + tileOffset.X][y + tileOffset.Y].Render(spriteBatch);

                    for (int e = 0; e < tiles[x + tileOffset.X][y + tileOffset.Y].enemySpawns.Count; e++)
                    {
                        enemies.SpawnEnemy(tiles[x + tileOffset.X][y + tileOffset.Y].enemySpawns[e], new Vector2(xpos, ypos));
                    }
                    tiles[x + tileOffset.X][y + tileOffset.Y].enemySpawns.Clear();
                }
            }
        }
Example #2
0
        private void LoadLevel(int levelID, int totalPlayers)
        {
            if (!File.Exists(Path.Combine(StorageContainer.TitleLocation, "Content\\Maps\\level_" + levelID + ".xml")))
            {
                WinGame();
                return;
            }
            levelsXml.Load(Path.Combine(StorageContainer.TitleLocation, "Content\\Maps\\level_" + levelID + ".xml"));
            map = new Map(gameArea);
            XmlNode levelData = levelsXml.DocumentElement.SelectSingleNode("/levels/level[@id='" + levelID + "']");
            //If there is no level data then the game must have been completed
            if (levelData == null)
            {
                WinGame();
                return;
            }
            //Load Map Data
            XmlNode mapData = levelData.SelectSingleNode("map");
            Texture2D tileTextures = content.Load<Texture2D>(levelData.SelectSingleNode("map/@texture_path").InnerText);

            XmlNodeList enemyTexturesNodes = levelData.SelectNodes("enemyTextures/texture");
            Texture2D[] enemyTextures = new Texture2D[enemyTexturesNodes.Count];

            for (int t = 0; t < enemyTexturesNodes.Count; t++)
                enemyTextures[t] = content.Load<Texture2D>(enemyTexturesNodes[t].SelectSingleNode("@path").InnerText);

            enemies = new EnemySpawner(enemyTextures);
            projectiles = new ProjectileManager();

            Point scrollSpeed = new Point(Convert.ToInt32(levelData.SelectSingleNode("map/@scroll_x").InnerText), Convert.ToInt32(levelData.SelectSingleNode("map/@scroll_y").InnerText));
            XmlNodeList tileColumns = mapData.SelectNodes("column");

            map.tiles = new Tile[tileColumns.Count][];
            int tileWidth = 0;
            int tileHeight = 0;
            for (int x = 0; x < tileColumns.Count; x++)
            {
                XmlNodeList tileRows = tileColumns[x].SelectNodes("tile");
                map.tiles[x] = new Tile[tileRows.Count];
                for (int y = 0; y < tileRows.Count; y++)
                {
                    XmlNode tileCell = tileRows[y];
                    //get all graphic types
                    XmlNodeList graphics = tileCell.SelectNodes("graphics/graphic");
                    Vector4[] types = new Vector4[graphics.Count];
                    for (int g = 0; g < graphics.Count; g++)
                    {
                        XmlNode graphic = graphics[g];
                        types[g] = new Vector4(Convert.ToInt32(graphic.SelectSingleNode("@sprite_x").InnerText), Convert.ToInt32(graphic.SelectSingleNode("@sprite_y").InnerText), (float)Convert.ToDouble(graphic.SelectSingleNode("@rotation").InnerText), Convert.ToInt32(graphic.SelectSingleNode("@flip").InnerText));
                    }

                    int power = Convert.ToInt32(tileCell.SelectSingleNode("@power").InnerText);
                    int energy = Convert.ToInt32(tileCell.SelectSingleNode("@energy").InnerText);
                    int timer = 2;
                    int worth = Convert.ToInt32(tileCell.SelectSingleNode("@worth").InnerText);
                    bool destroyable = System.Convert.ToBoolean(tileCell.SelectSingleNode("@destroyable").InnerText);
                    bool collidable = System.Convert.ToBoolean(tileCell.SelectSingleNode("@collidable").InnerText);

                    tileWidth = Convert.ToInt32(tileCell.SelectSingleNode("dimensions/@width").InnerText);
                    tileHeight = Convert.ToInt32(tileCell.SelectSingleNode("dimensions/@height").InnerText);

                    //digest enemy spawn points
                    XmlNodeList enemiesToSpawn = tileCell.SelectNodes("enemies/enemy");
                    List<int> enemySpawns = new List<int>();
                    for (int e = 0; e < enemiesToSpawn.Count; e++)
                    {
                        XmlNode enemy = enemiesToSpawn[e];
                        int enemyToAdd=Convert.ToInt32(enemy.SelectSingleNode("@type").InnerText);
                        enemySpawns.Add(enemyToAdd);
                        endOfLevel += enemies.AddLevelEnders(enemyToAdd);
                    }

                    map.tiles[x][y] = new Tile(enemySpawns, 0.0f, new Point(x, y), types,
                                            destroyable, collidable, power, energy, timer, worth, tileTextures,
                                            tileWidth, tileHeight);
                    map.tiles[x][y].alive = true;
                }
            }
            map.scrollSpeed = scrollSpeed;
            map.tileSize = new Point(tileWidth, tileHeight);

            //Load Player Data
            XmlNodeList playerNodes = levelData.SelectNodes("players/player");

            players = new Player[totalPlayers];
            playerHuds = new PlayerHud[totalPlayers];
            SpriteFont playerhudFont = content.Load<SpriteFont>("Fonts/hudfont");
            Texture2D pixelDot = content.Load<Texture2D>("Sprites/pixel");

            PlayerIndex[] playerIndexes = new PlayerIndex[] { PlayerIndex.One, PlayerIndex.Two, PlayerIndex.Three, PlayerIndex.Four };
            Color[] playerColors = new Color[] { Color.Red, Color.Green };
            for (int p = 0; p < totalPlayers; p++)
            {
                XmlNodeList weapons = playerNodes[p].SelectNodes("weapons/weapon");
                Texture2D projectileTexture = content.Load<Texture2D>(playerNodes[p].SelectSingleNode("@projectile_path").InnerText);
                Texture2D playerTexture = content.Load<Texture2D>(playerNodes[p].SelectSingleNode("@texture_path").InnerText);
                float pWeight = (float) Convert.ToDouble(playerNodes[p].SelectSingleNode("@weight").InnerText);
                int pPower = Convert.ToInt32(playerNodes[p].SelectSingleNode("@power").InnerText);
                //energy and shield is reset to full every map load
                int pEnergy = Convert.ToInt32(playerNodes[p].SelectSingleNode("@energy").InnerText);
                int pShield = Convert.ToInt32(playerNodes[p].SelectSingleNode("@shield").InnerText);
                int pWorth = Convert.ToInt32(playerNodes[p].SelectSingleNode("@worth").InnerText);

                //lives are set to current
                int pLives = Convert.ToInt32(playerNodes[p].SelectSingleNode("@lives").InnerText);
                if (playerLives[p] > -1)
                    pLives = playerLives[p];

                int pWidth = Convert.ToInt32(playerNodes[p].SelectSingleNode("dimensions/@width").InnerText);
                int pHeight = Convert.ToInt32(playerNodes[p].SelectSingleNode("dimensions/@height").InnerText);
                int pTileX = Convert.ToInt32(playerNodes[p].SelectSingleNode("tile_start/@x").InnerText);
                int pTileY = Convert.ToInt32(playerNodes[p].SelectSingleNode("tile_start/@y").InnerText);

                float xpos = ((pTileX * tileWidth)) + gameArea.X;
                float ypos = ((pTileY * tileHeight)) + gameArea.Y;

                players[p] = new Player(new Vector2(xpos, ypos), pShield, playerIndexes[p], projectileTexture, weapons.Count, pLives, pWeight, 0.01f, new Vector2(-4.0f, 5.0f), true, true, pPower, pEnergy, 18, pWorth, playerTexture, pWidth, pHeight, 1, 0.025f, 1, false);
                //set the player score to the current level score held by the player
                players[p].score = playerScores[p];
                players[p].Prepare(players[p].startPosition);

                //set player huds
                playerHuds[p] = new PlayerHud(pixelDot, playerhudFont, players[p], playerColors[p], new Vector2(60 + (600 * p), gameArea.Top + 60), 0.4f);

                //set weapons
                //If there are no set weapons then use default weapons
                if (playerWeapons[p]==null)
                {
                    playerWeapons[p] = new Weapon[weapons.Count];
                    for (int w = 0; w < weapons.Count; w++)
                    {
                        int wProjectiles = Convert.ToInt32(weapons[w].SelectSingleNode("@max_projectiles").InnerText);
                        int wRate = Convert.ToInt32(weapons[w].SelectSingleNode("@fire_rate").InnerText);
                        int wType = Convert.ToInt32(weapons[w].SelectSingleNode("@projectile_type").InnerText);
                        players[p].ChangeWeapon(w, wProjectiles, wRate, wType);
                    }
                }
                else
                {
                    for (int w = 0; w < playerWeapons[p].Length; w++)
                    {
                        Weapon tmpWep = playerWeapons[p][w];
                        players[p].ChangeWeapon(w, tmpWep.maxProjectiles, tmpWep.fireRate, tmpWep.ammoType);
                    }
                }
            }
        }