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 sasklacz

Hi all. I'd like to make my entities react to player, and start moving/attacking him when he's with their predefined range (like 100 pixels or similar). Any tips how to tackle this ? I'm new to impact so probably I'm missing something basic. Thanks in advance.

1 decade ago by Joncom

/* enemy.js */
viewDistance: 100,
update: function() {
    // Is the player in sight?
    if(this.distanceTo(ig.game.player) <= this.viewDistance) {
        // Start moving toward and attacking the player.
    }
    this.parent();
}

It's a start.

1 decade ago by jurnacsr

Combine the above with Entity's angleTo method
viewDistance: 100,

// the angle this entity can see the player, to each site of the 'front'
viewAngle: 45,
update: function() {
  // Is the player in sight?
  if ((this.distanceTo(ig.game.player) <= this.viewDistance) && (Math.abs(this.angleTo(ig.game.player)) <= this.viewAngle) {
    /// start moving/firing at player
  }
}

You'll have to handle the radians/degrees stuff, but the concept remains the same.

1 decade ago by Joncom

@jurnacsr:
Are you sure that works?
It seems like it would work when facing right, but not when facing left.

/>			</div>
		</div>
			<div class=

1 decade ago by lTyl

// Enemy.js
                var player = ig.game.getEntitiesByType(EntityPlayer)[0],
                       xd = this.pos.x - player.pos.x,
                       yd = this.pos.y - player.pos.y,
                      distance = Math.sqrt(xd*xd + yd*yd);

Square Root isn't the fastest way of doing it, but it works...

1 decade ago by jurnacsr

@Joncom - you may be right. My geo/trig isn't really up to snuff, so maybe the angles would be off.
Could you take 45 degrees from the player's angle? As in, 45 deg +/- from the player's angle?

In fact, the more I think about it, my solution wouldn't work - it would assume that the player always faces right. You would have to test if the other entity's angle is within 45 degrees of the player's current angle.
Page 1 of 1
« first « previous next › last »