EventMapper/assets/scripts/script.js

161 lines
3.8 KiB
JavaScript
Raw Normal View History

2024-06-01 06:31:08 +00:00
import kaplay from "./modules/kaplay.js";
2024-06-01 06:31:08 +00:00
const map = document.querySelector("map");
2024-05-29 15:15:05 +00:00
2024-06-01 06:31:08 +00:00
class KaplayMap {
kp;
opts = {
minZoomLevel: 0,
maxZoomLevel: 3,
};
2024-05-29 15:15:05 +00:00
2024-06-01 06:31:08 +00:00
#zoomLevel = 0;
#scaleTween = null;
startMousePos = null;
startCamPos = null;
prevCamPos = null;
moveVelocity = null;
moveFriction = 0.25;
moveDead = 1;
#moveTween = null;
constructor(kp, opts) {
this.kp = kp;
this.opts = {
...this.opts,
...opts,
};
// Zooming
this.kp.onScroll((delta) => {
const scrollDist = -delta.y / 120;
this.zoomTo(this.zoomLevel + scrollDist, this.kp.mousePos());
});
// Dragging
this.kp.onUpdate(() => {
const curCamPos = this.kp.camPos();
const camScale = 1 / this.kp.camScale().y;
// ||| Completely unintelligible to the average person |||
// vvv Sorry y'all xwx vvv
if (this.kp.isMousePressed()) {
this.startMousePos = this.kp.mousePos();
this.startCamPos = this.kp.camPos();
} else if (this.kp.isMouseReleased() && this.prevCamPos != null) {
this.moveVelocity = this.prevCamPos.sub(curCamPos).scale(1 / camScale);
}
if (this.kp.isMouseDown()) {
this.prevCamPos = this.kp.camPos();
this.kp.camPos(
this.startCamPos.sub(
this.kp.mousePos().sub(this.startMousePos).scale(camScale)
)
);
} else if (this.moveVelocity?.x != 0 && this.moveVelocity?.y != 0)
this.kp.camPos(curCamPos.sub(this.moveVelocity?.scale(camScale) ?? 0));
// ^^^ Completely unintelligible to the average person ^^^
// ||| Sorry y'all xwx |||
if (this.moveVelocity == null) return;
if (
this.moveVelocity.x <= this.moveDead &&
this.moveVelocity.x >= -this.moveDead
)
this.moveVelocity.x = 0;
if (
this.moveVelocity.y <= this.moveDead &&
this.moveVelocity.y >= -this.moveDead
)
this.moveVelocity.y = 0;
this.moveVelocity = this.moveVelocity.scale(1 - this.moveFriction);
});
}
2024-05-29 15:15:05 +00:00
2024-06-01 06:31:08 +00:00
get zoomLevel() {
return this.#zoomLevel;
}
2024-05-29 15:15:05 +00:00
2024-06-01 06:31:08 +00:00
set zoomLevel(newZoom) {
const cameraZoom = this.kp.camScale().y;
2024-05-29 15:15:05 +00:00
2024-06-01 06:31:08 +00:00
let addLinear = newZoom;
2024-05-29 15:15:05 +00:00
2024-06-01 06:31:08 +00:00
if (addLinear < this.opts.minZoomLevel) addLinear = this.opts.minZoomLevel;
if (addLinear > this.opts.maxZoomLevel) addLinear = this.opts.maxZoomLevel;
2024-05-29 15:15:05 +00:00
2024-06-01 06:31:08 +00:00
let linearToLog = Math.pow(2, addLinear);
this.#zoomLevel = addLinear;
2024-05-29 15:15:05 +00:00
2024-06-01 06:31:08 +00:00
if (this.#scaleTween != null) {
this.#scaleTween.finish();
}
2024-05-29 15:15:05 +00:00
2024-06-01 06:31:08 +00:00
this.#scaleTween = this.kp.tween(
cameraZoom,
linearToLog,
0.25,
this.kp.camScale,
this.kp.easings.easeOutQuad
);
this.#scaleTween.then(() => {
this.#scaleTween = null;
});
2024-05-29 15:15:05 +00:00
}
2024-06-01 06:31:08 +00:00
zoomTo(newZoom, position) {
const curCamPos = this.kp.camPos();
const camScale = 1 / this.kp.camScale().y;
2024-05-29 15:15:05 +00:00
2024-06-01 06:31:08 +00:00
const diff = this.kp.center().sub(position).scale(camScale);
2024-05-29 15:15:05 +00:00
2024-06-01 06:31:08 +00:00
this.zoomLevel = newZoom;
let newZoomLog = 1 / Math.pow(2, newZoom);
2024-05-29 15:15:05 +00:00
2024-06-01 06:31:08 +00:00
const newDiff = this.kp.center().sub(position).scale(newZoomLog);
2024-05-29 15:15:05 +00:00
2024-06-01 06:31:08 +00:00
if (newZoom < this.opts.minZoomLevel) return;
if (newZoom > this.opts.maxZoomLevel) return;
2024-05-29 15:15:05 +00:00
2024-06-01 06:31:08 +00:00
if (this.#moveTween != null) {
this.#moveTween.finish();
}
2024-05-29 15:15:05 +00:00
2024-06-01 06:31:08 +00:00
this.#moveTween = this.kp.tween(
curCamPos,
curCamPos.sub(diff).add(newDiff),
0.25,
this.kp.camPos,
this.kp.easings.easeOutQuad
);
this.#moveTween.then(() => {
this.#moveTween = null;
});
}
2024-05-29 15:15:05 +00:00
}
2024-06-01 06:31:08 +00:00
const kp = kaplay({
canvas: map,
focus: true,
loadingScreen: false,
crisp: false,
debug: false,
global: false,
maxFPS: 120,
background: "404040",
});
2024-05-29 15:15:05 +00:00
2024-06-01 06:31:08 +00:00
const kaplaymap = new KaplayMap(kp, {});
2024-05-29 15:15:05 +00:00
2024-06-01 06:31:08 +00:00
const bean = kp.loadBean();
2024-05-29 15:15:05 +00:00
2024-06-01 06:31:08 +00:00
kp.add([kp.sprite("bean"), kp.pos(kp.center())]);