UNPKG

the-world-engine

Version:

three.js based, unity like game engine for browser

89 lines (80 loc) 3.4 kB
import { b2ShapeType } from "../collision/b2_shape.js"; import { b2CircleContact } from "./b2_circle_contact.js"; import { b2PolygonContact } from "./b2_polygon_contact.js"; import { b2PolygonAndCircleContact } from "./b2_polygon_circle_contact.js"; import { b2EdgeAndCircleContact } from "./b2_edge_circle_contact.js"; import { b2EdgeAndPolygonContact } from "./b2_edge_polygon_contact.js"; import { b2ChainAndCircleContact } from "./b2_chain_circle_contact.js"; import { b2ChainAndPolygonContact } from "./b2_chain_polygon_contact.js"; export class b2ContactRegister { constructor() { this.pool = []; this.createFcn = null; this.destroyFcn = null; this.primary = false; } } export class b2ContactFactory { constructor() { this.m_registers = []; this.InitializeRegisters(); } AddType(t, o, n, e) { const c = []; function poolCreateFcn() { return c.pop() || t(); } function poolDestroyFcn(t) { c.push(t); } this.m_registers[n][e].pool = c; this.m_registers[n][e].createFcn = poolCreateFcn; this.m_registers[n][e].destroyFcn = poolDestroyFcn; this.m_registers[n][e].primary = true; if (n !== e) { this.m_registers[e][n].pool = c; this.m_registers[e][n].createFcn = poolCreateFcn; this.m_registers[e][n].destroyFcn = poolDestroyFcn; this.m_registers[e][n].primary = false; } } InitializeRegisters() { for (let t = 0; t < b2ShapeType.e_shapeTypeCount; t++) { this.m_registers[t] = []; for (let o = 0; o < b2ShapeType.e_shapeTypeCount; o++) { this.m_registers[t][o] = new b2ContactRegister; } } this.AddType(b2CircleContact.Create, b2CircleContact.Destroy, b2ShapeType.e_circleShape, b2ShapeType.e_circleShape); this.AddType(b2PolygonAndCircleContact.Create, b2PolygonAndCircleContact.Destroy, b2ShapeType.e_polygonShape, b2ShapeType.e_circleShape); this.AddType(b2PolygonContact.Create, b2PolygonContact.Destroy, b2ShapeType.e_polygonShape, b2ShapeType.e_polygonShape); this.AddType(b2EdgeAndCircleContact.Create, b2EdgeAndCircleContact.Destroy, b2ShapeType.e_edgeShape, b2ShapeType.e_circleShape); this.AddType(b2EdgeAndPolygonContact.Create, b2EdgeAndPolygonContact.Destroy, b2ShapeType.e_edgeShape, b2ShapeType.e_polygonShape); this.AddType(b2ChainAndCircleContact.Create, b2ChainAndCircleContact.Destroy, b2ShapeType.e_chainShape, b2ShapeType.e_circleShape); this.AddType(b2ChainAndPolygonContact.Create, b2ChainAndPolygonContact.Destroy, b2ShapeType.e_chainShape, b2ShapeType.e_polygonShape); } Create(t, o, n, e) { const c = t.GetType(); const i = n.GetType(); const a = this.m_registers[c][i]; if (a.createFcn) { const c = a.createFcn(); if (a.primary) { c.Reset(t, o, n, e); } else { c.Reset(n, e, t, o); } return c; } else { return null; } } Destroy(t) { const o = t.m_fixtureA.GetType(); const n = t.m_fixtureB.GetType(); const e = this.m_registers[o][n]; if (e.destroyFcn) { e.destroyFcn(t); } } }