import { Room , Client } from "colyseus" ; import { Entity } from "./Entity" ; import { State } from "./State" ; interface MouseMessage { x : number ; y : number ; } export class ArenaRoom extends Room < State > { onCreate ( ) { this . setState ( new State ( ) ) ; this . state . initialize ( ) ; this . onMessage ( "mouse" , ( client , message ) => { const entity = this . state . entities [ client . sessionId ] ; if ( ! entity ) { console . log ( "DEAD PLAYER ACTING..." ) ; return ; } const dst = Entity . distance ( entity , message as Entity ) ; entity . speed = ( dst < 20 ) ? 0 : Math . min ( dst / 15 , 4 ) ; entity . angle = Math . atan2 ( entity . y - message . y , entity . x - message . x ) ; } ) ; this . setSimulationInterval ( ( ) => this . state . update ( ) ) ; } onJoin ( client , options ) { console . log ( client . sessionId , "JOINED" ) ; this . state . createPlayer ( client . sessionId ) ; } onLeave ( client ) { console . log ( client . sessionId , "LEFT!" ) ; const entity = this . state . entities [ client . sessionId ] ; if ( entity ) { entity . dead = true ; } } }