diff --git a/src/assets/images/tiles_grass_32.png b/src/assets/images/tiles_grass_32.png index e2896cf..79f8b83 100644 Binary files a/src/assets/images/tiles_grass_32.png and b/src/assets/images/tiles_grass_32.png differ diff --git a/src/context.zig b/src/context.zig index 34937e4..36eae93 100644 --- a/src/context.zig +++ b/src/context.zig @@ -14,7 +14,7 @@ pub const Context = struct { palette: Palette = undefined, shouldQuit: bool = false, showFPS: bool = true, - showDebug: bool = true, + showDebug: bool = false, scene: ?Scene = null, events: allegro.EventQueue, diff --git a/src/engine/assets.zig b/src/engine/assets.zig index b7863cf..6e69092 100644 --- a/src/engine/assets.zig +++ b/src/engine/assets.zig @@ -114,7 +114,7 @@ pub const Sprite = struct { var i: i32 = 0; while (i < n) : (i += 1) { const left = @mod(i, horizontal) * width; - const top = @divTrunc(i, horizontal) * width; + const top = @divTrunc(i, horizontal) * height; frames[@intCast(usize, i)] = try bitmap.sub(left, top, width, height); } diff --git a/src/game/tile_map.zig b/src/game/tile_map.zig index dd54559..9a8fc21 100644 --- a/src/game/tile_map.zig +++ b/src/game/tile_map.zig @@ -27,6 +27,22 @@ pub fn TileMap(comptime Value: type) type { } }; + pub const Ordinals = struct { + left: bool, + top: bool, + right: bool, + bottom: bool, + + pub fn count(self: Ordinals) usize { + var n: usize = 0; + if (self.left) n += 1; + if (self.top) n += 1; + if (self.right) n += 1; + if (self.bottom) n += 1; + return n; + } + }; + allocator: std.mem.Allocator, columns: std.AutoHashMap(i64, Column), @@ -64,6 +80,24 @@ pub fn TileMap(comptime Value: type) type { return null; } + pub fn getOrdinals(self: Self, x: i64, y: i64, ofType: Value) Ordinals { + return Ordinals{ + .left = self.isOfType(x - 1, y, ofType), + .top = self.isOfType(x, y - 1, ofType), + .right = self.isOfType(x + 1, y, ofType), + .bottom = self.isOfType(x, y + 1, ofType), + }; + } + + pub fn isOfType(self: Self, x: i64, y: i64, ofType: Value) bool { + if (self.get(x, y)) |value| { + if (value == ofType) { + return true; + } + } + return false; + } + pub fn set(self: *Self, x: i64, y: i64, value: Value) !void { const c = try self.ensureColumn(x); try c.set(y, value); diff --git a/src/game_scene.zig b/src/game_scene.zig index 2c3506a..f28e309 100644 --- a/src/game_scene.zig +++ b/src/game_scene.zig @@ -50,11 +50,7 @@ pub const GameScene = struct { const renderer = ctx.renderer; const viewport = renderer.viewport; - const center = viewport.center(); - _ = center; const bounds = viewport.bounds; - const scale = viewport.scale; - _ = scale; const background = renderer.textures.get("background_jungle").?; const backgroundDisplacementFactor = -0.01; @@ -66,30 +62,46 @@ pub const GameScene = struct { const tileBounds = self.game.tilesInView(); + const tiles = self.game.level.tiles; + const collectables = self.game.level.collectables; + var x = tileBounds.min.x; while (x < tileBounds.max.x) : (x += 1) { const randomDirtOffset = self.game.randomTileOffset(x, 99, 3); self.game.drawSpriteFrame("tiles_dirt_32", 0 + randomDirtOffset, x, 19); self.game.drawSpriteFrame("tiles_dirt_32", 3 + randomDirtOffset, x, 20); - if (x < 0) continue; - - const tiles = self.game.level.tiles.ensureColumn(x) catch continue; - const collectables = self.game.level.collectables.ensureColumn(x) catch continue; var y = tileBounds.min.y; - while (y < tileBounds.max.y) : (y += 1) { - if (tiles.get(y)) |tile| { + while (y <= tileBounds.max.y) : (y += 1) { + if (tiles.get(x, y)) |tile| { switch (tile) { game.Level.Tile.Grass => { - const randomOffset = self.game.randomTileOffset(x, y, 2); - self.game.drawSpriteFrame("tiles_grass_32", 1 + randomOffset, x, y - 1); - self.game.drawSpriteFrame("tiles_grass_32", 5 + randomOffset, x, y); + const ordinals = tiles.getOrdinals(x, y, game.Level.Tile.Grass); + var offset: usize = 17; + switch (ordinals.count()) { + 1 => { + if (ordinals.right) offset = 0; + if (ordinals.left) offset = 3; + }, + 2 => { + if (ordinals.right and ordinals.left) offset = 1 + self.game.randomTileOffset(x, y, 2); + if (ordinals.top and ordinals.bottom) offset = 12; + if (ordinals.right and ordinals.bottom) offset = 4; + if (ordinals.left and ordinals.bottom) offset = 5; + }, + 3 => { + if (!ordinals.left) offset = 6; + if (!ordinals.right) offset = 7; + }, + else => {}, + } + self.game.drawSpriteFrame("tiles_grass_32", offset, x, y - 1); }, // else => {}, } } - if (collectables.get(y)) |collectable| { + if (collectables.get(x, y)) |collectable| { switch (collectable) { game.Level.Collectable.Star => { const distanceToPlayer = engine.Point.init(x, y).float().distance(self.game.playerPosition); diff --git a/src/main.zig b/src/main.zig index 384f0ad..eec7b83 100644 --- a/src/main.zig +++ b/src/main.zig @@ -50,7 +50,7 @@ pub fn main() !void { try renderer.sprites.addFromTextures(renderer.textures, "character_lion_48", 48, 48); try renderer.sprites.addFromTextures(renderer.textures, "item_star_32", 32, 32); try renderer.sprites.addFromTextures(renderer.textures, "tiles_dirt_32", 32, 32); - try renderer.sprites.addFromTextures(renderer.textures, "tiles_grass_32", 32, 32); + try renderer.sprites.addFromTextures(renderer.textures, "tiles_grass_32", 32, 64); allegro.convertMemoryBitmaps();