// parse a collection of variables from a file public static VariableSystem readFrom(string fileName) { if (fileName == null || fileName.Length == 0) { return(null); } string data; // open the file StreamReader input = null; try { input = File.OpenText(fileName); } catch (Exception) { return(null); } VariableSystem variables = new VariableSystem(); string category = null; int categoryIndex = -1; // read to the end of the file while ((data = input.ReadLine()) != null) { data = data.Trim(); if (data.Length == 0) { category = null; categoryIndex = -1; continue; } // parse a category if (data[0] == '[' && data[data.Length - 1] == ']') { category = data.Substring(1, data.Length - 2); categoryIndex = variables.addCategory(category); } // parse a variable else { Variable v = Variable.parseFrom(data); if (v != null) { v.category = categoryIndex; variables.add(v); } } } if (input != null) { input.Close(); } return(variables); }
public static Terrain parseFrom(StreamReader input, SpriteSheetCollection spriteSheets) { if (input == null || spriteSheets == null) { return(null); } VariableSystem properties = new VariableSystem(); // store all of the animation properties String data; Variable property; do { data = input.ReadLine(); if (data == null) { return(null); } data = data.Trim(); if (data.Length == 0) { continue; } property = Variable.parseFrom(data); if (property == null) { return(null); } properties.add(property); } while (properties.size() < 2); String heightmapName = properties.getValue("Heightmap Name"); if (heightmapName == null) { return(null); } string visibleString = properties.getValue("Visible"); if (visibleString == null) { return(null); } bool visible = visibleString.Equals("True", StringComparison.OrdinalIgnoreCase); // create the object Terrain newObject = new Terrain(visible, heightmapName); return(newObject); }
// parse and create a sprite sheet from a text file input stream public static SpriteSheet parseFrom(StreamReader instream, ContentManager content) { if (instream == null || content == null) { return(null); } string input = null; string data = null; Variable v; VariableSystem properties = new VariableSystem(); string spriteSheetName, spriteSheetFileName = null; SpriteSheetType spriteSheetType = SpriteSheetType.Invalid; Sprite spriteSheetImage = null; SpriteSheet spriteSheet = null; try { while ((input = instream.ReadLine()) != null) { // trim the current line being read data = input.Trim(); // if the line is blank, discard it if (data.Length == 0) { continue; } // otherwise, if it contains information else { // attempt to parse a variable from the data and store it v = Variable.parseFrom(data); // if successful, store it if (v != null) { properties.add(v); } // if the variable is a specification of the number of sprites or // the number of grids to be parsed, begin doing so if (v != null && (v.id.Equals("Number of Sprites", StringComparison.OrdinalIgnoreCase) || v.id.Equals("Number of Grids", StringComparison.OrdinalIgnoreCase) || (properties.contains("SpriteSheet Name") && properties.contains("SpriteSheet Type") && properties.contains("File Name") && properties.getValue("SpriteSheet Type").Equals("Single", StringComparison.OrdinalIgnoreCase)))) { // get the sprite sheet name and verify it exists spriteSheetName = properties.getValue("SpriteSheet Name"); if (spriteSheetName == null) { return(null); } // parse the sprite sheet type and ensure it is valid spriteSheetType = parseType(properties.getValue("SpriteSheet Type")); if (spriteSheetType == SpriteSheetType.Invalid) { return(null); } // get the sprite sheet file name spriteSheetFileName = properties.getValue("File Name"); if (spriteSheetFileName == null) { return(null); } // load the sprite to be parsed by the sprite sheet system spriteSheetImage = new Sprite(spriteSheetFileName, content); spriteSheetImage.type = SpriteType.SpriteSheet; // =============================================================================== // Single Sprite ================================================================= // =============================================================================== if (spriteSheetType == SpriteSheetType.Single) { VariableSystem spriteAttributes = new VariableSystem(); for (int i = 0; i < 4; i++) { spriteAttributes.add(Variable.parseFrom(instream.ReadLine())); } // get the sprite name String spriteNameData = spriteAttributes.getValue("Sprite Name"); if (spriteNameData == null) { return(null); } // get the sprite type String spriteTypeData = spriteAttributes.getValue("Sprite Type"); if (spriteTypeData == null) { return(null); } SpriteType spriteType = Sprite.parseType(spriteTypeData); if (spriteType == SpriteType.Unknown) { return(null); } // get the offset of the sprite String offsetData = spriteAttributes.getValue("Pixel Offset"); if (offsetData == null) { return(null); } String[] offsetValues = offsetData.Split(','); if (offsetValues.Length != 2) { return(null); } // get the size of the sprite String sizeData = spriteAttributes.getValue("Size"); if (sizeData == null) { return(null); } String[] sizeValues = sizeData.Split(','); if (sizeValues.Length != 2) { return(null); } // create a rectangle representing the first cell in the current grid Rectangle grid; try { grid = new Rectangle(int.Parse(offsetValues[0]), int.Parse(offsetValues[1]), int.Parse(sizeValues[0]), int.Parse(sizeValues[1])); } catch (Exception) { return(null); } Sprite sprite = new Sprite(spriteSheetFileName, content); sprite.name = spriteNameData; sprite.parentName = spriteSheetName; sprite.index = 0; sprite.type = spriteType; spriteSheet = new SpriteSheet(); spriteSheet.image = sprite; spriteSheet.name = spriteSheetName; spriteSheet.addSprite(sprite); } // =============================================================================== // Arbitrary Offset SpriteSheet ================================================== // =============================================================================== else if (spriteSheetType == SpriteSheetType.ArbitraryOffsets) { VariableSystem spriteAttributes = new VariableSystem(); int numberOfSprites; // verify that the number of sprites is valid try { numberOfSprites = int.Parse(v.value); } catch (Exception) { return(null); } if (numberOfSprites <= 0) { return(null); } int spriteIndex; string spriteName, spriteType; Rectangle grid; Rectangle[] offsets = new Rectangle[numberOfSprites]; string[] spriteNames = new string[numberOfSprites]; SpriteType[] spriteTypes = new SpriteType[numberOfSprites]; for (int i = 0; i < numberOfSprites; i++) { spriteNames[i] = null; spriteTypes[i] = SpriteType.Unknown; offsets[i] = new Rectangle(0, 0, 0, 0); } // loop through and collect the attributes for each sprite for (int i = 0; i < numberOfSprites; i++) { spriteAttributes.clear(); // get the 5 sprite attributes (index, name, type, offset, size) for (int j = 0; j < 5; j++) { spriteAttributes.add(Variable.parseFrom(instream.ReadLine())); } // store the sprite index value and validate it try { spriteIndex = int.Parse(spriteAttributes.getValue("Sprite")); } catch (Exception) { return(null); } if (spriteIndex < 0 || spriteIndex >= numberOfSprites) { return(null); } // get the sprite name spriteName = spriteAttributes.getValue("Name"); if (spriteName == null) { return(null); } // get the sprite type (as a string) spriteType = spriteAttributes.getValue("Type"); if (spriteType == null) { return(null); } // parse the sprite's offset in its parent sprite sheet string temp = spriteAttributes.getValue("Pixel Offset"); if (temp == null) { return(null); } string[] offsetValues = temp.Split(','); if (offsetValues.Length != 2) { return(null); } // parse the size of the sprite temp = spriteAttributes.getValue("Size"); if (temp == null) { return(null); } string[] sizeValues = temp.Split(','); if (sizeValues.Length != 2) { return(null); } // attempt to create a source grid based off of these specifications try { grid = new Rectangle(int.Parse(offsetValues[0]), int.Parse(offsetValues[1]), int.Parse(sizeValues[0]), int.Parse(sizeValues[1])); } catch (Exception) { return(null); } // assign the values for temporary storage spriteNames[spriteIndex] = spriteName; spriteTypes[spriteIndex] = Sprite.parseType(spriteType); offsets[spriteIndex] = new Rectangle(grid.X, grid.Y, grid.Width, grid.Height); } // once the attributes and offsets for all of the sprites have been collected, create the sprite sheet spriteSheet = new SpriteSheet(spriteSheetImage, offsets); spriteSheet.name = spriteSheetName; // loop through the temporarily stored sprite attributes and assign them to each corresponding sprite for (int i = 0; i < numberOfSprites; i++) { spriteSheet.getSprite(i).name = spriteNames[i]; spriteSheet.getSprite(i).index = i; spriteSheet.getSprite(i).parentName = spriteSheetName; spriteSheet.getSprite(i).type = spriteTypes[i]; } } // =============================================================================== // Single Grid SpriteSheet ======================================================= // =============================================================================== else if (spriteSheetType == SpriteSheetType.SingleGrid) { VariableSystem spriteAttributes = new VariableSystem(); Rectangle grid; Axis direction; int numberOfRows, numberOfColumns, numberOfSprites; // get the number of sprites to obtain attributes for try { numberOfSprites = int.Parse(v.value); } catch (Exception) { return(null); } // get the grid offset string temp = properties.getValue("Pixel Offset"); if (temp == null) { return(null); } string[] offsetValues = temp.Split(','); if (offsetValues.Length != 2) { return(null); } // get the size of each cell within the grid temp = properties.getValue("Size"); if (temp == null) { return(null); } string[] sizeValues = temp.Split(','); if (sizeValues.Length != 2) { return(null); } // convert the offset and cell size to a rectangle try { grid = new Rectangle(int.Parse(offsetValues[0]), int.Parse(offsetValues[1]), int.Parse(sizeValues[0]), int.Parse(sizeValues[1])); } catch (Exception) { return(null); } // get the direction to parse the sprites in direction = parseAxis(properties.getValue("Direction")); // get the number of rows in the grid try { numberOfRows = int.Parse(properties.getValue("Number of Rows")); } catch (Exception) { return(null); } // get the number of columns in the grid try { numberOfColumns = int.Parse(properties.getValue("Number of Columns")); } catch (Exception) { return(null); } // create the sprite sheet based on the corresponding specifications spriteSheet = new SpriteSheet(spriteSheetImage, grid, direction, numberOfRows, numberOfColumns); spriteSheet.name = spriteSheetName; int spriteIndex; string spriteName, spriteType; // loop through the sprite sheet specifications for (int i = 0; i < numberOfSprites; i++) { // obtain the 3 specifications for the current sprite (index, name, type) for (int j = 0; j < 3; j++) { spriteAttributes.add(Variable.parseFrom(instream.ReadLine())); } // get the sprite index and verify it try { spriteIndex = int.Parse(spriteAttributes.getValue("Sprite")); } catch (Exception) { return(null); } if (spriteIndex < 0 || spriteIndex >= spriteSheet.size()) { return(null); } // get the sprite name spriteName = spriteAttributes.getValue("Name"); if (spriteName == null) { return(null); } // get the sprite type (as a string) spriteType = spriteAttributes.getValue("Type"); if (spriteType == null) { return(null); } // assign the attributes to the corresponding sprite spriteSheet.getSprite(spriteIndex).name = spriteName; spriteSheet.getSprite(spriteIndex).index = spriteIndex; spriteSheet.getSprite(spriteIndex).parentName = spriteSheetName; spriteSheet.getSprite(spriteIndex).type = Sprite.parseType(spriteType); // clear the list of attributes presently associated with the current sprite spriteAttributes.clear(); } } // =============================================================================== // Multiple Grids SpriteSheet ==================================================== // =============================================================================== else if (spriteSheetType == SpriteSheetType.MultipleGrids) { int numberOfGrids; int spriteIndexOffset = 0; // verify that the number of grids to parse is specified and valid try { numberOfGrids = int.Parse(v.value); } catch (Exception) { return(null); } if (numberOfGrids < 1) { return(null); } // create an empty sprite sheet spriteSheet = new SpriteSheet(); spriteSheet.name = spriteSheetName; spriteSheet.image = spriteSheetImage; // temporary storage VariableSystem gridAttributes = new VariableSystem(); int[] numberOfSprites = new int[numberOfGrids]; Rectangle[] grid = new Rectangle[numberOfGrids]; Axis[] direction = new Axis[numberOfGrids]; int[] numberOfRows = new int[numberOfGrids]; int[] numberOfColumns = new int[numberOfGrids]; for (int i = 0; i < numberOfGrids; i++) { numberOfSprites[i] = -1; grid[i] = new Rectangle(0, 0, 0, 0); direction[i] = Axis.Horizontal; numberOfRows[i] = -1; numberOfColumns[i] = -1; } // loop through all of the grids for (int i = 0; i < numberOfGrids; i++) { // obtain the 7 attributes for each grid (index, offset, size, parse direction, number of rows, number of columns, number of sprite attributes) for (int j = 0; j < 7; j++) { gridAttributes.add(Variable.parseFrom(instream.ReadLine())); } // get the index of the current grid int gridIndex; try { gridIndex = int.Parse(gridAttributes.getValue("Grid")); } catch (Exception) { return(null); } if (gridIndex < 0 || gridIndex >= numberOfGrids) { return(null); } // get the number of sprites within the current grid and validate it try { numberOfSprites[gridIndex] = int.Parse(gridAttributes.getValue("Number of Sprites")); } catch (Exception) { return(null); } // get the offset of the current grid string temp = gridAttributes.getValue("Pixel Offset"); if (temp == null) { return(null); } string[] offsetValues = temp.Split(','); if (offsetValues.Length != 2) { return(null); } // get the size of each cell within the current grid temp = gridAttributes.getValue("Size"); if (temp == null) { return(null); } string[] sizeValues = temp.Split(','); if (sizeValues.Length != 2) { return(null); } // create a rectangle representing the first cell in the current grid try { grid[gridIndex] = new Rectangle(int.Parse(offsetValues[0]), int.Parse(offsetValues[1]), int.Parse(sizeValues[0]), int.Parse(sizeValues[1])); } catch (Exception) { return(null); } // obtain the direction in which to parse the sprites from the current sprite sheet grid direction[gridIndex] = parseAxis(gridAttributes.getValue("Direction")); // get the number of rows in the current grid try { numberOfRows[gridIndex] = int.Parse(gridAttributes.getValue("Number of Rows")); } catch (Exception) { return(null); } // get the number of columns in the current grid try { numberOfColumns[gridIndex] = int.Parse(gridAttributes.getValue("Number of Columns")); } catch (Exception) { return(null); } // start parsing at the initial offset // loop through all of the rows and columns incrementing the x position and y position as appropriate // parse each sprite according to the current offset and size and store it in the main sprite collection int xPos = grid[gridIndex].X; int yPos = grid[gridIndex].Y; for (int j = 0; j < numberOfRows[gridIndex]; j++) { for (int k = 0; k < numberOfColumns[gridIndex]; k++) { spriteSheet.addSprite(new Sprite(spriteSheetImage, new Rectangle(xPos, yPos, grid[gridIndex].Width, grid[gridIndex].Height))); if (direction[gridIndex] == Axis.Horizontal) { xPos += grid[gridIndex].Width + 2; } else { yPos += grid[gridIndex].Height + 2; } } if (direction[gridIndex] == Axis.Horizontal) { yPos += grid[gridIndex].Height + 2; xPos = grid[gridIndex].X; } else { xPos += grid[gridIndex].Width + 2; yPos = grid[gridIndex].Y; } } int spriteIndex; string spriteName, spriteType; VariableSystem spriteAttributes = new VariableSystem(); // loop through the sprite attribute specifications for the current grid for (int j = 0; j < numberOfSprites[gridIndex]; j++) { // read in the 3 sprite attributes (index, name, type) and store them for (int k = 0; k < 3; k++) { spriteAttributes.add(Variable.parseFrom(instream.ReadLine())); } // get the sprite index and validate it try { spriteIndex = spriteIndexOffset + int.Parse(spriteAttributes.getValue("Sprite")); } catch (Exception) { return(null); } if (spriteIndex < spriteIndexOffset || spriteIndex >= spriteSheet.size()) { return(null); } // get the sprite name spriteName = spriteAttributes.getValue("Name"); if (spriteName == null) { return(null); } // get the sprite type spriteType = spriteAttributes.getValue("Type"); if (spriteType == null) { return(null); } // assign the current set of attributes to the corresponding sprite spriteSheet.getSprite(spriteIndex).name = spriteName; spriteSheet.getSprite(spriteIndex).index = spriteIndex; spriteSheet.getSprite(spriteIndex).parentName = spriteSheetName; spriteSheet.getSprite(spriteIndex).type = Sprite.parseType(spriteType); // clear the current collection of sprite attributes spriteAttributes.clear(); } // clear the current collection of grid attributes and update the Sprite index offset (current position in the main SpriteSheet collection) gridAttributes.clear(); spriteIndexOffset = spriteSheet.size(); } } // return null if sprite sheet type is invalid else { return(null); } // return the current sprite sheet return(spriteSheet); } } } } catch (Exception) { } return(null); }
public static SpriteAnimation parseFrom(StreamReader input, SpriteSheetCollection spriteSheets) { if (input == null || spriteSheets == null) { return(null); } SpriteAnimation animation; VariableSystem animationProperties = new VariableSystem(); // store all of the animation properties String data; Variable property; do { data = input.ReadLine(); if (data == null) { return(null); } data = data.Trim(); if (data.Length == 0) { continue; } property = Variable.parseFrom(data); if (property == null) { return(null); } animationProperties.add(property); } while(animationProperties.size() < 7); // parse the animation name String animationName = animationProperties.getValue("Animation Name"); if (animationName == null) { return(null); } // parse the animation type SpriteAnimationType animationType = parseType(animationProperties.getValue("Animation Type")); if (animationType == SpriteAnimationType.Invalid) { return(null); } // parse the duration of the animation float duration; try { duration = float.Parse(animationProperties.getValue("Animation Duration")); } catch (Exception) { return(null); } if (duration <= 0) { return(null); } // parse the number of frames in the animation int numberOfFrames; try { numberOfFrames = int.Parse(animationProperties.getValue("Number of Frames")); } catch (Exception) { return(null); } if (numberOfFrames <= 0) { return(null); } // get the sprite sheet name String spriteSheetName = animationProperties.getValue("SpriteSheet Name"); if (spriteSheetName == null) { return(null); } // get the sprite name String spriteName = animationProperties.getValue("Sprite Name"); if (spriteName == null) { return(null); } string startIndex = animationProperties.getValue("Start Index"); int iStartIndex = 1; if (startIndex != null) { iStartIndex = int.Parse(startIndex); } animation = new SpriteAnimation(animationName, duration, animationType); // get the sprite sheet which contains the animation SpriteSheet spriteSheet = spriteSheets.getSpriteSheet(spriteSheetName); if (spriteSheet == null) { return(null); } // obtain and add the sprites to the sprite animation Sprite sprite; for (int i = 0; i < numberOfFrames; i++) { sprite = spriteSheet.getSprite(spriteName + " " + (i + iStartIndex)); if (sprite == null) { return(null); } animation.addSprite(sprite); } return(animation); }