In my game, I've created a list of gameObjects (instances of other classes such as Player) in my Main that I can loop through and render in one function, rather than rendering them all individually. This is my render function;
this.gameObjects = [];
addObj(obj){
  this.gameObjects.push(obj);
}
render(){
  this.gameObjects.forEach(obj => obj.render());
}This works no problem if I want to add another gameObject using my main class like this;
let main = new Main();
let player = new Player(10, 30, 10, 10, 'red');
main.addObject(player);
main.start();However, I want to add another gameObject, called Projectile within my Player class. The problem is my Player class doesn't have access to Main's addObj function.
My idea was to create a singleton, that holds a list of game objects, like this;
class Repository{
  constructor(){
    if(this.instance){
      return this.instance;
    }
    
    this.list = [];
    this.instance = this;
  }
  
  get obj(){...}
  addObj(obj){...}
}The tl:dr of the question is;
- Is this the best way to go forward?
- How would I access this singleton? By importing it everywhere I needed it?
- Create a global variable with an instance? How would I do this?
- Am I over thinking this? Should I just pass main in to each class and access its methods that way?
 
    