Jump to content

multiple enemies, group or single?


Recommended Posts

I am working on a little game, just for my own personal use and learning and I came up with an interesting question that I can't seem to find the answer to. I will have a player and a bunch of enemies, now the enemies will be of 5 different types (different sprites, but the same basic rules as far as health and interaction logic), so I started thinking, would it be better to have an enemies group where each enemy gets added to the group, then in the update I would do something like...


update() {
    this.game.physics.arcade.overlap(this.player, this.enemies, this.hitPlayer, null, this);
    // the rest of the logic for moving the player and shooting and whatnot

 since each enemy will have the same logic in terms of how much damage it does and whether or not it killed the player, or died.

or should I have each enemy be their own entity and check for overlapping on each one like this...


update() {
    this.game.physics.arcade.overlap(this.player, this.spikeEnemy, this.hitPlayer, null, this);
    this.game.physics.arcade.overlap(this.player, this.creeperEnemy, this.hitPlayer, null, this);
    this.game.physics.arcade.overlap(this.player, this.hoppingEnemy, this.hitPlayer, null, this);
    //rest of the logic here

Essentially I am trying to find out if the engine would prefer to have all enemies in a single group, then check for overlap, or if it is easier on the engine to have them as individual entities, then check for overlap.


I realize that making more calls is generally less productive, but does anyone know the trade-off point? At what point is it easier and less intensive for the engine to check on a group vs several single entities?

This is only about the memory consumption and what the engine prefers, not necessarily coding standards or pattern standards.

I am really hoping that someone has some good insight on this, I am sure I am not the only person to ever try to determine which is the best route to take. Thank you all for your time and I appreciate any help or insight anyone might have. 

Link to comment
Share on other sites

I would use groups. That's why they exist in the first place so that code become more legible. Besides the point that you raised consider the following: can the enemies hit each other? If YES then you will have to do permutations between each of the entities which will make for very long code. I would just use 3 main groups :

- for the player

- for the non-player entities (npe)

- for the non-player objects (npo)

Then add groups for other "stuff" like bullets, weather (rain, snow), hazards( fire, ice, water) , fog of war.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...