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
3 changes: 2 additions & 1 deletion src/callbacks/block/touch/hurt.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const std = @import("std");

const main = @import("main");
const @"cubyz:health" = main.entity.components.@"cubyz:health";

dps: f32,
damageType: main.game.DamageType,
Expand All @@ -26,6 +27,6 @@ pub fn init(zon: main.ZonElement, _: main.callbacks.Creator) ?*@This() {
pub fn run(self: *@This(), params: main.callbacks.BlockTouchCallback.Params) main.callbacks.Result {
std.debug.assert(params.entity == &main.game.Player.super); // TODO: Implement on the server side
const damage = self.dps*@as(f32, @floatCast(params.deltaTime));
main.sync.addHealth(-damage, self.damageType, .client, main.game.Player.id);
@"cubyz:health".client.addHealth(main.game.Player.id, -damage);
return .handled;
}
31 changes: 27 additions & 4 deletions src/entity.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const std = @import("std");
const main = @import("main.zig");
const utils = main.utils;
const BinaryReader = utils.BinaryReader;
const vec = main.vec;
const Mat4f = vec.Mat4f;
const Vec3d = vec.Vec3d;
Expand Down Expand Up @@ -29,10 +31,12 @@ pub const Entity = enum(u32) {
};
pub const EntityComponentId = u32;
const EntityComponentVTable = struct {
serverLoad: *const fn (entity: Entity, reader: *main.utils.BinaryReader, version: u32) EntityComponentLoadError!void,
clientLoad: *const fn (entity: Entity, reader: *main.utils.BinaryReader, version: u32) EntityComponentLoadError!void,
serverLoad: *const fn (entity: Entity, reader: *BinaryReader, version: u32) EntityComponentLoadError!void,
clientLoad: *const fn (entity: Entity, reader: *BinaryReader, version: u32) EntityComponentLoadError!void,
serverUnload: *const fn (entity: Entity) void,
clientUnload: *const fn (entity: Entity) void,
modifyServerComponent: *const fn (entity: Entity, reader: *BinaryReader) void,
modifyClientComponent: *const fn (entity: Entity, reader: *BinaryReader) void,
};
var componentList: []?EntityComponentVTable = undefined;

Expand All @@ -51,6 +55,8 @@ pub fn initComponents() void {
.clientLoad = @field(components, decl.name).client.load,
.serverUnload = @field(components, decl.name).server.unload,
.clientUnload = @field(components, decl.name).client.unload,
.modifyServerComponent = @field(components, decl.name).server.modifyComponent,
.modifyClientComponent = @field(components, decl.name).client.modifyComponent,
};
} else {
std.log.err("entity components: Duplicate list id {}.", .{componentId});
Expand All @@ -66,7 +72,7 @@ pub fn loadComponent(comptime side: main.sync.Side, componentId: EntityComponent
std.log.err("unknown Component Id {} ", .{componentId});
return error.UnknownComponentId;
}
var componentReader = main.utils.BinaryReader.init(componentData);
var componentReader = BinaryReader.init(componentData);
if (componentList[componentId]) |vtable| {
switch (side) {
.server => vtable.serverLoad(entity, &componentReader, componentVersion) catch |err| {
Expand Down Expand Up @@ -97,6 +103,23 @@ pub fn unloadComponent(comptime side: main.sync.Side, componentId: EntityCompone
}
}

pub fn modifyComponent(comptime side: main.sync.Side, componentId: EntityComponentId, entity: Entity, componentData: []const u8) EntityComponentLoadError!void {
if (componentId >= componentList.len) {
std.log.err("unknown Component Id {} ", .{componentId});
return error.UnknownComponentId;
}
var componentReader = BinaryReader.init(componentData);
if (componentList[componentId]) |vtable| {
switch (side) {
.server => vtable.modifyServerComponent(entity, &componentReader),
.client => vtable.modifyClientComponent(entity, &componentReader),
}
} else {
std.log.err("unknown Component Id {} ", .{componentId});
return error.UnknownComponentId;
}
}

pub const client = struct {
pub fn init() void {
inline for (@typeInfo(components).@"struct".decls) |decl| {
Expand Down Expand Up @@ -185,7 +208,7 @@ pub fn loadComponentsFromBase64(base64Data: []const u8, entity: Entity, comptime
const data = main.utils.fromBase64(main.stackAllocator, base64Data) catch return EntityComponentLoadError.DecodingBase64;
defer main.stackAllocator.free(data);

var reader = main.utils.BinaryReader.init(data);
var reader = BinaryReader.init(data);
var lastError: EntityComponentLoadError!void = {};
while (reader.remaining.len != 0) {
const componentId: EntityComponentId = reader.readVarInt(EntityComponentId) catch return EntityComponentLoadError.UnreadableId;
Expand Down
1 change: 1 addition & 0 deletions src/entityComponent/_list.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pub const @"cubyz:bag" = @import("bag.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:health" = @import("health.zig");
9 changes: 9 additions & 0 deletions src/entityComponent/_template.zig
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ pub const client = struct {
pub fn init() void {}
pub fn deinit() void {}
pub fn clear() void {}

pub fn modifyComponent(entity: Entity, reader: *utils.BinaryReader) void {
_ = entity;
_ = reader;
}
};
// ############################# Server only stuff ################################
pub const server = struct {
Expand All @@ -70,4 +75,8 @@ pub const server = struct {
pub fn unload(entity: Entity) void {
_ = entity;
}
pub fn modifyComponent(entity: Entity, reader: *utils.BinaryReader) void {
_ = entity;
_ = reader;
}
};
8 changes: 8 additions & 0 deletions src/entityComponent/bag.zig
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ pub const client = struct {
const bag = components.fetchRemove(entity) catch return;
bag.bag.deinit();
}
pub fn modifyComponent(entity: Entity, reader: *utils.BinaryReader) void {
_ = entity;
_ = reader;
}
};

// ############################# Server only stuff ################################
Expand Down Expand Up @@ -103,4 +107,8 @@ pub const server = struct {
const bag = components.fetchRemove(entity) catch return;
bag.bag.deinit();
}
pub fn modifyComponent(entity: Entity, reader: *utils.BinaryReader) void {
_ = entity;
_ = reader;
}
};
156 changes: 156 additions & 0 deletions src/entityComponent/health.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
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");
const Self = @This();

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

var playerBagSizeLimit = 120;

// ############################# Client only stuff ################################
pub const client = struct {
const Component = struct {
health: f32,
maxHealth: 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 getHealth(entity: Entity) ?f32 {
return (components.get(entity) orelse return null).health;
}
pub fn getMaxHealth(entity: Entity) ?f32 {
return (components.get(entity) orelse return null).maxHealth;
}
pub fn addHealth(entity: Entity, healthChange: f32) void {
var binaryWriter = main.utils.BinaryWriter.init(main.stackAllocator);
defer binaryWriter.deinit();
binaryWriter.writeFloat(f32, healthChange);
main.network.protocols.EntityComponentUpdate.modify(main.game.world.?.conn, entity, Self.entityComponentID, binaryWriter.data.items);
}

pub fn load(entity: Entity, reader: *utils.BinaryReader, version: u32) main.entity.EntityComponentLoadError!void {
if (version != entityComponentVersion) return error.InvalidComponentVersion;
const component = components.add(main.globalAllocator, entity);
const health = &component.health;
const maxHealth = &component.maxHealth;
health.* = reader.readFloat(f32) catch return error.UnreadableComponentData;
maxHealth.* = reader.readFloat(f32) catch return error.UnreadableComponentData;
}
pub fn unload(entity: Entity) void {
components.remove(entity) catch {};
}
pub fn modifyComponent(entity: Entity, reader: *utils.BinaryReader) void {
_ = entity;
_ = reader;
}
};

// ############################# Server only stuff ################################
pub const server = struct {
pub const Component = struct {
health: f32,
maxHealth: f32,
pub fn save(self: Component, writer: *utils.BinaryWriter, audience: main.entity.AudienceInfo) main.entity.ComponentSaveBehaviour {
if (audience != .disk and audience != .playerHimself) return .discard;
writer.writeFloat(f32, self.health);
writer.writeFloat(f32, self.maxHealth);
return .save;
}
};
pub var components: main.utils.SparseSet(Component, Entity) = .{};

pub fn init() void {
components = .{};
}
pub fn deinit() void {
components.deinit(main.globalAllocator);
}

pub fn get(entity: Entity) ?Component {
return (components.get(entity) orelse return null).*;
}
pub fn getHealth(entity: Entity) ?f32 {
return (components.get(entity) orelse return null).health;
}
pub fn getMaxHealth(entity: Entity) ?f32 {
return (components.get(entity) orelse return null).maxHealth;
}

pub fn loadFromData(entity: Entity, reader: *utils.BinaryReader, version: u32) main.entity.EntityComponentLoadError!void {
if (version != entityComponentVersion) return error.InvalidComponentVersion;
const component = components.add(main.globalAllocator, entity);
const health = &component.health;
const maxHealth = &component.maxHealth;
health.* = reader.readFloat(f32) catch return error.UnreadableComponentData;
maxHealth.* = reader.readFloat(f32) catch return error.UnreadableComponentData;
}
pub fn loadFromNum(entity: Entity, givenHealth: f32) void {
const component = components.add(main.globalAllocator, entity);
const health = &component.health;
const maxHealth = &component.maxHealth;
health.* = givenHealth;
maxHealth.* = givenHealth;
}
pub fn unload(entity: Entity) void {
components.remove(entity) catch {};
}

pub fn modifyComponent(entity: Entity, reader: *utils.BinaryReader) void {
const addedHealth = reader.readFloat(f32) catch return;
addHealth(entity, addedHealth);
}

pub fn addHealth(entity: Entity, healthChange: f32) void {
const health = &(components.get(entity) orelse return).health;
health.* += healthChange;
std.log.debug("modifed component {}", .{health});

if (health.* <= 0) {
die(entity);
}

main.entity.server.transmitChange(Self, entity);
}

fn die(entity: Entity) void {
const component = components.get(entity) orelse return;
const health = &component.health;
const maxHealth = &component.maxHealth;
health.* = maxHealth.*;
}
};
8 changes: 8 additions & 0 deletions src/entityComponent/model.zig
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ pub const client = struct {
pub fn get(entity: Entity) ?*Component {
return components.get(entity);
}
pub fn modifyComponent(entity: Entity, reader: *utils.BinaryReader) void {
_ = entity;
_ = reader;
}
};

// ############################# Server only stuff ################################
Expand Down Expand Up @@ -114,4 +118,8 @@ pub const server = struct {
pub fn get(entity: Entity) ?*const Component {
return components.get(entity);
}
pub fn modifyComponent(entity: Entity, reader: *utils.BinaryReader) void {
_ = entity;
_ = reader;
}
};
10 changes: 10 additions & 0 deletions src/entityComponent/permissions.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ pub const client = struct {
pub fn init() void {}
pub fn deinit() void {}
pub fn clear() void {}

pub fn modifyComponent(entity: Entity, reader: *utils.BinaryReader) void {
_ = entity;
_ = reader;
}
};
// ############################# Server only stuff ################################
pub const server = struct {
Expand Down Expand Up @@ -84,4 +89,9 @@ pub const server = struct {
const permissions = components.fetchRemove(entity) catch return;
permissions.permissions.deinit();
}

pub fn modifyComponent(entity: Entity, reader: *utils.BinaryReader) void {
_ = entity;
_ = reader;
}
};
10 changes: 10 additions & 0 deletions src/entityComponent/player.zig
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ pub const client = struct {
pub fn get(entity: Entity) ?*Component {
return components.get(entity);
}

pub fn modifyComponent(entity: Entity, reader: *utils.BinaryReader) void {
_ = entity;
_ = reader;
}
};

// ############################# Server only stuff ################################
Expand Down Expand Up @@ -92,4 +97,9 @@ pub const server = struct {
pub fn get(entity: Entity) ?*Component {
return components.get(entity);
}

pub fn modifyComponent(entity: Entity, reader: *utils.BinaryReader) void {
_ = entity;
_ = reader;
}
};
4 changes: 2 additions & 2 deletions src/game.zig
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const settings = @import("settings.zig");
const Block = main.blocks.Block;
const physics = main.physics;
const KeyBoard = main.KeyBoard;
const @"cubyz:health" = main.entity.components.@"cubyz:health";

pub const camera = struct { // MARK: camera
pub var rotation: Vec3f = Vec3f{0, 0, 0};
Expand Down Expand Up @@ -204,7 +205,6 @@ pub const Player = struct { // MARK: Player
Player.super.pos = spawnPos;
Player.super.vel = .{0, 0, 0};

Player.super.health = Player.super.maxHealth;
Player.super.energy = Player.super.maxEnergy;

Player.eye = .{};
Expand Down Expand Up @@ -764,7 +764,7 @@ pub fn update(deltaTime: f64) void { // MARK: update()
const velocityChange = @abs(@abs(prevVel[2]) - @abs(Player.super.vel[2]));
const damage: f32 = @floatCast(@round(@max((velocityChange*velocityChange)/(2*physics.baseGravity) - 7, 0))/2);
if (damage > 0.01) {
main.sync.addHealth(-damage, .fall, .client, Player.id);
@"cubyz:health".server.addHealth(Player.id, -damage);
}
}
physics.calculateVerticalCollisionEyeMovement(deltaTime, &Player.eye, didCollide, Player.onGround, wasOnGround, prevPos, Player.super.pos, prevVel, Player.super.vel, motion, Player.steppingHeight()[2]);
Expand Down
Loading
Loading