I have separated all code in my game into game model and logic and UI logic (using JME). I’m trying to find an elegant way of getting the game objects referenced when a user interacts with a Spatial
. I’ve tried the following:
-
Currently I extend
Node
toItemNode
,EnemyNode
(etc.) storing a reference to the relevant game object. I then use instanceof to work out what the Node actually is. This seems a hack. -
I have tried using
setUserData
but it assumes a certain serialisation model that doesn’t apply to my game objects (I use a custom serialisation). -
I have also tried storing the reference in the associated controls (
ItemControl
,EnemyControl
etc.) and then finding out if the Spatial is a specific type of object usinggetControl(ItemControl.class)
. Then I can get the game object usingcontrol.getItem()
or equivalent. This seems a pretty indirect method to me but perhaps it’s what is intended?
What would be most elegant (IMHO) is if there was a generic version of Node
that actually referenced the game object:
class GameNode<T> extends Node {
GameNode(T entity) { ... }
public T getEntity() { ... }
}
So that I could, for example, say:
Node itemNode = new GameNode<Item>(item);
itemNode.addControl(new ItemControl(item));
Has something like that been attempted by anyone?
Any guidance would be appreciated.