diff --git a/client/entity/local_player.ts b/client/entity/local_player.ts index 64a54bf..768c5ba 100644 --- a/client/entity/local_player.ts +++ b/client/entity/local_player.ts @@ -59,6 +59,14 @@ const WATER_HOP = 0.36 / TICK_DELTA; const SWIM_DRAG = 0.9; // and it turns up or down toward where it's looking, this much of the way each tick, like minecraft's Player.travel const SWIM_STEER = 0.06; +// flying, see /fly, like minecraft's creative flight: the keys add 0.05 blocks a tick and it keeps 0.91 of its speed, +// settling at 11 blocks a second, twice that sprinting. there's no gravity, jump and sneak push it up and down 0.15 +// blocks a tick, and it keeps 0.6 of that each tick, rising and sinking 7.5 blocks a second +const FLY_ACCELERATION = 0.05 / TICK_DELTA; +const FLY_FRICTION = 0.91; +const FLY_SPRINT_SPEED = 2; +const FLY_VERTICAL = 0.15 / TICK_DELTA; +const FLY_VERTICAL_DRAG = 0.6; // on land, the entity's own const LAND_GRAVITY = -32; const LAND_DRAG = 0.98; @@ -103,6 +111,8 @@ export class LocalPlayer extends Player { sneaking = false; // stays on after letting go of the key, see #update_sprinting sprinting = false; + // what the server says, see /fly + flying = false; #fov_scale = 1; #prev_fov_scale = 1; #prev_eye_height = PLAYER_EYE_HEIGHT; @@ -123,15 +133,16 @@ export class LocalPlayer extends Player { tick() { for (const effect of this.effects) if (effect.duration > 0) effect.duration -= 1; - this.#in_water = this.in_water; + // flying ignores water + this.#in_water = !this.flying && this.in_water; const was_swimming = this.swimming; const input = this.client.screen ? NO_INPUT : this.#read_input(); this.#apply_input(input); // after sprinting is, so letting go stands it up the same tick this.#update_swimming(); // out of the water but still in the short box, under something low, it falls like on land - this.gravity = !this.#in_water ? LAND_GRAVITY : this.swimming ? 0 : WATER_GRAVITY; - this.drag = this.#in_water ? WATER_DRAG : LAND_DRAG; + this.gravity = this.flying ? 0 : !this.#in_water ? LAND_GRAVITY : this.swimming ? 0 : WATER_GRAVITY; + this.drag = this.flying ? FLY_VERTICAL_DRAG : this.#in_water ? WATER_DRAG : LAND_DRAG; if (this.swimming && this.#in_water) this.#steer(input); this.#prev_eye_height = this.eye_height; const eye_height = this.swimming @@ -170,7 +181,7 @@ export class LocalPlayer extends Player { // the surface too. it only stands back up where there's room, until then it stays in the short box #update_swimming() { if (!this.swimming) { - if (this.sprinting && this.eyes_in_water) this.set_swimming(true); + if (this.sprinting && this.eyes_in_water && !this.flying) this.set_swimming(true); return; } if (this.sprinting && this.#in_water) return; @@ -211,6 +222,7 @@ export class LocalPlayer extends Player { } override horizontal_friction() { + if (this.flying) return FLY_FRICTION; if (this.#in_water) return this.swimming ? SWIM_DRAG : WATER_DRAG; return this.on_ground ? GROUND_FRICTION : AIR_FRICTION; } @@ -256,10 +268,18 @@ export class LocalPlayer extends Player { input_z /= size; } - this.sneaking = input.sneak; + // sneaking while flying goes down instead + this.sneaking = input.sneak && !this.flying; const sin = Math.sin(this.yaw); const cos = Math.cos(this.yaw); this.#update_sprinting(input, sin, cos); + if (this.flying) { + const acceleration = FLY_ACCELERATION * (this.sprinting ? FLY_SPRINT_SPEED : 1); + this.vx += (sin * input_z + cos * input_x) * acceleration; + this.vz += (cos * input_z - sin * input_x) * acceleration; + this.vy += (Number(input.jump) - Number(input.sneak)) * FLY_VERTICAL; + return; + } // sprinting is no faster in water, swimming is const acceleration = this.#in_water ? WATER_ACCELERATION * (input.sneak ? SNEAK_SPEED : 1) @@ -291,7 +311,8 @@ export class LocalPlayer extends Player { // like minecraft's LocalPlayer.aiStep: the key starts sprinting while moving forward, and it keeps sprinting // until it stops moving forward, sneaks, gets hungry or runs into a wall, even after letting go of the key #update_sprinting(input: MoveInput, sin: number, cos: number) { - const can_sprint = input.z < 0 && !input.sneak && this.food > SPRINT_MIN_FOOD; + // like minecraft, flying sprints even hungry, and sneaking to go down doesn't stop it + const can_sprint = input.z < 0 && (this.flying || (!input.sneak && this.food > SPRINT_MIN_FOOD)); // how far it's looking into what it hit in the last move, the way it's facing is -sin, -cos const into_wall = sin * this.colliding_x + cos * this.colliding_z; if (!can_sprint || into_wall > SPRINT_WALL_ANGLE) { diff --git a/client/packet_listener.ts b/client/packet_listener.ts index aa6d11f..a359a73 100644 --- a/client/packet_listener.ts +++ b/client/packet_listener.ts @@ -84,6 +84,9 @@ export class ClientPacketListener { case "attack_speed": client.player.attack_speed = message.value; break; + case "flying": + client.player.flying = message.flying; + break; case "effects": client.player.effects = message.effects; break; diff --git a/common/protocol.ts b/common/protocol.ts index 0e23660..1fbe0cc 100644 --- a/common/protocol.ts +++ b/common/protocol.ts @@ -241,6 +241,8 @@ export type ServerMessage = | { type: "vitals"; health: number; max_health: number; food: number; saturation: number; air: number } // the player's own attack speed attribute, when it changes, for the attack indicator under the crosshair | { type: "attack_speed"; value: number } + // whether the player flies, see /fly, when it changes + | { type: "flying"; flying: boolean } // the player's own effects that show an icon, when they change. duration is in ticks, -1 for forever, and the // client counts it down itself, so it isn't sent every tick | { type: "effects"; effects: { id: string; amplifier: number; duration: number }[] } diff --git a/docs/modding/misc/commands.md b/docs/modding/misc/commands.md index 8b689df..1695b06 100644 --- a/docs/modding/misc/commands.md +++ b/docs/modding/misc/commands.md @@ -115,6 +115,7 @@ Commands can only be registered during `setup`. | `/time add ` | Moves time forward | | `/locatebiome ` | Says where the nearest biome of that kind is, up to 6400 blocks away | | `/tp ` | Teleports you there | +| `/fly` | Toggles flying: jump goes up, sneak goes down, sprint goes faster | | `/tps` | Shows how the game loop keeps up | | `/skin [url] [slim\|normal]` | Changes your skin to a 64×64 PNG, or back to the default | | `/help [command]` | Every command's usage, or one's | diff --git a/server/game/game_server.ts b/server/game/game_server.ts index 907c93b..970bf6a 100644 --- a/server/game/game_server.ts +++ b/server/game/game_server.ts @@ -1283,6 +1283,16 @@ export class GameServer { ), ); + // TODO: only let operators do this + register( + literal("fly").describe("Toggles flying").executes((context) => { + const player = context.source.server_player; + player.flying = !player.flying; + player.fall_distance = 0; + context.source.send_message(player.flying ? "You can fly" : "You can't fly anymore"); + }), + ); + register( literal("tps").describe("Shows how the game loop keeps up").executes((context) => { if (!this.loop) { @@ -1497,6 +1507,10 @@ export class GameServer { player.fall_distance = 0; return; } + if (player.flying) { + player.fall_distance = 0; + return; + } const vitals = player.vitals; const in_liquid = this.#in_liquid(x, y, z); @@ -1649,6 +1663,8 @@ export class GameServer { ); const attack_speed = player.attribute(ATTRIBUTES.attack_speed); sync("attack_speed", attack_speed, () => ({ type: "attack_speed", value: attack_speed })); + const flying = player.flying; + sync("flying", flying, () => ({ type: "flying", flying })); const effects = [...player.effects.values()] .filter((effect) => effect.show_icon) .map(({ type, amplifier, duration }) => ({ id: type.id, amplifier, duration })); diff --git a/server/game/player.ts b/server/game/player.ts index 259d41a..81a5a0b 100644 --- a/server/game/player.ts +++ b/server/game/player.ts @@ -33,6 +33,8 @@ export interface SavedPlayer { // base64 png, see /skin skin?: string; slim?: boolean; + // see /fly + flying?: boolean; } // the most ticks the server moves a knocked back player, in case they never land, like in water @@ -53,6 +55,8 @@ export class ServerPlayer extends LivingEntity { override eye_height = PLAYER_EYE_HEIGHT; // lying flat in the short box, as their client says. their client decides, like where they are swimming = false; + // flying, from /fly. their client moves them without gravity, and they don't fall or get hungry from moving + flying = false; inventory = new Container(INVENTORY_SIZE); // hunger and saturation, health is the entity's @@ -225,6 +229,7 @@ export class ServerPlayer extends LivingEntity { vitals: { health: this.health, ...this.vitals.save() }, skin: this.skin, slim: this.slim || undefined, + flying: this.flying || undefined, }; } @@ -240,5 +245,6 @@ export class ServerPlayer extends LivingEntity { this.load_health(saved.vitals?.health ?? this.max_health); this.skin = saved.skin; this.slim = saved.slim ?? false; + this.flying = saved.flying === true; } } diff --git a/tests/vitals_test.ts b/tests/vitals_test.ts index 5f9cf16..4922713 100644 --- a/tests/vitals_test.ts +++ b/tests/vitals_test.ts @@ -223,3 +223,17 @@ Deno.test("/tp x y z teleports you there", async () => { t.send(1, { type: "chat", text: "/tp 10 120.5 -3" }); assertEquals([t.player.x, t.player.y, t.player.z], [10, 120.5, -3]); }); + +Deno.test("/fly toggles flying, which is sent to the player, and flying doesn't fall", async () => { + const t = await setup(); + t.take(1); + t.send(1, { type: "chat", text: "/fly" }); + assert(t.player.flying); + t.game.tick(); + assert(t.take(1).some((m: Msg) => m.type === "flying" && m.flying), "the player hears they fly"); + t.move(110); + for (let y = 109; y >= 100; y--) t.move(y); + assertEquals(t.player.health, MAX_HEALTH); + t.send(1, { type: "chat", text: "/fly" }); + assert(!t.player.flying); +});