31 lines
863 B
Zig
31 lines
863 B
Zig
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();
|
|
}
|