UNPKG

the-world-engine

Version:

three.js based, unity like game engine for browser

105 lines 2.86 kB
export class CollisionLayerMaskConverter { tr; rr; er; constructor(t) { const r = Object.entries(t); r.sort(((t, r) => { const o = Object.entries(t[1]).length; const e = Object.entries(r[1]).length; return e - o; })); const o = new Set; for (const t of r) { if (o.has(t[0])) { throw new Error(`Duplicate layer name: ${t[0]}`); } o.add(t[0]); } this.tr = new Map; this.rr = new Map; this.er = new Map; for (let t = 0, o = 1; t < r.length; ++t, o <<= 1) { const e = r[t][0]; this.tr.set(e, o); this.rr.set(o, e); this.er.set(o, 0); } for (let t = 0; t < r.length; ++t) { const o = r[t]; const e = o[0]; const n = Object.entries(o[1]); for (let t = 0; t < n.length; ++t) { const r = n[t][0]; const o = n[t][1]; const s = this.tr.get(e); const i = this.tr.get(r); const a = this.er.get(s); const c = this.er.get(i); if (o) { this.er.set(s, a | i); this.er.set(i, c | s); } } } } nameToLayer(t) { const r = this.tr.get(t); if (r === undefined) { throw new Error("Layer not found"); } return r; } layerToName(t) { const r = this.rr.get(t); if (r === undefined) { throw new Error("Layer not found"); } return r; } createMaskFromName(...t) { let r = 0; for (let o = 0; o < t.length; ++o) { r |= this.nameToLayer(t[o]); } return r; } createMaskFromLayer(...t) { let r = 0; for (let o = 0; o < t.length; ++o) { r |= t[o]; } return r; } getMaskFromName(t) { const r = this.tr.get(t); if (r === undefined) throw new Error("Layer not found"); const o = this.er.get(r); if (o === undefined) throw new Error("Mask not found"); return o; } getMaskFromLayer(t) { const r = this.er.get(t); if (r === undefined) throw new Error("Mask not found"); return r; } copy(t) { this.tr.clear(); this.rr.clear(); this.er.clear(); for (const [r, o] of t.tr) { this.tr.set(r, o); } for (const [r, o] of t.rr) { this.rr.set(r, o); } for (const [r, o] of t.er) { this.er.set(r, o); } } clone() { const t = new CollisionLayerMaskConverter({}); t.copy(this); return t; } }