Change how ticking works

This commit is contained in:
2026-09-25 16:13:38 -03:00
parent 213363d0be
commit 5fb23cf404
9 changed files with 169 additions and 92 deletions
+11 -11
View File
@@ -3,8 +3,8 @@ import { InputManager } from "../input_manager.ts";
import { ClientInventories } from "../inventory.ts";
import { Player } from "./player.ts";
// how often the position goes to the server
const SEND_POSITION_INTERVAL = 1 / 10;
// the position goes to the server every other tick
const SEND_POSITION_TICKS = 2;
// the player this client controls, like minecraft's LocalPlayer
export class LocalPlayer extends Player {
@@ -14,22 +14,22 @@ export class LocalPlayer extends Player {
move_speed = 4;
jump_force = 6.7;
#send_timer = 0;
#ticks_since_sent = 0;
constructor(client: Client, id: string, name: string) {
super(client.level, id, name);
this.client = client;
}
tick(delta: number) {
tick() {
if (!this.client.screen) {
this.#apply_input();
}
this.move(delta);
this.#send_position(delta);
this.move();
this.#send_position();
}
// turns with the mouse, like minecraft's MouseHandler.turnPlayer
// turns with the mouse every frame, not every tick, like minecraft's MouseHandler.turnPlayer
turn(mouse_dx: number, mouse_dy: number) {
this.yaw += -mouse_dx * 0.001;
this.pitch += -mouse_dy * 0.001;
@@ -75,12 +75,12 @@ export class LocalPlayer extends Player {
}
}
#send_position(delta: number) {
this.#send_timer += delta;
if (this.#send_timer < SEND_POSITION_INTERVAL) {
#send_position() {
this.#ticks_since_sent += 1;
if (this.#ticks_since_sent < SEND_POSITION_TICKS) {
return;
}
this.#send_timer = 0;
this.#ticks_since_sent = 0;
this.client.connection.send({
type: "move",
x: this.x,