Block placing

This commit is contained in:
2026-01-08 13:27:35 +01:00
parent d96946d1a0
commit 133994d2ef
3 changed files with 198 additions and 89 deletions

View File

@@ -177,6 +177,19 @@ vertical_fov_deg: f32 = 80,
movement_state: MovementState,
maybe_raycast_hit: ?Chunks.RaycastHit = null,
block_index: usize = 0,
pub const blocks = [9][:0]const u8{
"Bricks.json",
"ChiseledStoneBricks.json",
"Cobblestone.json",
"DiamondBlock.json",
"GoldBlock.json",
"IronBlock.json",
"OakPlanks.json",
"SmoothStone.json",
"StoneBricks.json",
};
pub const camera_height_vx = 1.62;
@@ -214,6 +227,12 @@ pub fn onKeyDown(self: *Player, key: glfw.Key) void {
self.x_input.onKeyDown(key);
self.y_input.onKeyDown(key);
self.z_input.onKeyDown(key);
const key_code = @intFromEnum(key);
if (key_code >= @intFromEnum(glfw.Key.one) and key_code <= @intFromEnum(glfw.Key.nine)) {
self.block_index = @intCast(key_code - @intFromEnum(glfw.Key.one));
std.log.info("Picked block {s}", .{blocks[self.block_index]});
}
}
pub fn onKeyUp(self: *Player, key: glfw.Key) void {
@@ -231,15 +250,36 @@ pub fn onMouseDown(self: *Player, button: glfw.MouseButton, game: *Game) void {
if (self.maybe_raycast_hit) |raycast_hit| {
switch (button) {
.left => {
game.chunks.destroyVoxelAt(
game.chunks.setVoxelAt(
raycast_hit.voxel,
.air,
game.engine,
&game.blocks,
game.descriptor_pool,
game.per_batch_descriptor_set_layout,
game.allocator,
) catch |err| {
std.log.err("Error while destroying voxel {f}: {}", .{ raycast_hit.voxel, err });
};
},
.right => blk: {
const target_vx = raycast_hit.voxel.add(raycast_hit.side.getSignVector());
const id = game.blocks.getOrLoad(game.engine, &game.materials, &game.textures, blocks[self.block_index], game.allocator) catch |err| {
std.log.err("Error while placing voxel at {f}: {}", .{ target_vx, err });
break :blk;
};
game.chunks.setVoxelAt(
target_vx,
id,
game.engine,
&game.blocks,
game.descriptor_pool,
game.per_batch_descriptor_set_layout,
game.allocator,
) catch |err| {
std.log.err("Error while placing voxel at {f}: {}", .{ target_vx, err });
};
},
else => {},
}
}