Isle-Slingers/js/physics/rules/index.js

60 lines
2.2 KiB
JavaScript
Raw Normal View History

/* ISLE SLINGERS -- physics/rules/index.js (submodule: rules)
* Modularization pass (behavior-neutral split of the original
* js/core.js monolith). Installs its units into ONE shared physics scope `P`
* that every physics submodule sees -- the same single-scope semantics the
* monolith had. Must load before js/physics/index.js (the aggregator that
* attaches the public ISLE_CORE API). Surface installed: checkEndStates, birdsRemaining, starsFor.
*/
(function (root, factory) {
var part = factory();
if (typeof module !== 'undefined' && module.exports) module.exports = part;
else (root.ISLE_PHYSICS_PARTS = root.ISLE_PHYSICS_PARTS || []).push(part);
})(typeof globalThis !== 'undefined' ? globalThis : this, function () {
'use strict';
return function (P) {
P.checkEndStates = function(g) {
if (g.status === 'won' || g.status === 'lost') return;
var alive = 0;
for (var i = 0; i < g.enemies.length; i++) if (!g.enemies[i].dead) alive++;
if (g.hadEnemies && alive === 0 && g.launched > 0) {
g.status = 'won';
g.wonAt = g.time;
g.score += P.BIRD_BONUS * P.birdsRemaining(g);
g.events.push({ type: 'level_won', x: g.camera.x, y: 400 });
return;
}
var b = P.activeBird(g);
var inFlight = false;
// S-5: a LANDED bird is still a live body (it can fall, smash, and kill
// while it exists) -- the level must not be declared lost while one is out
// in the world.
var landedOut = false;
for (var k = 0; k < g.birds.length; k++) {
if (g.birds[k].state === 'flying') inFlight = true;
if (g.birds[k].state === 'landed') landedOut = true;
}
if (!inFlight && !landedOut && !b && g.status === 'flying') {
// last bird spent, enemies remain
g.status = 'lost';
g.lostAt = g.time;
g.events.push({ type: 'level_lost', x: g.camera.x, y: 400 });
}
}
P.birdsRemaining = function(g) {
var n = 0;
for (var i = 0; i < g.birds.length; i++) if (g.birds[i].state === 'queued') n++;
return n;
}
P.starsFor = function(g) {
var th = g.level.stars;
if (!th) return 0;
if (g.score >= th[2]) return 3;
if (g.score >= th[1]) return 2;
if (g.score >= th[0] || g.status === 'won') return 1;
return 0;
}
};
});