more rendering code, not much changes, beginning refactor of the rendering API

This commit is contained in:
2026-04-02 06:31:30 +02:00
parent 92418e7e4e
commit 60313f5373
7 changed files with 317 additions and 263 deletions
+18 -19
View File
@@ -2,10 +2,9 @@ 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 size: number;
private pos: Vector2 = Vector2.zero;
private vel: Vector2 = Vector2.zero;
@@ -29,11 +28,10 @@ export class Shimeji {
private bounds: { minX: number; minY: number; maxX: number; maxY: number };
constructor(element: HTMLElement, canvas: HTMLCanvasElement, renderer: Renderer) {
this.element = element;
constructor(size: number = 40, canvas: HTMLCanvasElement, renderer: Renderer) {
this.size = size;
this.canvas = canvas;
this.renderer = renderer;
this.rect = element.getBoundingClientRect();
this.bounds = {
minX: 0,
@@ -43,13 +41,10 @@ export class Shimeji {
};
this.pos.set(window.innerWidth / 2, window.innerHeight / 2);
this.vel.set(Math.floor(Math.random() * 5000) - 2500, Math.floor(Math.random() * 5000) - 2500);
this.updateBounds();
window.addEventListener('resize', () => this.updateBounds());
console.log(this.element.getBoundingClientRect());
this.setupDragEvents();
}
private updateBounds(): void {
@@ -94,8 +89,8 @@ export class Shimeji {
}
}
private setupDragEvents(): void {
this.element.addEventListener('mousedown', (e) => {
/* private setupDragEvents(): void {
this.element.addEventListener('mousedown', (e) => {
e.preventDefault();
this.isBeingDragged = true;
this.element.style.cursor = 'grabbing';
@@ -129,9 +124,10 @@ export class Shimeji {
this.element.addEventListener('dragstart', (e) => {
e.preventDefault();
});
}
} */
private updatePhysics(dt: number): void {
console.log(this.vel.x, this.vel.y);
if (this.isBeingDragged) {
const direction: Vector2 = this.mousePos.sub(this.pivot).sub(this.pos);
@@ -148,17 +144,21 @@ export class Shimeji {
this.vel.y += this.GRAVITY;
this.vel.mulEq(this.DAMPING);
if (this.pos.y + this.rect.width >= this.bounds.maxY) {
if (this.pos.y + this.size >= this.bounds.maxY) {
this.vel.x *= this.FRICTION;
}
this.vel.deadZoneEq(0.00001);
//if (Math.abs(this.vel.x) <= 0.00001) this.vel.x = 0;
//if (Math.abs(this.vel.y) <= 0.00001) this.vel.y = 0;
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;
const leftBound = this.size;
const rightBound = this.bounds.maxX - this.size;
if (this.pos.x < leftBound) {
this.pos.x = leftBound;
@@ -168,8 +168,8 @@ export class Shimeji {
this.vel.x = -this.RESTITUTION * this.vel.x;
}
const topBound = this.rect.height / 2;
const bottomBound = this.bounds.maxY - this.rect.height / 2;
const topBound = this.size;
const bottomBound = this.bounds.maxY - this.size;
if (this.pos.y < topBound) {
this.pos.y = topBound;
@@ -192,8 +192,7 @@ export class Shimeji {
}
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);
this.renderer.Draw(this.pos, this.size);
/* if(this.isBeingDragged) {
this.drawRay(