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 fugufish

have a question about design:

am making a 2d side scrolling shooter like Biolab.

on EACH enemy entity, I have the following code to track the distance btw. player and enemy.

		update:function(){
			var player = ig.game.getEntitiesByType( EntityPlayer )[0];
			if( player ) {
				var ydist=Math.abs(player.pos.y-this.pos.y);
				var xdist=Math.abs(player.pos.x-this.pos.x);
                                
                                // do stuff, like fire upon the player
                        }

if I have 50 entities in a level, all positioned at different areas, this means that that's 50 'get' calls on my Player. Is this too much for the machine to handle?

Any alternative to this method?

1 decade ago by fugufish

my alternative:

activate ( or spawn) the enemy ONLY when the camera (or screen) is within the specified area.

this means as our player progresses through the level, enemies are spawned just a little 'beyond' the camera's (or screen's) window, giving the same effect as pre-spawning all enemies.

This also saves more computing power.

1 decade ago by dominic

Calculating the distance to another entity is quite fast. You can easily do that a couple of thousand times per frame. What (potentially) makes your code slow, is the call to .getEntitiesByType(). This functions scans through all entities each time it is called.

Instead, consider giving the Player entity a name (e.g. &039;player') and then use #.getEntityByName('player'). This should be much faster.

You could also save a reference to the player directly in your game instance and then refer to it from anywhere. E.g.:

// In your Game class
loadLevel: function( data ) {
	this.parent( data );
	
	// Find the player once at level load
	this.player = this.getEntitiesByType( EntityPlayer )[0];
}


…


// In your entities
update: function(){
	if( ig.game.player ) {
		var ydist=Math.abs(ig.game.player.pos.y-this.pos.y);
		var xdist=Math.abs(ig.game.player.pos.x-this.pos.x);
		
		// btw: there's also a "distanceTo()" method 
		// for entities:
		var dist = this.distanceTo( ig.game.player );
		
		// do stuff, like fire upon the player
	}
}

1 decade ago by BFresh

I&039;ve been working on finalizing my game and making it playable on mobile phones in a number of ways and just wanted to say that each of my enemy entities had plenty of #.getEntitiesByType() references to find my player to find distance to attack, etc. Making just once reference to my player in my main game instance and using that reference inside all of my entities as well as only updating my entities only if the player is within a screen distance of them has made it night and day on a mobile. I no longer have the lag of a level full of entities doing their thing each frame and it's awesome!
Page 1 of 1
« first « previous next › last »