1 decade ago
by gxxaxx
I've been trying to figure a means to dynamically duplicate an entity.
The idea is that if/when a monster bumps into the duper entity, then that monster is doubled.
I could put a dupe method in each of the monsters. But I was wondering if there was a means to have the duper ent figure you "who just bumped me" then have the duper monster double the arbitrary entity that bumped it.
Not really all that up to date on javascript -- so I don't know if there is a simple way to unambiguously get the constructor of the entity and then call spawnEnity.
The more I keep looking the better it looks to have the dupe method in each of the individual monster classes.
Just checking to see if anyone knows a bit of voodoo that will make this work.
What about, inside your Duper entity's collideWith() method, calling this.game.spawnEntity(typeof(other), x, y, other.settings)?
1 decade ago
by dominic
typeof(other)
will just report
"object"
, so that wouldn't work. JavaScript doesn't have the facilities to get an object's class name directly, because the class name isn't always unambiguous. E.g.:
MyAwesomeArray = Array;
var a = new MyAwesomeArray();
a instanceof( Array ); // => true
a instanceof( MyAwesomeArray ); // => true
// hypothetical "classof" operator:
classof( a ); // Which one is it now? Array or MyAwesomeArray?
You could however add a
className
property to your entities yourself:
EntitySpike = ig.Entity.extend({
className: 'EntitySpike',
...
});
And spawn it in the
check()
method of your Duper:
check: function( other ) {
// Make sure className is set for this Entity
if( other.className ) {
// TODO: adjust x and y so that you don't create an
// infinite loop of spawning entities :D
ig.game.spawnEntity( other.className, this.pos.x, this.pos.y );
}
}
On a sidenote, Cocoa's NSObject has the isMemberOfClass: and isKindOfClass: protocol methods to help differentiate between the 2.
MyAwesomeArray = Array;
var a = new MyAwesomeArray();
a.isMemberOfClass (Array); // => false
a.isKindOfClass (Array); // => true
a.isMemberOfClass (MyAwesomeArray); // => true
JS' instanceof behaves like Cocoa's isKindOfClass:, then.
Could have been useful for this problem to have memberof (k) and kindof (k) functions in JS but I don't know enough of the language yet.
1 decade ago
by gxxaxx
Thanks for the responses.
The adding a className as a property to the class works fine.
The only trick was to save a copy of the initial settings object so that I could use it later to truly dupe the entity as it is defined by the class AND by the weltmeister settings.
// rough outline
EntityMonster = ig.Entity.extend({
settings: {},
init: function( x, y, settings ) {
this.settings = settings;
this.parent(x,y,settings);
}
check: function( other ) {
// Make sure className is set for this Entity
if( other.className && someLogicThatSaysYes() ) {
ig.game.spawnEntity( other.className, this.pos.x, this.pos.y, other.settings );
}
}
Thanks for the many helpful replies.
Page 1 of 1
« first
« previous
next ›
last »