Make server control knockback

This commit is contained in:
2026-09-30 10:54:09 -03:00
parent 4fbcca6851
commit dc70ada7c4
8 changed files with 99 additions and 53 deletions
+31 -11
View File
@@ -39,9 +39,13 @@ export interface SavedPlayer {
slim?: boolean;
}
// the most ticks the server moves a knocked back player, in case they never land, like in water
const MAX_KNOCKBACK_TICKS = 20;
// a player is a living entity, like minecraft's Player: they have health, can be hurt and pushed around by mobs and
// other players, and have effects. their client moves them though, so they don't walk, fall or think here. they're
// kept apart from the entity manager's entities, clients hear about players through their own messages
// other players, and have effects. their client moves them, except while they're knocked back: then the server does
// until they land, so it works even when their client isn't running, like in a background tab. they're kept apart
// from the entity manager's entities, clients hear about players through their own messages
export class ServerPlayer extends LivingEntity {
readonly type = "bworld:player";
readonly conn: number;
@@ -55,6 +59,11 @@ export class ServerPlayer extends LivingEntity {
inventory = new Container(INVENTORY_SIZE);
// hunger and saturation, health is the entity's
vitals = new Vitals();
// counts every position the server gave them. their client says which one it's heard of with each move, and moves
// from before the latest are ignored, they'd put them back where they were
teleport_id = 0;
// ticks the server has been moving them for, undefined when their client does
knockback_ticks: number | undefined;
// when the last move came, to tell walking from sprinting
last_move_tick = 0;
crafting = new Container(CRAFTING_SIZE);
@@ -100,12 +109,31 @@ export class ServerPlayer extends LivingEntity {
return true;
}
// their client moves them, this only burns, falls out of the world and counts effects and hurt time down
// their client moves them, this only burns, falls out of the world and counts effects and hurt time down. while
// knocked back it moves them like a mob that isn't walking anywhere, landing hands them back to their client
override tick() {
this.base_tick();
if (!this.dead) {
this.tick_effects();
}
if (this.knockback_ticks === undefined) return;
this.forward = this.strafe = this.speed = 0;
this.travel();
this.knockback_ticks += 1;
if ((this.on_ground && this.vy <= 0) || this.knockback_ticks >= MAX_KNOCKBACK_TICKS) {
this.knockback_ticks = undefined;
}
}
override knockback(strength: number, dx: number, dz: number) {
super.knockback(strength, dx, dz);
if (this.vx || this.vy || this.vz) this.knockback_ticks ??= 0;
}
// back to their client, where they are now
stop_knockback() {
this.knockback_ticks = undefined;
this.vx = this.vy = this.vz = 0;
}
// everything that hurts a player goes through the game server first, which lets mods change or cancel it
@@ -127,14 +155,6 @@ export class ServerPlayer extends LivingEntity {
this.level.on_death(this, source);
}
// knockback is how fast their client should move them, sent with the next sync and cleared
take_motion(): [number, number, number] | undefined {
if (!this.vx && !this.vy && !this.vz) return undefined;
const motion: [number, number, number] = [this.vx, this.vy, this.vz];
this.vx = this.vy = this.vz = 0;
return motion;
}
get held_item(): ItemStack | undefined {
return this.inventory.get_item(this.selected_slot);
}