/// <summary> /// Instanciates a new background. /// </summary> /// <param name="map">The map to draw for the level.</param> /// <param name="backgroundElements">The backgroundelements to draw for the level.</param> public Background(string[] map, BackgroundElement[] backgroundElements) { int spriteWidth = 32; y = (map.Length * spriteWidth) - GameForm.GameArea.Height; focusRectangle = new Rectangle(0, map.Length * spriteWidth, GameForm.GameArea.Width, GameForm.GameArea.Height); Height = focusRectangle.Y - GameForm.GameArea.Height; background = new Bitmap(GameForm.GameArea.Width, map.Length * spriteWidth); Graphics g = Graphics.FromImage(background); imgAttribs = new ImageAttributes(); imgAttribs.SetColorKey(Color.FromArgb(0, 66, 173), Color.FromArgb(0, 66, 173)); //Loop trough all map strings. for(int i = 0; i < map.Length; ++i) { //Create a char array of all map elements. char[] mapElements = map[i].ToCharArray(); //Loop trough all chars in the map string. for(int j = 0; j < mapElements.Length; ++j) { //Drawnig the map element to the map bitmap. int index = SpriteList.Instance.GetTileIndex(mapElements[j]); int n = SpriteList.Instance.Tiles.Length; g.DrawImage(SpriteList.Instance.Tiles[index], j * spriteWidth, i * spriteWidth); } } //Loop trough all background elements. for(int i = 0; i < backgroundElements.Length; ++i) { BackgroundElement e = backgroundElements[i]; //Draw the map element to the map bitmap. if(e.Width > 32) g.DrawImage(SpriteList.Instance.BigBackgroundElements[e.SpriteIndex], new Rectangle(e.X, e.Y, e.Width, e.Height), 0, 0, e.Width, e.Height, GraphicsUnit.Pixel, imgAttribs); } //Dispose the graphics element used to draw the background. g.Dispose(); }
/// <summary> /// Method instanciating a new level based on a XML level file. /// </summary> /// <param name="filename">The path to the XML level file.</param> public Level(string filename) { //Loading a XmlDocument based on the filepath. XmlDocument xml = new XmlDocument(); xml.Load(filename); //Setting the scrollspeed of the level. The speed is set by pixels pr. second. Speed = Convert.ToInt32(xml.GetElementsByTagName("Level")[0].Attributes["Speed"].InnerText); Name = xml.GetElementsByTagName("Level")[0].Attributes["Name"].InnerText; //Loading the map ASCII table. char[] seperator = {'\n'}; string mapString = xml.GetElementsByTagName("Map")[0].InnerText; Map = mapString.Split(seperator); //Selecting all background elements. XmlNodeList bgElements = xml.GetElementsByTagName("Element"); BackgroundElements = new BackgroundElement[bgElements.Count]; //Creating all background elements. for(int i = 0; i < bgElements.Count; ++i) { BackgroundElements[i] = new BackgroundElement( Convert.ToInt32(bgElements[i].Attributes["X"].InnerText), Convert.ToInt32(bgElements[i].Attributes["Y"].InnerText), Convert.ToInt32(bgElements[i].Attributes["Height"].InnerText), Convert.ToInt32(bgElements[i].Attributes["Width"].InnerText), Convert.ToInt32(bgElements[i].Attributes["SpriteIndex"].InnerText)); } //Selecting all bonus elements. XmlNodeList bonusElements = xml.GetElementsByTagName("Bonus"); Bonuses = new Bonus[bonusElements.Count]; //Creating all bonus elements. for(int i = 0; i < bonusElements.Count; ++i) { Bonuses[i] = new Bonus( Convert.ToInt32(bonusElements[i].Attributes["X"].InnerText), Convert.ToInt32(bonusElements[i].Attributes["Y"].InnerText), Convert.ToInt32(bonusElements[i].Attributes["Speed"].InnerText) / GameForm.FramesPerSecond, (BonusType)Convert.ToInt32(bonusElements[i].Attributes["BonusType"].InnerText)); } //Selecting all enemies. XmlNodeList enemies = xml.GetElementsByTagName("Enemy"); Enemies = new Enemy[enemies.Count]; //Creating all enemies. for(int i = 0; i < enemies.Count; ++i) { Enemies[i] = new Enemy( Convert.ToInt32(enemies[i].Attributes["X"].InnerText), Convert.ToInt32(enemies[i].Attributes["Y"].InnerText), Convert.ToInt32(enemies[i].Attributes["Speed"].InnerText) / GameForm.FramesPerSecond, (MovePattern)Convert.ToInt32(enemies[i].Attributes["MovePattern"].InnerText), (EnemyType)Convert.ToInt32(enemies[i].Attributes["EnemyType"].InnerText), (BulletType)Convert.ToInt32(enemies[i].Attributes["BulletType"].InnerText), Convert.ToInt32(enemies[i].Attributes["BulletPower"].InnerText), (double)(Convert.ToDouble(enemies[i].Attributes["BulletSpeed"].InnerText) / GameForm.FramesPerSecond), (double)(Convert.ToDouble(enemies[i].Attributes["BulletReloadTime"].InnerText) / (double)1000) * GameForm.FramesPerSecond, Convert.ToInt32(enemies[i].Attributes["Power"].InnerText), Convert.ToInt32(enemies[i].Attributes["Score"].InnerText)); } //Creating the background map for this level. BackgroundMap = new Background(Map, BackgroundElements); BackgroundMap.Speed = Speed / GameForm.FramesPerSecond; //Creating a arraylist to hold all the bullets on the level. Bullets = new ArrayList(); }