diff --git a/.gitignore b/.gitignore index 74ee24f..d35ea3c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ zig-cache zig-out allegro.path +**/*~ diff --git a/src/assets/images/tiles_dirt_32.png b/src/assets/images/tiles_dirt_32.png index 71b71f4..ab00f86 100644 Binary files a/src/assets/images/tiles_dirt_32.png and b/src/assets/images/tiles_dirt_32.png differ diff --git a/src/assets/images/tiles_grass_32.png b/src/assets/images/tiles_grass_32.png index 9ff3ce0..e2896cf 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/game.zig b/src/game.zig index 3557b9e..425db38 100644 --- a/src/game.zig +++ b/src/game.zig @@ -1,3 +1,4 @@ pub const Animation = @import("game/animation.zig").Animation; pub const Game = @import("game/game.zig").Game; pub const Level = @import("game/level.zig").Level; +pub const TileMap = @import("game/tile_map.zig").TileMap; diff --git a/src/game/game.zig b/src/game/game.zig index 7329d41..f50d06c 100644 --- a/src/game/game.zig +++ b/src/game/game.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const allegro = @import("allegro"); const engine = @import("../engine.zig"); const Animation = @import("animation.zig").Animation; const Renderer = @import("../renderer.zig").Renderer; @@ -10,6 +11,8 @@ pub const Game = struct { Right, }; + pub const TileSize = 32; + level: Level, health: i64 = 4, @@ -58,6 +61,14 @@ pub const Game = struct { self.renderer.drawSpriteFrameV(spriteName, frame, view.x, view.y); } + pub fn debugHighlightTile(self: Game, position: engine.PointF) void { + const renderer = self.renderer; + const topLeft = self.tileP(position); + const topLeftScreen = renderer.viewport.viewToScreenP(topLeft); + const tileSizeScreen = TileSize * renderer.viewport.scale; + renderer.textures.get("opaque").?.drawTintedScaled(allegro.mapRgba(127, 127, 127, 127), topLeftScreen.x, topLeftScreen.y, tileSizeScreen, tileSizeScreen); + } + pub fn moveCharacter(self: *Game, distance: f32) void { self.playerPosition.x += distance; self.isPlayerWalking = true; diff --git a/src/game/level.zig b/src/game/level.zig index b6c688f..bd4cc5b 100644 --- a/src/game/level.zig +++ b/src/game/level.zig @@ -1,71 +1,8 @@ const std = @import("std"); const engine = @import("../engine.zig"); +const TileMap = @import("tile_map.zig").TileMap; pub const Level = struct { - pub fn Column(comptime Value: type) type { - return struct { - const Self = @This(); - - values: std.AutoHashMap(i64, Value), - - pub fn init(allocator: std.mem.Allocator) Self { - return Self{ - .values = std.AutoHashMap(i64, Value).init(allocator), - }; - } - - pub fn deinit(self: Self) void { - self.values.deinit(); - } - - pub fn get(self: Self, row: i64) ?Value { - return self.values.get(row); - } - - pub fn set(self: *Self, row: i64, value: Value) !void { - try self.values.put(row, value); - } - }; - } - - pub fn Components(comptime Value: type) type { - return struct { - const Self = @This(); - - allocator: std.mem.Allocator, - columns: std.ArrayList(Column(Value)), - - pub fn init(allocator: std.mem.Allocator) Self { - return Self{ - .allocator = allocator, - .columns = std.ArrayList(Column(Value)).init(allocator), - }; - } - - pub fn deinit(self: Self) void { - for (self.columns.items) |item| { - item.deinit(); - } - self.columns.deinit(); - } - - pub fn column(self: Self, i: i64) *Column(Value) { - return &self.columns.items[@intCast(usize, i)]; - } - - fn ensureColumns(self: *Self, n: usize) !void { - while (self.columns.items.len < n) { - try self.columns.append(Column(Value).init(self.allocator)); - } - } - - pub fn set(self: *Self, c: i64, r: i64, value: Value) !void { - try self.ensureColumns(@intCast(usize, c) + 1); - try self.column(c).set(r, value); - } - }; - } - pub const Tile = enum { Grass, }; @@ -75,8 +12,8 @@ pub const Level = struct { }; character: engine.Point, - tiles: Components(Tile), - collectables: Components(Collectable), + tiles: TileMap(Tile), + collectables: TileMap(Collectable), pub fn load(allocator: std.mem.Allocator, path: []const u8) !Level { const path_ = try std.fs.realpathAlloc(allocator, path); @@ -96,8 +33,8 @@ pub const Level = struct { _ = n; var character: ?engine.Point = null; - var tiles = Components(Tile).init(allocator); - var collectables = Components(Collectable).init(allocator); + var tiles = TileMap(Tile).init(allocator); + var collectables = TileMap(Collectable).init(allocator); var lines = std.mem.split(u8, data, "\n"); var y: i64 = 0; diff --git a/src/game/tile_map.zig b/src/game/tile_map.zig new file mode 100644 index 0000000..bca375d --- /dev/null +++ b/src/game/tile_map.zig @@ -0,0 +1,61 @@ +const std = @import("std"); + +pub fn TileMap(comptime Value: type) type { + return struct { + const Self = @This(); + + pub const Column = struct { + values: std.AutoHashMap(i64, Value), + + pub fn init(allocator: std.mem.Allocator) Column { + return Column{ + .values = std.AutoHashMap(i64, Value).init(allocator), + }; + } + + pub fn deinit(self: Column) void { + self.values.deinit(); + } + + pub fn get(self: Column, row: i64) ?Value { + return self.values.get(row); + } + + pub fn set(self: *Column, row: i64, value: Value) !void { + try self.values.put(row, value); + } + }; + + allocator: std.mem.Allocator, + columns: std.ArrayList(Column), + + pub fn init(allocator: std.mem.Allocator) Self { + return Self{ + .allocator = allocator, + .columns = std.ArrayList(Column).init(allocator), + }; + } + + pub fn deinit(self: Self) void { + for (self.columns.items) |item| { + item.deinit(); + } + self.columns.deinit(); + } + + pub fn column(self: Self, i: i64) *Column { + return &self.columns.items[@intCast(usize, i)]; + } + + pub fn ensureColumns(self: *Self, n: usize) !void { + while (self.columns.items.len < n) { + try self.columns.append(Column.init(self.allocator)); + } + } + + pub fn set(self: *Self, c: i64, r: i64, value: Value) !void { + try self.ensureColumns(@intCast(usize, c) + 1); + try self.column(c).set(r, value); + } + }; +} diff --git a/src/game_scene.zig b/src/game_scene.zig index 9d898d4..57bfbd4 100644 --- a/src/game_scene.zig +++ b/src/game_scene.zig @@ -108,11 +108,7 @@ pub const GameScene = struct { if (ctx.showDebug) { ctx.renderer.printTextV("debug", ctx.palette.background.text, 0.01, 0.1, Renderer.TextAlignment.Left, "Character: ({d}, {d})", .{ self.game.playerPosition.x, self.game.playerPosition.y }); ctx.renderer.printTextV("debug", ctx.palette.background.text, 0.01, 0.15, Renderer.TextAlignment.Left, "Tiles: ({d}, {d}) -> ({d}, {d})", .{ tileBounds.min.x, tileBounds.min.y, tileBounds.max.x, tileBounds.max.y }); - - // const characterTopLeft = self.game.tileP(self.game.playerPosition); - // const characterTopLeftScreen = renderer.viewport.viewToScreenP(characterTopLeft); - // const tileWidthScreen = 32 * viewport.scale; - // renderer.textures.get("opaque").?.drawTintedScaled(allegro.mapRgba(127, 127, 127, 127), characterTopLeftScreen.x, characterTopLeftScreen.y, tileWidthScreen, tileWidthScreen); + // self.game.debugHighlightTile(self.game.playerPosition); } } };