Backlog/extras/gameExtras/src/objectSpawner.zig

61 lines
1.7 KiB
Zig

allocator: std.mem.Allocator,
objectList: *core.GameObjectList,
windowOpen: bool = true,
spawnFuncs: std.ArrayListUnmanaged(SpawnEntry) = .{},
spawnPosition: core.Vectorf = .{},
pub const SpawnEntry = struct {
typeName: []const u8,
spawnFunc: core.GameObjectList.SpawnFunc,
};
pub fn create(allocator: std.mem.Allocator) !*@This() {
const self = try allocator.create(@This());
self.* = .{
.allocator = allocator,
.objectList = try core.GameObjectList.create(allocator),
};
return self;
}
pub fn tick(self: *@This(), dt: f64) void {
self.objectList.tick(dt);
if (self.windowOpen) {
if (ig.begin("object Spawner", null, .{})) {
for (self.spawnFuncs.items) |entry| {
if (ig.smallButton(entry.typeName.ptr)) {
if (entry.spawnFunc(self.objectList)) |new| {
const entity = new.vtable.getEntity.?(new.ptr);
self.prepareEntity(entity);
}
}
}
ig.end();
}
}
}
pub fn addSpawnFunction(self: *@This(), comptime name: []const u8, comptime T: type) !void {
try self.spawnFuncs.append(self.allocator, .{ .typeName = name, .spawnFunc = core.GameObjectList.spawnObjectFunction(T) });
}
pub fn prepareEntity(self: *@This(), entity: core.Entity) void {
if (entity.fetch(core.Scene)) |scene| {
scene.setPosition(self.spawnPosition);
}
}
pub fn destroy(self: *@This()) void {
self.spawnFuncs.deinit(self.allocator);
self.objectList.destroy();
self.allocator.destroy(self);
}
const std = @import("std");
const backlog = @import("Backlog");
const core = backlog.core;
const rend = backlog.rend;
const ig = backlog.imgui.api;