@nice2dev/game-engine 0.1.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (112) hide show
  1. package/CHANGELOG.md +193 -1
  2. package/dist/cjs/audio/AudioBridge.js +454 -0
  3. package/dist/cjs/audio/AudioBridge.js.map +1 -0
  4. package/dist/cjs/devtools/GameplayAnalytics.js +651 -0
  5. package/dist/cjs/devtools/GameplayAnalytics.js.map +1 -0
  6. package/dist/cjs/dialogue/DialogueSystem.js +1023 -0
  7. package/dist/cjs/dialogue/DialogueSystem.js.map +1 -0
  8. package/dist/cjs/editor/NiceGameEditor.js +569 -71
  9. package/dist/cjs/editor/NiceGameEditor.js.map +1 -1
  10. package/dist/cjs/engine/SaveSystemV2.js +494 -0
  11. package/dist/cjs/engine/SaveSystemV2.js.map +1 -0
  12. package/dist/cjs/i18n/useTranslation.js +11 -11
  13. package/dist/cjs/index.js +90 -1
  14. package/dist/cjs/index.js.map +1 -1
  15. package/dist/cjs/input/GamepadNavigation.js +21 -21
  16. package/dist/cjs/input/useGamepads.js +6 -6
  17. package/dist/cjs/integration/IconSprite.js +281 -0
  18. package/dist/cjs/integration/IconSprite.js.map +1 -0
  19. package/dist/cjs/inventory/InventorySystem.js +930 -0
  20. package/dist/cjs/inventory/InventorySystem.js.map +1 -0
  21. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/AbortController.js.map +1 -1
  22. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/AccessTokenHttpClient.js.map +1 -1
  23. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/DefaultHttpClient.js.map +1 -1
  24. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/DefaultReconnectPolicy.js.map +1 -1
  25. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/Errors.js.map +1 -1
  26. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/FetchHttpClient.js.map +1 -1
  27. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/HandshakeProtocol.js.map +1 -1
  28. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/HeaderNames.js.map +1 -1
  29. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/HttpClient.js.map +1 -1
  30. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/HttpConnection.js.map +1 -1
  31. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/HubConnection.js.map +1 -1
  32. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/HubConnectionBuilder.js.map +1 -1
  33. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/IHubProtocol.js.map +1 -1
  34. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/ILogger.js.map +1 -1
  35. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/ITransport.js.map +1 -1
  36. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/JsonHubProtocol.js.map +1 -1
  37. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/Loggers.js.map +1 -1
  38. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/LongPollingTransport.js.map +1 -1
  39. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/MessageBuffer.js.map +1 -1
  40. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/ServerSentEventsTransport.js.map +1 -1
  41. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/Subject.js.map +1 -1
  42. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/TextMessageFormat.js.map +1 -1
  43. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/Utils.js.map +1 -1
  44. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/WebSocketTransport.js.map +1 -1
  45. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/XhrHttpClient.js.map +1 -1
  46. package/dist/cjs/node_modules/@microsoft/signalr/dist/esm/pkg-version.js.map +1 -1
  47. package/dist/cjs/quest/QuestSystem.js +924 -0
  48. package/dist/cjs/quest/QuestSystem.js.map +1 -0
  49. package/dist/cjs/rendering/WebGPURenderPipeline.js +658 -0
  50. package/dist/cjs/rendering/WebGPURenderPipeline.js.map +1 -0
  51. package/dist/cjs/xr/ARVR.js.map +1 -1
  52. package/dist/esm/audio/AudioBridge.js +446 -0
  53. package/dist/esm/audio/AudioBridge.js.map +1 -0
  54. package/dist/esm/devtools/GameplayAnalytics.js +639 -0
  55. package/dist/esm/devtools/GameplayAnalytics.js.map +1 -0
  56. package/dist/esm/dialogue/DialogueSystem.js +1008 -0
  57. package/dist/esm/dialogue/DialogueSystem.js.map +1 -0
  58. package/dist/esm/editor/NiceGameEditor.js +556 -58
  59. package/dist/esm/editor/NiceGameEditor.js.map +1 -1
  60. package/dist/esm/engine/SaveSystemV2.js +487 -0
  61. package/dist/esm/engine/SaveSystemV2.js.map +1 -0
  62. package/dist/esm/index.js +11 -3
  63. package/dist/esm/index.js.map +1 -1
  64. package/dist/esm/integration/IconSprite.js +266 -0
  65. package/dist/esm/integration/IconSprite.js.map +1 -0
  66. package/dist/esm/inventory/InventorySystem.js +924 -0
  67. package/dist/esm/inventory/InventorySystem.js.map +1 -0
  68. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/AbortController.js.map +1 -1
  69. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/AccessTokenHttpClient.js.map +1 -1
  70. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/DefaultHttpClient.js.map +1 -1
  71. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/DefaultReconnectPolicy.js.map +1 -1
  72. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/Errors.js.map +1 -1
  73. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/FetchHttpClient.js.map +1 -1
  74. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/HandshakeProtocol.js.map +1 -1
  75. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/HeaderNames.js.map +1 -1
  76. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/HttpClient.js.map +1 -1
  77. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/HttpConnection.js.map +1 -1
  78. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/HubConnection.js.map +1 -1
  79. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/HubConnectionBuilder.js.map +1 -1
  80. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/IHubProtocol.js.map +1 -1
  81. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/ILogger.js.map +1 -1
  82. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/ITransport.js.map +1 -1
  83. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/JsonHubProtocol.js.map +1 -1
  84. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/Loggers.js.map +1 -1
  85. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/LongPollingTransport.js.map +1 -1
  86. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/MessageBuffer.js.map +1 -1
  87. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/ServerSentEventsTransport.js.map +1 -1
  88. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/Subject.js.map +1 -1
  89. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/TextMessageFormat.js.map +1 -1
  90. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/Utils.js.map +1 -1
  91. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/WebSocketTransport.js.map +1 -1
  92. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/XhrHttpClient.js.map +1 -1
  93. package/dist/esm/node_modules/@microsoft/signalr/dist/esm/pkg-version.js.map +1 -1
  94. package/dist/esm/quest/QuestSystem.js +916 -0
  95. package/dist/esm/quest/QuestSystem.js.map +1 -0
  96. package/dist/esm/rendering/WebGPURenderPipeline.js +642 -0
  97. package/dist/esm/rendering/WebGPURenderPipeline.js.map +1 -0
  98. package/dist/esm/xr/ARVR.js.map +1 -1
  99. package/dist/types/__tests__/setup.d.ts +1 -1
  100. package/dist/types/audio/AudioBridge.d.ts +199 -0
  101. package/dist/types/devtools/GameplayAnalytics.d.ts +279 -0
  102. package/dist/types/dialogue/DialogueSystem.d.ts +326 -0
  103. package/dist/types/dialogue/index.d.ts +2 -0
  104. package/dist/types/editor/NiceGameEditor.d.ts +12 -1
  105. package/dist/types/engine/SaveSystemV2.d.ts +155 -0
  106. package/dist/types/index.d.ts +19 -3
  107. package/dist/types/integration/IconSprite.d.ts +196 -0
  108. package/dist/types/inventory/InventorySystem.d.ts +336 -0
  109. package/dist/types/performance/WebGPUCompute.d.ts +0 -10
  110. package/dist/types/quest/QuestSystem.d.ts +287 -0
  111. package/dist/types/rendering/WebGPURenderPipeline.d.ts +255 -0
  112. package/package.json +7 -1
@@ -1,5 +1,5 @@
1
- import { jsx, jsxs } from 'react/jsx-runtime';
2
- import { createContext, useReducer, useRef, useMemo, useCallback, useContext, useEffect } from 'react';
1
+ import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
2
+ import React, { createContext, useReducer, useRef, useState, useCallback, useMemo, useContext, useEffect } from 'react';
3
3
  import { EventBus } from '../core/EventBus.js';
4
4
  import { World } from '../ecs/World.js';
5
5
  import { SceneManager } from '../scene/SceneManager.js';
