220 likes | 315 Views
Learn how to create a starting room using a 2D array of integers for a 2D terrain. Each integer corresponds to a tile type, determining its appearance and passability. Discover how to extract the tile location from a spritesheet and draw the terrain effectively by traversing the array.
E N D
A Starting Room Is it just a background image?
A Starting Room(it’s just a 16x10 2D array of “ints”!) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 1 2 3 4 5 6 7 8 9
SpriteSheet 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 0 1 2 3 4 5 6 7 Note: each sprite is 16x16 pixels big, with a 1 pixel border (17x17)
SpriteSheet • Problem: what’s the location of the 61st tile? 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 0 1 2 3 4 5 6 7
SpriteSheet • 18 sprites wide by 8 sprites high 18 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 0 1 2 3 8 4 5 6 7
SpriteSheet srcX = tileType % 18; // 61 % 18 = 7 srcY = tileType / 18; // 61 / 18 = 3 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 0 1 2 3 4 5 6 7
SpriteSheet srcX = (tileType % 18) * TILE_SIZE; srcY = (tileType / 18) * TILE_SIZE; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 0 1 2 3 4 5 6 7
Drawing the Terrain srcRect tileType == 61 destRect
Drawing the Terrain srcRect tileType == 61 destRect
Drawing the Terrain srcRect tileType == 61 destRect
Drawing the Terrain srcRect tileType == 61 destRect
Drawing the Terrain srcRect tileType == 61 destRect
Drawing the Terrain srcRect tileType == 61 destRect
Drawing the Terrain srcRect tileType == 61 destRect
Drawing the Terrain srcRect tileType == 2 destRect
Drawing the Terrain srcRect tileType == 2 destRect
Drawing the Terrain srcRect tileType == 61 destRect
Drawing the Terrain srcRect tileType == 61 destRect
Terrain • Define a Room • use 2D Array of Tiles • stores the spritesheet • Each Tile • is passed a number to define its type • calculates its srcX and srcY from the spritesheet • has a “passable” attribute • To draw background, traverse the 2D Array