/// <summary> /// Get texture rectangle for tile type /// </summary> /// <param name="level">Level object tile is bound to</param> /// <param name="tile">Tile needed texture rect for</param> /// <returns>Rectangle used to get texture data from sprite sheet</returns> private Rectangle GetRect(Level level, Tile tile) { int offsetX = 0; int offsetY = 0; // Calculate offset in sprite sheet for the tile type switch (tile.TileType) { case TileType.TILE_TYPE_WALL: { offsetX = 1; } break; case TileType.TILE_TYPE_STORAGE: { offsetX = 4; } break; case TileType.TILE_TYPE_BOX: { offsetX = 3; // Check if the box stands on a storage tile // then change to another type. if (level.IsTrigger((int)tile.PositionX, (int)tile.PositionY)) { offsetX = 2; } } break; case TileType.TILE_TYPE_START: { offsetX = 0; offsetY = 1; } break; case TileType.TILE_TYPE_FLOOR: { offsetX = 0; } break; case TileType.TILE_TYPE_NONE: default: // First sprite is the empty break; } // Create a rectangle for fetching texture data for this tile Rectangle rc = new Rectangle(offsetX * m_tileSpriteWidth, offsetY * m_tileSpriteHeight, m_tileSpriteWidth, m_tileSpriteHeight); return rc; }
/// <summary> /// Draw the level tiles /// </summary> /// <param name="level"></param> public void DrawLevel(Level level, Player player) { // Calculate a new camera scale to fit the level to as much as possible of the screen // using the viewport height and level height as reference float newCameraScaleX = ((float)m_graphicsDevice.Viewport.Width) / ((float)(((float)level.Width + 1) * m_tileWidth * m_camera.Scale.X)); float newCameraScaleY = ((float)m_graphicsDevice.Viewport.Height) / ((float)(((float)level.Height + 1) * m_tileHeight * m_camera.Scale.Y)); float smallestScale = newCameraScaleX < newCameraScaleY ? newCameraScaleX : newCameraScaleY; smallestScale = smallestScale > 1 ? 1 : smallestScale; m_camera.SetScale(smallestScale, smallestScale); // Translate camera to center on the level float cameraX = (m_graphicsDevice.Viewport.Width / 2) - ((level.Width * m_tileWidth * m_camera.Scale.X) / 2); float cameraY = (m_graphicsDevice.Viewport.Height / 2) - ((level.Height * m_tileHeight * m_camera.Scale.Y) / 2); m_camera.Translate(cameraX, cameraY); // Bind sprite batcher m_spriteBatch.Begin( ); // Draw statical tiles List<Tile> staticTiles = level.StaticTiles; foreach (Tile tile in staticTiles) { DrawTile(level, tile); } // Draw moveable tiles List<Tile> moveableTiles = level.MoveableTiles; foreach (Tile tile in moveableTiles) { DrawTile(level, tile); } // Draw player tile DrawPlayer(level, player); // Unbind the sprite batcher m_spriteBatch.End( ); // Reset camera scale m_camera.SetScale(1, 1); }
/// <summary> /// Draw tile object /// </summary> /// <param name="level">Level object tile is bound to</param> /// <param name="tile">Tile object</param> private void DrawTile(Level level, Tile tile) { // Calculate the tile position Vector2 tilePosition = m_camera.GetTranslatedPosition(new Vector2((((float)tile.PositionX)) * m_tileWidth, (((float)tile.PositionY)) * m_tileHeight)); Vector2 tileSize = new Vector2((m_tileWidth * m_camera.Scale.X), (m_tileHeight * m_camera.Scale.Y)); tileSize.X += 0.99f; tileSize.Y += 0.99f; // Get the source rectangle for the tile sprite in tile sheet Rectangle srcRect = GetRect(level, tile); // Create rectangle and draw it Rectangle destRect = new Rectangle((int)tilePosition.X, (int)tilePosition.Y, (int)tileSize.X, (int)tileSize.Y); m_spriteBatch.Draw(m_tileSheet, destRect, srcRect, Color.White); }
public GameModel(Level level, IEventListener listener) { // Load our dummy level we use for debugging. m_level = level; // Create player object from player tile m_player = new Player(m_level.PlayerTile); // Set current time as start time m_startTime = DateTime.Now; // Save event listener m_listener = listener; }