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 Jon123

I was wondering if there was a way of getting a collection of possible points in the game and randomly pick one as a spawn point for another entity?

I need to avoid collision tiles and other entities before I spawn a random entity.

Thanks
Jon

1 decade ago by stillen

Can you give some more details of what you are looking to do?

You could set up one entity that has an array of points/locations and that then spawns an entity. The array could be resorted before each time the spawning function happens. That would be somewhat random.

You could also, depending on the logic needed, place a bunch of place holder entities in the level builder, that don't have any draw functions. This would allow you to place them where ever needed.

Then have a game function that gets all entities by type, and then randomly spawns the coin at one of those locations.

1 decade ago by Jon123

Yeah I was thinking of having entities that just acted as spawn locations, which is probably the easiest idea.

I initially wanted coins to appear anywhere in the game, which would require a lot of spawn point entities manually being placed side-by-side using the Weltmeister.

I was hoping to access an array of tiles from the game and determine which tiles were collision tiles/free space. I could then pick one at random to act as a spawn point.

1 decade ago by stillen

You can loop through the array of background or collision tiles, whichever works better for your game logic and then make grab their coordinates to use as spawning.

For example, if you loop through the collision layer tile array, and do a check if that tile is a collision tile. If it is not, then you have a spawn point.

http://impactjs.com/documentation/class-reference/collisionmap

1 decade ago by JayJayBird

Here's some code from the "HTML5 Game Development with ImpactJS" book I read:

For the entity check (x & y are the random spawn coordinates for your coin):

       getEntitiesAt: function(x, y) {
            var n = ig.game.entities.length;
            var ents = [];
            
            for(i = 0; i < n; i++) {
                var ent = ig.game.entities[i];
                x0 = ent.pos.x;
                x1 = x0 + ent.size.x;
                y0 = ent.pos.y;
                y1 = y0 + ent.size.y;
                
                if (x0 <= x && x1 > x && y0 <= y && y1 > y) {return true;}
            }
            return false;
       }

For the collision map check:

       CollisionCheck: function(x, y) {
            var Map = ig.game.collisionMap;
            var coin = new EntityCoin();
            var res = Map.trace(x, y, x + coin.size.x, y + coin.size.y, coin.size.x, coin.size.y);
                                                                                                                                    // true if there's a collision on either x or y axis
            return res.collision.x || res.collision.y;
       }

If both return false, you have a safe spawn location.

1 decade ago by Jon123

Ah thanks, this is what I was trying to find!

1 decade ago by JayJayBird

You're welcome. :-)
Page 1 of 1
« first « previous next › last »