Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by Rocket13531

Hi, I'm creating a 2d vertical scrolling game (where you climb up) and want to have a portion of the game "rain" enemy entities at random coordinates (starting above the screen).

The challenge I'm trying to think through, and would like some advice on, is how to spawn entities so that they do not spawn on a collision tile or on top of another entity. I know how to check if a coordinate is/isn't on the collision map, but I don't know how to make sure there is not an entity on a given coordinate.

Any advice regarding this, and/or my approach in general, would be greatly appreciated!

Thanks

1 decade ago by Joncom

Before spawning/raining an entity, look through all the existing entities and check if the random position you've come up with will intersect with any of the entities on the x-axis... If it does, come up with a new random position and try again.

1 decade ago by drhayes

Joncom is right. Don't worry about performance problems involved in that approach until you actually see them.

1 decade ago by Rocket13531

Thanks guys, I appreciate your help! I addressed the collisionMap, but not the entity collisions (yet).

I ended up writing an entity class that creates the falling entities. It checks for available positions just above the screen that do not have collisions. If no insertion points are available, it checks the "row" above...and so on and so forth. If positions are available, I store them in an array and randomly select one of them the spawn the entity there. For anyone who is curious...this is the method that handles most of that:

getSpawnPosGroup: function(row) {
            
            var columns = Math.floor(ig.system.width / this.tileSize);
            
            var rowPosY = ig.game.getScreenPos().top - this.tileSize;
            
            var colPosX = ig.game.getScreenPos().left;
            
            var ret = [];

            for(var i = 0; i < columns; i++) {
            
                var ypos = rowPosY - (row * this.size.y);
            
                var xpos = colPosX + (i * this.tileSize);
            
                if(
                    !ig.game.collisionMap.getTile(xpos, ypos) &&
                    !ig.game.collisionMap.getTile(xpos + this.size.x, ypos) &&
                    !ig.game.collisionMap.getTile(xpos + this.size.y, ypos + this.size.y) &&
                    !ig.game.collisionMap.getTile(xpos, ypos + this.size.y)
                ) {
                    ret.push({x: xpos, y: ypos});
                }
            
            }
            //No spots available on this row. Recursively call this meth incrementing to the next row
            if(ret.length < 1) return this.getSpawnPosGroup(row + 1) ;
            
            
            return ret;
            
        },
Page 1 of 1
« first « previous next › last »