|
1 | | -import { Sound } from "../engine/bundle"; |
2 | 1 | import { getDebugInfo } from "../helpers"; |
3 | 2 | import { Part } from "./Part"; |
4 | 3 | import { Scene } from "./Scene"; |
@@ -62,6 +61,47 @@ export class Game extends Part { |
62 | 61 | } |
63 | 62 | } |
64 | 63 |
|
| 64 | + clone(memo = new Map()): this { |
| 65 | + if (memo.has(this)) { |
| 66 | + return memo.get(this); |
| 67 | + } |
| 68 | + |
| 69 | + // Game constructor requires canvas, width, height, name |
| 70 | + // We cannot clone the canvas directly, so we'll create a placeholder |
| 71 | + // The user will need to re-assign a real canvas after cloning |
| 72 | + const clonedGame = new Game({ |
| 73 | + name: this.name, |
| 74 | + canvas: document.createElement('canvas'), // Placeholder canvas |
| 75 | + devmode: this.devmode, |
| 76 | + width: this.width, |
| 77 | + height: this.height, |
| 78 | + disableAntiAliasing: !this.context.imageSmoothingEnabled, // Infer from original context |
| 79 | + showtoolTips: this.showtoolTips |
| 80 | + }); |
| 81 | + |
| 82 | + memo.set(this, clonedGame); |
| 83 | + |
| 84 | + this._cloneProperties(clonedGame, memo); |
| 85 | + |
| 86 | + // Reset properties that are tied to the DOM or internal state |
| 87 | + clonedGame.canvas = undefined as any; // User must provide a real canvas |
| 88 | + clonedGame.context = undefined as any; // Context will be derived from new canvas |
| 89 | + clonedGame.currentScene = undefined; // Will be set by start() or setScene() |
| 90 | + // clonedGame.childrenArray is handled by _cloneProperties |
| 91 | + clonedGame.hovering = undefined; // Reset hovering part |
| 92 | + clonedGame.tooltipLocked = undefined; // Reset tooltip lock |
| 93 | + clonedGame.lastMousePosition = { x: 0, y: 0 }; // Reset mouse position |
| 94 | + clonedGame.scaleFactor = 1; // Reset scale factor |
| 95 | + clonedGame.canvasOffset = { x: 0, y: 0 }; // Reset canvas offset |
| 96 | + clonedGame.messageHook = undefined; // Clear message hook |
| 97 | + clonedGame._isRunning = false; // Reset running state |
| 98 | + clonedGame._isPaused = false; // Reset paused state |
| 99 | + clonedGame._animationFrameId = undefined; // Clear animation frame ID |
| 100 | + clonedGame._lastUpdateTime = 0; // Reset last update time |
| 101 | + |
| 102 | + return clonedGame as this; |
| 103 | + } |
| 104 | + |
65 | 105 | changeCanvasSize(width: number, height: number) { |
66 | 106 | this.canvas.width = width; |
67 | 107 | this.canvas.height = height; |
|
0 commit comments