sc4
Version:
A command line utility for automating SimCity 4 modding tasks & modifying savegames
68 lines (67 loc) • 2.25 kB
JavaScript
import Vector3 from './vector-3.js';
// # Bbox
// A class for representing a bounding box of an occupant object. A lot of the
// savegame data structures include minX, minY, minZ, maxX, maxY, maxZ, so it
// makes sense to put it in a separate data structure, especially because it
// makes parsing & serializing them easier to read.
export class Box3 extends Array {
// ## constructor(min, max)
constructor(min = [0, 0, 0], max = [0, 0, 0]) {
super(new Vector3(...min), new Vector3(...max));
}
get min() { return this[0]; }
get max() { return this[1]; }
get minX() { return this[0][0]; }
get minY() { return this[0][1]; }
get minZ() { return this[0][2]; }
get maxX() { return this[1][0]; }
get maxY() { return this[1][1]; }
get maxZ() { return this[1][2]; }
set minX(value) { this[0][0] = value; }
set minY(value) { this[0][1] = value; }
set minZ(value) { this[0][2] = value; }
set maxX(value) { this[1][0] = value; }
set maxY(value) { this[1][1] = value; }
set maxZ(value) { this[1][2] = value; }
// ## translate(offset)
// Translates the box with the given vector, and returns a *new* box.
translate(offset) {
return new Box3(this.min.add(offset), this.max.add(offset));
}
// ## parse(rs)
parse(rs, opts = { range: false }) {
if (opts.range) {
let minX = rs.float();
let maxX = rs.float();
let minY = rs.float();
let maxY = rs.float();
let minZ = rs.float();
let maxZ = rs.float();
this[0] = new Vector3(minX, minY, minZ);
this[1] = new Vector3(maxX, maxY, maxZ);
}
else {
this[0] = rs.vector3();
this[1] = rs.vector3();
}
return this;
}
// ## write(ws)
write(ws, opts = { range: false }) {
let [min, max] = this;
if (opts.range) {
ws.float(min.x);
ws.float(max.x);
ws.float(min.y);
ws.float(max.y);
ws.float(min.z);
ws.float(max.z);
}
else {
ws.vector3(min);
ws.vector3(max);
}
return this;
}
}
export default Box3;