146 lines
4.5 KiB
TypeScript
146 lines
4.5 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
import { physics } from "$/common/physics.ts";
|
|
import type { ClientLevel } from "../level/client_level.ts";
|
|
|
|
// anything that exists in the level and moves, like minecraft's Entity. position is the middle of its feet.
|
|
// it's simulated in fixed ticks (common/constants.ts), frames draw it between its last two positions
|
|
export abstract class Entity {
|
|
id: string;
|
|
level: ClientLevel;
|
|
|
|
x = 0;
|
|
y = 0;
|
|
z = 0;
|
|
// where it was at the start of the tick, what frames interpolate from
|
|
prev_x = 0;
|
|
prev_y = 0;
|
|
prev_z = 0;
|
|
// blocks per second
|
|
vx = 0;
|
|
vy = 0;
|
|
vz = 0;
|
|
yaw = 0;
|
|
pitch = 0;
|
|
|
|
// the collision box, width is used for both x and z
|
|
width: number;
|
|
height: number;
|
|
eye_height: number;
|
|
// blocks per second squared
|
|
gravity = -32;
|
|
// what's left of its speed after each tick in the air, like minecraft's 0.98. it caps falling speed
|
|
// at 49 * -gravity * TICK_DELTA, 78 blocks per second with minecraft's gravity
|
|
drag = 0.98;
|
|
// what it walks up onto without jumping, see move_body
|
|
step_height = 0;
|
|
// it can still step up this many ticks after leaving the ground
|
|
step_grace_ticks = 0;
|
|
// ticks since it was last on the ground
|
|
#air_ticks = 0;
|
|
// how far the limbs have swung and how hard they swing (0 to 1), like minecraft's WalkAnimationState
|
|
walk_position = 0;
|
|
walk_speed = 0;
|
|
prev_walk_speed = 0;
|
|
|
|
// which way it hit something on each axis in the last move: 1 or -1, 0 for nothing.
|
|
// 1 on y means it's standing on something
|
|
colliding_x = 0;
|
|
colliding_y = 0;
|
|
colliding_z = 0;
|
|
|
|
constructor(level: ClientLevel, id: string, width: number, height: number, eye_height: number) {
|
|
this.level = level;
|
|
this.id = id;
|
|
this.width = width;
|
|
this.height = height;
|
|
this.eye_height = eye_height;
|
|
}
|
|
|
|
get on_ground() {
|
|
return this.colliding_y === 1;
|
|
}
|
|
|
|
// in water, like the server's Entity.in_water
|
|
get in_water() {
|
|
return physics.in_water(this, (x, y, z) => this.level.is_fluid(x, y, z));
|
|
}
|
|
|
|
// its eyes are in water
|
|
get eyes_in_water() {
|
|
return physics.eyes_in_water(this, this.eye_height, (x, y, z) => this.level.is_fluid(x, y, z));
|
|
}
|
|
|
|
// after moving each tick. full speed is 5 blocks per second, a bit faster than walking
|
|
update_walk_animation() {
|
|
const moved = Math.hypot(this.x - this.prev_x, this.z - this.prev_z);
|
|
this.prev_walk_speed = this.walk_speed;
|
|
this.walk_speed += (Math.min(moved * 4, 1) - this.walk_speed) * 0.4;
|
|
this.walk_position += this.walk_speed;
|
|
}
|
|
|
|
walk_animation(partial_tick: number) {
|
|
return {
|
|
position: this.walk_position - this.walk_speed * (1 - partial_tick),
|
|
speed: this.prev_walk_speed + (this.walk_speed - this.prev_walk_speed) * partial_tick,
|
|
};
|
|
}
|
|
|
|
// jumps there, without interpolating from where it was
|
|
set_position(x: number, y: number, z: number) {
|
|
this.x = this.prev_x = x;
|
|
this.y = this.prev_y = y;
|
|
this.z = this.prev_z = z;
|
|
}
|
|
|
|
save_previous_position() {
|
|
this.prev_x = this.x;
|
|
this.prev_y = this.y;
|
|
this.prev_z = this.z;
|
|
}
|
|
|
|
// where to draw it, partial_tick is how far the frame is between the last tick and the next
|
|
render_position(partial_tick: number) {
|
|
return {
|
|
x: this.prev_x + (this.x - this.prev_x) * partial_tick,
|
|
y: this.prev_y + (this.y - this.prev_y) * partial_tick,
|
|
z: this.prev_z + (this.z - this.prev_z) * partial_tick,
|
|
};
|
|
}
|
|
|
|
// how high the eyes are, for the camera. a frame is partial_tick of the way to the next tick
|
|
render_eye_height(_partial_tick: number) {
|
|
return this.eye_height;
|
|
}
|
|
|
|
// how much wider the view is from its eyes, like minecraft widening it while sprinting
|
|
render_fov_scale(_partial_tick: number) {
|
|
return 1;
|
|
}
|
|
|
|
// one step of the game, TICK_DELTA seconds
|
|
abstract tick(): void;
|
|
|
|
// what's left of its sideways speed after each tick, from where it was at the start of the tick like minecraft,
|
|
// so the tick it jumps still has the ground's friction
|
|
horizontal_friction() {
|
|
return this.drag;
|
|
}
|
|
|
|
// falls and moves by its velocity for one tick (see move_body), then slows down from drag and friction
|
|
move() {
|
|
const is_solid = (x: number, y: number, z: number) => this.level.collision_shape(x, y, z);
|
|
const can_step = this.#air_ticks <= this.step_grace_ticks && this.vy <= 0;
|
|
const friction = this.horizontal_friction();
|
|
const collisions = physics.move_body(this, this.gravity, is_solid, this.step_height, can_step);
|
|
this.colliding_x = collisions.x;
|
|
this.colliding_y = collisions.y;
|
|
this.colliding_z = collisions.z;
|
|
this.#air_ticks = this.on_ground ? 0 : this.#air_ticks + 1;
|
|
this.vx *= friction;
|
|
this.vy *= this.drag;
|
|
this.vz *= friction;
|
|
}
|
|
}
|