import { Engine , NullEngine , Scene , Vector3 , OimoJSPlugin , ArcRotateCamera } from 'babylonjs' ; import { Area } from './objects/area' ; import { Player } from './objects/player' ; import { Router } from './router' ; import { players , PlayerState } from './state' ; import { CONFIG } from './../config' ; export class World { private engine : Engine ; private scene : Scene ; private area : Area ; private playerObjs : { [ id ] : Player ; } = { } ; constructor ( ) { this . engine = new NullEngine ( ) ; this . scene = new Scene ( this . engine ) ; this . area = new Area ( this . scene ) ; } public init ( ) { var camera = new ArcRotateCamera ( "Camera" , 0 , 0.8 , 100 , Vector3 . Zero ( ) , this . scene ) ; const OIMO = require ( "oimo" ) ; this . scene . enablePhysics ( new Vector3 ( 0 , - 20 , 0 ) , new OimoJSPlugin ( undefined , OIMO ) ) ; this . area . init ( ) ; this . scene . executeWhenReady ( ( ) => { this . engine . runRenderLoop ( ( ) => { this . scene . render ( ) ; } ) ; setInterval ( ( ) => { Object . keys ( this . playerObjs ) . forEach ( ( key ) => { let lv = this . playerObjs [ key ] . playerMesh . physicsImpostor . getLinearVelocity ( ) ; let av = this . playerObjs [ key ] . playerMesh . physicsImpostor . getAngularVelocity ( ) ; let pos = this . playerObjs [ key ] . playerMesh . position ; players [ key ] . angularVelocity = av ; players [ key ] . linearVelocity = lv ; players [ key ] . position = pos ; } ) ; Router . updateWorld ( ) ; } , ( 1000 / CONFIG . server_update_rate ) ) ; } ) ; } public applyMovment ( key , data ) { let dir = this . createVector ( data . direction ) ; let force = data . force ; this . playerObjs [ key ] . applyMovement ( dir , force ) ; } public createPlayer ( id , name ) { let player = new Player ( this . scene ) ; player . init ( id ) ; this . playerObjs [ id ] = player ; let playerState = new PlayerState ( ) ; playerState . linearVelocity = player . playerMesh . physicsImpostor . getLinearVelocity ( ) ; playerState . angularVelocity = player . playerMesh . physicsImpostor . getAngularVelocity ( ) ; playerState . name = name ; playerState . uuid = id ; playerState . position = player . playerMesh . position ; players [ id ] = playerState ; } public removePlayer ( id ) { let ps = players [ id ] ; delete players [ id ] ; let player = this . playerObjs [ id ] ; if ( player !== undefined ) player . playerMesh . dispose ( ) ; delete this . playerObjs [ id ] ; } private createVector ( config ) { let vec = new Vector3 ( config . x , config . y , config . z ) ; return vec ; } }