Fly command
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user