Hey, been a while since I've posted here asking for a hand, but this has defeated me.
I'm basically trying to recreate the Mover entity (which is in the entity pack), but have it get to a spot, wait a certain amount of time, then cary on with it's route.
Has anyone done anything like this, or can someone throw me a metaphorical bone?
Thanks :)
EDIT: changed on 2012.03.18
I'm using this for my own prototypes; it's not deeply tested but has worked for simple stuff so far.
For example, say you want to stop your player for 2 seconds, after which he should resume his former course, you'd invoke it like this:
myPlayer.tempSet('vel', {x:0, y:0}, 2);
Inject at own risk:
ig.Entity.inject({
// this.tempSet('speed', 0, 5.5);
tempSet: function(propertyName, tempValue, duration)
{
if (! this.tempChanges)
this.tempChanges = [];
this.tempChanges.push(
{
name: propertyName,
oldValue: this[propertyName],
timer: new ig.Timer(duration)
});
this[propertyName] = tempValue;
},
update: function()
{
if (this.tempChanges)
{
for (var i=0; i<this.tempChanges.length; i++)
{
var change = this.tempChanges[i];
if (change && change.timer.delta() > 0)
{
this[change.name] = change.oldValue;
this.tempChanges[i] = null;
}
}
}
this.parent();
}
});
In your case, looking at the mover's code, you could try (untested):
update: function()
{
var oldDistance = 0;
var target = ig.game.getEntityByName( this.targets[this.currentTarget] );
if (target)
{
oldDistance = this.distanceTo(target);
var angle = this.angleTo(target);
this.vel.x = Math.cos(angle) * this.speed;
this.vel.y = Math.sin(angle) * this.speed;
}
else
{
this.vel.x = 0;
this.vel.y = 0;
}
this.parent();
// Are we close to the target or has the distance actually increased?
// -> Set new target
var newDistance = this.distanceTo(target);
if (target && (newDistance > oldDistance || newDistance < 0.5))
{
this.pos.x = target.pos.x + target.size.x/2 - this.size.x/2;
this.pos.y = target.pos.y + target.size.y/2 - this.size.y/2;
// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
// some movers may have been given a pauseFor property in WM,
// in which case, stop for a while...
if (this.target.pauseFor)
this.speed.tempSet(0, this.target.pauseFor);
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Set our next target
this.currentTarget = this.targets.length>1 ? (this.currentTarget+1) % this.targets.length : 0;
}
},
Hmmm. I had read your question but had skipped over the post's title. My "solution" doesn't fit within the framework supplied with the biolab-game-pack, which, now that I've looked at it, I realize is quite elegant.
Will give it some thought. Will throw bone if one found.
Thanks Alexandre! I'll read through your code and see if I can blend it. :) if you do find something, let me know. :)
I've considered making the Mover move to a Delay entity and when it reaches it starting the delay timer. When it's finished the timer it triggers the Mover moving to the next point.
In my head, this makes sense. Not sure how I'll be able to get it working though.
This may do the trick, I think. Just substitute the void entities with gates, set the mover's targets to these gates, and give a duration to each that will set the time during which targeting entity does not move.
/*
This entity stops all entities that collide with it, sending them on their way after a specified delay.
Keys for Weltmeister:
checks
Specifies which type of entities are handled: A, B or BOTH
Default: BOTH
delay
Delay in seconds after which the entities are sent on their way
default: 1
*/
ig.module(
'game.entities.gate'
)
.requires(
'impact.entity'
)
.defines(function(){
EntityGate = ig.Entity.extend({
_wmScalable: true,
_wmDrawBox: true,
_wmBoxColor: 'rgba(255, 0, 0, 0.7)',
size: {x:8, y:8},
type: ig.Entity.TYPE.NONE,
checkAgainst: ig.Entity.TYPE.BOTH,
collides: ig.Entity.COLLIDES.NEVER,
delay: 1,
arrivals: [],
departures: [],
init: function(x, y, settings)
{
if (settings.checks)
{
this.checkAgainst = ig.Entity.TYPE[settings.checks.toUpperCase()] || ig.Entity.TYPE.BOTH;
delete settings.check;
}
if (settings.delay)
{
this.delay = settings.delay;
delete settings.delay;
}
this.parent(x, y, settings);
},
check: function(other)
{
// we only act on approximate position center alignments
if (other.distanceTo(this) > 0.5)
return;
// status at this gate?
var inTransit = (this.arrivals.indexOf(other) != -1);
var departing = (this.departures.indexOf(other) != -1);
// we only check-in new arrivals
if (inTransit || departing)
return;
// process
ig.log("stop!");
this.arrivals.push(other);
other.gate = {
vel:other.vel,
update:other.update,
timer:new ig.Timer(this.delay)
};
other.vel = {x:0, y:0};
other.update = function(){};
},
update: function()
{
// Triage:
// decide who stays a bit longer and who is now ready to leave,
// and forget about those who already left and are now out of range
var visitor;
for (var i=0; i<this.arrivals.length; i++)
{
visitor = this.arrivals[i];
if (visitor.gate.timer.delta() > 0)
this.departures.push(visitor);
}
var outOfRange = [];
for (var i=0; i<this.departures.length; i++)
{
visitor = this.departures[i];
if (this.arrivals.indexOf(visitor) != -1)
{
ig.log("go!");
visitor.vel = visitor.gate.vel;
visitor.update = visitor.gate.update;
visitor.gate = undefined;
this.arrivals.splice(this.arrivals.indexOf(visitor));
}
else if (this.outOfRange(visitor))
{
outOfRange.push(visitor);
}
}
for (var i=0; i<outOfRange.length; i++)
{
visitor = outOfRange[i];
this.departures.splice(this.departures.indexOf(visitor));
}
},
outOfRange: function(visitor)
{
var x0 = this.pos.x;
var x1 = this.pos.x + this.size.x;
var y0 = this.pos.y;
var y1 = this.pos.y + this.size.y;
var vx0 = visitor.pos.x;
var vx1 = visitor.pos.x + visitor.size.x;
var vy0 = visitor.pos.y;
var vy1 = visitor.pos.y + visitor.size.y;
return (x1 < vx0 || vx1 < x0 || y1 < vy0 || vy1 < y0);
},
});
});
see code section of forum for new entities waypoints and blockers
You guys (and the rest of the community) are the reason Impact is the best ever.
Page 1 of 1
« first
« previous
next ›
last »