Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/entityComponent/_list.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
pub const @"cubyz:_template" = @import("_template.zig");
pub const @"cubyz:bag" = @import("bag.zig");
pub const @"cubyz:broken" = @import("broken.zig");
pub const @"cubyz:model" = @import("model.zig");
pub const @"cubyz:permissions" = @import("permissions.zig");
pub const @"cubyz:player" = @import("player.zig");
pub const @"cubyz:swing_arm" = @import("swing_arm.zig");
105 changes: 105 additions & 0 deletions src/entityComponent/broken.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
const std = @import("std");

const main = @import("main");
const chunk = main.chunk;
const Entity = main.entity.Entity;
const ServerChunk = chunk.ServerChunk;
const game = main.game;
const graphics = main.graphics;
const ZonElement = main.ZonElement;
const renderer = main.renderer;
const settings = main.settings;
const utils = main.utils;
const BinaryReader = utils.BinaryReader;
const BinaryWriter = utils.BinaryWriter;
const vec = main.vec;
const Mat4f = vec.Mat4f;
const Vec3d = vec.Vec3d;
const Vec3f = vec.Vec3f;
const Vec4f = vec.Vec4f;
const Vec3i = vec.Vec3i;
const NeverFailingAllocator = main.heap.NeverFailingAllocator;
const blocks = main.blocks;
const World = game.World;
const ServerWorld = main.server.ServerWorld;
const items = main.items;
const ItemStack = items.ItemStack;
const random = main.random;

const c = @import("c");

pub var entityComponentID: main.entity.EntityComponentId = undefined;
pub const entityComponentVersion = 0;

// ############################# Client only stuff ################################
pub const client = struct {
const Component = struct {
progress: f32,
blockPos: Vec3i,
};
pub var components: main.utils.SparseSet(Component, Entity) = .{};

pub fn init() void {}
pub fn deinit() void {
components.deinit(main.globalAllocator);
}
pub fn clear() void {
components.clear();
}
pub fn load(entity: Entity, reader: *utils.BinaryReader, version: u32) main.entity.EntityComponentLoadError!void {
if (version != 0) return error.InvalidComponentVersion;
const progress = reader.readFloat(f32) catch return error.UnreadableComponentData;
const blockPos = reader.readVec(Vec3i) catch return error.UnreadableComponentData;
const ptr = components.get(entity) orelse components.add(main.globalAllocator, entity);
ptr.* = Component{
.progress = progress,
.blockPos = blockPos,
};
}
pub fn unload(entity: Entity) void {
components.remove(entity) catch {};
}
pub fn get(entity: Entity) ?*Component {
return components.get(entity);
}
};
// ############################# Server only stuff ################################
pub const server = struct {
pub const Component = struct {
progress: f32,
blockPos: Vec3i,
pub fn save(self: Component, writer: *utils.BinaryWriter, audience: main.entity.AudienceInfo) main.entity.ComponentSaveBehaviour {
writer.writeFloat(f32, self.progress);
writer.writeVec(Vec3i, self.blockPos);
if (audience == .disk) return .discard;
return .save;
}
};
var components: main.utils.SparseSet(Component, Entity) = undefined;
pub fn init() void {
components = .{};
}
pub fn deinit() void {
components.deinit(main.globalAllocator);
}
pub fn loadFromData(entity: Entity, _: *utils.BinaryReader, version: u32) main.entity.EntityComponentLoadError!void {
if (version != 0) return error.InvalidComponentVersion;
load(entity);
}
pub fn load(entity: Entity) void {
put(entity, Component{
.progress = 0,
.blockPos = .{0, 0, 0},
});
}
pub fn unload(entity: Entity) void {
components.remove(entity) catch {};
}
pub fn put(entity: Entity, renderComponent: Component) void {
const ptr = components.get(entity) orelse components.add(main.globalAllocator, entity);
ptr.* = renderComponent;
}
pub fn get(entity: Entity) ?*Component {
return components.get(entity);
}
};
105 changes: 105 additions & 0 deletions src/entityComponent/swing_arm.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
const std = @import("std");

const main = @import("main");
const chunk = main.chunk;
const Entity = main.entity.Entity;
const ServerChunk = chunk.ServerChunk;
const game = main.game;
const graphics = main.graphics;
const ZonElement = main.ZonElement;
const renderer = main.renderer;
const settings = main.settings;
const utils = main.utils;
const BinaryReader = utils.BinaryReader;
const BinaryWriter = utils.BinaryWriter;
const vec = main.vec;
const Mat4f = vec.Mat4f;
const Vec3d = vec.Vec3d;
const Vec3f = vec.Vec3f;
const Vec4f = vec.Vec4f;
const Vec3i = vec.Vec3i;
const NeverFailingAllocator = main.heap.NeverFailingAllocator;
const blocks = main.blocks;
const World = game.World;
const ServerWorld = main.server.ServerWorld;
const items = main.items;
const ItemStack = items.ItemStack;
const random = main.random;

