133 lines
3.4 KiB
JavaScript
133 lines
3.4 KiB
JavaScript
|
export class GameObjManager {
|
||
|
map;
|
||
|
floormanager;
|
||
|
|
||
|
floorObject;
|
||
|
roomObjects = new Map([]);
|
||
|
currentRoom;
|
||
|
|
||
|
constructor(map) {
|
||
|
this.map = map;
|
||
|
}
|
||
|
|
||
|
generateFloor() {
|
||
|
if (this.floorObject) this.floorObject.destroy();
|
||
|
|
||
|
const currentFloor = this.floormanager.floors.get(
|
||
|
this.floormanager.currentFloor
|
||
|
);
|
||
|
|
||
|
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.area(),
|
||
|
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);
|
||
|
|
||
|
this.map.zoomToAbs(this.map.opts.minZoomLevel, bounds.center());
|
||
|
}
|
||
|
|
||
|
generateRooms() {
|
||
|
if (this.roomObjects.size > 0) {
|
||
|
this.roomObjects.forEach((x) => x.destroy());
|
||
|
this.roomObjects.clear();
|
||
|
}
|
||
|
|
||
|
const currentRooms = this.floormanager.rooms.get(
|
||
|
this.floormanager.currentFloor
|
||
|
);
|
||
|
|
||
|
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: 1,
|
||
|
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
|
||
|
) {
|
||
|
this.currentRoom = 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,
|
||
|
size: 24 * camScale,
|
||
|
pos: polygon.bbox().center(),
|
||
|
color: this.map.kp.WHITE,
|
||
|
anchor: "center",
|
||
|
});
|
||
|
});
|
||
|
|
||
|
this.floorObject.add(obj);
|
||
|
});
|
||
|
}
|
||
|
}
|