I'm using a LevelManager to load Tiled maps in my libGDX game, which stores:
Static:
- final
Size
of map in Tiles - final
Size
of map in Pixels - final
Size
of an individual tile TiledMap ACTIVE_MAP
World WORLD
When the map is loaded, it creates a new World and stores it in the static WORLD
, later populating it with static bodies based on objects found in the map's Collision layer.
My reasoning for using static is down to the fact that logically, only one map will be playable at any one time, however I've got it in my head that only constants and singletons should be static, so this feels strange for me. Is there a better, more efficient way of handling this?
Should I have one World per Level, or a global World independent of what TiledMap is active?
In other words, should I initialize WORLD
once and leave it as is, making it final, or continue what I am doing and initialize it as a new World whenever a new TiledMap is loaded?
How do I correctly dispose of all bodies within the World, essentially clearing it?
I'm going to have to do this every time I load a new map, regardless of whether or not WORLD
is global.
constants and singletons should be static
, this is not true, constants arestatic final
. Static has nothing to do with constant, it is just a shared variable all across yourLevelManager
class, its value can change. If you follow java naming conventions you should name itactiveMap
notACTIVE_MAP
, since its not final, just static. Also in java you don't have to dispose anything. It has an automated garbage collector. \$\endgroup\$ – dimitris93 Jul 13 '15 at 14:27world.dispose()
function for that. Does it not do what you require ? If not, you could iterate through every single body and callworld.destroyBody(body)
, even though I am pretty sure that is whatworld.dispose()
should be doing and perhaps even more. \$\endgroup\$ – dimitris93 Jul 13 '15 at 14:46