const c = @import("c");

pub var entityComponentID: main.entity.EntityComponentId = undefined;
pub const entityComponentVersion = 0;

// ############################# Client only stuff ################################
pub const client = struct {
const Component = struct {
currentSwingProgress: f32,
currentSwingTime: f32,
};
pub var components: main.utils.SparseSet(Component, Entity) = .{};

pub fn init() void {}
pub fn deinit() void {
components.deinit(main.globalAllocator);
}
pub fn clear() void {
components.clear();
}
pub fn load(entity: Entity, reader: *utils.BinaryReader, version: u32) main.entity.EntityComponentLoadError!void {
if (version != 0) return error.InvalidComponentVersion;
const currentSwingProgress = reader.readFloat(f32) catch return error.UnreadableComponentData;
const currentSwingTime = reader.readFloat(f32) catch return error.UnreadableComponentData;
const ptr = components.get(entity) orelse components.add(main.globalAllocator, entity);
ptr.* = Component{
.currentSwingProgress = currentSwingProgress,
.currentSwingTime = currentSwingTime,
};
}
pub fn unload(entity: Entity) void {
components.remove(entity) catch {};
}
pub fn get(entity: Entity) ?*Component {
return components.get(entity);
}
};
// ############################# Server only stuff ################################
pub const server = struct {
pub const Component = struct {
currentSwingProgress: f32,
currentSwingTime: f32,
pub fn save(self: Component, writer: *utils.BinaryWriter, audience: main.entity.AudienceInfo) main.entity.ComponentSaveBehaviour {
writer.writeFloat(f32, self.currentSwingProgress);
writer.writeFloat(f32, self.currentSwingTime);
if (audience == .disk) return .discard;
return .save;
}
};
var components: main.utils.SparseSet(Component, Entity) = undefined;
pub fn init() void {
components = .{};
}
pub fn deinit() void {
components.deinit(main.globalAllocator);
}
pub fn loadFromData(entity: Entity, _: *utils.BinaryReader, version: u32) main.entity.EntityComponentLoadError!void {
if (version != 0) return error.InvalidComponentVersion;
load(entity);
}
pub fn load(entity: Entity) void {
put(entity, Component{
.currentSwingProgress = 0,
.currentSwingTime = 0,
});
}
pub fn unload(entity: Entity) void {
components.remove(entity) catch {};
}
pub fn put(entity: Entity, renderComponent: Component) void {
const ptr = components.get(entity) orelse components.add(main.globalAllocator, entity);
ptr.* = renderComponent;
}
pub fn get(entity: Entity) ?*Component {
return components.get(entity);
}
};
12 changes: 6 additions & 6 deletions src/game.zig
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,12 @@ pub const Player = struct { // MARK: Player
}

pub fn placeBlock(mods: main.Window.Key.Modifiers) void {
if (main.renderer.MeshSelection.selectedBlockPos) |blockPos| blk: {
const mesh = main.renderer.mesh_storage.getMesh(.initFromWorldPos(blockPos, 1)) orelse break :blk;
const block = mesh.chunk.getBlock(blockPos[0] - mesh.pos.wx, blockPos[1] - mesh.pos.wy, blockPos[2] - mesh.pos.wz);
if (main.game.Player.super.getSelected(inventory.getItem(selectedSlot), .client)) |selected| blk: {
const mesh = main.renderer.mesh_storage.getMesh(.initFromWorldPos(selected.blockPos, 1)) orelse break :blk;
const block = mesh.chunk.getBlock(selected.blockPos[0] - mesh.pos.wx, selected.blockPos[1] - mesh.pos.wy, selected.blockPos[2] - mesh.pos.wz);
const onInteract = block.onInteract();
if (!mods.shift) {
if (onInteract.run(.{.blockPos = blockPos, .block = block, .chunk = mesh.chunk}) == .handled) return;
if (onInteract.run(.{.blockPos = selected.blockPos, .block = block, .chunk = mesh.chunk}) == .handled) return;
}
}

Expand Down Expand Up @@ -224,8 +224,8 @@ pub const Player = struct { // MARK: Player
}

pub fn acquireSelectedBlock() void {
if (main.renderer.MeshSelection.selectedBlockPos) |selectedPos| {
const block = main.renderer.mesh_storage.getBlockFromRenderThread(selectedPos[0], selectedPos[1], selectedPos[2]) orelse return;
if (main.game.Player.super.getSelected(inventory.getItem(selectedSlot), .client)) |selected| {
const block = main.renderer.mesh_storage.getBlockFromRenderThread(selected.blockPos[0], selected.blockPos[1], selected.blockPos[2]) orelse return;

const item: items.Item = for (0..items.itemListSize) |idx| {
const baseItem: main.items.BaseItemIndex = @enumFromInt(idx);
Expand Down
Loading
Loading