From e66908ca7159b584cd33a002fedf6a2fb8447e21 Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Sun, 4 Jun 2023 18:23:40 +0200 Subject: [PATCH] Refactored Level Components out for reuse and renamed it to TileMap. Refactored highlighting a single tile out. --- .gitignore | 1 + src/assets/images/tiles_dirt_32.png | Bin 3708 -> 2192 bytes src/assets/images/tiles_grass_32.png | Bin 2902 -> 3087 bytes src/game.zig | 1 + src/game/game.zig | 11 ++++ src/game/level.zig | 73 ++------------------------- src/game/tile_map.zig | 61 ++++++++++++++++++++++ src/game_scene.zig | 6 +-- 8 files changed, 80 insertions(+), 73 deletions(-) create mode 100644 src/game/tile_map.zig 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 71b71f438db960bb85715feea5bbdcf9c2de45c8..ab00f86d59d8633685a7980ef42389bce5fbb8b5 100644 GIT binary patch delta 2179 zcmV-}2z>Ya9FP%^B!6vbLqkw$V`BgSD3NuMJ4*vW6oo(I6OE+MLc}6P6pIK!LA29Y zqzNK1f>E2z<|XW|%Wi^*tzcsjun=v075)Ql1zQC{Py`V>dtZ&nddCO}5@(scA7|!V zX3l^wZs?}l6amvJI*C{`IGQv;Wx!jGBE`{)iao{ z;dBk_1;-Q~6b_k2N&h{Mx`{t)&CN>fmDz_&f*4VPq$#nCL6IS;mSk4n7y0o`vmzc{ z{x)kk;>c4FZ+}?Sl61{V&553s&tOgE{x?rIH!@u5?vFzAqvyT$L+dttl|AqC(DS~I zpz#6Dt*U=TavFOVy-hs1C=()EwNBfZ)W%_bCcsYU26m(rf`(h=l z-*t}fAh|A2bOpP6Fd+K_OWzLdXc3$%Ae{gJ010qNS$}f?02U+w02U+xn-#_s000Kb zNkl6he_`wjgC2}xTRNc&>M53!3Bu{&op z!_HD9Meapf$6R2&+$A}eG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yx>0N%KWS{DOgz)HP%B*BXJYtN-8jx1V? z*$ug##qlI(Jz{_uSAd5Q&%i^<5vrI1U*T;^)-7Qh2iBFs?3)shrN&H;)`&|bXTu8*AA7&AeX|}a&{Mc4R^Dc=aJRl+{NY2Ca{w4~F*eGJU~O|89-FdFowR^;^GVr# z;<+{rcw^8J1DM3?dm(I+YfTAA+E^aS3ju@x!4qvw))hev$+H1VLByy@d1zf=aT+yX z8PCtP7uhG@MaJfsBL>JDO~A=HxtD4@Ju66#6MWvlOKkHzBY)Sj=lu+-=qBLilM%R% z8KrFXHEB-gQKC8a%X&XKRzXt@unaSW5NNzmO+moo{STseNk7usq9mPIk0BFq>*g;H zti>??MGElVK<1lF`FGi;)kWnz&bZbM5I7VNbP4JGK(hf#A!}MsbYbQxrq%?0HU##J zW6NJeXc-x&iFA>V16~_*S~oyod{~TlEbkQwt_LpVIChQ5TdKPJeif zvELVJ-2fD)KhO(CEr9{lS$Q0r_X&77Ar7@_YQqH9VAEe5tt0}*P*N6_r8d@BR)&?# Z{~zNBiOW69S^xk5002ovPDHLkV1gk`8qxp& diff --git a/src/assets/images/tiles_grass_32.png b/src/assets/images/tiles_grass_32.png index 9ff3ce016d9b593bf27389cfc1c2f2847e744353..e2896cfcfdad47de6d4f796f2a0d9cb1e1eb7f3b 100644 GIT binary patch literal 3087 zcmb`J`9Bkm!mWMH&_O3nIr{#H7Ni%e?~CMK()hs31?H0l@h-LH z^@$=3COllzU>HR-u{F{QG~?!wAGV4!O4Ol;)Goz2Ou^m|&q*t70;j-To);_?pu<+B z^;7W&gbIwq28Y$2xw!Az6|`jABbm(0i^Zdvu#BVEGAhrIavckes!HzM3c-r<fJ~WaxhqrsruQkVaRte*H{I(81!aJ!eCqF9kmoYKK6`u!NmKjf#i^hRC7u-a^j zL^l}vg8w5>-1Dxxre+~(O!qgp*{qKn;|t?kREL9Z4<2hQuTdVd{OI~Yj*0wbHRtfsAyS2-3!@k z1Hj&lzG+-)o>Lw|;99|jcZ`pW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iE? zHTt@*646wl^10P%(Z7dFjRo}J{8C(N9V?dxIQ=_+QK=rQ!_Ij14Al8o$Xrhv!!Q4$ N*;+YTR+{@9`Ug-`;a~s& 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); } } };