2024-06-02 05:39:23 +00:00
|
|
|
export class GameObjManager {
|
2024-06-03 19:35:45 +00:00
|
|
|
opts = {
|
|
|
|
floorMinZoom: 1,
|
|
|
|
roomMinZoom: 2,
|
|
|
|
};
|
2024-06-02 05:39:23 +00:00
|
|
|
map;
|
|
|
|
|
2024-06-05 07:30:05 +00:00
|
|
|
mainmanager;
|
|
|
|
|
2024-06-02 05:39:23 +00:00
|
|
|
floorObject;
|
|
|
|
roomObjects = new Map([]);
|
|
|
|
|
2024-06-05 07:30:05 +00:00
|
|
|
constructor(map, opts) {
|
2024-06-02 05:39:23 +00:00
|
|
|
this.map = map;
|
2024-06-03 19:35:45 +00:00
|
|
|
this.opts = {
|
|
|
|
...this.opts,
|
|
|
|
opts,
|
|
|
|
};
|
2024-06-05 07:30:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
generateFloor() {
|
2024-06-02 05:39:23 +00:00
|
|
|
if (this.floorObject) this.floorObject.destroy();
|
|
|
|
|
2024-06-05 07:30:05 +00:00
|
|
|
const currentFloor = this.mainmanager.getCurrentFocusObject("floor");
|
2024-06-02 05:39:23 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-06-03 03:54:02 +00:00
|
|
|
zoomToFloor() {
|
|
|
|
const objectBounds = this.floorObject.renderArea().bbox();
|
|
|
|
|
2024-06-03 19:35:45 +00:00
|
|
|
this.map.zoomToAbs(
|
|
|
|
this.map.opts.minZoomLevel + this.opts.floorMinZoom,
|
|
|
|
objectBounds.center()
|
|
|
|
);
|
2024-06-03 03:54:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
zoomToRoom(id) {
|
|
|
|
const selectedObject = this.roomObjects.get(id);
|
|
|
|
if (selectedObject == null) return;
|
|
|
|
|
|
|
|
const objectBounds = selectedObject.renderArea().bbox();
|
|
|
|
|
2024-06-03 19:35:45 +00:00
|
|
|
this.map.zoomToAbs(
|
|
|
|
this.map.opts.minZoomLevel + this.opts.roomMinZoom,
|
|
|
|
objectBounds.center()
|
|
|
|
);
|
2024-06-03 03:54:02 +00:00
|
|
|
}
|
|
|
|
|
2024-06-02 05:39:23 +00:00
|
|
|
generateRooms() {
|
|
|
|
if (this.roomObjects.size > 0) {
|
|
|
|
this.roomObjects.forEach((x) => x.destroy());
|
|
|
|
this.roomObjects.clear();
|
|
|
|
}
|
|
|
|
|
2024-06-05 07:30:05 +00:00
|
|
|
const currentRooms = this.mainmanager.getAllFocusObject("room");
|
2024-06-02 05:39:23 +00:00
|
|
|
|
|
|
|
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),
|
|
|
|
{
|
2024-06-03 03:54:02 +00:00
|
|
|
clickForgiveness: 5,
|
2024-06-02 05:39:23 +00:00
|
|
|
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
|
|
|
|
) {
|
2024-06-05 07:30:05 +00:00
|
|
|
window.location.hash = room.id;
|
2024-06-02 05:39:23 +00:00
|
|
|
}
|
|
|
|
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,
|
2024-06-03 19:35:45 +00:00
|
|
|
width: polygon.bbox().width,
|
2024-06-02 05:39:23 +00:00
|
|
|
size: 24 * camScale,
|
|
|
|
pos: polygon.bbox().center(),
|
|
|
|
color: this.map.kp.WHITE,
|
2024-06-03 19:35:45 +00:00
|
|
|
align: "center",
|
2024-06-02 05:39:23 +00:00
|
|
|
anchor: "center",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
this.floorObject.add(obj);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|