Make server control knockback
This commit is contained in:
+24
-11
@@ -466,7 +466,16 @@ export class GameServer {
|
||||
teleport(player: ServerPlayer, x: number, y: number, z: number) {
|
||||
Object.assign(player, { x, y, z });
|
||||
player.fall_distance = 0;
|
||||
this.#send(player, { type: "teleport", x, y, z });
|
||||
player.stop_knockback();
|
||||
this.#place(player);
|
||||
}
|
||||
|
||||
// tells their client where they are now (and how fast they're going, while the server moves them), and everyone
|
||||
// else where they are
|
||||
#place(player: ServerPlayer, motion?: [number, number, number]) {
|
||||
const { x, y, z } = player;
|
||||
player.teleport_id += 1;
|
||||
this.#send(player, { type: "teleport", id: player.teleport_id, x, y, z, ...(motion && { motion }) });
|
||||
this.#update_view(player);
|
||||
this.#broadcast({ type: "player_move", id: player.id, x, y, z, yaw: player.yaw, pitch: player.pitch }, player);
|
||||
}
|
||||
@@ -709,6 +718,11 @@ export class GameServer {
|
||||
if (![x, y, z, yaw, pitch].every(is_number)) {
|
||||
return;
|
||||
}
|
||||
// the server is moving them, or it's from before the last time it did
|
||||
const stale = is_number(message.teleport) && message.teleport < player.teleport_id;
|
||||
if (player.knockback_ticks !== undefined || stale) {
|
||||
return;
|
||||
}
|
||||
this.#on_move(player, x, y, z);
|
||||
Object.assign(player, { x, y, z, yaw, pitch });
|
||||
this.#broadcast({ type: "player_move", id: player.id, x, y, z, yaw, pitch }, player);
|
||||
@@ -1381,8 +1395,14 @@ export class GameServer {
|
||||
|
||||
#tick_vitals() {
|
||||
for (const player of this.#players.values()) {
|
||||
// burning, effects, and falling out of the world
|
||||
// burning, effects, falling out of the world, and being knocked back
|
||||
const knocked_back = player.knockback_ticks !== undefined;
|
||||
player.tick();
|
||||
if (knocked_back && player.alive) {
|
||||
// the last one hands what's left of the slide to their client
|
||||
this.#place(player, [player.vx, player.vy, player.vz]);
|
||||
if (player.knockback_ticks === undefined) player.stop_knockback();
|
||||
}
|
||||
player.vitals.tick(
|
||||
player.health,
|
||||
(amount) => this.heal_player(player, amount),
|
||||
@@ -1399,6 +1419,8 @@ export class GameServer {
|
||||
const ticks = Math.max(1, this.#tick - player.last_move_tick);
|
||||
player.last_move_tick = this.#tick;
|
||||
const horizontal = Math.hypot(dx, dz);
|
||||
// knockback only lifts them off the ground
|
||||
player.on_ground = this.#on_ground(x, y, z);
|
||||
// teleports and the first move after joining aren't walking
|
||||
if (horizontal > TELEPORT_DISTANCE || Math.abs(dy) > TELEPORT_DISTANCE) {
|
||||
player.fall_distance = 0;
|
||||
@@ -1421,8 +1443,6 @@ export class GameServer {
|
||||
if (dy < 0 && !in_liquid) {
|
||||
player.fall_distance -= dy;
|
||||
}
|
||||
// knockback only lifts them off the ground
|
||||
player.on_ground = this.#on_ground(x, y, z);
|
||||
if (player.on_ground) {
|
||||
const damage = Math.ceil(player.fall_distance - SAFE_FALL);
|
||||
player.fall_distance = 0;
|
||||
@@ -1530,13 +1550,6 @@ export class GameServer {
|
||||
}
|
||||
};
|
||||
|
||||
// knockback, their client moves them
|
||||
const motion = player.take_motion();
|
||||
if (motion) {
|
||||
const [vx, vy, vz] = motion;
|
||||
this.#send(player, { type: "set_motion", vx, vy, vz });
|
||||
}
|
||||
|
||||
// with lore, so it's resent when only the lore changed
|
||||
const inventory = player.inventory.to_synced_data();
|
||||
sync("inventory", inventory, () => ({ type: "container", container: "inventory", items: inventory }));
|
||||
|
||||
+31
-11
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user