Files
gornodox.dev/src/lib/stores/shimeji.ts
T

213 lines
5.3 KiB
TypeScript

import Renderer from '$lib/shimeji/renderer/renderer';
import Vector2 from '../vector';
export class Shimeji {
private element: HTMLElement;
private canvas: HTMLCanvasElement;
private renderer: Renderer;
private rect: DOMRect;
private pos: Vector2 = Vector2.zero;
private vel: Vector2 = Vector2.zero;
private mousePos: Vector2 = Vector2.zero;
private pivot: Vector2 = Vector2.zero;
private isBeingDragged: boolean = false;
private rafId: number | null = null;
private prevDeltaTime: number = 0;
private readonly TIMESCALE: number = 1;
private readonly RESTITUTION: number = 0.4;
private readonly CURSOR_STRENGTH: number = 500;
private readonly DAMPING: number = 1;
private readonly MASS: number = 1;
private readonly FRICTION: number = 0.9;
private readonly GRAVITY: number = 50;
private bounds: { minX: number; minY: number; maxX: number; maxY: number };
constructor(element: HTMLElement, canvas: HTMLCanvasElement, renderer: Renderer) {
this.element = element;
this.canvas = canvas;
this.renderer = renderer;
this.rect = element.getBoundingClientRect();
this.bounds = {
minX: 0,
minY: 0,
maxX: window.innerWidth,
maxY: window.innerHeight
};
this.pos.set(window.innerWidth / 2, window.innerHeight / 2);
this.updateBounds();
window.addEventListener('resize', () => this.updateBounds());
console.log(this.element.getBoundingClientRect());
this.setupDragEvents();
}
private updateBounds(): void {
this.bounds = {
minX: 0,
minY: 0,
maxX: window.innerWidth,
maxY: window.innerHeight
};
this.canvas.width = window.innerWidth;
this.canvas.height = window.innerHeight;
this.renderer.UpdateBounds(window.innerWidth, window.innerHeight);
}
start(): void {
if (this.rafId !== null) return;
const firstFrame = (delta: number) => {
this.prevDeltaTime = delta;
this.rafId = requestAnimationFrame(animate);
};
const animate = (delta: number) => {
let dt = (delta - this.prevDeltaTime) / 1000;
dt = Math.min(dt, 0.05 * this.TIMESCALE);
this.prevDeltaTime = delta;
this.updatePhysics(dt);
this.applyBoundaries();
this.render();
this.rafId = requestAnimationFrame(animate);
};
this.rafId = requestAnimationFrame(firstFrame);
}
stop(): void {
if (this.rafId !== null) {
cancelAnimationFrame(this.rafId);
this.rafId = null;
}
}
private setupDragEvents(): void {
this.element.addEventListener('mousedown', (e) => {
e.preventDefault();
this.isBeingDragged = true;
this.element.style.cursor = 'grabbing';
const parent = this.element.parentElement;
if (parent) parent.style.cursor = 'grabbing';
this.pivot.x = e.offsetX - this.rect.width / 2;
this.pivot.y = e.offsetY - this.rect.height / 2;
});
document.addEventListener('mousemove', (e) => {
this.mousePos.x = e.pageX;
this.mousePos.y = e.pageY;
});
document.addEventListener('mouseup', (e) => {
if (this.isBeingDragged) {
this.isBeingDragged = false;
this.element.style.cursor = 'grab';
const parent = this.element.parentElement;
if (parent) parent.style.cursor = 'auto';
this.pivot = Vector2.zero;
}
this.canvas.getContext('2d')!.clearRect(0, 0, this.canvas.width, this.canvas.height);
});
this.element.addEventListener('dragstart', (e) => {
e.preventDefault();
});
}
private updatePhysics(dt: number): void {
if (this.isBeingDragged) {
const direction: Vector2 = this.mousePos.sub(this.pivot).sub(this.pos);
const force: Vector2 = direction.mul(this.CURSOR_STRENGTH);
const acceleration: Vector2 = force.div(this.MASS).deadZone(0.001);
acceleration.mulEq(dt);
this.vel.addEq(acceleration);
this.vel.mulEq(0.8);
}
this.vel.y += this.GRAVITY;
this.vel.mulEq(this.DAMPING);
if (this.pos.y + this.rect.width >= this.bounds.maxY) {
this.vel.x *= this.FRICTION;
}
const outVel = this.vel.mul(dt);
this.pos.addEq(outVel);
}
private applyBoundaries(): void {
const leftBound = this.rect.width / 2;
const rightBound = this.bounds.maxX - this.rect.width / 2;
if (this.pos.x < leftBound) {
this.pos.x = leftBound;
this.vel.x = -this.RESTITUTION * this.vel.x;
} else if (this.pos.x > rightBound) {
this.pos.x = rightBound;
this.vel.x = -this.RESTITUTION * this.vel.x;
}
const topBound = this.rect.height / 2;
const bottomBound = this.bounds.maxY - this.rect.height / 2;
if (this.pos.y < topBound) {
this.pos.y = topBound;
this.vel.y = -this.RESTITUTION * this.vel.y;
} else if (this.pos.y > bottomBound) {
this.pos.y = bottomBound;
this.vel.y = -this.RESTITUTION * this.vel.y;
}
}
private drawRay(pos1: { x: number; y: number }, pos2: { x: number; y: number }) {
const ctx = this.canvas.getContext('2d')!;
ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
ctx.strokeStyle = 'white';
ctx.lineWidth = 0.1;
ctx.beginPath();
ctx.moveTo(pos1.x, pos1.y);
ctx.lineTo(pos2.x, pos2.y);
ctx.stroke();
}
private render(): void {
this.element.style.transform = `translate(${this.pos.x}px, ${this.pos.y}px)`;
this.renderer.Draw(this.pos, this.rect.width / 2);
/* if(this.isBeingDragged) {
this.drawRay(
{x: this.mouseX, y: this.mouseY},
{x: this.x + this.pivotX, y: this.y + this.pivotY}
)
} */
}
addImpulse(x: number, y: number): void {
this.vel.x += x;
this.vel.y += y;
}
moveTo(pos: number) {}
}