@@ -154,6 +154,24 @@ const NiceGameEditor = ({ project, onSave, className, style, }) => {
154
154
  const undoStack = undoRef.current;
155
155
  const sceneManager = sceneManagerRef.current;
156
156
  const events = eventsRef.current;
157
+ const [worldVersion, setWorldVersion] = useState(0);
158
+ const [consoleLogs, setConsoleLogs] = useState([]);
159
+ const logIdRef = useRef(0);
160
+ const refreshWorld = useCallback(() => setWorldVersion(v => v + 1), []);
161
+ const addLog = useCallback((level, message) => {
162
+ setConsoleLogs(prev => [...prev.slice(-199), { id: logIdRef.current++, level, message, timestamp: Date.now() }]);
163
+ }, []);
164
+ const createNewEntity = useCallback((name, components = ['Transform']) => {
165
+ const entity = world.createEntity(name);
166
+ for (const comp of components) {
167
+ world.addComponent(entity.id, comp);
168
+ }
169
+ dispatch({ type: 'SELECT', entities: [entity.id] });
170
+ dispatch({ type: 'MARK_DIRTY' });
171
+ refreshWorld();
172
+ addLog('info', `Created entity "${name}" (#${entity.id})`);
173
+ return entity.id;
174
+ }, [world, refreshWorld, addLog]);
157
175
  // Keyboard shortcuts
158
176
  const shortcuts = useMemo(() => [
159
177
  { key: 'z', ctrl: true, action: () => undoStack.undo(), description: 'Undo' },
@@ -207,6 +225,8 @@ const NiceGameEditor = ({ project, onSave, className, style, }) => {
207
225
  }
208
226
  dispatch({ type: 'DESELECT_ALL' });
209
227
  dispatch({ type: 'MARK_DIRTY' });
228
+ refreshWorld();
229
+ addLog('info', `Deleted ${toDelete.length} entity(ies)`);
210
230
  undoStack.push({
211
231
  description: `Delete ${toDelete.length} entity(ies)`,
212
232
  undo: () => {
@@ -226,7 +246,12 @@ const NiceGameEditor = ({ project, onSave, className, style, }) => {
226
246
  undoStack,
227
247
  sceneManager,
228
248
  events,
229
- }), [state, world, undoStack, sceneManager, events]);
249
+ worldVersion,
250
+ refreshWorld,
251
+ createNewEntity,
252
+ consoleLogs,
253
+ addLog,
254
+ }), [state, world, undoStack, sceneManager, events, worldVersion, refreshWorld, createNewEntity, consoleLogs, addLog]);
230
255
  return (jsx(EditorContext.Provider, { value: ctxValue, children: jsxs("div", { className: `nice-game-editor ${className !== null && className !== void 0 ? className : ''}`, style: {
231
256
  display: 'flex',
232
257
  flexDirection: 'column',
@@ -240,20 +265,86 @@ const NiceGameEditor = ({ project, onSave, className, style, }) => {
240
265
  ...style,
241
266
  }, children: [jsx(EditorToolbar, {}), jsxs("div", { style: { display: 'flex', flex: 1, overflow: 'hidden' }, children: [((_a = state.panels.find(p => p.id === 'hierarchy')) === null || _a === void 0 ? void 0 : _a.visible) && (jsx(EditorPanelContainer, { title: "Hierarchy", width: 250, position: "left", children: jsx(HierarchyPanel, {}) })), jsx("div", { style: { flex: 1, position: 'relative', overflow: 'hidden' }, children: jsx(EditorViewport, {}) }), ((_b = state.panels.find(p => p.id === 'inspector')) === null || _b === void 0 ? void 0 : _b.visible) && (jsx(EditorPanelContainer, { title: "Inspector", width: 300, position: "right", children: jsx(InspectorPanel, {}) }))] }), jsxs("div", { style: { display: 'flex', borderTop: '1px solid #45475a' }, children: [((_c = state.panels.find(p => p.id === 'assets')) === null || _c === void 0 ? void 0 : _c.visible) && (jsx(EditorPanelContainer, { title: "Assets", height: 180, position: "bottom", children: jsx(AssetPanel, {}) })), ((_d = state.panels.find(p => p.id === 'console')) === null || _d === void 0 ? void 0 : _d.visible) && (jsx(EditorPanelContainer, { title: "Console", height: 180, position: "bottom", children: jsx(ConsolePanel, {}) }))] })] }) }));
242
267
  };
243
- /* ── Sub-components (stubs for now, will be expanded) ─────────── */
268
+ /* ── Helpers ───────────────────────────────────────────────────── */
269
+ function getEntityBounds(entityId, world) {
270
+ const shape = world.getComponent(entityId, 'Shape');
271
+ if (shape) {
272
+ if (shape.shape === 'circle')
273
+ return { x: -shape.radius, y: -shape.radius, w: shape.radius * 2, h: shape.radius * 2 };
274
+ return { x: -shape.width / 2, y: -shape.height / 2, w: shape.width, h: shape.height };
275
+ }
276
+ const sprite = world.getComponent(entityId, 'Sprite');
277
+ if (sprite) {
278
+ return { x: -sprite.width * sprite.anchor.x, y: -sprite.height * sprite.anchor.y, w: sprite.width, h: sprite.height };
279
+ }
280
+ const text = world.getComponent(entityId, 'Text');
281
+ if (text)
282
+ return { x: -30, y: -text.fontSize / 2, w: 60, h: text.fontSize };
283
+ return { x: -8, y: -8, w: 16, h: 16 };
284
+ }
285
+ function getEntityWorldBounds(entityId, world) {
286
+ const transform = world.getComponent(entityId, 'Transform');
287
+ if (!transform)
288
+ return null;
289
+ const b = getEntityBounds(entityId, world);
290
+ return { x: transform.position.x + b.x, y: transform.position.y + b.y, w: b.w, h: b.h };
291
+ }
292
+ function getCursorForTool(tool) {
293
+ switch (tool) {
294
+ case 'move': return 'move';
295
+ case 'rotate': return 'crosshair';
296
+ case 'scale': return 'nwse-resize';
297
+ case 'pan': return 'grab';
298
+ default: return 'default';
299
+ }
300
+ }
301
+ function colorToCSS(c) {
302
+ return `rgba(${Math.round(c.r * 255)},${Math.round(c.g * 255)},${Math.round(c.b * 255)},${c.a})`;
303
+ }
304
+ function cssToColor(hex) {
305
+ const m = hex.match(/^#?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i);
306
+ if (!m)
307
+ return { r: 1, g: 1, b: 1, a: 1 };
308
+ return { r: parseInt(m[1], 16) / 255, g: parseInt(m[2], 16) / 255, b: parseInt(m[3], 16) / 255, a: 1 };
309
+ }
310
+ function colorToHex(c) {
311
+ const r = Math.round(c.r * 255).toString(16).padStart(2, '0');
312
+ const g = Math.round(c.g * 255).toString(16).padStart(2, '0');
313
+ const b = Math.round(c.b * 255).toString(16).padStart(2, '0');
314
+ return `#${r}${g}${b}`;
315
+ }
316
+ const ENTITY_PRESETS = [
317
+ { label: 'Empty', icon: '○', components: ['Transform'] },
318
+ { label: 'Sprite', icon: '🖼', components: ['Transform', 'Sprite'] },
319
+ { label: 'Shape', icon: '■', components: ['Transform', 'Shape'] },
320
+ { label: 'Text', icon: 'T', components: ['Transform', 'Text'] },
321
+ { label: 'Physics Body', icon: '⚡', components: ['Transform', 'Shape', 'Collider2D', 'RigidBody2D'] },
322
+ { label: 'Camera', icon: '📷', components: ['Transform', 'CameraTarget'] },
323
+ ];
324
+ /* ── Sub-components ───────────────────────────────────────────── */
244
325
  const EditorToolbar = () => {
245
- const { state, dispatch } = useEditor();
326
+ const { state, dispatch, createNewEntity, undoStack } = useEditor();
327
+ const [showAddMenu, setShowAddMenu] = useState(false);
246
328
  const tools = ['select', 'move', 'rotate', 'scale', 'pan'];
247
329
  const toolIcons = { select: '⊕', move: '✥', rotate: '↻', scale: '⇲', pan: '✋' };
330
+ const btnStyle = (active) => ({
331
+ background: active ? '#585b70' : 'transparent',
332
+ border: 'none', color: '#cdd6f4', padding: '4px 8px',
333
+ borderRadius: 4, cursor: 'pointer', fontSize: 14,
334
+ });
248
335
  return (jsxs("div", { style: {
249
336
  display: 'flex', alignItems: 'center', gap: 4,
250
337
  padding: '4px 8px', borderBottom: '1px solid #45475a',
251
338
  backgroundColor: '#181825',
252
- }, children: [tools.map(t => (jsx("button", { onClick: () => dispatch({ type: 'SET_TOOL', tool: t }), style: {
253
- background: state.tool === t ? '#585b70' : 'transparent',
254
- border: 'none', color: '#cdd6f4', padding: '4px 8px',
255
- borderRadius: 4, cursor: 'pointer', fontSize: 14,
256
- }, title: t, children: toolIcons[t] }, t))), jsx("div", { style: { flex: 1 } }), jsxs("span", { style: { fontSize: 11, opacity: 0.6 }, children: [state.mode.toUpperCase(), " | ", state.tool, " | Zoom: ", (state.zoom * 100) | 0, "%", state.dirty ? ' •' : ''] })] }));
339
+ }, children: [tools.map(t => (jsx("button", { onClick: () => dispatch({ type: 'SET_TOOL', tool: t }), style: btnStyle(state.tool === t), title: `${t} (${t[0].toUpperCase()})`, children: toolIcons[t] }, t))), jsx("div", { style: { width: 1, height: 20, background: '#45475a', margin: '0 4px' } }), jsxs("div", { style: { position: 'relative' }, children: [jsx("button", { onClick: () => setShowAddMenu(!showAddMenu), style: { ...btnStyle(false), fontSize: 12, padding: '4px 10px', background: '#2f9e44', color: '#fff' }, children: "+ Entity" }), showAddMenu && (jsx("div", { style: {
340
+ position: 'absolute', top: '100%', left: 0, zIndex: 100,
341
+ background: '#1e1e2e', border: '1px solid #45475a', borderRadius: 4,
342
+ padding: 4, minWidth: 150, marginTop: 2,
343
+ }, children: ENTITY_PRESETS.map(p => (jsxs("button", { onClick: () => { createNewEntity(p.label, p.components); setShowAddMenu(false); }, style: {
344
+ display: 'block', width: '100%', textAlign: 'left',
345
+ background: 'transparent', border: 'none', color: '#cdd6f4',
346
+ padding: '4px 8px', cursor: 'pointer', borderRadius: 2, fontSize: 12,
347
+ }, children: [p.icon, " ", p.label] }, p.label))) }))] }), jsx("div", { style: { width: 1, height: 20, background: '#45475a', margin: '0 4px' } }), jsx("button", { onClick: () => dispatch({ type: 'SET_MODE', mode: state.mode === 'play' ? 'edit' : 'play' }), style: btnStyle(state.mode === 'play'), title: "Play/Stop", children: state.mode === 'play' ? '⏹' : '▶' }), jsx("button", { onClick: () => dispatch({ type: 'SET_MODE', mode: state.mode === 'pause' ? 'play' : 'pause' }), style: btnStyle(state.mode === 'pause'), title: "Pause", disabled: state.mode === 'edit', children: "\u23F8" }), jsx("div", { style: { width: 1, height: 20, background: '#45475a', margin: '0 4px' } }), jsx("button", { onClick: () => undoStack.undo(), style: btnStyle(false), title: "Undo (Ctrl+Z)", disabled: !undoStack.canUndo, children: "\u21A9" }), jsx("button", { onClick: () => undoStack.redo(), style: btnStyle(false), title: "Redo (Ctrl+Y)", disabled: !undoStack.canRedo, children: "\u21AA" }), jsx("div", { style: { width: 1, height: 20, background: '#45475a', margin: '0 4px' } }), jsx("button", { onClick: () => dispatch({ type: 'SET_ZOOM', zoom: state.zoom * 0.8 }), style: btnStyle(false), title: "Zoom Out", children: "\u2212" }), jsxs("span", { style: { fontSize: 11, minWidth: 40, textAlign: 'center' }, children: [(state.zoom * 100) | 0, "%"] }), jsx("button", { onClick: () => dispatch({ type: 'SET_ZOOM', zoom: state.zoom * 1.25 }), style: btnStyle(false), title: "Zoom In", children: "+" }), jsx("button", { onClick: () => { dispatch({ type: 'SET_ZOOM', zoom: 1 }); dispatch({ type: 'SET_PAN', offset: { x: 0, y: 0 } }); }, style: btnStyle(false), title: "Reset View", children: "\u2302" }), jsx("div", { style: { width: 1, height: 20, background: '#45475a', margin: '0 4px' } }), jsx("button", { onClick: () => dispatch({ type: 'TOGGLE_GRID' }), style: btnStyle(state.showGrid), title: "Toggle Grid (Ctrl+G)", children: "\u25A6" }), jsx("button", { onClick: () => dispatch({ type: 'TOGGLE_SNAP' }), style: btnStyle(state.snapToGrid), title: "Toggle Snap", children: "\u229E" }), jsx("div", { style: { flex: 1 } }), jsxs("span", { style: { fontSize: 11, opacity: 0.6 }, children: [state.mode.toUpperCase(), " | ", state.tool, " | ", state.dirty ? '● Unsaved' : '✓ Saved'] })] }));
257
348
  };
