smolworld / src /js /entity.js
p3nGu1nZz's picture
✨ Refactor physics module; add math utilities and event handling classes; update state structure for 3D entities
7ffaa9e
import { Transform } from './transform.js';
import { Matrix } from './matrix.js';
import { getConfig } from './config.js';
export class Entity {
constructor() {
this.transform = new Transform();
this.boundingBox = { width: 0, height: 0 };
}
draw(ctx, camera) {
const config = getConfig();
const viewMatrix = camera.getViewMatrix();
const worldMatrix = this.transform.getWorldMatrix();
const finalMatrix = Matrix.multiply(worldMatrix, viewMatrix);
const position = Matrix.transformPoint(finalMatrix, { x: 0, y: 0, z: 0 });
const scale = this.transform.scale.x * camera.scale;
// Draw entity as a simple red semi-transparent rectangle
const width = this.boundingBox.width * scale;
const height = this.boundingBox.height * scale;
ctx.fillStyle = 'rgba(255, 0, 0, 0.2)';
ctx.fillRect(
position.x - width / 2,
position.y - height / 2,
width,
height
);
// Draw debug bounding box if enabled
if (config.debug.showBoundingBoxes) {
ctx.save();
ctx.strokeStyle = 'red';
ctx.lineWidth = 1;
ctx.strokeRect(
position.x - width / 2,
position.y - height / 2,
width,
height
);
ctx.restore();
}
}
// Override this in derived classes
drawEntity(ctx, position, scale) { }
}