i don't know

This commit is contained in:
Vicente Ferrari Smith 2026-02-19 22:12:59 +01:00
parent 35bbc5cbfd
commit 077ceb810e
5 changed files with 66 additions and 96 deletions

View File

@ -2,21 +2,29 @@ const rl = @import("raylib");
const shared = @import("shared"); const shared = @import("shared");
const zm = @import("zmath"); const zm = @import("zmath");
pub const Elf = struct { // pub const Elf = struct {
elf: shared.entity.Elf, // elf: shared.entity.Elf,
boy: rl.Texture, // boy: rl.Texture,
pub fn init() Elf { // pub fn init() Elf {
return .{ // return .{
.elf = .{ .hp = 10, .pos = zm.f32x4(0, 0, 0, 0) }, // .elf = .{ .hp = 10, .pos = zm.f32x4(0, 0, 0, 0) },
.boy = rl.loadTexture("assets/tile_0000.png") catch unreachable, // .boy = rl.loadTexture("assets/tile_0000.png") catch unreachable,
}; // };
} // }
pub fn draw(self: *Elf) void { // pub fn draw(self: *Elf) void {
const src = rl.Rectangle.init(0, 0, @floatFromInt(self.boy.width), @floatFromInt(self.boy.height)); // const src = rl.Rectangle.init(0, 0, @floatFromInt(self.boy.width), @floatFromInt(self.boy.height));
const dst = rl.Rectangle.init(100 + 100 * zm.cos(@as(f32, @floatCast(rl.getTime()))), 100 + 100 * zm.sin(@as(f32, @floatCast(rl.getTime()))), 2.0 * @as(f32, @floatFromInt(self.boy.width)), 2.0 * @as(f32, @floatFromInt(self.boy.height))); // const dst = rl.Rectangle.init(100 + 100 * zm.cos(@as(f32, @floatCast(rl.getTime()))), 100 + 100 * zm.sin(@as(f32, @floatCast(rl.getTime()))), 2.0 * @as(f32, @floatFromInt(self.boy.width)), 2.0 * @as(f32, @floatFromInt(self.boy.height)));
self.boy.drawPro(src, dst, .{ .x = 0, .y = 0 }, 0, .white); // self.boy.drawPro(src, dst, .{ .x = 0, .y = 0 }, 0, .white);
} // }
}; // };
pub fn draw_soldier(self: *shared.entity.Soldier) void {
}
pub fn draw_alien(self: *shared.entity.Alien) void {
}

View File

