Improve physics

This commit is contained in:
schmelczerandras 2020-10-06 09:26:56 +02:00
parent 8b87b68dae
commit ec0b700313
14 changed files with 181 additions and 71 deletions

View file

@ -2,12 +2,19 @@ import ioserver, { Socket } from 'socket.io';
import express from 'express';
import { Server } from 'http';
import cors from 'cors';
import { applyArrayPlugins, Random, TransportEvents, deserializeCommand } from 'shared';
import {
applyArrayPlugins,
Random,
TransportEvents,
deserializeCommand,
StepCommand,
} from 'shared';
import './index.html';
import { Player } from './players/player';
import { PhysicalContainer } from './physics/containers/physical-container';
import { createDungeon } from './map/create-dungeon';
import { glMatrix } from 'gl-matrix';
import { DeltaTimeCalculator } from './helper/delta-time-calculator';
glMatrix.setMatrixArrayType(Array);
@ -26,6 +33,8 @@ objects.initialize();
const app = express();
const deltaTimeCalculator = new DeltaTimeCalculator();
app.use(
cors({
origin: (origin, callback) => {
@ -50,7 +59,6 @@ app.get('/', function (req, res) {
io.on('connection', (socket: SocketIO.Socket) => {
socket.on(TransportEvents.PlayerJoining, () => {
const player = new Player(objects, socket);
socket.on(TransportEvents.PlayerToServer, (text: string) => {
const command = deserializeCommand(JSON.parse(text));
player.sendCommand(command);
@ -71,3 +79,14 @@ io.on('connection', (socket: SocketIO.Socket) => {
server.listen(port, () => {
console.log(`server started at http://localhost:${port}`);
});
const handlePhysics = () => {
const delta = deltaTimeCalculator.getNextDeltaTimeInMilliseconds();
const step = new StepCommand(delta);
objects.sendCommand(step);
setTimeout(handlePhysics, 5);
};
handlePhysics();