attempt to fix glitchy, not yet
This commit is contained in:
parent
3084fefc4f
commit
16cffd0b25
@ -11,6 +11,8 @@ import {
|
|||||||
import { GameEvents } from '../types/events';
|
import { GameEvents } from '../types/events';
|
||||||
import { GameplayComponent } from './gameplay';
|
import { GameplayComponent } from './gameplay';
|
||||||
|
|
||||||
|
const INVERSE_MAX_FPS = 0.016;
|
||||||
|
|
||||||
export class Game extends Engine {
|
export class Game extends Engine {
|
||||||
public events = new EventEmitter<GameEvents>();
|
public events = new EventEmitter<GameEvents>();
|
||||||
|
|
||||||
@ -29,10 +31,14 @@ export class Game extends Engine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
loop(now: number): void {
|
loop(now: number): void {
|
||||||
const delta = this.getDelta(now);
|
let delta = this.getDelta(now);
|
||||||
this.running && requestAnimationFrame((ts) => this.loop(ts));
|
this.running && requestAnimationFrame((ts) => this.loop(ts));
|
||||||
this.update(delta);
|
|
||||||
this.render.render();
|
this.render.render();
|
||||||
|
|
||||||
|
while (delta >= INVERSE_MAX_FPS) {
|
||||||
|
this.update(0.016);
|
||||||
|
delta -= INVERSE_MAX_FPS;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async loadLevel(path: string) {
|
async loadLevel(path: string) {
|
||||||
|
@ -8,11 +8,12 @@ import {
|
|||||||
EventEmitter,
|
EventEmitter,
|
||||||
Renderer,
|
Renderer,
|
||||||
randomUUID,
|
randomUUID,
|
||||||
|
SpawnEvent,
|
||||||
} from '@freeblox/engine';
|
} from '@freeblox/engine';
|
||||||
|
|
||||||
import { Quaternion, Vector3 } from 'three';
|
import { Quaternion, Vector3 } from 'three';
|
||||||
import { ThirdPersonCamera } from './camera';
|
import { ThirdPersonCamera } from './camera';
|
||||||
import { GameEvents, SpawnEvent } from '../types/events';
|
import { GameEvents } from '../types/events';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gameplay manager.
|
* Gameplay manager.
|
||||||
@ -71,21 +72,9 @@ export class GameplayComponent extends EngineComponent {
|
|||||||
|
|
||||||
this.look.copy(this.move).normalize();
|
this.look.copy(this.move).normalize();
|
||||||
this.character?.localToWorld(this.look);
|
this.character?.localToWorld(this.look);
|
||||||
|
|
||||||
this.character?.setVelocity(this.move);
|
|
||||||
|
|
||||||
const look = this.move.clone().normalize();
|
const look = this.move.clone().normalize();
|
||||||
if (this.move.length()) {
|
|
||||||
this.character?.setLook(look);
|
|
||||||
}
|
|
||||||
|
|
||||||
let jump = false;
|
|
||||||
if (this.jump) {
|
|
||||||
jump = true;
|
|
||||||
this.jump = false;
|
|
||||||
this.character?.jump();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
let jump = this.jump;
|
||||||
if (!this.move.equals(this.prevVelocity) || jump !== this.prevJump) {
|
if (!this.move.equals(this.prevVelocity) || jump !== this.prevJump) {
|
||||||
this.events.emit('sendPlayer', {
|
this.events.emit('sendPlayer', {
|
||||||
playerId: this.uuid,
|
playerId: this.uuid,
|
||||||
@ -95,12 +84,24 @@ export class GameplayComponent extends EngineComponent {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.character?.setVelocity(this.move);
|
||||||
|
|
||||||
|
if (this.move.length()) {
|
||||||
|
this.character?.setLook(look);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.jump) {
|
||||||
|
this.jump = false;
|
||||||
|
this.character?.jump();
|
||||||
|
}
|
||||||
|
|
||||||
this.prevVelocity.copy(this.move);
|
this.prevVelocity.copy(this.move);
|
||||||
this.prevJump = jump;
|
this.prevJump = jump;
|
||||||
}
|
}
|
||||||
|
|
||||||
override dispose(): void {
|
override dispose(): void {
|
||||||
this.cleanUpEvents?.();
|
this.cleanUpEvents?.();
|
||||||
|
this.server.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async loadCharacter(
|
public async loadCharacter(
|
||||||
|
@ -43,6 +43,7 @@ export class PhysicsWorldComponent extends EngineComponent {
|
|||||||
const gravity = new Vector3(0, this.world.gravity, 0);
|
const gravity = new Vector3(0, this.world.gravity, 0);
|
||||||
const world = new physicsEngine.World(gravity);
|
const world = new physicsEngine.World(gravity);
|
||||||
this.physicsWorld = world;
|
this.physicsWorld = world;
|
||||||
|
this.physicsWorld.timestep = 0.016;
|
||||||
this.physicsEngine = physicsEngine;
|
this.physicsEngine = physicsEngine;
|
||||||
this.initializePhysicsScene();
|
this.initializePhysicsScene();
|
||||||
});
|
});
|
||||||
@ -65,12 +66,16 @@ export class PhysicsWorldComponent extends EngineComponent {
|
|||||||
const object = this.trackedObjects.find(
|
const object = this.trackedObjects.find(
|
||||||
(obj) => event.object === obj.uuid
|
(obj) => event.object === obj.uuid
|
||||||
) as PhysicsObject;
|
) as PhysicsObject;
|
||||||
if (!object) return;
|
if (!object || !object.rigidBody) return;
|
||||||
object.rigidBody?.setTranslation(event.position, false);
|
if (object instanceof Humanoid) {
|
||||||
object.rigidBody?.setRotation(event.quaternion, false);
|
object.synchronize(event);
|
||||||
event.velocity && object.rigidBody?.setLinvel(event.velocity, false);
|
} else {
|
||||||
event.angularVelocity &&
|
object.rigidBody?.setTranslation(event.position, false);
|
||||||
object.rigidBody?.setAngvel(event.angularVelocity, false);
|
object.rigidBody?.setRotation(event.quaternion, false);
|
||||||
|
event.velocity && object.rigidBody?.setLinvel(event.velocity, false);
|
||||||
|
event.angularVelocity &&
|
||||||
|
object.rigidBody?.setAngvel(event.angularVelocity, false);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const characterMoveEvent = (event: PlayerEvent) => {
|
const characterMoveEvent = (event: PlayerEvent) => {
|
||||||
|
@ -16,6 +16,7 @@ import { NameTag } from './nametag.object';
|
|||||||
import { CanvasUtils } from '../canvas/utils';
|
import { CanvasUtils } from '../canvas/utils';
|
||||||
import { PhysicsTicking } from '../physics';
|
import { PhysicsTicking } from '../physics';
|
||||||
import type Rapier from '@dimforge/rapier3d';
|
import type Rapier from '@dimforge/rapier3d';
|
||||||
|
import { ServerTransformEvent } from '..';
|
||||||
|
|
||||||
export type HumanoidBodyPart =
|
export type HumanoidBodyPart =
|
||||||
| 'Head'
|
| 'Head'
|
||||||
@ -210,6 +211,11 @@ export class Humanoid extends GameObject implements PhysicsTicking {
|
|||||||
tick(dt: number): void {
|
tick(dt: number): void {
|
||||||
if (!this.ready) return;
|
if (!this.ready) return;
|
||||||
|
|
||||||
|
// Stick to ground
|
||||||
|
if (this.grounded) {
|
||||||
|
this._appliedGravity.y = 0;
|
||||||
|
}
|
||||||
|
|
||||||
// Apply rigidbody transforms to object from last process tick
|
// Apply rigidbody transforms to object from last process tick
|
||||||
if (this.rigidBody) {
|
if (this.rigidBody) {
|
||||||
this.parent!.position.copy(this.rigidBody.translation() as any);
|
this.parent!.position.copy(this.rigidBody.translation() as any);
|
||||||
@ -240,11 +246,6 @@ export class Humanoid extends GameObject implements PhysicsTicking {
|
|||||||
this.parent?.lookAt(this._currentLookAt);
|
this.parent?.lookAt(this._currentLookAt);
|
||||||
|
|
||||||
this.applyRotation(this.parent!.quaternion);
|
this.applyRotation(this.parent!.quaternion);
|
||||||
|
|
||||||
// Stick to ground
|
|
||||||
if (this.grounded) {
|
|
||||||
this._appliedGravity.y = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setWalkAnimationState(index: number) {
|
setWalkAnimationState(index: number) {
|
||||||
@ -285,6 +286,15 @@ export class Humanoid extends GameObject implements PhysicsTicking {
|
|||||||
this.detach();
|
this.detach();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
synchronize(event: ServerTransformEvent) {
|
||||||
|
if (!this.rigidBody) return;
|
||||||
|
this.parent!.position.copy(this.rigidBody.translation() as any);
|
||||||
|
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() {
|
private createNameTag() {
|
||||||
if (this.nameTag) {
|
if (this.nameTag) {
|
||||||
this.nameTag.dispose();
|
this.nameTag.dispose();
|
||||||
|
@ -109,6 +109,12 @@ export class GameSocket implements Disposable {
|
|||||||
console.log('player joined', playerId, playerName);
|
console.log('player joined', playerId, playerName);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case PacketType.PLAYER_QUIT: {
|
||||||
|
const playerId = incoming.read(String)!;
|
||||||
|
const playerName = incoming.read(String)!;
|
||||||
|
console.log('player quit', playerId, playerName);
|
||||||
|
break;
|
||||||
|
}
|
||||||
case PacketType.PLAYER_CHARACTER: {
|
case PacketType.PLAYER_CHARACTER: {
|
||||||
const playerId = incoming.read(String)!;
|
const playerId = incoming.read(String)!;
|
||||||
const playerName = incoming.read(String)!;
|
const playerName = incoming.read(String)!;
|
||||||
|
@ -73,8 +73,8 @@ export interface ServerTransformEvent {
|
|||||||
object: string;
|
object: string;
|
||||||
position: Vector3;
|
position: Vector3;
|
||||||
quaternion: Quaternion;
|
quaternion: Quaternion;
|
||||||
velocity?: Vector3;
|
velocity: Vector3;
|
||||||
angularVelocity?: Vector3;
|
angularVelocity: Vector3;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface PlayerEvent {
|
export interface PlayerEvent {
|
||||||
|
Loading…
Reference in New Issue
Block a user