Files
Robot-Clash/game/robot.js

59 lines
2.6 KiB
JavaScript
Raw Normal View History

2026-11-13 00:41:54 +01:00
/* Robot Clash - workshop rules and robot stats from parts (no DOM). Needs parts.js.
RC.validate(build) -> { ok, errors: ['need_chassis' | 'overweight' | 'no_power' ...], weight, power, supply }
RC.stats(build) -> combat stats { hp, reduce, mass, accel, top, turn, grip, push, len, wid, h, rad,
weapon, tower, invertible, lowProfile, frontArmour, value }
build = { chassis: id, motor: id, wheels: id, battery: id, armour: id|null, weapon: id|null, tower: id|null } */
(function (root) {
'use strict';
const RC = (root.RC = root.RC || (typeof require === 'function' ? require('./parts.js') : {}));
const part = (b, slot) => (b && b[slot] ? RC.PARTS[b[slot]] || null : null);
RC.HP_SCALE = 3; // catalogue hp x this = hit points in the arena
RC.validate = build => {
const errors = [];
let weight = 0, power = 0, supply = 0;
for (const slot of RC.SLOTS) {
const p = part(build, slot);
if (!p) { if (RC.REQUIRED[slot]) errors.push('need_' + slot); continue; }
if (p.slot !== slot) { errors.push('bad_' + slot); continue; }
weight += p.weight;
if (slot === 'battery') supply += p.power; else power += p.power || 0;
}
if (weight > RC.WEIGHT_LIMIT) errors.push('overweight');
if (supply && power > supply) errors.push('no_power');
return { ok: errors.length === 0, errors, weight, power, supply };
};
RC.stats = build => {
const ch = part(build, 'chassis'), mo = part(build, 'motor'), wh = part(build, 'wheels');
const ar = part(build, 'armour'), we = part(build, 'weapon'), to = part(build, 'tower');
const v = RC.validate(build);
const mass = Math.max(30, v.weight);
let value = 0;
for (const slot of RC.SLOTS) { const p = part(build, slot); if (p) value += p.cost; }
const len = ch ? ch.len : 1.3, wid = ch ? ch.wid : 1.0;
return {
hp: ((ch ? ch.hp : 80) + (ar ? ar.hp : 0)) * RC.HP_SCALE,
reduce: ar ? ar.reduce : 0,
mass,
accel: (mo ? mo.force : 500) / mass * (wh ? Math.min(1.2, wh.grip / 8) : 1),
top: (mo ? mo.speed : 3.5) * (wh ? wh.speedMul : 1) * (1.12 - mass / 900),
turn: (wh ? wh.turn : 2.5) * (1.15 - mass / 650),
grip: wh ? wh.grip : 6,
push: wh ? wh.push : 1,
len, wid, h: ch ? ch.h : 0.42,
rad: Math.hypot(len, wid) / 2 * 0.82,
weapon: we, tower: to,
invertible: !!(wh && wh.invertible),
lowProfile: !!(ch && ch.lowProfile),
frontArmour: ch && ch.frontArmour ? ch.frontArmour : 0,
value,
};
};
if (typeof module !== 'undefined' && module.exports) module.exports = RC;
})(typeof window !== 'undefined' ? window : globalThis);