serialization getting better maybe??
This commit is contained in:
1 parent
da8589a700
commit
b686052636
7 files changed
+131
-88
No files matched your search
+69
-14
@@ -1,6 +1,8 @@
|
||||
const std = @import("std");
|
||||
const entity = @import("entity.zig");
|
||||
const zm = @import("zmath");
|
||||
const bufzilla = @import("bufzilla");
|
||||
|
||||
const entity = @import("entity.zig");
|
||||
|
||||
pub const SERVER_PORT: u16 = 1337;
|
||||
|
||||
@@ -11,7 +13,7 @@ pub const MessageType = enum(u8) {
|
||||
|
||||
pub const SpawnEntity = struct {
|
||||
id: entity.entity_id,
|
||||
kind: EntityKind,
|
||||
kind: entity.EntityKind,
|
||||
payload: []const u8,
|
||||
};
|
||||
|
||||
@@ -39,23 +41,76 @@ pub fn read_message(reader: *std.Io.Reader, allocator: std.mem.Allocator) !struc
|
||||
};
|
||||
}
|
||||
|
||||
pub const EntityKind = enum(u8) {
|
||||
Player,
|
||||
Monster,
|
||||
Projectile,
|
||||
pub const Elf_v1 = struct {
|
||||
id: entity.id = entity.INVALID_ID,
|
||||
pos: zm.Vec,
|
||||
hp: i32,
|
||||
|
||||
pub fn init(elf: *entity.Elf) Elf_v1 {
|
||||
return .{
|
||||
.id = elf.id,
|
||||
.pos = elf.pos,
|
||||
.hp = elf.hp,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn encode(self: Elf_v1, w: bufzilla.Writer) !void {
|
||||
try w.write(self.id, entity.id);
|
||||
//try w.write(self.pos, zm.Vec);
|
||||
try w.write(self.hp, i32);
|
||||
}
|
||||
|
||||
pub fn decode(r: *std.Io.Reader) !Elf_v1 {
|
||||
return .{
|
||||
.id = try r.readInt(u64, .little),
|
||||
.pos = try readVec4(r),
|
||||
.vel = try readVec4(r),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
pub const Hello = struct {
|
||||
msg: []const u8,
|
||||
pub const Monster_v1 = struct {
|
||||
id: entity.id = entity.INVALID_ID,
|
||||
pos: zm.Vec,
|
||||
hp: i32,
|
||||
|
||||
pub fn encode(self: Monster_v1, w: *std.Io.Writer) !void {
|
||||
try w.writeInt(u64, self.id, .little);
|
||||
try writeVec4(w, self.pos);
|
||||
try writeVec4(w, self.vel);
|
||||
}
|
||||
|
||||
pub fn decode(r: *std.Io.Reader) !Monster_v1 {
|
||||
return .{
|
||||
.id = try r.readInt(u64, .little),
|
||||
.pos = try readVec4(r),
|
||||
.vel = try readVec4(r),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
pub fn sendHello(writer: *std.io.Writer, hello: Hello) !void {
|
||||
try writer.print("{s}\n", .{hello.msg});
|
||||
try writer.flush();
|
||||
}
|
||||
pub const Projectile_v1 = struct {
|
||||
id: entity.id = entity.INVALID_ID,
|
||||
pos: zm.Vec,
|
||||
vel: zm.Vec,
|
||||
|
||||
fn writeVec4(w: *std.Io.Writer, v: zm.Vec4) !void {
|
||||
const a = zm.vecToArray(v);
|
||||
pub fn encode(self: Projectile_v1, w: *std.Io.Writer) !void {
|
||||
try w.writeInt(u64, self.id, .little);
|
||||
try writeVec4(w, self.pos);
|
||||
try writeVec4(w, self.vel);
|
||||
}
|
||||
|
||||
pub fn decode(r: *std.Io.Reader) !Projectile_v1 {
|
||||
return .{
|
||||
.id = try r.readInt(u64, .little),
|
||||
.pos = try readVec4(r),
|
||||
.vel = try readVec4(r),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
fn writeVec4(w: *std.Io.Writer, v: zm.Vec) !void {
|
||||
const a = zm.vecToArr4(v);
|
||||
inline for (a) |f| {
|
||||
try w.writeFloat(f32, f, .little);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user