Question about creating Control's and best practices

I’m new to jMonkey and game programming in general. I’ve been reading a lot of tutorials and looking at examples. My general understanding is you use a control to tell your spatial what to do. and you setUserData() to hold the information about the spatial (health, inventory, etc)

What I’m trying to do is set up my game so that I can do things like easily set up 3D levels by just using lines of code like

[java]NPC npc1 = new NPC();
npc1.setStartingLocation(5,2,1); //set location
npc1.setArchetype(NPC.SCARED_ARCHETYPE); //set AI type

NPC npc2 = new NPC();

With the intent that I can either create new NPCs in the initialization of an AppState… OR I can create new NPCs during the game inside of the AppStates update() method if the player does something to cause new NPCs to spawn.

I’ve coded this NPC and have him displaying to the screen, just I don’t know how to organize the code in a logic way. Currently this NPC is made up of a complex system of nodes and subnodes with very specific settings. There’s probably 100 lines of code in the initiliaze() of the AppState class I’m making just loading the model, configuring it, adding the Bullet physics control, creating the animation channel etc…

Then there’s also a lot of code in the update() method, all devoted to this NPC (making the walk animation play when he’s walking etc etc) as well as the AI (right now he just walks in circles but its AI by soime standard)

There’s no way I could feasibly make for instance, 2 of these NPCs. the code would be a horrible mess all over my AppState class.

My question is… how do I pull the initializtion code and the update code out of the AppState and in to its own class? (or classes)

Here’s the best solutions I could come up with. Neither of them really work in my mind

  • For the update code - create my own Control (lets say I call it BraveAIControl) with the AI and animation code. then add the control to the main spatial…

The issue with this being that it feels weird that I would be writing a control that is going to call on other controls of the spatial (the ai code calls on the CharacterControl for physics movement, the animation code calls on the AnimControl for animation). This control is ultimately dependant on the spatial having these other controls added to it (and with the same setting, eg if I created a different NPC with different animation available, BraveAIControl wouln’t work for it)

  • For the initializtion code - I really have no idea where to put this. I don’t want to just throw it in a function in the AppState. I want it to be in a class somewhere so any AppState can spawn an NPC by making a new NPC instance.

I’m sorry about the wall of text, but does anyone have any suggestions on how I can solve this issue? I really can’t find any good examples of jmonkey games that don’t just have all of the game logic splattered in to one big file

The tutorial examples etc are deliberately kept small and self contained, hence all in one file.

I’d have a factory class to build NPCs - that factory class is responsible for getting the correct model, attaching the right controls, etc.

Incidentally if you create a j3o file for your model with animations then an animation and skeleton control are already present, you don’t need to do anything there.

I’d then create an interface and/or abstract base class to be each AI. Then specific AIs can implement their own version of it.

Your innermost npc factory method then looks something like:

private static buildNPC(String modelName, NPCAI ai, whatever else (spawn location?)

That creates the model identified by the string, attaches the AI, spawns it in the correct location, etc.

You then have public methods that abstract this out a bit so you can say createNPC(blah) and it will look up the correct model, Ai type, etc for that NPC. That data can be organised however you like.