Java initialize array to another

Started by
0 comments, last by rip-off 9 years, 6 months ago

I am making a top-down game.Inside Player class I have a function:


public void update() { // called every frame
 // some code...
 Tile[] map = level.getMap(); // level is a private member
 int mapWidth = level.getWidth();
 // some collision code...

}

and I have:


public void setLevel(Level level) {
 this.level = level;
}

Would it be faster (not only faster,but better written code) to do it like this (have a temporary array every frame) or to have just map and width stored as privates and then to initialize them in setLevel to level.getMap() and level.getWidth()?

Sorry for bad explanation, or/and if this is a simple question, I just want to check that out.

Thanks,

MatejaS

Advertisement

Personally, even for simple games I prefer for collision to be handled at a higher level than the individual objects involved. In a simple game, objects might have hard-coded responses to collision (e.g. bullet hits player, remove bullet and call player.hurt()). In larger games, an object can be notified when a collision occurs and can determine its own reaction. Larger games again typically separate physical simulation from game objects entirely by introducing a physics engine.

For smallish games, the actually collision detection might be in the Level object, or even a higher object again such as a "World" object which is aware of both the level and the game entities - it can be nicer to let the Level concentrate on handling tiles and not worrying about the game objects too.

I'm not sure the two approaches you mention specifically make a huge performance difference, other than having a collision logic scattered into a set of different game objects complicates adding broad algorithmic optimisations (e.g. spatial partitioning). However, a third alternative to the approaches mentioned is to avoid storing any state in the Player but instead pass either the level or the Tiles to the update() method as parameters.

This topic is closed to new replies.

Advertisement