const core = @import("core"); const net = @import("net.zig"); const std = @import("std"); pub const EnetTransport = @import("enet/EnetTransport.zig"); pub fn net_log(comptime fmt: []const u8, args: anytype) void { core.printInner(core.defaultHighlight, "[NET ]: " ++ fmt ++ "\n", args); } pub fn net_logs(comptime fmt: []const u8) void { core.printInner(core.defaultHighlight, "[NET ]: " ++ fmt ++ "\n", .{}); } pub fn net_err(comptime fmt: []const u8, args: anytype) void { core.printInner(core.errorHighlight, "[NET ]: ERROR!! " ++ fmt ++ "\n", args); } pub fn net_errs(comptime fmt: []const u8) void { core.printInner(core.errorHighlight, "[NET ]: ERROR!! " ++ fmt ++ "\n", .{}); } pub const TransportRef = core.Reference(net.TransportInterface); pub const NetEngine = struct { pub var NeonObjectTable: core.EngineObjectVTable = core.EngineObjectVTable.from(@This(), "net.NetEngine"); allocator: std.mem.Allocator, arena: std.heap.ArenaAllocator, transport: ?TransportRef = null, pub fn init(allocator: std.mem.Allocator) !*@This() { const self = try allocator.create(@This()); self.* = @This(){ .allocator = allocator, .arena = std.heap.ArenaAllocator.init(allocator), }; return self; } pub fn arenaAllocator(self: *@This()) std.mem.Allocator { return self.arena.allocator(); } pub fn initalizeTransport(self: *@This(), comptime T: type) !TransportRef { const transport = try core.createObject(T, .{ .can_tick = true }); const interface = TransportRef{ .ptr = transport, .vtable = T.TransportInterfaceVTable, }; self.transport = interface; net.log("transport initialized {s}", .{@typeName(T)}); return interface; } pub fn shutdown(self: *@This()) void { net_logs("NetEngine shutting down"); _ = self; } pub fn deinit(self: *@This()) void { self.arena.deinit(); self.allocator.destroy(self); } pub fn tick(self: *@This(), deltaTime: f64) void { _ = self; _ = deltaTime; // Network tick logic will go here } }; // responsibilities // // NetEngine // - all gameobjects and high level gameplay talks to this one, and queues up messages // // TransportInterface // - manages connection - startServer, stopServer, startClient, stopClient // -