This commit is contained in:
Vicente Ferrari Smith 2026-01-06 16:36:11 +01:00
parent 5dad7572b7
commit 01144b2bd4
3 changed files with 59 additions and 0 deletions

26
src/client/client.zig Normal file
View File

@ -0,0 +1,26 @@
const std = @import("std");
const ConnectState = union(enum) {
disconnected,
connecting,
connected: std.net.Stream,
err: anyerror,
};
const Client = struct {
state: ConnectState = .disconnected,
pub fn startConnect(self: *Client, addr: std.net.Address) void {
if (self.state == .connecting or self.state == .connected)
return;
self.state = .connecting;
const stream = std.net.tcpConnectToAddress(addr) catch |err| {
self.state = .{ .err = err };
return;
};
self.state = .{ .connected = stream };
}
};

30
src/server/chunk.zig Normal file
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();
}

3
src/server/server.zig Normal file
View File

@ -0,0 +1,3 @@
const shared = @import("shared");
pub var next_entity_id: shared.entity.entity_id = 1;