Where to create bullets - in Player or in World?

Started by
4 comments, last by L. Spiro 7 years, 1 month ago

Hello all,

I want to create a new bullet when I press 'space'. The easiest way to do it is to put all the logic in the Player class.

On the other hand, I must follow OOP concepts, which means that all entity creation should be handled in one place only and not scattered in different classes because later I may create something in my NPC class and then somewhere else and I will have object creation all over the place.

Now if I want to do everything in the World class, I need to pass a lot of different parameters like playerPos, playerDir, isProjectileLaunched, etc. to the World class, which doesn't look good.

How is this normally done? I want to have object creation in one place and also make the code pretty.

Thanks for reading. Any suggestions are appreciated.

Advertisement

World could have a reference to player (since the player is in (or on?) the world, right?) then you can just grab the needed values player->getPos() etc.

If you have some external class handling your game objects, that one should be what creates the projectile.

Player input shouldn't be handled inside the player class. The input should drive the player from the outside, something like if(is_key_pressed) { player->move(dir, speed) }

I want to create a new bullet when I press 'space'. The easiest way to do it is to put all the logic in the Player class.

For bullet creation, that could be the case, but is bullet movement then also in the Player class?

In general, you follow the principle of responsibility. Something in your program is responsible for handling these small, fast flying objects from birth to death and everything in-between, and it makes sense to move everything related to those objects to that spot. Player doesn't sound like a logical spot, nor does world (world is more like 'glue' to keep everything together to me), but ymmv.

On the other hand, I must follow OOP concepts

This sounds restricting. You basically assume that everything can be properly modeled in OOP, which is not true. If something can be nicely modeled in OOP, then by all means, do that, but if not, imho don't be afraid to deviate to a better solution than OOP would give you.

As for getting access to the object that manages the bullets, you can either give a reference to it during construction of the player (it won't change during the game anyway), or you can ask the world for the object, and then interact with it in your player class.

Why would a bullet care about a 'Player'. The bullet just needs an origin and a direction of flight of flight. The system itself shouldn't care about where those come from. So say you put, the bullet handling code in your player, then further along you decided that now you want to handle bullet impact, audio, effects etc. are those going to be stuffed into the player class too. Although OO principles is not the be cure for everything, there is no substitute for good 'functional decomposition'. Your system should be broken down into functional blocks of a small enough granularity to simplify implementation, and allow it decoupling with the other system(s) it interacts with. I don't know the extent of your project or the other pieces involved, but I suggest you think about how the bullet will interact will all these components and from there you will see where to best locate it.

Hello all,

I want to create a new bullet when I press 'space'. The easiest way to do it is to put all the logic in the Player class.

On the other hand, I must follow OOP concepts, which means that all entity creation should be handled in one place only and not scattered in different classes because later I may create something in my NPC class and then somewhere else and I will have object creation all over the place.

Now if I want to do everything in the World class, I need to pass a lot of different parameters like playerPos, playerDir, isProjectileLaunched, etc. to the World class, which doesn't look good.

How is this normally done? I want to have object creation in one place and also make the code pretty.

Thanks for reading. Any suggestions are appreciated.

I would spawn a SpawnBullet/SpawnResource message and have whatever needs to deal with that message pick it up. This way you can stick the control handling in player or player input handling but not have the responsibility of creating a bullet inside the player. Also this message should carry all needed information to spawn that resource in the world.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

Players don't own bullets; if they did then the bullet would disappear when the player is removed from memory (which may never happen for your character but would happen for enemies often).

Bullets are world objects, just like enemies, your character, ammo drops, the stage/level, the sun, etc.

Your character doesn't spawn bullets, nor handle its own movements. As I outlined here, a character can't know about physics and the world around it. The game world knows what a player and bullets are, and it manages them both as per the rules of game logic and physics.


Bullets are spawned into the scene manager and live their own lives separately from any other object. Just as enemies, items, etc., all are managed by the scene manager and live their own lives.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

This topic is closed to new replies.

Advertisement