public void Initialize(SpriteSheetCollection spriteSheets) { SpriteSheet spriteSheet = spriteSheets.getSpriteSheet("Tree"); if (spriteSheet == null) { return; } for (int i = 1; i < 9; i += 1) { Sprite sprite = spriteSheet.getSprite(string.Concat("Tree ", i.ToString())); if (sprite == null) { return; } //chris edited this sprite.m_SpriteDepth = 0.49f; m_lTreeSprites.Add(sprite); } for (int i = 0; i < TOTAL_STARTING_TREES; i++) { Vector2 pos = new Vector2(Firecracker.random.Next(0, Firecracker.level.dimensions.X * Firecracker.level.gridSize), Firecracker.random.Next(0, Firecracker.level.dimensions.Y * Firecracker.level.gridSize)); if (Terrain.Instance == null || Terrain.Instance.isPositionWalkable(pos)) { int RandomIndex = Firecracker.random.Next(0, 4); StaticObject newTree = new StaticObject(pos, m_lTreeSprites[RandomIndex]); if (newTree != null) { Firecracker.level.addObject(newTree); m_lForestList.Add(newTree); } } } }
// add a collection of frames to the animation from a sprite sheet public void addSprites(SpriteSheet spriteSheet) { if (spriteSheet == null) { return; } for (int i = 0; i < spriteSheet.size(); i++) { addSprite(spriteSheet.getSprite(i)); } }
public Asteroid(Vector2 TargetPosition) : base() { m_vTargetPosition = TargetPosition; position = m_vTargetPosition + new Vector2(400, -500); SpriteSheet spriteSheet = Firecracker.spriteSheets.getSpriteSheet("Asteroid"); if (spriteSheet != null) { Sprite sprite = spriteSheet.getSprite("Asteroid"); if (sprite != null) { m_sprite = sprite; m_sprite.m_SpriteDepth = 0.1f; } } }
public SpiderFire(Vector2 thePos) : base() { m_lFireObjects = new List <FireNode>(); m_position = thePos; SpriteSheet spriteSheet = Firecracker.spriteSheets.getSpriteSheet("ExplosionObject"); if (spriteSheet != null) { Sprite sprite = spriteSheet.getSprite("Explosion 3"); if (sprite != null) { m_sprite = sprite; for (int i = 0; i < 4; i++) { CreateNewNode(position, 5); } } } }
// 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 GameTile parseFrom(StreamReader input, SpriteSheetCollection spriteSheets) { if (input == null || spriteSheets == null) { return(null); } // get the object's position Variable position = Variable.parseFrom(input.ReadLine()); if (position == null || !position.id.Equals("Position", StringComparison.OrdinalIgnoreCase)) { return(null); } // Get the layer depth of this sprite Variable layerDepth = Variable.parseFrom(input.ReadLine()); if (layerDepth == null || !layerDepth.id.Equals("LayerDepth", StringComparison.OrdinalIgnoreCase)) { return(null); } // get the sprite's name Variable spriteName = Variable.parseFrom(input.ReadLine()); if (spriteName == null || !spriteName.id.Equals("Sprite Name", StringComparison.OrdinalIgnoreCase)) { return(null); } // get the name of the spritesheet in which the sprite is found Variable spriteSheetName = Variable.parseFrom(input.ReadLine()); if (spriteSheetName == null || !spriteSheetName.id.Equals("SpriteSheet Name", StringComparison.OrdinalIgnoreCase)) { return(null); } // get the object's sprite SpriteSheet spriteSheet = spriteSheets.getSpriteSheet(spriteSheetName.value); if (spriteSheet == null) { return(null); } Sprite sprite = spriteSheet.getSprite(spriteName.value); if (sprite == null) { return(null); } // parse the sprite's position String[] positionData = position.value.Split(','); if (positionData.Length != 2) { return(null); } Vector2 newPosition; try { newPosition.X = Int32.Parse(positionData[0]); newPosition.Y = Int32.Parse(positionData[1]); } catch (Exception) { return(null); } // create the object GameTile newObject = new GameTile(newPosition, sprite); newObject.sprite.m_SpriteDepth = float.Parse(layerDepth.value); newObject.updateInitialValues(); return(newObject); }
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); }