public TileGrid(int tileWidth,int tileHeight,int numXTiles,int numYTiles, Vector2 offset,SpriteSheet tileSheet, GraphicsDeviceManager graphicsComponent) { if (graphicsComponent == null) { throw new ArgumentNullException ("graphicsComponent"); } graphics = graphicsComponent; sheet = tileSheet; width = numXTiles; height = numYTiles; cellWidth = tileWidth; cellHeight = tileHeight; worldOffset = offset; visibleTiles = new Rectangle (0, 0, width, height); grid = new int[width][]; for (int i = 0; i < width; i++) { grid [i] = new int[height]; for (int j = 0; j < height; j++) { grid [i] [j] = 0; } } scaleValue = Vector2.One; zoomValue = 1.0f; CameraPosition = Vector2.Zero; graphicsComponent.DeviceReset += new EventHandler (OnGraphicsComponentDeviceReset); OnGraphicsComponentDeviceReset (this, new EventArgs ()); }
public TileGrid (int tileWidth,int tileHeight,int numXTiles,int numYTiles, Vector2 offset,SpriteSheet tileSheet, GraphicsDeviceManager graphicsComponent) { if (graphicsComponent == null) { throw new ArgumentNullException ("graphicsComponent"); } graphics = graphicsComponent; sheet = tileSheet; width = numXTiles; height = numYTiles; cellWidth = tileWidth; cellHeight = tileHeight; worldOffset = offset; visibleTiles = new Rectangle (0, 0, width, height); grid = new int[width][]; for (int i = 0; i < width; i++) { grid [i] = new int[height]; for (int j = 0; j < height; j++) { grid [i] [j] = 0; } } scaleValue = Vector2.One; zoomValue = 1.0f; CameraPosition = Vector2.Zero; graphicsComponent.DeviceReset += delegate(object sender, EventArgs e) { displaySize.X = 320; // TODO graphics.GraphicsDevice.PresentationParameters.BackBufferWidth; displaySize.Y = 480; // TODO graphics.GraphicsDevice.PresentationParameters.BackBufferHeight; visibilityChanged = true; }; // graphicsComponent.DeviceReset += // new EventHandler (OnGraphicsComponentDeviceReset); // // OnGraphicsComponentDeviceReset (this, new EventArgs ()); // } // void OnGraphicsComponentDeviceReset (object sender, EventArgs e) // { // displaySize.X = 320; // // TODO graphics.GraphicsDevice.PresentationParameters.BackBufferWidth; // // displaySize.Y = 480; // // TODO graphics.GraphicsDevice.PresentationParameters.BackBufferHeight; // // visibilityChanged = true; // } }
public AnimatedSprite(SpriteSheet spriteSheet, int frameWidth, int frameHeight, int padding, int rows, int columns, Point startFrame, int frames) { if (spriteSheet == null) { throw new ArgumentNullException("spriteSheet"); } int spriteAreaHeight = (frameHeight + padding) * rows - padding; int spriteAreaWidth = (frameWidth + padding) * columns - padding; //first, make sure the sheet is possible if ((spriteAreaWidth > spriteSheet.Texture.Width) || (spriteAreaHeight > spriteSheet.Texture.Height)) { throw new ArgumentException( "The layout specified is too large for the SpriteSheet." ); } sheet = spriteSheet; numFrames = frames; int startFrameIndex = startFrame.Y * columns + startFrame.X; //now auto-generate the animation data, //left to right, top to bottom. int frameIndex = 0; for (int i = startFrameIndex; i < (numFrames+startFrameIndex); i++) { int x =(i % columns); int y = (i / columns); int left = ( x * (frameWidth+ padding)); int top = (y * (frameHeight + padding)); top = top % spriteAreaHeight; sheet.AddSourceSprite(frameIndex, new Rectangle(left, top, frameWidth, frameHeight)); frameIndex++; } }
/// <summary> /// Load the graphics content. /// </summary> protected override void LoadContent() { //When the backbuffer resolution changes, this part of the //LoadContent calback is used to reset the screen center screenCenter = new Vector2( (float)graphics.GraphicsDevice.Viewport.Width / 2f, (float)graphics.GraphicsDevice.Viewport.Height / 2f); #region Set up Graphics resources Texture2D groundTexture = Content.Load<Texture2D>("ground"); Texture2D cloudTexture = Content.Load<Texture2D>("clouds"); spriteBatch = new SpriteBatch(graphics.GraphicsDevice); #endregion #region Set Up Tile Sources //set up the tile sheets with source rectangles //for each of the different sprites cloudSheet = new SpriteSheet(cloudTexture); cloudSheet.AddSourceSprite((int)TileName.Clouds, new Rectangle(0, 0, 1024, 1024)); groundSheet = new SpriteSheet(groundTexture); groundSheet.AddSourceSprite((int)TileName.Base, new Rectangle(0, 0, 510, 510)); groundSheet.AddSourceSprite((int)TileName.Detail1, new Rectangle(514, 0, 255, 255)); groundSheet.AddSourceSprite((int)TileName.Detail2, new Rectangle(769, 0, 255, 255)); groundSheet.AddSourceSprite((int)TileName.Detail3, new Rectangle(514, 256, 255, 255)); groundSheet.AddSourceSprite((int)TileName.Detail4, new Rectangle(769, 256, 255, 255)); groundSheet.AddSourceSprite((int)TileName.SoftDetail1, new Rectangle(514, 514, 255, 255)); groundSheet.AddSourceSprite((int)TileName.SoftDetail2, new Rectangle(769, 514, 255, 255)); groundSheet.AddSourceSprite((int)TileName.SoftDetail3, new Rectangle(514, 769, 255, 255)); groundSheet.AddSourceSprite((int)TileName.SoftDetail4, new Rectangle(769, 769, 255, 255)); groundSheet.AddSourceSprite((int)TileName.Rocks1, new Rectangle(0, 514, 255, 255)); groundSheet.AddSourceSprite((int)TileName.Rocks2, new Rectangle(256, 514, 255, 255)); groundSheet.AddSourceSprite((int)TileName.Rocks3, new Rectangle(0, 769, 255, 255)); groundSheet.AddSourceSprite((int)TileName.Rocks4, new Rectangle(256, 769, 255, 255)); #endregion #region Setup Tile Grids //Create the ground layer tile groundLayer = new TileGrid(510, 510, numTiles, numTiles, Vector2.Zero, groundSheet, graphics); //calculate the number of detial tiles, which are //half the size of the base tiles, so there are //twice as many (minus one since they are being offset) int numDetailTiles = (numTiles * 2 - 1); //add an offset to break up the pattern detailLayer = new TileGrid(255, 255, numDetailTiles, numDetailTiles, new Vector2(127, 127), groundSheet, graphics); rockLayer = new TileGrid(255, 255, numDetailTiles, numDetailTiles, new Vector2(0, 0), groundSheet, graphics); int numCloudTiles = numTiles / 6 + 1; cloudLayer = new TileGrid(1024, 1024, numCloudTiles, numCloudTiles, Vector2.Zero, cloudSheet, graphics); //These loops fill the datas with some appropriate data. //The clouds and ground clutter have been randomized. for (int i = 0; i < numTiles; i++) { for (int j = 0; j < numTiles; j++) { groundLayer.SetTile(i, j, 1); } } for (int i = 0; i < numDetailTiles; i++) { for (int j = 0; j < numDetailTiles; j++) { switch (rand.Next(20)) { case 0: detailLayer.SetTile(i, j, (int)TileName.Detail1); break; case 1: detailLayer.SetTile(i, j, (int)TileName.Detail2); break; case 2: detailLayer.SetTile(i, j, (int)TileName.Detail3); break; case 3: detailLayer.SetTile(i, j, (int)TileName.Detail4); break; case 4: case 5: detailLayer.SetTile(i, j, (int)TileName.SoftDetail1); break; case 6: case 7: detailLayer.SetTile(i, j, (int)TileName.SoftDetail2); break; case 8: case 9: detailLayer.SetTile(i, j, (int)TileName.SoftDetail3); break; case 10: case 11: detailLayer.SetTile(i, j, (int)TileName.SoftDetail4); break; } } } for (int i = 0; i < numDetailTiles; i++) { for (int j = 0; j < numDetailTiles; j++) { switch (rand.Next(25)) { case 0: rockLayer.SetTile(i, j, (int)TileName.Rocks1); break; case 1: rockLayer.SetTile(i, j, (int)TileName.Rocks2); break; case 2: rockLayer.SetTile(i, j, (int)TileName.Rocks3); break; case 3: case 4: rockLayer.SetTile(i, j, (int)TileName.Rocks4); break; } } } for (int i = 0; i < numCloudTiles; i++) { for (int j = 0; j < numCloudTiles; j++) { cloudLayer.SetTile(i, j, (int)TileName.Clouds); } } #endregion // Set up AnimatedSprite animatedSpriteSheet = new SpriteSheet(Content.Load<Texture2D>("ball")); animatedSprite = new AnimatedSprite(animatedSpriteSheet, 254, 254, 1, 4, 4, new Point(1, 0), 15); // Set Up a 2D Camera camera = new Camera2D(); ResetToInitialPositions(); //assuming a resolution change, need to update the sprite's "position" animatedSprite.Origin = camera.Position - animatedSpritePosition; animatedSprite.Position = screenCenter; }