aboutsummaryrefslogtreecommitdiff
path: root/src/map.zig
blob: d9b44964580432f426ad95e231b0eb86140054b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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)];
    }
};