export class GameObjManager { opts = { floorMinZoom: 1, roomMinZoom: 2, }; map; mainmanager; floorObject; roomObjects = new Map([]); constructor(map, opts) { this.map = map; this.opts = { ...this.opts, opts, }; } generateFloor() { if (this.floorObject) this.floorObject.destroy(); const currentFloor = this.mainmanager.getCurrentFocusObject("floor"); const polygon = new this.map.kp.Polygon( currentFloor.poly.map(([x, y]) => this.map.kp.vec2(x, y)) ); this.floorObject = this.map.kp.make([ this.map.kp.polygon(polygon.pts), this.map.kp.outline(1, this.map.kp.BLACK), this.map.kp.color(this.map.kp.Color.fromHex("303030")), this.map.kp.pos(), this.map.kp.z(5), ]); const bounds = polygon.bbox(); this.map.camBounds = bounds; this.floorObject.onUpdate(() => { const camScale = 1 / this.map.kp.camScale().y; this.floorObject.outline.width = 8 * camScale; }); this.floorObject.onDraw(() => { const camScale = 1 / this.map.kp.camScale().y; this.map.kp.drawText({ text: currentFloor.name, size: 24 * camScale, pos: this.map.kp.vec2(-4 * camScale, -8 * camScale), color: this.map.kp.WHITE, anchor: "botleft", }); }); this.map.kp.add(this.floorObject); } zoomToFloor() { const objectBounds = this.floorObject.renderArea().bbox(); this.map.zoomToAbs( this.map.opts.minZoomLevel + this.opts.floorMinZoom, objectBounds.center() ); } zoomToRoom(id) { const selectedObject = this.roomObjects.get(id); if (selectedObject == null) return; const objectBounds = selectedObject.renderArea().bbox(); this.map.zoomToAbs( this.map.opts.minZoomLevel + this.opts.roomMinZoom, objectBounds.center() ); } generateRooms() { if (this.roomObjects.size > 0) { this.roomObjects.forEach((x) => x.destroy()); this.roomObjects.clear(); } const currentRooms = this.mainmanager.getAllFocusObject("room"); currentRooms.forEach((room) => { const polygon = new this.map.kp.Polygon( room.poly.map(([x, y]) => this.map.kp.vec2(x, y)) ); const obj = this.map.kp.make([ this.map.kp.polygon(polygon.pts), this.map.kp.outline(1, this.map.kp.BLACK), this.map.kp.color(this.map.kp.Color.fromHex("303030")), this.map.kp.area(), this.map.kp.pos(), this.map.kp.z(6), { clickForgiveness: 5, startClickPosition: null, }, ]); obj.onHover(() => { obj.color = this.map.kp.Color.fromHex("505050"); }); obj.onHoverEnd(() => { obj.color = this.map.kp.Color.fromHex("303030"); }); this.roomObjects.set(room.id, obj); obj.onUpdate(() => { const camScale = 1 / this.map.kp.camScale().y; obj.outline.width = 8 * camScale; if (this.map.kp.isMousePressed() && obj.isHovering()) { obj.startClickPosition = this.map.kp.mousePos(); obj.color = this.map.kp.Color.fromHex("202020"); } if (this.map.kp.isMouseReleased() && obj.isHovering()) { const endClickPosition = this.map.kp.mousePos(); if ( obj.startClickPosition && obj.startClickPosition.dist(endClickPosition) < obj.clickForgiveness ) { window.location.hash = room.id; } obj.color = this.map.kp.Color.fromHex("505050"); this.map.clearMouseMode(); } }); obj.onDraw(() => { const camScale = 1 / this.map.kp.camScale().y; this.map.kp.drawText({ text: room.name, width: polygon.bbox().width, size: 24 * camScale, pos: polygon.bbox().center(), color: this.map.kp.WHITE, align: "center", anchor: "center", }); }); this.floorObject.add(obj); }); } }