This commit is contained in:
vfs committed 2026-01-06 16:36:11 +01:00
1 parent 5dad7572b7
commit 01144b2bd4
3 files changed
+59

No files matched your search

+30
View File
@@ -0,0 +1,30 @@
const std = @import("std");
const shared = @import("shared");
const server = @import("server.zig");
pub fn spawn(chunk: *shared.chunk.Chunk(), comptime T: type, allocator: std.mem.Allocator, value: T, w: *std.Io.Writer) !void {
const id = server.next_entity_id;
server.next_entity_id += 1;
var entity = value;
entity.id = id;
inline for (@typeInfo(shared.chunk.Chunk()).@"struct".fields) |field| {
if (field.type == shared.chunk.Storage(T)) {
try @field(chunk, field.name).items.append(allocator, entity);
return;
}
}
// serialize entity
var buffer: [64]u8 = undefined;
var fbs = std.io.fixedBufferStream(&buffer);
try T.encode(entity, fbs.writer());
try shared.protocol.write_message(
w,
.spawn_entity,
fbs.getWritten(),
);
try w.flush();
}