diff options
Diffstat (limited to 'src/map.zig')
| -rw-r--r-- | src/map.zig | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/map.zig b/src/map.zig new file mode 100644 index 0000000..d9b4496 --- /dev/null +++ b/src/map.zig @@ -0,0 +1,42 @@ +const std = @import("std"); + +// Let's pretend that our units are meters +pub const MAX_HEIGHT: f32 = 2.5; + +pub const Cell = struct { + height: f32, + wall_texture: u8 = 0, + floor_texture: u8 = 0, + ceiling_texture: u8 = 0, + + const floor = Cell{ .height = 0 }; +}; + +pub const Map = struct { + width: u32, + height: u32, + size: f32 = 64, + cells: std.ArrayList(Cell), + + pub fn deinit(self: Map) void { + self.cells.deinit(); + } + + pub fn new(width: u32, height: u32, alloc: *std.mem.Allocator) !Map { + var cells = std.ArrayList(Cell).init(alloc); + try cells.ensureTotalCapacity(width * height); + try cells.appendNTimes(Cell.floor, width * height); + + return Map{ .width = width, .height = height, .cells = cells }; + } + + pub fn inBounds(self: Map, x: i32, y: i32) bool { + return (x < self.width and x >= 0 and y < self.height and y >= 0); + } + + pub fn lookup(self: Map, x: i32, y: i32) Cell { + // live dangerously, no bounds check + std.debug.assert(x >= 0 and y >= 0); // compiler hint? + return self.cells.items[@intCast(u32, y) * self.width + @intCast(u32, x)]; + } +}; |
