From 902e62ff4fdd9b5bd26ee7d5be9ccae2b051f248 Mon Sep 17 00:00:00 2001 From: RSamaium Date: Thu, 30 Nov 2023 21:04:03 +0100 Subject: [PATCH] feat: inject function --- .../client/src/Effects/TransitionScene.ts | 4 +++- packages/client/src/Gui/Gui.ts | 10 ++++---- packages/client/src/KeyboardControls.ts | 8 ++++--- packages/client/src/Renderer.ts | 16 +++++++------ packages/client/src/RpgClientEngine.ts | 13 +++++++--- packages/client/src/Scene/Map.ts | 10 ++++---- packages/client/src/Scene/Scene.ts | 12 ++++++---- packages/client/src/Tilemap/index.ts | 6 ++--- packages/client/src/clientEntryPoint.ts | 6 ++--- packages/client/src/index.ts | 4 +++- packages/common/src/AbstractObject.ts | 3 +-- packages/common/src/Game.ts | 7 +++++- packages/common/src/Inject.ts | 15 ++++++++++++ packages/common/src/index.ts | 3 ++- packages/sample2/main/scene-map.ts | 4 +--- packages/server/src/Game/Map.ts | 1 - packages/server/src/Player/Player.ts | 21 ++++++++++------ packages/server/src/Scenes/Map.ts | 5 ++-- packages/server/src/entry-point.ts | 24 ++++++++++--------- packages/server/src/index.ts | 1 + packages/server/src/server.ts | 8 +++---- 21 files changed, 112 insertions(+), 69 deletions(-) create mode 100644 packages/common/src/Inject.ts diff --git a/packages/client/src/Effects/TransitionScene.ts b/packages/client/src/Effects/TransitionScene.ts index 678a0d34..eb1558c4 100644 --- a/packages/client/src/Effects/TransitionScene.ts +++ b/packages/client/src/Effects/TransitionScene.ts @@ -3,14 +3,16 @@ import { RpgClientEngine } from "../RpgClientEngine"; import { FrameOptions } from "../Sprite/Spritesheet"; import { Timeline } from "./Timeline"; import { Container } from "pixi.js" +import { inject } from "@rpgjs/common"; export class TransitionScene { private frameIndex: number = 0 private animations: FrameOptions[][] = [] private updateSubscription: Subscription private complete: Function = () => {} + private clientEngine: RpgClientEngine = inject(RpgClientEngine) - constructor(private clientEngine: RpgClientEngine, private container: Container) { } + constructor(private container: Container) { } addFadeIn() { return this.addFading(1, 0) diff --git a/packages/client/src/Gui/Gui.ts b/packages/client/src/Gui/Gui.ts index 5a30785d..fe4a0ac0 100644 --- a/packages/client/src/Gui/Gui.ts +++ b/packages/client/src/Gui/Gui.ts @@ -1,4 +1,4 @@ -import { RpgCommonPlayer, Utils } from '@rpgjs/common' +import { RpgCommonPlayer, Utils, inject } from '@rpgjs/common' import { RpgSound } from '../Sound/RpgSound' import { RpgClientEngine, RpgResource } from '../index' import { RpgRenderer } from '../Renderer' @@ -36,10 +36,10 @@ export class Gui { public currentScene: Scene | null = null private librariesInstances: any[] = [] - async _initialize(clientEngine: RpgClientEngine, guiEl: HTMLDivElement) { - this.clientEngine = clientEngine - this.renderer = clientEngine.renderer - this.gameEngine = clientEngine.gameEngine + async _initialize(guiEl: HTMLDivElement) { + this.clientEngine = inject(RpgClientEngine) + this.renderer = inject(RpgRenderer) + this.gameEngine = inject(GameEngineClient) const { gui } = this.renderer.options for (let ui of gui) { diff --git a/packages/client/src/KeyboardControls.ts b/packages/client/src/KeyboardControls.ts index 5808f4c3..0a6fd9f9 100644 --- a/packages/client/src/KeyboardControls.ts +++ b/packages/client/src/KeyboardControls.ts @@ -1,4 +1,4 @@ -import { DefaultInput, Direction, Input, Utils } from '@rpgjs/common' +import { DefaultInput, Direction, Input, Utils, inject } from '@rpgjs/common' import { ControlOptions, Controls } from '@rpgjs/types'; import { RpgClientEngine } from './RpgClientEngine'; @@ -191,6 +191,8 @@ const inverseKeyCodeTable = inverse(keyCodeTable) type BoundKey = { actionName: string, options: ControlOptions, parameters?: any } export class KeyboardControls { + private clientEngine: RpgClientEngine = inject(RpgClientEngine) + private keyState: { [keyName: string]: { isDown: boolean, @@ -204,8 +206,8 @@ export class KeyboardControls { private lastKeyPressed: number | null = null private _controlsOptions: Controls = {} - constructor(private clientEngine: RpgClientEngine) { - const { globalConfig } = clientEngine + constructor() { + const { globalConfig } = this.clientEngine this.setupListeners(); this.setInputs({ ...DefaultInput, diff --git a/packages/client/src/Renderer.ts b/packages/client/src/Renderer.ts index b422efd6..f3d18266 100644 --- a/packages/client/src/Renderer.ts +++ b/packages/client/src/Renderer.ts @@ -1,4 +1,4 @@ -import { RpgPlugin, HookClient, Utils } from '@rpgjs/common' +import { RpgPlugin, HookClient, Utils, inject } from '@rpgjs/common' import { SceneMap } from './Scene/Map' import { Scene } from './Scene/Scene' import { Scene as PresetScene } from './Presets/Scene' @@ -23,6 +23,9 @@ enum ContainerName { } export class RpgRenderer { + private clientEngine: RpgClientEngine = inject(RpgClientEngine) + private gameEngine: GameEngineClient = inject(GameEngineClient) + public vm: ComponentPublicInstance public app: App public readonly stage: Container = new Container() @@ -38,7 +41,6 @@ export class RpgRenderer { private _height: number = 400 private canvasEl: HTMLElement private selector: HTMLElement - private gameEngine: GameEngineClient = this.clientEngine.gameEngine private loadingScene = { transitionIn: new Subject(), transitionOut: new Subject() @@ -47,7 +49,7 @@ export class RpgRenderer { private prevObjectScene: any = {} public transitionMode: TransitionMode = TransitionMode.Fading - constructor(private clientEngine: RpgClientEngine) { + constructor() { this.clientEngine.tick.subscribe(({ timestamp, deltaRatio, frame, deltaTime }) => { this.draw(timestamp, deltaTime, deltaRatio, frame) }) @@ -135,7 +137,7 @@ export class RpgRenderer { this.fadeContainer.visible = false this.fadeContainer.alpha = 0 - await RpgGui._initialize(this.clientEngine, this.guiEl) + await RpgGui._initialize(this.guiEl) this.resize() } @@ -205,7 +207,7 @@ export class RpgRenderer { this.loadingScene.transitionOut.complete() } if (this.transitionMode == TransitionMode.Fading) { - new TransitionScene(this.clientEngine, this.fadeContainer) + new TransitionScene(this.fadeContainer) .addFadeOut() .onComplete(finish) .start() @@ -232,7 +234,7 @@ export class RpgRenderer { switch (name) { case PresetScene.Map: const sceneClass = scenes[PresetScene.Map] || SceneMap - this.scene = new sceneClass(this.gameEngine, this.renderer, { + this.scene = new sceneClass(this.renderer, { screenWidth: this.renderer.screen.width, screenHeight: this.renderer.screen.height, drawMap: this.options.drawMap @@ -248,7 +250,7 @@ export class RpgRenderer { RpgPlugin.emit(HookClient.AfterSceneLoading, this.scene) } if (this.transitionMode == TransitionMode.Fading) { - new TransitionScene(this.clientEngine, this.fadeContainer) + new TransitionScene(this.fadeContainer) .addFadeIn() .onComplete(finish) .start() diff --git a/packages/client/src/RpgClientEngine.ts b/packages/client/src/RpgClientEngine.ts index 0a0b1c06..871f4197 100644 --- a/packages/client/src/RpgClientEngine.ts +++ b/packages/client/src/RpgClientEngine.ts @@ -15,6 +15,7 @@ import { RpgCommonMap, Scheduler, Control, + inject, } from '@rpgjs/common' import { RpgSound } from './Sound/RpgSound' import { SceneMap } from './Scene/Map' @@ -48,6 +49,7 @@ export class RpgClientEngine { * * @prop {RpgRenderer} [renderer] * @readonly + * @deprecated Use `inject(RpgRenderer)` instead. Will be removed in v5 * @memberof RpgClientEngine * */ public renderer: RpgRenderer @@ -74,6 +76,7 @@ export class RpgClientEngine { * Get the class managing the keyboard * * @prop {KeyboardControls} [controls] + * @deprecated Use `inject(KeyboardControls)` instead. Will be removed in v5 * @readonly * @memberof RpgClientEngine * */ @@ -107,6 +110,10 @@ export class RpgClientEngine { private serverFps: number = 60 private scheduler: Scheduler = new Scheduler() private _serverUrl: string = '' + /** + * * @deprecated Use `inject(GameEngineClient)` instead. Will be removed in v5 + */ + public gameEngine = inject(GameEngineClient) /** * Read objects synchronized with the server @@ -124,7 +131,7 @@ export class RpgClientEngine { envs?: object = {} - constructor(public gameEngine: GameEngineClient, private options) { + constructor(private options) { this.envs = options.envs || {} this.tick.subscribe(({ timestamp, deltaTime }) => { if (timestamp != -1) this.step(timestamp, deltaTime) @@ -132,7 +139,7 @@ export class RpgClientEngine { } private async _init() { - this.renderer = new RpgRenderer(this) + this.renderer = inject(RpgRenderer) const pluginLoadRessource = async (hookName: string, type: string) => { const resource = this.options[type] || [] @@ -180,7 +187,7 @@ export class RpgClientEngine { } } - this.controls = new KeyboardControls(this) + this.controls = inject(KeyboardControls) } private addResource(resourceClass, cb) { diff --git a/packages/client/src/Scene/Map.ts b/packages/client/src/Scene/Map.ts index 856a3130..8cc0e9bb 100644 --- a/packages/client/src/Scene/Map.ts +++ b/packages/client/src/Scene/Map.ts @@ -1,14 +1,13 @@ -import { RpgCommonMap, RpgPlugin, HookClient, RpgShape, Utils, RpgCommonPlayer } from '@rpgjs/common' +import { RpgCommonMap, RpgPlugin, HookClient, RpgShape, Utils, RpgCommonPlayer, inject } from '@rpgjs/common' import TileMap from '../Tilemap' import * as _PixiViewport from 'pixi-viewport' import { type Viewport } from 'pixi-viewport' import { Scene, SceneObservableData, SceneSpriteLogic } from './Scene' import { spritesheets } from '../Sprite/Spritesheets' import { RpgSound } from '../Sound/RpgSound' -import { GameEngineClient } from '../GameEngine' import { TiledLayerType, TiledMap } from '@rpgjs/tiled' import { RpgComponent } from '../Components/Component' -import { CameraOptions } from '@rpgjs/types' +import { type CameraOptions } from '@rpgjs/types' import { Assets, Container, Point, IRenderer, DisplayObjectEvents, utils, FederatedPointerEvent } from 'pixi.js' import { EventLayer } from './EventLayer' @@ -53,10 +52,9 @@ export class SceneMap extends Scene { shapes = {} constructor( - public game: GameEngineClient, private renderer: IRenderer, private options: { screenWidth?: number, screenHeight?: number, drawMap?: boolean } = {}) { - super(game) + super() if (options.drawMap === undefined) this.options.drawMap = true this.onInit() } @@ -97,7 +95,7 @@ export class SceneMap extends Scene { RpgCommonMap.bufferClient.set(obj.id, this.gameMap) - this.tilemap = new TileMap(this.gameMap.getData(), this.game.renderer) + this.tilemap = new TileMap(this.gameMap.getData()) // TODO: Remove this Assets.reset() diff --git a/packages/client/src/Scene/Scene.ts b/packages/client/src/Scene/Scene.ts index 7d1f5869..1c912540 100644 --- a/packages/client/src/Scene/Scene.ts +++ b/packages/client/src/Scene/Scene.ts @@ -1,4 +1,4 @@ -import { RpgPlugin, HookClient, DefaultInput } from '@rpgjs/common' +import { RpgPlugin, HookClient, DefaultInput, inject } from '@rpgjs/common' import { KeyboardControls } from '../KeyboardControls' import RpgSprite from '../Sprite/Character' import { Animation } from '../Effects/Animation' @@ -29,7 +29,7 @@ export abstract class Scene { protected objects: Map = new Map() protected animationLayer: Container = new Container() - private controls: KeyboardControls + private controls: KeyboardControls = inject(KeyboardControls) private animations: Animation[] = [] private _data: BehaviorSubject = new BehaviorSubject({ @@ -37,6 +37,11 @@ export abstract class Scene { partial: {} }) + /** + * @deprecated Use `inject(GameEngineClient)` instead. Will be removed in v5 + */ + public game: GameEngineClient = inject(GameEngineClient) + /** * Listen to the movement of objects on stage * @@ -63,9 +68,8 @@ export abstract class Scene { [key: string]: any }> = new Subject() - constructor(public game: GameEngineClient) { + constructor() { const { globalConfig } = this.game.clientEngine - this.controls = this.game.clientEngine.controls const mergeInputs = { ...DefaultInput, ...(globalConfig.inputs || {}) diff --git a/packages/client/src/Tilemap/index.ts b/packages/client/src/Tilemap/index.ts index a43fc90f..9817aabc 100644 --- a/packages/client/src/Tilemap/index.ts +++ b/packages/client/src/Tilemap/index.ts @@ -1,4 +1,4 @@ -import { Utils } from '@rpgjs/common' +import { Utils, inject } from '@rpgjs/common' import ImageLayer from './ImageLayer' import TileLayer from './TileLayer' import TileSet from './TileSet' @@ -25,9 +25,9 @@ export default class TileMap extends Container { } = {} private tilesLayer: Container = new Container() private frameTile: number = 0 + private renderer: RpgRenderer = inject(RpgRenderer) - - constructor(private data: MapInfo, private renderer: RpgRenderer) { + constructor(private data: MapInfo) { super() this.x = 0 this.y = 0 diff --git a/packages/client/src/clientEntryPoint.ts b/packages/client/src/clientEntryPoint.ts index 513c0ebc..00dab5bb 100644 --- a/packages/client/src/clientEntryPoint.ts +++ b/packages/client/src/clientEntryPoint.ts @@ -1,4 +1,4 @@ -import { HookClient, loadModules, ModuleType } from '@rpgjs/common' +import { HookClient, inject, loadModules, ModuleType } from '@rpgjs/common' import { GameEngineClient } from './GameEngine' import { RpgClientEngine } from './RpgClientEngine' @@ -142,7 +142,5 @@ export default (modules: ModuleType[], options: RpgClientEntryPointOptions): Rpg } }) - const gameEngine = new GameEngineClient() - const clientEngine = new RpgClientEngine(gameEngine, options) - return clientEngine + return inject(RpgClientEngine, [options]) } \ No newline at end of file diff --git a/packages/client/src/index.ts b/packages/client/src/index.ts index f2ceb701..fbc869a4 100644 --- a/packages/client/src/index.ts +++ b/packages/client/src/index.ts @@ -16,6 +16,7 @@ export { SceneMap as RpgSceneMap } from './Scene/Map' export { RpgGui } from './Gui/Gui'; export { Timeline, Ease } from './Effects/Timeline'; export { RpgComponent, RpgComponent as RpgSprite } from './Components/Component' +export { KeyboardControls } from './KeyboardControls' export { World, room } from 'simple-room-client' import { spritesheets } from './Sprite/Spritesheets' @@ -23,4 +24,5 @@ import { sounds } from './Sound/Sounds' export const RpgResource = { spritesheets, sounds -} \ No newline at end of file +} +export { inject } from '@rpgjs/common' \ No newline at end of file diff --git a/packages/common/src/AbstractObject.ts b/packages/common/src/AbstractObject.ts index 3c266df1..a9df4ed9 100644 --- a/packages/common/src/AbstractObject.ts +++ b/packages/common/src/AbstractObject.ts @@ -53,12 +53,11 @@ export class AbstractObject { // notifier for destroy _destroy$: Subject = new Subject() - static get ACTIONS() { return ACTIONS } - constructor(private gameEngine: RpgCommonGame, public playerId: string) { + constructor(public gameEngine: RpgCommonGame, public playerId: string) { this._hitboxPos = new SAT.Vector(0, 0) this.setHitbox(this.width, this.height) this.position = { x: 0, y: 0, z: 0 } diff --git a/packages/common/src/Game.ts b/packages/common/src/Game.ts index d0480eb1..1f8ea040 100644 --- a/packages/common/src/Game.ts +++ b/packages/common/src/Game.ts @@ -39,7 +39,12 @@ export class RpgCommonGame extends EventEmitter { let event if (!playerId) playerId = generateUID() if (isClass(_class)) { - event = new _class(this, playerId) + if (this.side == GameSide.Client) { + event = new _class(this, playerId) + } + else { + event = new _class(playerId) + } } else { event = _class diff --git a/packages/common/src/Inject.ts b/packages/common/src/Inject.ts new file mode 100644 index 00000000..560e95ef --- /dev/null +++ b/packages/common/src/Inject.ts @@ -0,0 +1,15 @@ +const cacheInstances: { [key: string]: any } = {}; + +export function inject(service: new (...args: any[]) => T, args: any[] = []): T { + if (cacheInstances[service.name]) { + return cacheInstances[service.name] as T; + } + const instance = new service(...args); + if (instance['initialize']) instance['initialize'](...args) + cacheInstances[service.name] = instance; + return instance; +} + +export interface InjectInit { + initialize(...args: any[]): any +} \ No newline at end of file diff --git a/packages/common/src/index.ts b/packages/common/src/index.ts index 0b6f52f7..8e510dec 100644 --- a/packages/common/src/index.ts +++ b/packages/common/src/index.ts @@ -21,4 +21,5 @@ export { RpgCommonWorldMaps } from './WorldMaps' export { Vector2d } from './Vector2d' export { Direction } from '@rpgjs/types' export { transitionColor } from './Color' -export { DefaultInput } from './DefaultInput' \ No newline at end of file +export { DefaultInput } from './DefaultInput' +export { inject, InjectInit } from './Inject' \ No newline at end of file diff --git a/packages/sample2/main/scene-map.ts b/packages/sample2/main/scene-map.ts index 5bcc2a26..1ef525ae 100644 --- a/packages/sample2/main/scene-map.ts +++ b/packages/sample2/main/scene-map.ts @@ -2,9 +2,7 @@ import { RpgSceneMapHooks, RpgSceneMap, RpgGui } from '@rpgjs/client' const sceneMap: RpgSceneMapHooks = { onAfterLoading(scene) { - scene.on('click', (scene) => { - console.log(scene) - }) + }, } diff --git a/packages/server/src/Game/Map.ts b/packages/server/src/Game/Map.ts index b4d26021..950a3d9b 100644 --- a/packages/server/src/Game/Map.ts +++ b/packages/server/src/Game/Map.ts @@ -405,7 +405,6 @@ export class RpgMap extends RpgCommonMap { const ev = this.game.addEvent(event) const _shape = shape || this.getEventShape(ev.name) ev.map = this.id - ev.server = this._server ev.width = event.width || this.tileWidth ev.height = event.height || this.tileHeight if (_shape && _shape.properties) ev.properties = _shape.properties diff --git a/packages/server/src/Player/Player.ts b/packages/server/src/Player/Player.ts index 361a7c59..7341fc2a 100644 --- a/packages/server/src/Player/Player.ts +++ b/packages/server/src/Player/Player.ts @@ -1,4 +1,4 @@ -import { RpgCommonPlayer, Utils, RpgPlugin, RpgCommonGame, RpgCommonMap, Direction } from '@rpgjs/common' +import { RpgCommonPlayer, Utils, RpgPlugin, RpgCommonGame, RpgCommonMap, Direction, inject } from '@rpgjs/common' import { Room, WorldClass } from 'simple-room' import { RpgMap, EventPosOption } from '../Game/Map' import { Query } from '../Query' @@ -200,6 +200,7 @@ export class RpgPlayer extends RpgCommonPlayer { * ``` * @title Server Instance * @prop {RpgServerEngine} player.server + * @deprecated Use `inject(RpgServerEngine)` instead. Will be removed in v5 * @memberof Player * */ public server: RpgServerEngine @@ -218,10 +219,10 @@ export class RpgPlayer extends RpgCommonPlayer { position: Position } | undefined - constructor(gameEngine: RpgCommonGame, playerId: string) { - super(gameEngine, playerId) - this.initialize() - } + /** + * @deprecated Use `inject(RpgCommonGame)` instead. Will be removed in v5 + */ + public gameEngine: RpgCommonGame = inject(RpgCommonGame) // redefine type (as RpgPlayer) get otherPlayersCollision(): RpgPlayer[] { @@ -238,8 +239,14 @@ export class RpgPlayer extends RpgCommonPlayer { // a flag that lets the client know if the event is suppressed. The client can, for example, end animations before completely deleting the object (client side). deleted: boolean = false + constructor(public playerId: string) { + super(inject(RpgCommonGame), playerId) + this.initialize() + } + /** @internal */ initialize() { + this.server = inject(RpgServerEngine) this.expCurve = { basis: 30, extra: 20, @@ -1004,8 +1011,8 @@ export class RpgEvent extends RpgPlayer { mode: EventMode playerRelated: RpgPlayer | null = null - constructor(gameEngine: RpgCommonGame, playerId: string) { - super(gameEngine, playerId) + constructor(playerId: string) { + super(playerId) } async execMethod(methodName: string, methodData = []) { diff --git a/packages/server/src/Scenes/Map.ts b/packages/server/src/Scenes/Map.ts index 85f3101c..0f3a1809 100644 --- a/packages/server/src/Scenes/Map.ts +++ b/packages/server/src/Scenes/Map.ts @@ -1,5 +1,5 @@ -import { HookServer, RpgCommonMap, RpgPlugin, Utils } from '@rpgjs/common' +import { HookServer, RpgCommonMap, RpgPlugin, Utils, inject } from '@rpgjs/common' import { World } from 'simple-room' import { isTiledFormat, TiledMap } from '@rpgjs/tiled' import { MapOptions, MapData } from '../decorators/map' @@ -28,8 +28,9 @@ export class SceneMap { [mapId: string]: RpgClassMap } = {} private worldMaps: Map = new Map() + private server: RpgServerEngine = inject(RpgServerEngine) - constructor(sceneMapObject: SceneMapObject, private server: RpgServerEngine) { + constructor(sceneMapObject: SceneMapObject) { const { maps, worldMaps, events } = sceneMapObject this.maps = maps this.mapsById = {} diff --git a/packages/server/src/entry-point.ts b/packages/server/src/entry-point.ts index 4efefd29..e79502e4 100644 --- a/packages/server/src/entry-point.ts +++ b/packages/server/src/entry-point.ts @@ -1,4 +1,4 @@ -import { RpgCommonGame, HookServer, loadModules, ModuleType, GameSide, RpgPlugin } from '@rpgjs/common' +import { RpgCommonGame, HookServer, loadModules, ModuleType, GameSide, RpgPlugin, inject } from '@rpgjs/common' import { RpgServerEngine } from './server' import { RpgPlayer } from './Player/Player' import { RpgMatchMaker } from './MatchMaker' @@ -39,7 +39,7 @@ interface RpgServerEntryPointOptions { } export default async function (modules: ModuleType[], options: RpgServerEntryPointOptions): Promise { - const gameEngine = new RpgCommonGame(GameSide.Server) + const gameEngine = inject(RpgCommonGame, [GameSide.Server]) if (!options.globalConfig) options.globalConfig = {} @@ -91,14 +91,16 @@ export default async function (modules: ModuleType[], options: RpgServerEntryPoi return mod }) - const serverEngine = new RpgServerEngine(options.io, gameEngine, { - debug: {}, - updateRate: 10, - stepRate: 60, - timeoutInterval: 0, - countConnections: false, - playerProps, - ...options - }) + const serverEngine = inject(RpgServerEngine, [ + options.io, { + debug: {}, + updateRate: 10, + stepRate: 60, + timeoutInterval: 0, + countConnections: false, + playerProps, + ...options + } + ]) return serverEngine } diff --git a/packages/server/src/index.ts b/packages/server/src/index.ts index a2a15f8f..cbc1f36b 100644 --- a/packages/server/src/index.ts +++ b/packages/server/src/index.ts @@ -28,3 +28,4 @@ export { RpgMatchMaker } from './MatchMaker' export type { IStoreState } from './Interfaces/StateStore' export { Components } from './Player/ComponentManager' export { Gui } from './Gui/Gui' +export { inject } from '@rpgjs/common' diff --git a/packages/server/src/server.ts b/packages/server/src/server.ts index 62e040a8..e32a918c 100644 --- a/packages/server/src/server.ts +++ b/packages/server/src/server.ts @@ -3,7 +3,7 @@ import { RpgPlayer } from './Player/Player' import { Query } from './Query' import { DAMAGE_SKILL, DAMAGE_PHYSIC, DAMAGE_CRITICAL, COEFFICIENT_ELEMENTS } from './presets' import { World, WorldClass, Transport } from 'simple-room' -import { Utils, RpgPlugin, Scheduler, HookServer, RpgCommonGame, DefaultInput } from '@rpgjs/common' +import { Utils, RpgPlugin, Scheduler, HookServer, RpgCommonGame, DefaultInput, inject } from '@rpgjs/common' import { Observable } from 'rxjs'; import { Tick } from '@rpgjs/types'; import { Actor, Armor, Class, DatabaseTypes, Item, Skill, State, Weapon } from '@rpgjs/database'; @@ -49,6 +49,7 @@ export class RpgServerEngine { protected totalConnected: number = 0 private scheduler: Scheduler = new Scheduler() private playerProps: any + public gameEngine: RpgCommonGame = inject(RpgCommonGame) world: WorldClass = World workers: any @@ -60,7 +61,7 @@ export class RpgServerEngine { * @prop {Socket Io Server} [io] * @memberof RpgServerEngine */ - constructor(public io, public gameEngine: RpgCommonGame, public inputOptions) { + constructor(public io, public inputOptions) { this.envs = inputOptions.envs || {} if (this.inputOptions.workers) { console.log('workers enabled') @@ -387,12 +388,11 @@ export class RpgServerEngine { if (!existingUser) { const { token } = socket.handshake.auth - player = new RpgPlayer(this.gameEngine, playerId) + player = new RpgPlayer(playerId) player.session = token this.world.setUser(player, socket) - player.server = this player._init() if (!token) {