//***************** CLASS METHODS ******************* public MegaTile(Level containingLevel, Vector2 topLeftLocation, MegaTileType mTileType, int width, int height) { //Set the containing level level = containingLevel; //Create victims list victims = new List<Victim>(); //Set location of tile location = topLeftLocation; //Set the type of this tile megaTileType = mTileType; //Calculate the bounds of this tile bounds.X = (int)location.X; bounds.Y = (int)location.Y; bounds.Width = width; bounds.Height = height; //Calculate the bounds for the roads (whether there will be any or not) westRoadBounds = new Rectangle((int)location.X, (int)location.Y, megaTileType.RoadWidth, bounds.Height); northRoadBounds = new Rectangle((int)location.X, (int)location.Y, bounds.Width, megaTileType.RoadWidth); intersectionBounds = new Rectangle((int)location.X, (int)location.Y, megaTileType.RoadWidth, megaTileType.RoadWidth); //Set whether roads are enabled or not if (megaTileType.CanHaveRoads) { westRoad = true; northRoad = true; intersection = true; } int maxTries = 1; int tryCount = 0; //Look for required victims and add them in first foreach(MegaTileVictimConfig mtvConfig in megaTileType.PossibleVictims) { int genSize = rand.Next(mtvConfig.RequiredPopulation, mtvConfig.MaxPopulation); //Add in the required number of victims for (int i = 0; i < genSize; i++) { tryCount = 0; //Try to add the victim a certain number of times while (!tryAddingVictim(mtvConfig.VictimConfig) && tryCount < maxTries) { tryCount++; } } } }
//Read in mega tile type data //reader should be positioned on a MEGATILETYPE element private static void ReadMegaTileType(XmlTextReader reader, ContentManager content) { // TODO: add an "index" field to the tile description that you use for the flat level file, instead of basing it off order. string tempString; //Move to first attribute for this mega tile if (reader.MoveToFirstAttribute()) { //Create a new mega tile type MegaTileType tempMegaType = new MegaTileType(); //Loop through all attributes do { //Get the attribute name tempString = reader.Name.ToUpper(); try { //Check for all valid attributes if (tempString == "NAME") { tempMegaType.Name = reader.Value; } else if (tempString == "NUMBER") { tempMegaType.Number = int.Parse(reader.Value); } else if (tempString == "WIDTH") { tempMegaType.Width = int.Parse(reader.Value); } else if (tempString == "HEIGHT") { tempMegaType.Height = int.Parse(reader.Value); } else if (tempString == "BACKGROUNDTEXTURE") { tempMegaType.BackgroundTexture = content.Load<Texture2D>(reader.Value); } else if (tempString == "CANHAVEROADS") { tempMegaType.CanHaveRoads = bool.Parse(reader.Value); } //Set all the road textures given the prefix to use for all of them else if(tempString == "ROADTEXTUREPREFIX"){ tempMegaType.RoadTexturePrefix = reader.Value; tempMegaType.RoadHorizontalTexture = content.Load<Texture2D>(reader.Value + roadHorizonalSuffix); tempMegaType.RoadVerticalTexture = content.Load<Texture2D>(reader.Value + roadVerticalSuffix); tempMegaType.Road4WayTexture = content.Load<Texture2D>(reader.Value + road4WaySuffix); } else if (tempString == "ROADHORIZONTALTEXTURE") { tempMegaType.RoadHorizontalTexture = content.Load<Texture2D>(reader.Value); } else if (tempString == "ROADVERTICALTEXTURE") { tempMegaType.RoadVerticalTexture = content.Load<Texture2D>(reader.Value); } else if (tempString == "ROADWIDTH") { tempMegaType.RoadWidth = int.Parse(reader.Value); } else { throw new ApplicationException("Invalid Mega Tile Type attribute '" + tempString + "' on line " + reader.LineNumber); } } catch (Exception e) { throw new ApplicationException("Error parsing Mega Tile Type attribute on line " + reader.LineNumber + ": " + e.Message); } } while (reader.MoveToNextAttribute()); //Look for nested elements string attName; while (!(reader.NodeType == XmlNodeType.EndElement && reader.Name.ToUpper() == "MEGATILETYPE")) { reader.Read(); reader.MoveToContent(); if (reader.NodeType == XmlNodeType.Element) { tempString = reader.Name.ToUpper(); //Look for valid Victim type sub-elements if (tempString == "VICTIM") { MegaTileVictimConfig mtvConfig = ReadMegaTileVictimConfig(reader, content); tempMegaType.PossibleVictims.Add(mtvConfig); } else { throw new ApplicationException("Invalid Mega Tile Type Sub-Element '" + tempString + "' on line " + reader.LineNumber); } } } //Add the Mega tile type to the list megaTileTypes.Add(tempMegaType); } }