import { Camera } from "./camera" ; import { Lights } from "./lights" ; import { RouterService } from "./routing/router.service" ; const LEFT = 65 ; const RIGHT = 68 ; const UP = 87 ; const DOWN = 83 ; const JUMP = 32 ; export class Player { private scene : BABYLON . Scene ; private keyDown : object = { } ; private keyFired : object = { } ; public playerMesh : BABYLON . Mesh ; public id : string ; constructor ( scene , id ) { this . scene = scene ; this . id = id ; } public init ( lights ) { this . playerMesh = BABYLON . Mesh . CreateSphere ( "player_obj" , 12 , 4 , this . scene ) ; this . playerMesh . position . y = 16 ; var playerMaterial = new BABYLON . StandardMaterial ( "player_material" , this . scene ) ; playerMaterial . diffuseTexture = new BABYLON . Texture ( "public/assets/textures/green_tennis_ball.jpg" , this . scene ) ; this . playerMesh . material = playerMaterial ; this . playerMesh . physicsImpostor = new BABYLON . PhysicsImpostor ( this . playerMesh , BABYLON . PhysicsImpostor . SphereImpostor , { mass : 1 , friction : 100 , restitution : 0.35 } , this . scene ) ; lights . addShadowCaster ( this . playerMesh ) ; window . addEventListener ( 'keydown' , ( event ) => { this . keyDownEvt ( event ) ; } , false ) ; window . addEventListener ( 'keyup' , ( event ) => { this . keyUpEvt ( event ) ; } , false ) ; } private keyDownEvt ( evt ) { if ( ! this . keyFired [ evt . keyCode ] ) { this . keyDown [ evt . keyCode ] = true ; this . keyFired [ evt . keyCode ] = true ; } } private keyUpEvt ( evt ) { this . keyDown [ evt . keyCode ] = false ; this . keyFired [ evt . keyCode ] = false ; } private isOnGround ( ) { let isOnGround = false ; let minY = this . playerMesh . getBoundingInfo ( ) . minimum . y + this . playerMesh . position . y ; let pos = this . playerMesh . absolutePosition . clone ( ) ; pos . y = minY ; var ray = new BABYLON . Ray ( this . playerMesh . absolutePosition , new BABYLON . Vector3 ( 0 , - 1 , 0 ) ) ; let counter = 0 ; let info = this . scene . pickWithRay ( ray , ( mesh ) => { return ! ( mesh === this . playerMesh ) ; } ) ; if ( info . hit ) { let pickedY = info . pickedPoint . y ; isOnGround = ( pickedY + 0.2 ) >= minY ; } return isOnGround ; } public applyMovement ( camera , deltaTime ) { let contactPoint = this . playerMesh . absolutePosition . clone ( ) ; contactPoint . y += 20 ; let force = 0.002 * deltaTime ; let direction = camera . getCamDirection ( ) . multiplyByFloats ( force , force , force ) ; if ( this . keyDown [ UP ] ) { this . playerMesh . applyImpulse ( direction , contactPoint ) ; RouterService . sendInteraction ( camera . getCamDirection ( ) , force ) ; } if ( this . keyDown [ DOWN ] ) { this . playerMesh . applyImpulse ( direction . negate ( ) , contactPoint ) ; RouterService . sendInteraction ( camera . getCamDirection ( ) . negate ( ) , force ) ; } if ( this . keyDown [ LEFT ] ) { camera . changeCameraRotation ( 0.06 ) ; } if ( this . keyDown [ RIGHT ] ) { camera . changeCameraRotation ( - 0.06 ) ; } if ( this . keyDown [ JUMP ] ) { this . keyDown [ JUMP ] = false ; if ( this . isOnGround ( ) ) { let jump_direction = new BABYLON . Vector3 ( 0 , 1 , 0 ) ; force = 20 ; let jump = jump_direction . multiplyByFloats ( force , force , force ) ; this . playerMesh . applyImpulse ( direction . negate ( ) , this . playerMesh . absolutePosition ) ; RouterService . sendInteraction ( jump_direction , force ) ; } } } public update ( linearVelocity , angularVelocity , position ) { this . playerMesh . physicsImpostor . setAngularVelocity ( angularVelocity ) ; this . playerMesh . physicsImpostor . setLinearVelocity ( linearVelocity ) ; this . playerMesh . position = position ; } }