jonoxia/platform-game — Issue #20: Implement power-up API
Author: jonoxia
Created: 2011-10-26 22:25:29+00:00
State: open

Power-ups needs to be able to include:
1. permanent passive power-ups, like "pick this up now you have wall jump for the rest of the level";
2. time-limited power ups, like "pick this up and you get 15 seconds of invincibility"
3. triggered power ups, possibly with limited users, like "pick this up and you get ten missiles which you can fire with the X key"

so this needs to be quite a flexible API.

DefinePowerUp( {
name: "Wall Jump",
sprite: "http://some/site/walljump.png",
expires: false,
onCollect: function(powerup, player) {
player.addToStatusBar(powerup.sprite);
// player.addListener API needs some work -- what will fire events and what will they be called?

// several ways to implement something like this - what's a listener, what's state on an object,
// and what's an if-then check?
player.when("touchwall", function(player, event, world) {
// event tells us e.g. which side the wall was on
if (player.touchingFloor() == false) {
player.wallJumpStance = true;
player.setSprite("grab");
// todo set it to be left wall jump stance or right wall jump stance?
// todo require player to be holding button towards wall?
}
});
player.when("leavewall", function(player, event, world) {
player.wallJumpStance = false;
});
player.when("touchfloor", function(player, event, world) {
player.wallJumpStance = false;
player.setSprite(); // normal
});
player.when("jump-button", function(player, event, world) {
if (player.wallJumpStance == true) {
player.addVelocity(awayFromWall * 30, -30);
}
});
// TODO when and how can these event listeners be unregistered?
}
});

DefinePowerUp( {
name: "Invincibility",
sprite: "http://some/site/invincible.png",
expires: 10, // seconds
onCollect: function(powerup, player) {
// how does the asset loader know to cache this bgm file?
setBackgroundMusic("http://some/site/invincible-music.mid");
player.when("touchmonster", function(player, event, world) {
event.monster.kill();
event.preventDefault(); //????
});
player.flash(); // ????
},
onExpire: function(powerup, player) {
setBackgroundMusic(); // normal //????
player.stopFlashing(); // ????
player.unregister("touchmonster"); // ????
}
});

DefinePowerUp( {
name: "Rocket Launcher",
sprite: "",
expires: false,
ammo: 3, // presence of ammo property automatically adds ammo meter to status bar
// TODO do new special weapons just replace old ones or is there a switch-weapon button?
onCollect: function(powerup, player) {
player.when("fire-button", function(player, event, world) {
if (powerup.ammo == 0) {
return;
}
powerup.ammo -= 1; // triggers a setter which updates meter?
// TODO how about letting player aim projectiles?
player.launchProjectile({
sprite: "",// actually sprite sheet with left-and-right facing
gravity: false,
onContactPlayer: function(projectile, player) {
projectile.explode();
},
onContactMonster: function(projectile, monster) {
projectile.explode();
},
onContactWall: function(projectile, world) {
projectile.explode();
},
onLaunch: function(projectile, world) {
projectile.explode = function(){
world.allWithinDistance(projectile, 45, function(entity) {
entity.damage(3);
});
projectile.stop();
};
}
}, player.facing * 40, 0);
});
}
});

// events: Jump, touchfloor, getpowerup, touchmonster, touchobstacle, touchwall, touchcieling
// leavefloor, leavewall
// conditions: ledgeUnderneath, buttonPressed,