zzz/src/shared/entity.zig

78 lines
2.0 KiB
Zig

const std = @import("std");
const zm = @import("zmath");
const protocol = @import("protocol.zig");
pub const entity_id = u64;
pub const INVALID_ENTITY_ID: entity_id = 0;
pub const EntityKinds = .{
Player,
Monster,
Projectile,
};
pub const Player = struct {
id: entity_id = INVALID_ENTITY_ID,
pos: zm.Vec,
hp: i32,
pub fn encode(self: Projectile, w: *std.Io.Writer) !void {
try w.writeInt(u64, self.id, .little);
try protocol.writeVec4(w, self.pos);
try protocol.writeVec4(w, self.vel);
}
pub fn decode(r: *std.Io.Reader) !Projectile {
return .{
.id = try r.readInt(u64, .little),
.pos = try protocol.readVec4(r),
.vel = try protocol.readVec4(r),
};
}
};
pub const Monster = struct {
id: entity_id = INVALID_ENTITY_ID,
pos: zm.Vec,
hp: i32,
pub fn encode(self: Projectile, w: *std.Io.Writer) !void {
try w.writeInt(u64, self.id, .little);
try protocol.writeVec4(w, self.pos);
try protocol.writeVec4(w, self.vel);
}
pub fn decode(r: *std.Io.Reader) !Projectile {
return .{
.id = try r.readInt(u64, .little),
.pos = try protocol.readVec4(r),
.vel = try protocol.readVec4(r),
};
}
};
pub const Projectile = struct {
id: entity_id = INVALID_ENTITY_ID,
pos: zm.Vec,
vel: zm.Vec,
pub fn update(self: *Projectile) void {
self.pos = self.pos + self.vel;
std.log.info("pos=({})", .{self.pos});
}
pub fn encode(self: Projectile, w: *std.Io.Writer) !void {
try w.writeInt(u64, self.id, .little);
try protocol.writeVec4(w, self.pos);
try protocol.writeVec4(w, self.vel);
}
pub fn decode(r: *std.Io.Reader) !Projectile {
return .{
.id = try r.readInt(u64, .little),
.pos = try protocol.readVec4(r),
.vel = try protocol.readVec4(r),
};
}
};