summaryrefslogtreecommitdiff
path: root/src/context.zig
blob: 0bffbd4bd9063ccfd59e979368d922e1c30bafe8 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const std = @import("std");
const rl = @cImport(@cInclude("raylib.h"));
const root = @import("root.zig");
const Animation = @import("Animation.zig");

pub var camera: rl.Camera2D = undefined;
pub var grid: []root.MyRect = undefined;

pub const Grid = struct {
    buffer: std.MultiArrayList(root.Hex),
    cursor: usize,
    size: usize,

    const GridError = error{
        OutOfBounds,
    };

    //TODO think about what size really means
    pub fn init(
        allocator: std.mem.Allocator,
        size: usize,
    ) !Grid {
        var buffer: std.MultiArrayList(root.Hex) = .{};
        try buffer.ensureTotalCapacity(allocator, size * size);
        return .{
            .buffer = buffer,
            .cursor = 0,
            .size = size,
        };
    }

    // should only be used when standing up the buffer, afterwards should use q, r indexing
    pub fn initPush(self: *Grid, hex: root.Hex) GridError!void {
        if (self.cursor >= self.size * 2) {
            return GridError.OutOfBounds;
        }
        self.buffer.insertAssumeCapacity(self.cursor, hex);
    }

    pub fn set(self: *Grid, hex: root.Hex) GridError!void {
        const idx = hex.qr[1] * self.size + hex.qr[0];
        if (idx >= self.size * 2) {
            return GridError.OutOfBounds;
        }

        self.buffer.set(idx, hex);
    }
};
pub var hex_grid: Grid = undefined;
pub var hovered_coords: root.HexCoord = root.HexCoord{ .q = 0, .r = 0 };
pub var hovered_handle: ?usize = null;

pub const Dude = struct {
    world_coords: rl.Vector2,
    hex_coords: root.HexCoord,
    target_coords: root.HexCoord,
    animation: Animation,
};
pub var main_dude: Dude = undefined;

var Gpa = std.heap.GeneralPurposeAllocator(.{}){};
pub var gpa: std.mem.Allocator = Gpa.allocator();