Skip to content

Commit b476796

Browse files
committed
feat: implement HitboxManager and HitboxDebug classes and add Vite plugin to copy engine vendor files during build
1 parent 07874c4 commit b476796

2 files changed

Lines changed: 67 additions & 1 deletion

File tree

engine/sprite-processor.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,57 @@ class SpriteProcessor {
10571057
}
10581058
}
10591059

1060+
class HitboxManager {
1061+
constructor() {
1062+
this.regions = new Map();
1063+
this.visible = false;
1064+
}
1065+
1066+
register(id, x, y, w, h, label) {
1067+
this.regions.set(id, { id, x, y, w, h, label });
1068+
}
1069+
1070+
unregister(id) {
1071+
this.regions.delete(id);
1072+
}
1073+
1074+
clear() {
1075+
this.regions.clear();
1076+
}
1077+
1078+
get(id) {
1079+
return this.regions.get(id);
1080+
}
1081+
1082+
getAll() {
1083+
return Array.from(this.regions.values());
1084+
}
1085+
1086+
hitTest(px, py) {
1087+
for (const r of this.regions.values()) {
1088+
if (px >= r.x && px <= r.x + r.w && py >= r.y && py <= r.y + r.h) {
1089+
return r;
1090+
}
1091+
}
1092+
return null;
1093+
}
1094+
1095+
toggle() {
1096+
this.visible = !this.visible;
1097+
}
1098+
}
1099+
1100+
class HitboxDebug {
1101+
static draw(ctx, regions, cameraX = 0, cameraY = 0) {
1102+
if (!ctx) return;
1103+
for (const r of regions) {
1104+
ctx.strokeStyle = 'rgba(255,0,0,0.5)';
1105+
ctx.lineWidth = 1;
1106+
ctx.strokeRect(r.x - cameraX, r.y - cameraY, r.w, r.h);
1107+
}
1108+
}
1109+
}
1110+
10601111
class SpriteManager {
10611112
constructor(EngineClass = null, engineType = null) {
10621113
this.engine = EngineClass;

vite.config.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { readdirSync, existsSync } from 'node:fs';
1+
import { readdirSync, existsSync, cpSync, mkdirSync } from 'node:fs';
22
import { resolve } from 'node:path';
33
import { defineConfig } from 'vite';
44

@@ -15,6 +15,21 @@ export default defineConfig({
1515
port: 5173,
1616
open: '/',
1717
},
18+
plugins: [
19+
{
20+
name: 'copy-engine-vendors',
21+
closeBundle() {
22+
const engineDir = resolve(root, 'engine');
23+
const distEngine = resolve(root, 'dist', 'engine');
24+
if (!existsSync(distEngine)) mkdirSync(distEngine, { recursive: true });
25+
for (const f of readdirSync(engineDir)) {
26+
if (f.endsWith('.min.js')) {
27+
cpSync(resolve(engineDir, f), resolve(distEngine, f));
28+
}
29+
}
30+
},
31+
},
32+
],
1833
build: {
1934
target: 'es2020',
2035
minify: 'esbuild',

0 commit comments

Comments
 (0)