General game architecture questions

All the examples and tutorials I come across somehow store all the game stuff in one class ‘Main’, which extends from SimpleApplication. This is easy and convenient. But it also makes for big, unmaintainable classes.



Now I’m thinking: okay, this player can have his own class. I want to subclass Node so I can attach the Player instance to the scene graph. However, I do want the Player class to load its own models and texture and stuff, but I don’t have access to ‘assetManager’, since that’s only available in Main.



What are good approaches to making assetManager et all available to my other classes? Or maybe there’s a better approach?



All input is greatly appreciated.



And example:



[java]class Player extends Node {

// Load Textures, Models, etc.

// Handle ‘update(tpf)’

}[/java]



[java]class Main extends SimpleApplication {

private Node player;



public void simpleInit() {

player = new Player();

}

}[/java]

I tried a static object for my assetManager.



Example:

[java]

private static AssetManager AMNGR;



public static AssetManager getManager() {

if (AMNGR == null) {

createAssetManager();

}

return AMNGR;

}



private static void createAssetManager() {

AMNGR = JmeSystem.newAssetManager(Thread.currentThread()

.getContextClassLoader().getResource(

“com/jme3/asset/Desktop.cfg”));

}[/java]



But maybe there is an even better approach.

You shouldn’t extend Node for a Player Object.

However a Player object should be aware of it’s graphical representation which is a Node.



your class should look like this





class Player {

private Node model;



public Player(AssetManager assetManager){

// Load Textures, Models, etc.

model=assetManager.loadModel…

}



public Node getModel(){

return model;

}

}





class Main extends SimpleApplication {

private Player player;



public void simpleInit() {

player = new Player();

rootNode.attachchild(player.getModel());

}

}

nehon said:
You shouldn't extend Node for a Player Object.
However a Player object should be aware of it's graphical representation which is a Node.

your class should look like this
`

class Player {
private Node model;

public Player(AssetManager assetManager){
// Load Textures, Models, etc.
model=assetManager.loadModel.....
}

public Node getModel(){
return model;
}
}
`
`
class Main extends SimpleApplication {
private Player player;

public void simpleInit() {
player = new Player();
rootNode.attachchild(player.getModel());
}
}
`

Do you have a reason why it shouldn't be done this way or just a preference?

I have made a character class which extends PhysicsCharacterNode and I derive my Player from character. Inside the character class I have Model mymodel. What would be the downside of structuring my class this way vs just putting a reference to the physicscharacter node inside of character.


Edit: I also made my assetmanager static however I did something like this.

[java]
public class Helper
{
private static AssetManager assetmanager;

public static void setAssetmanager(AssetManager am)
{
assetmanager = am;
}

public static AssetManager getAM()
{
return assetmanager
}}
[/java]
Then inside the simpleapplication after assetmanager has been created i just do
Helper.setAssetManager(this.assetmanager) where this is the simpleapplication class.

There are 1000 ways of doing this really, with none being really the “good” one. Your approach is good too.

Mine is just an example and is surely not THE ONE way of doing this.



I don’t really like static accessors, but…it’s just a matter of taste.