Jump to content
  • Advertisement
Sign in to follow this  
MatejaS

Java initialize array to another

This topic is 1482 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!