Quote:
Originally Posted by JustinMind
I can't follow your code properly since some important parts are missing, but i assume you are not following the modularization principle. As Wurzelhüpfer suggested you should create a service (conventionally named as any.service.ts) and use the auto-injection done by Angular via the constructor:
Code:
constructor(private anyService: AnyService){}
To make this service available you have 2 options:
The prefered option (which makes services lazy-loadable aswell:
In your stage.service.ts
Code:
\@Injectable({
providedIn: 'root'
})
or alternatively call your service in the app.module.ts and your service keeps being a singleton.
EDIT:As i read your question once more my answer is not what you are looking for, i guess.
Can you share a simple example online, so we can reproduce it?
|
The thing is that the game was made in full ES6 javascript, so you can do something like this:
Stage.js
Code:
class Stage{
constructor(){
this._entities = new Set();
}
initGame(){
this.addEntity(new Player(...));
}
static get Instance() {
if (this._instance === undefined) {
this._instance = new Stage();
}
return this._instance;
}
addEntity(e){
this._entities.add(e);
}
destroy(e){
this._entities.delete(e);
}
Code:
class Bullet{
...
}
Code:
class Player{
fire(){
Stage.Instance.addEntity(new Bullet(...));
}
}
So the main.js file will look something like this:
Code:
Stage.Instance.initGame();
In plain javascript Stage class is loaded, then it creates a Player Entity, and that player instance uses Stage singleton to create another Entity bullet.
So what i did is create an angular component, and a bunch of classes for every javascript file, translate them to ts and try to use singleton pattern, but the way that angular works won't let me do that.
Injecting a service in every class that uses the singleton will be my approach, wish me luck and thx for your suggestions.