aboutsummaryrefslogtreecommitdiff
path: root/src/ecs/entity.zig
blob: 2ad85564a61229a0bd1b7605035211bda76ccbfd (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
const std = @import("std");

pub const Entity = struct {
    id: usize,
};

pub const EntityCollection = struct {
    allocator: std.mem.Allocator,
    next_id: usize,
    entities: std.AutoHashMap(usize, Entity),

    pub fn addEntity(self: *EntityCollection) !usize {
        try self.entities.put(self.next_id, .{
            .id = self.next_id,
        });

        self.next_id += 1;

        return self.next_id - 1;
    }

    pub fn init(allocator: std.mem.Allocator) EntityCollection {
        return .{
            .allocator = allocator,
            .next_id = 0,
            .entities = std.AutoHashMap(usize, Entity).init(allocator),
        };
    }

    pub fn deinit(self: *EntityCollection) void {
        self.entities.deinit();
    }
};

test "create entities" {
    try std.testing.expect(true);

    var ec = EntityCollection.init(std.testing.allocator);
    defer ec.deinit();

    for (0..128) |i| {
        const id = ec.addEntity();
        try std.testing.expectEqual(i, id);
    }
}