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

86 lines
3.1 KiB
JavaScript
Raw Normal View History

/* ISLE SLINGERS -- physics/state/index.js (submodule: state)
* 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: createGame, makeBird, activeBird.
*/
(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.createGame = function(levelDef) {
var g = {
level: levelDef,
world: { w: P.WORLD.w, ground: P.WORLD.ground },
time: 0,
score: 0,
birds: [],
blocks: [],
enemies: [],
events: [], // visual/audio events for the renderer
scorePopups: [], // {x,y,text,age}
hadEnemies: levelDef.enemies.length > 0,
camera: { x: levelDef.sling.x + 260, y: 560, zoom: 1, shake: 0, snap: true },
status: 'ready', // ready | flying | won | lost
activeBird: -1,
launched: 0,
wonAt: null,
lostAt: null,
stats: { impacts: 0, destroyedBlocks: 0, destroyedEnemies: 0, abilityUses: 0, distance: 0 }
};
g.blocks = levelDef.blocks.map(function (b, i) {
var m = P.MATERIALS[b.mat];
return {
id: i, x: b.x, y: b.y, w: b.w, h: b.h, mat: b.mat,
hp: m.hp, maxHp: m.hp,
vx: 0, vy: 0, rest: 1,
// S-3 rotational degree of freedom (topple). ang/angVel stay 0 for
// every supported stack, so pre-S-3 motion is preserved exactly.
ang: 0, angVel: 0, pivoting: false,
mass: m.density * b.w * b.h / 1400
};
});
g.enemies = levelDef.enemies.map(function (e, i) {
var t = P.ENEMY_TYPES[e.kind];
// level data y is the CENTER (the level-integrity test treats it the
// same way); spawning at e.y - r made every enemy drop 24-38px on
// load — a visible "objects moving on their own" wobble at start
return {
id: i, kind: e.kind, x: e.x, y: e.y, r: t.r,
hp: t.hp, maxHp: t.hp, vx: 0, vy: 0, rest: 1, dead: false, deathAge: 0
};
});
var i;
for (i = 0; i < levelDef.birds.length; i++) {
g.birds.push(P.makeBird(levelDef.birds[i], i));
}
g.activeBird = g.birds.length ? 0 : -1;
if (g.activeBird >= 0) {
var fb = g.birds[0];
fb.state = 'aiming';
fb.x = levelDef.sling.x;
fb.y = levelDef.sling.y;
}
return g;
}
P.makeBird = function(kind, idx) {
var t = P.BIRD_TYPES[kind];
return {
id: idx, kind: kind, r: t.r, mass: t.mass,
x: 0, y: 0, vx: 0, vy: 0,
state: 'queued', // queued | aiming | flying | spent
abilityUsed: false,
settleTimer: 0, age: 0,
launchT: 0
};
}
P.activeBird = function(g) { return g.activeBird >= 0 ? g.birds[g.activeBird] : null; }
};
});