258
349
  const EditorPanelContainer = ({ title, width, height, position, children }) => {
259
350
  const isHorizontal = position === 'bottom';
@@ -272,16 +363,99 @@ const EditorPanelContainer = ({ title, width, height, position, children }) => {
272
363
  }, children: title }), jsx("div", { style: { flex: 1, overflow: 'auto', padding: 4 }, children: children })] }));
273
364
  };
274
365
  const HierarchyPanel = () => {
275
- const { world, state, dispatch } = useEditor();
366
+ const { world, state, dispatch, createNewEntity, refreshWorld, addLog } = useEditor();
276
367
  const entities = world.allEntities().filter(e => e.parent == null);
277
- return (jsx("div", { children: entities.map(e => (jsxs("div", { onClick: () => dispatch({ type: 'SELECT', entities: [e.id] }), style: {
278
- padding: '2px 6px', cursor: 'pointer',
279
- backgroundColor: state.selectedEntities.includes(e.id) ? '#45475a' : 'transparent',
280
- borderRadius: 2,
281
- }, children: [e.name, " ", jsxs("span", { style: { opacity: 0.4 }, children: ["#", e.id] })] }, e.id))) }));
368
+ const getIcon = (id) => {
369
+ if (world.hasComponent(id, 'CameraTarget'))
370
+ return '📷';
371
+ if (world.hasComponent(id, 'Sprite'))
372
+ return '🖼';
373
+ if (world.hasComponent(id, 'Text'))
374
+ return 'T';
375
+ if (world.hasComponent(id, 'Shape'))
376
+ return '■';
377
+ if (world.hasComponent(id, 'ParticleEmitter'))
378
+ return '✨';
379
+ if (world.hasComponent(id, 'AudioSource'))
380
+ return '🔊';
381
+ return '○';
382
+ };
383
+ const renderEntity = (e, depth) => {
384
+ const isSelected = state.selectedEntities.includes(e.id);
385
+ const children = e.children.map(cid => world.getEntity(cid)).filter(Boolean);
386
+ return (jsxs(React.Fragment, { children: [jsxs("div", { onClick: () => dispatch({ type: 'SELECT', entities: [e.id] }), style: {
387
+ padding: '2px 6px', paddingLeft: 6 + depth * 16, cursor: 'pointer',
388
+ backgroundColor: isSelected ? '#45475a' : 'transparent',
389
+ borderRadius: 2, display: 'flex', alignItems: 'center', gap: 4, fontSize: 12,
390
+ }, children: [jsx("span", { style: { fontSize: 10, width: 14, textAlign: 'center' }, children: getIcon(e.id) }), jsx("span", { style: { flex: 1 }, children: e.name }), jsxs("span", { style: { opacity: 0.3, fontSize: 10 }, children: ["#", e.id] })] }), children.map(c => renderEntity(c, depth + 1))] }, e.id));
391
+ };
392
+ return (jsxs("div", { children: [jsxs("div", { style: { padding: '2px 6px', fontSize: 10, display: 'flex', gap: 4, borderBottom: '1px solid #313244', marginBottom: 4 }, children: [jsx("button", { onClick: () => createNewEntity('Entity'), style: { background: 'transparent', border: 'none', color: '#a6adc8', cursor: 'pointer', fontSize: 10 }, title: "Add Entity", children: "+" }), jsx("button", { onClick: () => {
393
+ if (state.selectedEntities.length > 0) {
394
+ const id = state.selectedEntities[0];
395
+ const child = world.createEntity('Child');
396
+ world.addComponent(child.id, 'Transform');
397
+ world.setParent(child.id, id);
398
+ dispatch({ type: 'SELECT', entities: [child.id] });
399
+ dispatch({ type: 'MARK_DIRTY' });
400
+ refreshWorld();
401
+ addLog('info', `Created child entity (#${child.id})`);
402
+ }
403
+ }, style: { background: 'transparent', border: 'none', color: '#a6adc8', cursor: 'pointer', fontSize: 10 }, title: "Add Child", disabled: state.selectedEntities.length === 0, children: "\u21B3+" }), jsx("span", { style: { flex: 1 } }), jsxs("span", { style: { color: '#6c7086' }, children: [world.entityCount, " entities"] })] }), entities.map(e => renderEntity(e, 0))] }));
404
+ };
405
+ /* ── Inspector sub-editors ─────────────────────────────────────── */
406
+ const fieldStyle = {
407
+ width: '100%', background: '#313244', color: '#cdd6f4', border: '1px solid #45475a',
408
+ borderRadius: 3, padding: '2px 4px', fontSize: 11,
282
409
  };