@ -179,6 +179,18 @@ pub fn main() !void {
// const lorem = @embedFile("embeds/lorem.txt"); // const lorem = @embedFile("embeds/lorem.txt");
for (the_chunk.entities.items) |e| {
switch (e) {
.Soldier => |soldier| {
entity.render_soldier(soldier);
},
.Alien => |alien| {
entity.render_alien(alien);
}
}
}
vollkorn[0].render_text( vollkorn[0].render_text(
"The night is long and cold outside.", "The night is long and cold outside.",
rl.Vector2{ .x = 0, .y = 0 }, rl.Vector2{ .x = 0, .y = 0 },
@ -188,7 +200,6 @@ pub fn main() !void {
false, false,
true true
); );
// rl.beginShaderMode(test_shader1); // rl.beginShaderMode(test_shader1);
// rl.gl.rlBegin(rl.gl.rl_quads); // rl.gl.rlBegin(rl.gl.rl_quads);
@ -352,9 +363,9 @@ fn on_message(allocator: std.mem.Allocator, msg: shared.protocol_v1.Message) !vo
} }
} }
fn on_spawn_entity(allocator: std.mem.Allocator, se: shared.protocol_v1.SpawnEntity) !void { fn on_spawn_entity(allocator: std.mem.Allocator, p: shared.protocol_v1.EntityPayload) !void {
// _ = allocator; _ = se; // _ = allocator; _ = se;
switch (se) { switch (p) {
.soldier => |s| { .soldier => |s| {
const soldier = shared.entity.Soldier{ const soldier = shared.entity.Soldier{
.id = s.id, .id = s.id,
@ -375,8 +386,8 @@ fn on_spawn_entity(allocator: std.mem.Allocator, se: shared.protocol_v1.SpawnEnt
} }
} }
fn on_update_entity(ue: shared.protocol_v1.UpdateEntity) void { fn on_update_entity(p: shared.protocol_v1.EntityPayload) void {
switch (ue) { switch (p) {
.soldier => |s| { .soldier => |s| {
if (chunk.findT(&the_chunk, shared.entity.Soldier, s.id)) |soldier| { if (chunk.findT(&the_chunk, shared.entity.Soldier, s.id)) |soldier| {
soldier.hp = s.hp; soldier.hp = s.hp;
@ -393,8 +404,8 @@ fn on_update_entity(ue: shared.protocol_v1.UpdateEntity) void {
} }
} }
fn on_despawn_entity(allocator: std.mem.Allocator, de: shared.protocol_v1.DespawnEntity) void { fn on_despawn_entity(allocator: std.mem.Allocator, p: shared.protocol_v1.EntityPayload) void {
switch (de) { switch (p) {
.soldier => |s| { _ = chunk.despawn(&the_chunk, allocator, s.id); }, .soldier => |s| { _ = chunk.despawn(&the_chunk, allocator, s.id); },
.alien => |a| { _ = chunk.despawn(&the_chunk, allocator, a.id); }, .alien => |a| { _ = chunk.despawn(&the_chunk, allocator, a.id); },
} }

View File

@ -44,7 +44,7 @@ pub fn broadcastChanges(chunk: *shared.chunk.Chunk, allocator: std.mem.Allocator
var writer = shared.bits.BitWriter.init(&aw); var writer = shared.bits.BitWriter.init(&aw);
for (chunk.entities.items) |e| { for (chunk.entities.items) |e| {
const msg = shared.protocol_v1.makeUpdateMessage(e); const msg = shared.protocol_v1.makeMessage(.update_entity, e);
try writer.serialize(allocator, msg); try writer.serialize(allocator, msg);
} }

View File

@ -158,7 +158,7 @@ fn on_connect(allocator: std.mem.Allocator, host: *enet.ENetHost, peer: *enet.EN
var writer = shared.bits.BitWriter.init(&aw); var writer = shared.bits.BitWriter.init(&aw);
for (the_chunk.entities.items) |e| { for (the_chunk.entities.items) |e| {
const msg = shared.protocol_v1.makeSpawnMessage(e); const msg = shared.protocol_v1.makeMessage(.spawn_entity, e);
try writer.serialize(allocator, msg); try writer.serialize(allocator, msg);
} }

View File

@ -14,52 +14,39 @@ const MessageKind = enum(c_char) {
}; };
pub const Message = union(MessageKind) { pub const Message = union(MessageKind) {
spawn_entity: SpawnEntity, spawn_entity: EntityPayload,
update_entity: UpdateEntity, update_entity: EntityPayload,
despawn_entity: DespawnEntity, despawn_entity: EntityPayload,
}; };
pub fn makeMessage(kind: MessageKind, e: anytype) Message {
const payload = payloadFromEntity(e);
return switch (kind) {
.spawn_entity => .{ .spawn_entity = payload },
.update_entity => .{ .update_entity = payload },
.despawn_entity => .{ .despawn_entity = payload },
};
}
pub fn payloadFromEntity(e: entity.Ref) EntityPayload {
return switch (e) {
.Soldier => |s| .{ .soldier = Soldier.init(s.*) },
.Alien => |a| .{ .alien = Alien.init(a.*) },
};
}
const EntityKind = enum(c_char) { const EntityKind = enum(c_char) {
soldier, soldier,
alien, alien,
}; };
pub const SpawnEntity = union(EntityKind) { pub const EntityPayload = union(EntityKind) {
soldier: Soldier, soldier: Soldier,
alien: Alien, alien: Alien,
}; };
pub const UpdateEntity = union(EntityKind) {
soldier: Soldier,
alien: Alien,
};
pub const DespawnEntity = union(EntityKind) {
soldier: Soldier,
alien: Alien,
};
pub fn makeSpawnMessage(e: entity.Ref) Message {
switch (e) {
.Soldier => |s| { return Soldier.makeSpawn(s.*); },
.Alien => |a| { return Alien.makeSpawn(a.*); },
}
}
pub fn makeUpdateMessage(e: entity.Ref) Message {
switch (e) {
.Soldier => |s| { return Soldier.makeUpdate(s.*); },
.Alien => |a| { return Alien.makeUpdate(a.*); },
}
}
pub fn makeDespawnMessage(e: entity.Ref) Message {
switch (e) {
.Soldier => |s| { return Soldier.makeDespawn(s.*); },
.Alien => |a| { return Alien.makeDespawn(a.*); },
}
}
pub const Soldier = struct { pub const Soldier = struct {
id: entity.id = entity.INVALID_ID, id: entity.id = entity.INVALID_ID,
hp: i32, hp: i32,
@ -74,24 +61,6 @@ pub const Soldier = struct {
.vel = soldier.vel, .vel = soldier.vel,
}; };
} }
pub fn makeSpawn(soldier: entity.Soldier) Message {
return .{
.spawn_entity = .{ .soldier = init(soldier) }
};
}
pub fn makeUpdate(soldier: entity.Soldier) Message {
return .{
.update_entity = .{ .soldier = init(soldier) }
};
}
pub fn makeDespawn(soldier: entity.Soldier) Message {
return .{
.despawn_entity = .{ .soldier = init(soldier) }
};
}
}; };
pub const Alien = struct { pub const Alien = struct {
@ -106,24 +75,6 @@ pub const Alien = struct {
.hp = alien.hp, .hp = alien.hp,
}; };
} }
pub fn makeSpawn(alien: entity.Alien) Message {
return .{
.spawn_entity = .{ .alien = init(alien) }
};
}
pub fn makeUpdate(alien: entity.Alien) Message {
return .{
.update_entity = .{ .alien = init(alien) }
};
}
pub fn makeDespawn(alien: entity.Alien) Message {
return .{
.despawn_entity = .{ .alien = init(alien) }
};
}
}; };
test "Protocol" { test "Protocol" {
@ -135,7 +86,7 @@ test "Protocol" {
.vel = .{5, 6, 7, 8}, .vel = .{5, 6, 7, 8},
}; };
const msg = makeSpawnMessage(s); const msg = makeMessage(.spawn_entity, s);
const allocator = std.testing.allocator; const allocator = std.testing.allocator;