mirror of
https://github.com/PixelGuys/Cubyz.git
synced 2025-08-03 19:28:49 -04:00
Remove the function inheritance pattern from the item drop manager.
My old java brain was responsible for this, and it has bothered me for a while. A simple if can achieve the same with much less indirection.
This commit is contained in:
parent
de2e4aedb4
commit
e718a4fc43
@ -64,9 +64,6 @@ pub const ItemDropManager = struct { // MARK: ItemDropManager
|
||||
|
||||
lastUpdates: ZonElement,
|
||||
|
||||
// TODO: Get rid of this inheritance pattern.
|
||||
internalAdd: *const fn(self: *ItemDropManager, i: u16, drop: ItemDrop) void,
|
||||
|
||||
pub fn init(self: *ItemDropManager, allocator: NeverFailingAllocator, world: ?*ServerWorld, gravity: f64) void {
|
||||
self.* = ItemDropManager {
|
||||
.allocator = allocator,
|
||||
@ -77,7 +74,6 @@ pub const ItemDropManager = struct { // MARK: ItemDropManager
|
||||
.world = world,
|
||||
.gravity = gravity,
|
||||
.airDragFactor = gravity/maxSpeed,
|
||||
.internalAdd = &defaultInternalAdd,
|
||||
};
|
||||
self.list.resize(self.allocator.allocator, maxCapacity) catch unreachable;
|
||||
}
|
||||
@ -248,7 +244,7 @@ pub const ItemDropManager = struct { // MARK: ItemDropManager
|
||||
while(self.changeQueue.dequeue()) |data| {
|
||||
switch(data) {
|
||||
.add => |addData| {
|
||||
self.internalAdd(self, addData[0], addData[1]);
|
||||
self.internalAdd(addData[0], addData[1]);
|
||||
},
|
||||
.remove => |index| {
|
||||
self.internalRemove(index);
|
||||
@ -257,8 +253,11 @@ pub const ItemDropManager = struct { // MARK: ItemDropManager
|
||||
}
|
||||
}
|
||||
|
||||
fn defaultInternalAdd(self: *ItemDropManager, i: u16, drop_: ItemDrop) void {
|
||||
fn internalAdd(self: *ItemDropManager, i: u16, drop_: ItemDrop) void {
|
||||
var drop = drop_;
|
||||
if(self.world == null) {
|
||||
ClientItemDropManager.clientSideInternalAdd(self, i, drop);
|
||||
}
|
||||
drop.reverseIndex = @intCast(self.size);
|
||||
self.list.set(i, drop);
|
||||
if(self.world != null) {
|
||||
@ -410,7 +409,6 @@ pub const ClientItemDropManager = struct { // MARK: ClientItemDropManager
|
||||
.lastTime = @as(i16, @truncate(std.time.milliTimestamp())) -% settings.entityLookback,
|
||||
};
|
||||
self.super.init(allocator, null, world.gravity);
|
||||
self.super.internalAdd = &overrideInternalAdd;
|
||||
self.interpolation.init(
|
||||
@ptrCast(self.super.list.items(.pos).ptr),
|
||||
@ptrCast(self.super.list.items(.vel).ptr)
|
||||
@ -455,18 +453,15 @@ pub const ClientItemDropManager = struct { // MARK: ClientItemDropManager
|
||||
self.lastTime = time;
|
||||
}
|
||||
|
||||
fn overrideInternalAdd(super: *ItemDropManager, i: u16, drop: ItemDrop) void {
|
||||
{
|
||||
mutex.lock();
|
||||
defer mutex.unlock();
|
||||
for(&instance.?.interpolation.lastVel) |*lastVel| {
|
||||
@as(*align(8)[ItemDropManager.maxCapacity]Vec3d, @ptrCast(lastVel))[i] = Vec3d{0, 0, 0};
|
||||
}
|
||||
for(&instance.?.interpolation.lastPos) |*lastPos| {
|
||||
@as(*align(8)[ItemDropManager.maxCapacity]Vec3d, @ptrCast(lastPos))[i] = drop.pos;
|
||||
}
|
||||
fn clientSideInternalAdd(_: *ItemDropManager, i: u16, drop: ItemDrop) void {
|
||||
mutex.lock();
|
||||
defer mutex.unlock();
|
||||
for(&instance.?.interpolation.lastVel) |*lastVel| {
|
||||
@as(*align(8)[ItemDropManager.maxCapacity]Vec3d, @ptrCast(lastVel))[i] = Vec3d{0, 0, 0};
|
||||
}
|
||||
for(&instance.?.interpolation.lastPos) |*lastPos| {
|
||||
@as(*align(8)[ItemDropManager.maxCapacity]Vec3d, @ptrCast(lastPos))[i] = drop.pos;
|
||||
}
|
||||
super.defaultInternalAdd(i, drop);
|
||||
}
|
||||
|
||||
pub fn remove(self: *ClientItemDropManager, i: u16) void {
|
||||
|
Loading…
x
Reference in New Issue
Block a user