410
+ const NumField = ({ label, value, onChange, step = 1 }) => (jsxs("div", { style: { display: 'flex', alignItems: 'center', gap: 4, marginBottom: 2 }, children: [jsx("span", { style: { fontSize: 10, minWidth: 50, color: '#a6adc8' }, children: label }), jsx("input", { type: "number", value: Math.round(value * 1000) / 1000, step: step, onChange: e => onChange(Number(e.target.value)), style: { ...fieldStyle, flex: 1 } })] }));
411
+ const ColField = ({ label, color, onChange }) => (jsxs("div", { style: { display: 'flex', alignItems: 'center', gap: 4, marginBottom: 2 }, children: [jsx("span", { style: { fontSize: 10, minWidth: 50, color: '#a6adc8' }, children: label }), jsx("input", { type: "color", value: colorToHex(color), onChange: e => onChange(cssToColor(e.target.value)), style: { width: 28, height: 20, border: 'none', padding: 0, cursor: 'pointer' } }), jsx("span", { style: { fontSize: 9, color: '#6c7086' }, children: colorToHex(color) })] }));
412
+ const TransformEditor = ({ entityId }) => {
413
+ const { world, refreshWorld, dispatch } = useEditor();
414
+ const t = world.getComponent(entityId, 'Transform');
415
+ if (!t)
416
+ return null;
417
+ const update = (fn) => { fn(); dispatch({ type: 'MARK_DIRTY' }); refreshWorld(); };
418
+ return (jsxs(Fragment, { children: [jsx(NumField, { label: "Pos X", value: t.position.x, onChange: v => update(() => { t.position.x = v; }) }), jsx(NumField, { label: "Pos Y", value: t.position.y, onChange: v => update(() => { t.position.y = v; }) }), jsx(NumField, { label: "Rotation", value: t.rotation * (180 / Math.PI), step: 5, onChange: v => update(() => { t.rotation = v * (Math.PI / 180); }) }), jsx(NumField, { label: "Scale X", value: t.scale.x, step: 0.1, onChange: v => update(() => { t.scale.x = v; }) }), jsx(NumField, { label: "Scale Y", value: t.scale.y, step: 0.1, onChange: v => update(() => { t.scale.y = v; }) })] }));
419
+ };
420
+ const ShapeEditor = ({ entityId }) => {
421
+ const { world, refreshWorld, dispatch } = useEditor();
422
+ const s = world.getComponent(entityId, 'Shape');
423
+ if (!s)
424
+ return null;
425
+ const update = (fn) => { fn(); dispatch({ type: 'MARK_DIRTY' }); refreshWorld(); };
426
+ return (jsxs(Fragment, { children: [jsxs("div", { style: { display: 'flex', alignItems: 'center', gap: 4, marginBottom: 2 }, children: [jsx("span", { style: { fontSize: 10, minWidth: 50, color: '#a6adc8' }, children: "Shape" }), jsxs("select", { value: s.shape, onChange: e => update(() => { s.shape = e.target.value; }), style: { ...fieldStyle, flex: 1 }, children: [jsx("option", { value: "rect", children: "Rectangle" }), jsx("option", { value: "circle", children: "Circle" }), jsx("option", { value: "polygon", children: "Polygon" }), jsx("option", { value: "line", children: "Line" })] })] }), s.shape !== 'circle' && jsx(NumField, { label: "Width", value: s.width, onChange: v => update(() => { s.width = v; }) }), s.shape !== 'circle' && jsx(NumField, { label: "Height", value: s.height, onChange: v => update(() => { s.height = v; }) }), s.shape === 'circle' && jsx(NumField, { label: "Radius", value: s.radius, onChange: v => update(() => { s.radius = v; }) }), jsx(ColField, { label: "Fill", color: s.fillColor, onChange: c => update(() => { Object.assign(s.fillColor, c); }) }), jsx(ColField, { label: "Stroke", color: s.strokeColor, onChange: c => update(() => { Object.assign(s.strokeColor, c); }) }), jsx(NumField, { label: "Stroke W", value: s.strokeWidth, onChange: v => update(() => { s.strokeWidth = v; }) })] }));
427
+ };
428
+ const SpriteEditor = ({ entityId }) => {
429
+ const { world, refreshWorld, dispatch } = useEditor();
430
+ const s = world.getComponent(entityId, 'Sprite');
431
+ if (!s)
432
+ return null;
433
+ const update = (fn) => { fn(); dispatch({ type: 'MARK_DIRTY' }); refreshWorld(); };
434
+ return (jsxs(Fragment, { children: [jsx(NumField, { label: "Width", value: s.width, onChange: v => update(() => { s.width = v; }) }), jsx(NumField, { label: "Height", value: s.height, onChange: v => update(() => { s.height = v; }) }), jsx(ColField, { label: "Tint", color: s.tint, onChange: c => update(() => { Object.assign(s.tint, c); }) }), jsx(NumField, { label: "Opacity", value: s.opacity, step: 0.1, onChange: v => update(() => { s.opacity = Math.max(0, Math.min(1, v)); }) }), jsx(NumField, { label: "Layer", value: s.layer, step: 1, onChange: v => update(() => { s.layer = v; }) }), jsxs("div", { style: { display: 'flex', alignItems: 'center', gap: 4, marginBottom: 2 }, children: [jsx("span", { style: { fontSize: 10, minWidth: 50, color: '#a6adc8' }, children: "Visible" }), jsx("input", { type: "checkbox", checked: s.visible, onChange: e => update(() => { s.visible = e.target.checked; }) })] })] }));
435
+ };
436
+ const TextEditor = ({ entityId }) => {
437
+ const { world, refreshWorld, dispatch } = useEditor();
438
+ const t = world.getComponent(entityId, 'Text');
439
+ if (!t)
440
+ return null;
441
+ const update = (fn) => { fn(); dispatch({ type: 'MARK_DIRTY' }); refreshWorld(); };
442
+ return (jsxs(Fragment, { children: [jsxs("div", { style: { marginBottom: 2 }, children: [jsx("span", { style: { fontSize: 10, color: '#a6adc8' }, children: "Text" }), jsx("input", { type: "text", value: t.text, onChange: e => update(() => { t.text = e.target.value; }), style: { ...fieldStyle, width: '100%' } })] }), jsx(NumField, { label: "Font Size", value: t.fontSize, onChange: v => update(() => { t.fontSize = v; }) }), jsxs("div", { style: { display: 'flex', alignItems: 'center', gap: 4, marginBottom: 2 }, children: [jsx("span", { style: { fontSize: 10, minWidth: 50, color: '#a6adc8' }, children: "Align" }), jsxs("select", { value: t.align, onChange: e => update(() => { t.align = e.target.value; }), style: { ...fieldStyle, flex: 1 }, children: [jsx("option", { value: "left", children: "Left" }), jsx("option", { value: "center", children: "Center" }), jsx("option", { value: "right", children: "Right" })] })] }), jsx(ColField, { label: "Color", color: t.color, onChange: c => update(() => { Object.assign(t.color, c); }) })] }));
443
+ };
444
+ const RigidBodyEditor = ({ entityId }) => {
445
+ const { world, refreshWorld, dispatch } = useEditor();
446
+ const rb = world.getComponent(entityId, 'RigidBody2D');
447
+ if (!rb)
448
+ return null;
449
+ const update = (fn) => { fn(); dispatch({ type: 'MARK_DIRTY' }); refreshWorld(); };
450
+ return (jsxs(Fragment, { children: [jsxs("div", { style: { display: 'flex', alignItems: 'center', gap: 4, marginBottom: 2 }, children: [jsx("span", { style: { fontSize: 10, minWidth: 50, color: '#a6adc8' }, children: "Body" }), jsxs("select", { value: rb.bodyType, onChange: e => update(() => { rb.bodyType = e.target.value; }), style: { ...fieldStyle, flex: 1 }, children: [jsx("option", { value: "dynamic", children: "Dynamic" }), jsx("option", { value: "static", children: "Static" }), jsx("option", { value: "kinematic", children: "Kinematic" })] })] }), jsx(NumField, { label: "Mass", value: rb.mass, step: 0.1, onChange: v => update(() => { rb.mass = v; }) }), jsx(NumField, { label: "Drag", value: rb.drag, step: 0.01, onChange: v => update(() => { rb.drag = v; }) }), jsx(NumField, { label: "Bounce", value: rb.restitution, step: 0.05, onChange: v => update(() => { rb.restitution = v; }) }), jsx(NumField, { label: "Friction", value: rb.friction, step: 0.05, onChange: v => update(() => { rb.friction = v; }) }), jsx(NumField, { label: "Gravity", value: rb.gravityScale, step: 0.1, onChange: v => update(() => { rb.gravityScale = v; }) })] }));
451
+ };
452
+ const ADDABLE_COMPONENTS = [
453
+ 'Transform', 'Sprite', 'Shape', 'Collider2D', 'RigidBody2D',
454
+ 'Text', 'Animator', 'Script', 'AudioSource', 'ParticleEmitter', 'CameraTarget',
455
+ ];
283
456
  const InspectorPanel = () => {
284
- const { state, world } = useEditor();
457
+ const { state, world, dispatch, refreshWorld, addLog } = useEditor();
458
+ const [showAddComp, setShowAddComp] = useState(false);
285
459
  if (state.selectedEntities.length === 0) {
286
460
  return jsx("div", { style: { opacity: 0.5, padding: 8 }, children: "No selection" });
287
461
  }
@@ -289,26 +463,86 @@ const InspectorPanel = () => {
289
463
  const entity = world.getEntity(entityId);
290
464
  if (!entity)
291
465
  return null;
292
- const compTypes = [
293
- 'Transform', 'Sprite', 'Shape', 'Collider2D', 'RigidBody2D',
294
- 'Text', 'Animator', 'Script', 'AudioSource', 'ParticleEmitter', 'CameraTarget',
295
- ];
296
- return (jsxs("div", { children: [jsx("div", { style: { fontWeight: 'bold', padding: '4px 0' }, children: entity.name }), compTypes.map(type => {
297
- const comp = world.getComponent(entityId, type);
298
- if (!comp)
299
- return null;
300
- return (jsxs("div", { style: { marginTop: 8, borderTop: '1px solid #313244', paddingTop: 4 }, children: [jsx("div", { style: { fontSize: 11, fontWeight: 'bold', textTransform: 'uppercase' }, children: type }), jsx("pre", { style: { fontSize: 10, opacity: 0.7, whiteSpace: 'pre-wrap', margin: 0 }, children: JSON.stringify(comp, null, 2) })] }, type));
301
- })] }));
466
+ const compTypes = ADDABLE_COMPONENTS;
467
+ const presentComponents = compTypes.filter(type => world.hasComponent(entityId, type));
468
+ const componentEditors = {
469
+ Transform: TransformEditor,
470
+ Shape: ShapeEditor,
471
+ Sprite: SpriteEditor,
472
+ Text: TextEditor,
473
+ RigidBody2D: RigidBodyEditor,
474
+ };
475
+ return (jsxs("div", { children: [jsxs("div", { style: { padding: '4px 0', marginBottom: 4, borderBottom: '1px solid #313244' }, children: [jsx("input", { type: "text", value: entity.name, onChange: e => { entity.name = e.target.value; dispatch({ type: 'MARK_DIRTY' }); refreshWorld(); }, style: { ...fieldStyle, fontWeight: 'bold', fontSize: 13 } }), jsxs("span", { style: { fontSize: 9, color: '#6c7086' }, children: ["ID: ", entityId] })] }), presentComponents.map(type => {
476
+ const Editor = componentEditors[type];
477
+ return (jsxs("div", { style: { marginBottom: 8, borderTop: '1px solid #313244', paddingTop: 4 }, children: [jsxs("div", { style: { display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 4 }, children: [jsx("span", { style: { fontSize: 11, fontWeight: 'bold', textTransform: 'uppercase', color: '#89b4fa' }, children: type }), type !== 'Transform' && (jsx("button", { onClick: () => {
478
+ world.removeComponent(entityId, type);
479
+ dispatch({ type: 'MARK_DIRTY' });
480
+ refreshWorld();
481
+ addLog('info', `Removed ${type} from #${entityId}`);
482
+ }, style: { background: 'transparent', border: 'none', color: '#f38ba8', cursor: 'pointer', fontSize: 10 }, title: `Remove ${type}`, children: "\u2715" }))] }), Editor ? jsx(Editor, { entityId: entityId }) : (jsx("pre", { style: { fontSize: 10, opacity: 0.6, whiteSpace: 'pre-wrap', margin: 0 }, children: JSON.stringify(world.getComponent(entityId, type), null, 2) }))] }, type));
483
+ }), jsxs("div", { style: { marginTop: 8, position: 'relative' }, children: [jsx("button", { onClick: () => setShowAddComp(!showAddComp), style: { width: '100%', padding: '4px 8px', background: '#313244', border: '1px solid #45475a', color: '#cdd6f4', borderRadius: 4, cursor: 'pointer', fontSize: 11 }, children: "+ Add Component" }), showAddComp && (jsx("div", { style: {
484
+ position: 'absolute', bottom: '100%', left: 0, right: 0,
485
+ background: '#1e1e2e', border: '1px solid #45475a', borderRadius: 4,
486
+ padding: 4, marginBottom: 2, maxHeight: 200, overflow: 'auto', zIndex: 10,
487
+ }, children: compTypes.filter(t => !world.hasComponent(entityId, t)).map(type => (jsx("button", { onClick: () => {
488
+ world.addComponent(entityId, type);
489
+ dispatch({ type: 'MARK_DIRTY' });
490
+ refreshWorld();
491
+ addLog('info', `Added ${type} to #${entityId}`);
492
+ setShowAddComp(false);
493
+ }, style: {
494
+ display: 'block', width: '100%', textAlign: 'left',
495
+ background: 'transparent', border: 'none', color: '#cdd6f4',
496
+ padding: '3px 6px', cursor: 'pointer', borderRadius: 2, fontSize: 11,
497
+ }, children: type }, type))) }))] })] }));
302
498
  };
303
499
  const AssetPanel = () => {
304
- return jsx("div", { style: { opacity: 0.5, padding: 8 }, children: "Asset browser \u2014 drop files here" });
500
+ const [tab, setTab] = useState('all');
501
+ const tabBtnStyle = (active) => ({
502
+ padding: '2px 8px', border: 'none', borderRadius: '3px 3px 0 0', cursor: 'pointer', fontSize: 10,
503
+ background: active ? '#313244' : 'transparent', color: active ? '#cdd6f4' : '#6c7086',
504
+ });
505
+ const assets = [
506
+ { name: 'player.png', type: 'sprites', icon: '🖼' },
507
+ { name: 'enemy.png', type: 'sprites', icon: '🖼' },
508
+ { name: 'tileset.png', type: 'sprites', icon: '🖼' },
509
+ { name: 'background.jpg', type: 'sprites', icon: '🖼' },
510
+ { name: 'jump.wav', type: 'audio', icon: '🔊' },
511
+ { name: 'music.mp3', type: 'audio', icon: '🎵' },
512
+ { name: 'coin.wav', type: 'audio', icon: '🔊' },
513
+ { name: 'playerController.ts', type: 'scripts', icon: '📜' },
514
+ { name: 'enemyAI.ts', type: 'scripts', icon: '📜' },
515
+ ];
516
+ const filtered = tab === 'all' ? assets : assets.filter(a => a.type === tab);
517
+ return (jsxs("div", { style: { display: 'flex', flexDirection: 'column', height: '100%' }, children: [jsxs("div", { style: { display: 'flex', gap: 2, padding: '2px 4px', borderBottom: '1px solid #313244' }, children: [['all', 'sprites', 'audio', 'scripts'].map(t => (jsx("button", { onClick: () => setTab(t), style: tabBtnStyle(tab === t), children: t.charAt(0).toUpperCase() + t.slice(1) }, t))), jsx("span", { style: { flex: 1 } }), jsx("button", { style: { background: 'transparent', border: 'none', color: '#a6adc8', cursor: 'pointer', fontSize: 10 }, title: "Import Asset", children: "+ Import" })] }), jsx("div", { style: { flex: 1, overflow: 'auto', display: 'grid', gridTemplateColumns: 'repeat(auto-fill, 80px)', gap: 4, padding: 4 }, children: filtered.map(a => (jsxs("div", { draggable: true, style: {
518
+ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 2,
519
+ padding: 4, background: '#313244', borderRadius: 4, cursor: 'grab',
520
+ fontSize: 9, textAlign: 'center',
521
+ }, children: [jsx("span", { style: { fontSize: 20 }, children: a.icon }), jsx("span", { style: { wordBreak: 'break-all' }, children: a.name })] }, a.name))) })] }));
305
522
  };
306
523
  const ConsolePanel = () => {
307
- return jsx("div", { style: { opacity: 0.5, padding: 8, fontFamily: 'monospace', fontSize: 11 }, children: "Console ready." });
524
+ const { consoleLogs } = useEditor();
525
+ const [filter, setFilter] = useState('all');
526
+ const scrollRef = useRef(null);
527
+ useEffect(() => {
528
+ if (scrollRef.current)
529
+ scrollRef.current.scrollTop = scrollRef.current.scrollHeight;
530
+ }, [consoleLogs.length]);
531
+ const filtered = filter === 'all' ? consoleLogs : consoleLogs.filter(l => l.level === filter);
532
+ const levelColors = { info: '#89b4fa', warn: '#f9e2af', error: '#f38ba8' };
533
+ const filterBtn = (f, label) => (jsx("button", { onClick: () => setFilter(f), style: { padding: '1px 6px', border: 'none', borderRadius: 2, cursor: 'pointer', fontSize: 9,
534
+ background: filter === f ? '#313244' : 'transparent', color: filter === f ? '#cdd6f4' : '#6c7086' }, children: label }));
535
+ return (jsxs("div", { style: { display: 'flex', flexDirection: 'column', height: '100%', fontFamily: 'monospace' }, children: [jsxs("div", { style: { display: 'flex', gap: 2, padding: '2px 4px', borderBottom: '1px solid #313244' }, children: [filterBtn('all', 'All'), " ", filterBtn('info', 'Info'), " ", filterBtn('warn', 'Warn'), " ", filterBtn('error', 'Error'), jsx("span", { style: { flex: 1 } }), jsxs("span", { style: { fontSize: 9, color: '#6c7086' }, children: [filtered.length, " entries"] })] }), jsx("div", { ref: scrollRef, style: { flex: 1, overflow: 'auto', fontSize: 10 }, children: filtered.length === 0 ? (jsx("div", { style: { opacity: 0.4, padding: 8 }, children: "Console ready." })) : filtered.map(log => (jsxs("div", { style: { padding: '1px 4px', borderBottom: '1px solid #181825', display: 'flex', gap: 6 }, children: [jsx("span", { style: { color: '#6c7086', minWidth: 55 }, children: new Date(log.timestamp).toLocaleTimeString([], { hour12: false }) }), jsxs("span", { style: { color: levelColors[log.level], fontWeight: 'bold', minWidth: 35 }, children: ["[", log.level.toUpperCase(), "]"] }), jsx("span", { children: log.message })] }, log.id))) })] }));
308
536
  };
309
537
  const EditorViewport = () => {
310
538
  const canvasRef = useRef(null);
311
- const { state } = useEditor();
539
+ const { state, dispatch, world, worldVersion, refreshWorld, undoStack } = useEditor();
540
+ const dragging = useRef(false);
541
+ const dragStart = useRef({ x: 0, y: 0 });
542
+ const dragEntityStart = useRef({ x: 0, y: 0 });
543
+ const panning = useRef(false);
544
+ const panStart = useRef({ x: 0, y: 0, px: 0, py: 0 });
545
+ // Draw loop
312
546
  useEffect(() => {
313
547
  const canvas = canvasRef.current;
314
548
  if (!canvas)
@@ -316,39 +550,303 @@ const EditorViewport = () => {
316
550
  const ctx = canvas.getContext('2d');
317
551
  if (!ctx)
318
552
  return;
319
- const draw = () => {
320
- const { width, height } = canvas.getBoundingClientRect();
321
- canvas.width = width;
322
- canvas.height = height;
323
- ctx.fillStyle = '#11111b';
324
- ctx.fillRect(0, 0, width, height);
325
- // Grid
326
- if (state.showGrid) {
327
- const gs = state.gridSize * state.zoom;
328
- ctx.strokeStyle = 'rgba(69, 71, 90, 0.3)';
329
- ctx.lineWidth = 0.5;
330
- const ox = state.panOffset.x % gs;
331
- const oy = state.panOffset.y % gs;
332
- for (let x = ox; x < width; x += gs) {
333
- ctx.beginPath();
334
- ctx.moveTo(x, 0);
335
- ctx.lineTo(x, height);
336
- ctx.stroke();
553
+ const { width, height } = canvas.getBoundingClientRect();
554
+ canvas.width = width;
555
+ canvas.height = height;
556
+ ctx.fillStyle = '#11111b';
557
+ ctx.fillRect(0, 0, width, height);
558
+ ctx.save();
559
+ ctx.translate(width / 2 + state.panOffset.x, height / 2 + state.panOffset.y);
560
+ ctx.scale(state.zoom, state.zoom);
561
+ // Grid
562
+ if (state.showGrid) {
563
+ const gs = state.gridSize;
564
+ const halfW = (width / 2) / state.zoom;
565
+ const halfH = (height / 2) / state.zoom;
566
+ const offX = -state.panOffset.x / state.zoom;
567
+ const offY = -state.panOffset.y / state.zoom;
568
+ const startX = Math.floor((offX - halfW) / gs) * gs;
569
+ const endX = Math.ceil((offX + halfW) / gs) * gs;
570
+ const startY = Math.floor((offY - halfH) / gs) * gs;
571
+ const endY = Math.ceil((offY + halfH) / gs) * gs;
572
+ ctx.strokeStyle = 'rgba(69, 71, 90, 0.3)';
573
+ ctx.lineWidth = 0.5 / state.zoom;
574
+ for (let x = startX; x <= endX; x += gs) {
575
+ ctx.beginPath();
576
+ ctx.moveTo(x, startY);
577
+ ctx.lineTo(x, endY);
578
+ ctx.stroke();
579
+ }
580
+ for (let y = startY; y <= endY; y += gs) {
581
+ ctx.beginPath();
582
+ ctx.moveTo(startX, y);
583
+ ctx.lineTo(endX, y);
584
+ ctx.stroke();
585
+ }
586
+ // Origin axes
587
+ ctx.strokeStyle = 'rgba(205, 214, 244, 0.15)';
588
+ ctx.lineWidth = 1 / state.zoom;
589
+ ctx.beginPath();
590
+ ctx.moveTo(0, startY);
591
+ ctx.lineTo(0, endY);
592
+ ctx.stroke();
593
+ ctx.beginPath();
594
+ ctx.moveTo(startX, 0);
595
+ ctx.lineTo(endX, 0);
596
+ ctx.stroke();
597
+ }
598
+ // Render entities
599
+ const entities = world.allEntities();
600
+ for (const entity of entities) {
601
+ if (!entity.enabled)
602
+ continue;
603
+ const transform = world.getComponent(entity.id, 'Transform');
604
+ if (!transform)
605
+ continue;
606
+ ctx.save();
607
+ ctx.translate(transform.position.x, transform.position.y);
608
+ ctx.rotate(transform.rotation);
609
+ ctx.scale(transform.scale.x, transform.scale.y);
610
+ const isSelected = state.selectedEntities.includes(entity.id);
611
+ // Shape
612
+ const shape = world.getComponent(entity.id, 'Shape');
613
+ if (shape && shape.visible) {
614
+ ctx.fillStyle = colorToCSS(shape.fillColor);
615
+ ctx.strokeStyle = colorToCSS(shape.strokeColor);
616
+ ctx.lineWidth = shape.strokeWidth;
617
+ switch (shape.shape) {
618
+ case 'rect':
619
+ ctx.fillRect(-shape.width / 2, -shape.height / 2, shape.width, shape.height);
620
+ if (shape.strokeWidth > 0)
621
+ ctx.strokeRect(-shape.width / 2, -shape.height / 2, shape.width, shape.height);
622
+ break;
623
+ case 'circle':
624
+ ctx.beginPath();
625
+ ctx.arc(0, 0, shape.radius, 0, Math.PI * 2);
626
+ ctx.fill();
627
+ if (shape.strokeWidth > 0)
628
+ ctx.stroke();
629
+ break;
630
+ case 'line':
631
+ ctx.beginPath();
632
+ ctx.moveTo(shape.lineStart.x, shape.lineStart.y);
633
+ ctx.lineTo(shape.lineEnd.x, shape.lineEnd.y);
634
+ ctx.stroke();
635
+ break;
636
+ case 'polygon':
637
+ if (shape.points.length > 2) {
638
+ ctx.beginPath();
639
+ ctx.moveTo(shape.points[0].x, shape.points[0].y);
640
+ for (let i = 1; i < shape.points.length; i++)
641
+ ctx.lineTo(shape.points[i].x, shape.points[i].y);
642
+ ctx.closePath();
643
+ ctx.fill();
644
+ if (shape.strokeWidth > 0)
645
+ ctx.stroke();
646
+ }
647
+ break;
337
648
  }
338
- for (let y = oy; y < height; y += gs) {
649
+ }
650
+ // Sprite
651
+ const sprite = world.getComponent(entity.id, 'Sprite');
652
+ if (sprite && sprite.visible) {
653
+ ctx.globalAlpha = sprite.opacity;
654
+ ctx.fillStyle = colorToCSS(sprite.tint);
655
+ const ax = -sprite.width * sprite.anchor.x;
656
+ const ay = -sprite.height * sprite.anchor.y;
657
+ ctx.fillRect(ax, ay, sprite.width, sprite.height);
658
+ // Sprite border
659
+ ctx.strokeStyle = 'rgba(255,255,255,0.2)';
660
+ ctx.lineWidth = 1 / state.zoom;
661
+ ctx.strokeRect(ax, ay, sprite.width, sprite.height);
662
+ // Sprite label
663
+ ctx.fillStyle = 'rgba(255,255,255,0.5)';
664
+ ctx.font = `${9 / state.zoom}px monospace`;
665
+ ctx.textAlign = 'center';
666
+ ctx.textBaseline = 'middle';
667
+ ctx.fillText(sprite.assetId || 'sprite', 0, 0);
668
+ ctx.globalAlpha = 1;
669
+ }
670
+ // Text
671
+ const text = world.getComponent(entity.id, 'Text');
672
+ if (text && text.visible && text.text) {
673
+ ctx.font = `${text.fontSize}px ${text.fontFamily}`;
674
+ ctx.fillStyle = colorToCSS(text.color);
675
+ ctx.textAlign = text.align;
676
+ ctx.textBaseline = text.baseline;
677
+ ctx.fillText(text.text, 0, 0);
678
+ }
679
+ // Particle emitter indicator
680
+ const particles = world.getComponent(entity.id, 'ParticleEmitter');
681
+ if (particles) {
682
+ ctx.strokeStyle = '#f9e2af';
683
+ ctx.lineWidth = 1 / state.zoom;
684
+ ctx.setLineDash([3 / state.zoom, 3 / state.zoom]);
685
+ ctx.beginPath();
686
+ ctx.arc(0, 0, 12, 0, Math.PI * 2);
687
+ ctx.stroke();
688
+ ctx.setLineDash([]);
689
+ ctx.fillStyle = '#f9e2af';
690
+ ctx.font = `${8 / state.zoom}px monospace`;
691
+ ctx.textAlign = 'center';
692
+ ctx.fillText('✨', 0, 3 / state.zoom);
693
+ }
694
+ // Camera indicator
695
+ const cam = world.getComponent(entity.id, 'CameraTarget');
696
+ if (cam) {
697
+ ctx.strokeStyle = '#a6e3a1';
698
+ ctx.lineWidth = 1.5 / state.zoom;
699
+ const hw = 60, hh = 34;
700
+ ctx.strokeRect(-hw, -hh, hw * 2, hh * 2);
701
+ ctx.font = `${8 / state.zoom}px monospace`;
702
+ ctx.fillStyle = '#a6e3a1';
703
+ ctx.textAlign = 'center';
704
+ ctx.fillText('📷 Camera', 0, -hh - 4 / state.zoom);
705
+ }
706
+ // Selection highlight
707
+ if (isSelected) {
708
+ const b = getEntityBounds(entity.id, world);
709
+ ctx.strokeStyle = '#89b4fa';
710
+ ctx.lineWidth = 2 / state.zoom;
711
+ ctx.setLineDash([4 / state.zoom, 4 / state.zoom]);
712
+ ctx.strokeRect(b.x - 2, b.y - 2, b.w + 4, b.h + 4);
713
+ ctx.setLineDash([]);
714
+ // Corner handles
715
+ const hs = 4 / state.zoom;
716
+ ctx.fillStyle = '#89b4fa';
717
+ ctx.fillRect(b.x - hs, b.y - hs, hs * 2, hs * 2);
718
+ ctx.fillRect(b.x + b.w - hs, b.y - hs, hs * 2, hs * 2);
719
+ ctx.fillRect(b.x - hs, b.y + b.h - hs, hs * 2, hs * 2);
720
+ ctx.fillRect(b.x + b.w - hs, b.y + b.h - hs, hs * 2, hs * 2);
721
+ }
722
+ // Entity name label (only if no visible shape/sprite/text OR selected)
723
+ if (isSelected || (!(shape === null || shape === void 0 ? void 0 : shape.visible) && !(sprite === null || sprite === void 0 ? void 0 : sprite.visible) && !(text === null || text === void 0 ? void 0 : text.visible))) {
724
+ ctx.font = `${10 / state.zoom}px monospace`;
725
+ ctx.fillStyle = isSelected ? '#89b4fa' : 'rgba(205, 214, 244, 0.4)';
726
+ ctx.textAlign = 'center';
727
+ ctx.textBaseline = 'bottom';
728
+ const b = getEntityBounds(entity.id, world);
729
+ ctx.fillText(entity.name, 0, b.y - 4 / state.zoom);
730
+ // Draw small dot for invisible/empty entities
731
+ if (!(shape === null || shape === void 0 ? void 0 : shape.visible) && !(sprite === null || sprite === void 0 ? void 0 : sprite.visible) && !(text === null || text === void 0 ? void 0 : text.visible)) {
732
+ ctx.fillStyle = isSelected ? '#89b4fa' : 'rgba(205, 214, 244, 0.3)';
339
733
  ctx.beginPath();
340
- ctx.moveTo(0, y);
341
- ctx.lineTo(width, y);
342
- ctx.stroke();
734
+ ctx.arc(0, 0, 3 / state.zoom, 0, Math.PI * 2);
735
+ ctx.fill();
343
736
  }
344
737
  }
345
- ctx.fillStyle = '#cdd6f4';
346
- ctx.font = '12px monospace';
347
- ctx.fillText(`Viewport — ${state.mode}`, 8, 16);
738
+ ctx.restore();
739
+ }
740
+ ctx.restore();
741
+ // HUD
742
+ ctx.fillStyle = '#6c7086';
743
+ ctx.font = '10px monospace';
744
+ ctx.textAlign = 'left';
745
+ ctx.textBaseline = 'top';
746
+ ctx.fillText(`Entities: ${world.entityCount} | Grid: ${state.gridSize}px`, 8, 8);
747
+ }, [state, worldVersion, world]);
748
+ // Mouse handlers
749
+ const screenToWorld = useCallback((e) => {
750
+ const canvas = canvasRef.current;
751
+ const rect = canvas.getBoundingClientRect();
752
+ const sx = e.clientX - rect.left;
753
+ const sy = e.clientY - rect.top;
754
+ return {
755
+ x: (sx - canvas.width / 2 - state.panOffset.x) / state.zoom,
756
+ y: (sy - canvas.height / 2 - state.panOffset.y) / state.zoom,
348
757
  };
349
- draw();
350
- }, [state]);
351
- return (jsx("canvas", { ref: canvasRef, style: { width: '100%', height: '100%', display: 'block' } }));
758
+ }, [state.zoom, state.panOffset]);
759
+ const handlePointerDown = useCallback((e) => {
760
+ // Middle click or pan tool = pan
761
+ if (e.button === 1 || (e.button === 0 && state.tool === 'pan')) {
762
+ panning.current = true;
763
+ panStart.current = { x: e.clientX, y: e.clientY, px: state.panOffset.x, py: state.panOffset.y };
764
+ e.target.setPointerCapture(e.pointerId);
765
+ return;
766
+ }
767
+ if (e.button !== 0)
768
+ return;
769
+ const { x: wx, y: wy } = screenToWorld(e);
770
+ // Hit test (reverse order for top-most)
771
+ const entities = world.allEntities();
772
+ let hitId = null;
773
+ for (let i = entities.length - 1; i >= 0; i--) {
774
+ const ent = entities[i];
775
+ if (!ent.enabled)
776
+ continue;
777
+ const bounds = getEntityWorldBounds(ent.id, world);
778
+ if (bounds && wx >= bounds.x && wx <= bounds.x + bounds.w && wy >= bounds.y && wy <= bounds.y + bounds.h) {
779
+ hitId = ent.id;
780
+ break;
781
+ }
782
+ }
783
+ if (hitId !== null) {
784
+ dispatch({ type: 'SELECT', entities: [hitId] });
785
+ if (state.tool === 'select' || state.tool === 'move') {
786
+ dragging.current = true;
787
+ dragStart.current = { x: wx, y: wy };
788
+ const t = world.getComponent(hitId, 'Transform');
789
+ if (t)
790
+ dragEntityStart.current = { x: t.position.x, y: t.position.y };
791
+ }
792
+ }
793
+ else {
794
+ dispatch({ type: 'DESELECT_ALL' });
795
+ }
796
+ e.target.setPointerCapture(e.pointerId);
797
+ }, [state.tool, state.panOffset, screenToWorld, world, dispatch]);
798
+ const handlePointerMove = useCallback((e) => {
799
+ if (panning.current) {
800
+ const dx = e.clientX - panStart.current.x;
801
+ const dy = e.clientY - panStart.current.y;
802
+ dispatch({ type: 'SET_PAN', offset: { x: panStart.current.px + dx, y: panStart.current.py + dy } });
803
+ return;
804
+ }
805
+ if (dragging.current && state.selectedEntities.length > 0) {
806
+ const { x: wx, y: wy } = screenToWorld(e);
807
+ const dx = wx - dragStart.current.x;
808
+ const dy = wy - dragStart.current.y;
809
+ const entityId = state.selectedEntities[0];
810
+ const t = world.getComponent(entityId, 'Transform');
811
+ if (t) {
812
+ let nx = dragEntityStart.current.x + dx;
813
+ let ny = dragEntityStart.current.y + dy;
814
+ if (state.snapToGrid) {
815
+ nx = Math.round(nx / state.gridSize) * state.gridSize;
816
+ ny = Math.round(ny / state.gridSize) * state.gridSize;
817
+ }
818
+ t.position.x = nx;
819
+ t.position.y = ny;
820
+ dispatch({ type: 'MARK_DIRTY' });
821
+ refreshWorld();
822
+ }
823
+ }
824
+ }, [state.selectedEntities, state.snapToGrid, state.gridSize, screenToWorld, world, dispatch, refreshWorld]);
825
+ const handlePointerUp = useCallback(() => {
826
+ if (dragging.current && state.selectedEntities.length > 0) {
827
+ const entityId = state.selectedEntities[0];
828
+ const t = world.getComponent(entityId, 'Transform');
829
+ if (t) {
830
+ const finalPos = { x: t.position.x, y: t.position.y };
831
+ const startPos = { ...dragEntityStart.current };
832
+ if (finalPos.x !== startPos.x || finalPos.y !== startPos.y) {
833
+ undoStack.push({
834
+ description: 'Move entity',
835
+ undo: () => { t.position.x = startPos.x; t.position.y = startPos.y; refreshWorld(); },
836
+ redo: () => { t.position.x = finalPos.x; t.position.y = finalPos.y; refreshWorld(); },
837
+ });
838
+ }
839
+ }
840
+ }
841
+ dragging.current = false;
842
+ panning.current = false;
843
+ }, [state.selectedEntities, world, undoStack, refreshWorld]);
844
+ const handleWheel = useCallback((e) => {
845
+ e.preventDefault();
846
+ const factor = e.deltaY > 0 ? 0.9 : 1.1;
847
+ dispatch({ type: 'SET_ZOOM', zoom: state.zoom * factor });
848
+ }, [state.zoom, dispatch]);
849
+ return (jsx("canvas", { ref: canvasRef, style: { width: '100%', height: '100%', display: 'block', cursor: getCursorForTool(state.tool) }, onPointerDown: handlePointerDown, onPointerMove: handlePointerMove, onPointerUp: handlePointerUp, onWheel: handleWheel, onContextMenu: e => e.preventDefault() }));
352
850
  };
353
851
 
354
852
  export { NiceGameEditor, UndoStack, deserializeProject, serializeProject, useEditor };