jonoxia/platform-game — Issue #18: Implement monster API
Author: jonoxia
Created: 2011-10-26 21:58:27+00:00
State: open

Imagining my ideal API for defining a new monster behavior, it might look something like this:

DefineMonster( {
name: "Spider", // to display in the monster selector interface in the level designer
sprites: "http://some/sitespider.png",

speed: 5,
jump: 0,
health: 1,
// size = 1 block by 1 block unless overridden
// initial x, y = defined by level editor

onContactPlayer: function(monster, player, contactDirection) {
// player object has full API for doing crazy stuff

if (contactDirection == "top") { // player landed on top of me
monster.damage(1);
player.bounce();
} else {
player.damage(2);
player.knockBack(10);
}
},

onMove: function(monster, world, tick) {
if ( tick % 20 == 0) { // every 20 ticks launch a web
monster.launchProjectile({sprite: "web.png",
gravity: true,
onContactPlayer: function(projectile, player) {
player.applyCondition("speed", -2, 10); // speed -2 for 10 secs
},
onContactMonster: function(projectile, monster) {
// don't do anything
},
onContactWall: function(projectile, world) {
projectile.stop();
}}
monster.facingDirection * 40, -20); // initial vx, vy
}
if (world.ledgeUnderneath(monster)) {
monster.reverseFacing();
} else if (world.wallInFrontOf(monster)) {
monster.reverseFacing();
} else {
monster.walkForward();
}
}

});