This commit is contained in:
Evert Prants 2023-06-28 22:12:55 +03:00
parent 16cffd0b25
commit 195eab1e7d
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
4 changed files with 17 additions and 7 deletions

View File

@ -11,10 +11,12 @@ import {
import { GameEvents } from '../types/events';
import { GameplayComponent } from './gameplay';
const INVERSE_MAX_FPS = 0.016;
const INVERSE_MAX_FPS = 1000 / 60;
const INVERSE_MAX_FPS_S = 1 / 60;
export class Game extends Engine {
public events = new EventEmitter<GameEvents>();
private delta = 0;
mount(element: HTMLElement): void {
super.mount(element);
@ -31,14 +33,20 @@ export class Game extends Engine {
}
loop(now: number): void {
let delta = this.getDelta(now);
this.getDelta(now);
this.running && requestAnimationFrame((ts) => this.loop(ts));
this.render.render();
while (delta >= INVERSE_MAX_FPS) {
this.update(0.016);
delta -= INVERSE_MAX_FPS;
while (this.delta >= INVERSE_MAX_FPS) {
this.update(INVERSE_MAX_FPS_S);
this.delta -= INVERSE_MAX_FPS;
}
this.render.render();
}
getDelta(now: number): number {
this.delta += now - this.lastTick;
this.lastTick = now;
return this.delta;
}
async loadLevel(path: string) {

View File

@ -25,6 +25,7 @@ export class PhysicsWorldComponent extends EngineComponent {
}
update(delta: number): void {
if (!this.physicsWorld) return;
// FIXME: physics is tied to the FPS
this.physicsWorld?.step();
for (const object of this.trackedObjects) object.tick(delta);

View File

@ -36,6 +36,7 @@ export class Humanoid extends GameObject implements PhysicsTicking {
private _maxHealth = 100;
private _velocity = new Vector3(0, 0, 0);
private _appliedGravity = new Vector3(0, 0, 0);
private _adjustedPosition = new Vector3(0, 0, 0);
private _grounded = true;
private _lookAt = new Vector3(0, 0, 1);
private _currentLookAt = new Vector3(0, 0, 1);
@ -292,7 +293,6 @@ export class Humanoid extends GameObject implements PhysicsTicking {
this.parent!.quaternion.copy(this.rigidBody.rotation() as any);
this.rigidBody?.setTranslation(event.position, false);
this.rigidBody?.setRotation(event.quaternion, false);
this.rigidBody?.setNextKinematicTranslation(event.velocity);
}
private createNameTag() {

View File

@ -15,5 +15,6 @@ export enum PacketType {
PLAYER_CHARACTER,
PLAYER_MOVEMENT,
PLAYER_CHAT,
PLAYER_EVENT,
ERROR,
}