dino-ge 1.2.2 → 1.2.6
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.
- package/dist/Circle.d.ts +0 -2
- package/dist/Circle.js +2 -11
- package/dist/Circle.js.map +1 -1
- package/dist/Engine.d.ts +22 -5
- package/dist/Engine.js +74 -63
- package/dist/Engine.js.map +1 -1
- package/dist/GameObject.d.ts +4 -9
- package/dist/GameObject.js +7 -0
- package/dist/GameObject.js.map +1 -1
- package/dist/Line.d.ts +0 -2
- package/dist/Line.js +2 -9
- package/dist/Line.js.map +1 -1
- package/dist/LineComponent.d.ts +19 -0
- package/dist/LineComponent.js +25 -0
- package/dist/LineComponent.js.map +1 -0
- package/dist/PhysicsSystem.d.ts +14 -0
- package/dist/PhysicsSystem.js +27 -0
- package/dist/PhysicsSystem.js.map +1 -0
- package/dist/Rectangle.d.ts +0 -2
- package/dist/Rectangle.js +2 -7
- package/dist/Rectangle.js.map +1 -1
- package/dist/RenderComponent.d.ts +13 -0
- package/dist/RenderComponent.js +12 -0
- package/dist/RenderComponent.js.map +1 -0
- package/dist/RenderingSystem.d.ts +22 -0
- package/dist/RenderingSystem.js +66 -0
- package/dist/RenderingSystem.js.map +1 -0
- package/dist/ShapeComponent.d.ts +24 -0
- package/dist/ShapeComponent.js +35 -0
- package/dist/ShapeComponent.js.map +1 -0
- package/dist/Sprite.d.ts +20 -17
- package/dist/Sprite.js +37 -80
- package/dist/Sprite.js.map +1 -1
- package/dist/SpriteComponent.d.ts +35 -0
- package/dist/SpriteComponent.js +90 -0
- package/dist/SpriteComponent.js.map +1 -0
- package/dist/System.d.ts +20 -0
- package/dist/System.js +7 -0
- package/dist/System.js.map +1 -0
- package/dist/Text.d.ts +18 -10
- package/dist/Text.js +37 -26
- package/dist/Text.js.map +1 -1
- package/dist/TextComponent.d.ts +29 -0
- package/dist/TextComponent.js +38 -0
- package/dist/TextComponent.js.map +1 -0
- package/dist/Tilemap.d.ts +0 -5
- package/dist/Tilemap.js +2 -18
- package/dist/Tilemap.js.map +1 -1
- package/dist/TilemapComponent.d.ts +20 -0
- package/dist/TilemapComponent.js +33 -0
- package/dist/TilemapComponent.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/Circle.ts +3 -11
- package/src/Engine.ts +77 -58
- package/src/GameObject.ts +11 -8
- package/src/Line.ts +3 -9
- package/src/LineComponent.ts +34 -0
- package/src/PhysicsSystem.ts +29 -0
- package/src/Rectangle.ts +3 -12
- package/src/RenderComponent.ts +15 -0
- package/src/RenderingSystem.ts +84 -0
- package/src/ShapeComponent.ts +50 -0
- package/src/Sprite.ts +45 -102
- package/src/SpriteComponent.ts +124 -0
- package/src/System.ts +22 -0
- package/src/Text.ts +53 -43
- package/src/TextComponent.ts +75 -0
- package/src/Tilemap.ts +8 -29
- package/src/TilemapComponent.ts +55 -0
- package/src/index.ts +9 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import System from './System.js';
|
|
2
|
+
import type GameObject from './GameObject.js';
|
|
3
|
+
/**
|
|
4
|
+
* A system that processes entities with RenderComponents.
|
|
5
|
+
* Handles camera transforms, frustum culling, and drawing.
|
|
6
|
+
*/
|
|
7
|
+
export default class RenderingSystem extends System {
|
|
8
|
+
private ctx;
|
|
9
|
+
constructor(ctx: CanvasRenderingContext2D);
|
|
10
|
+
/**
|
|
11
|
+
* Updates the rendering context (used when the engine is re-initialized).
|
|
12
|
+
* @param ctx The new canvas rendering context.
|
|
13
|
+
*/
|
|
14
|
+
setContext(ctx: CanvasRenderingContext2D): void;
|
|
15
|
+
/**
|
|
16
|
+
* Main rendering loop.
|
|
17
|
+
* @param entities The set of entities to process.
|
|
18
|
+
* @param deltaTime Time passed since the last frame (optional).
|
|
19
|
+
* @param debug Whether to draw debug overlays (hitboxes/tags).
|
|
20
|
+
*/
|
|
21
|
+
update(entities: Set<GameObject>, deltaTime?: number, debug?: boolean): void;
|
|
22
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import System from './System.js';
|
|
2
|
+
import RenderComponent from './RenderComponent.js';
|
|
3
|
+
import Engine from './Engine.js';
|
|
4
|
+
/**
|
|
5
|
+
* A system that processes entities with RenderComponents.
|
|
6
|
+
* Handles camera transforms, frustum culling, and drawing.
|
|
7
|
+
*/
|
|
8
|
+
export default class RenderingSystem extends System {
|
|
9
|
+
constructor(ctx) {
|
|
10
|
+
super();
|
|
11
|
+
this.ctx = ctx;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Updates the rendering context (used when the engine is re-initialized).
|
|
15
|
+
* @param ctx The new canvas rendering context.
|
|
16
|
+
*/
|
|
17
|
+
setContext(ctx) {
|
|
18
|
+
this.ctx = ctx;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Main rendering loop.
|
|
22
|
+
* @param entities The set of entities to process.
|
|
23
|
+
* @param deltaTime Time passed since the last frame (optional).
|
|
24
|
+
* @param debug Whether to draw debug overlays (hitboxes/tags).
|
|
25
|
+
*/
|
|
26
|
+
update(entities, deltaTime, debug = false) {
|
|
27
|
+
const bounds = Engine.camera.getViewportBounds(this.ctx.canvas.width, this.ctx.canvas.height);
|
|
28
|
+
this.ctx.save();
|
|
29
|
+
// Apply camera transform
|
|
30
|
+
this.ctx.scale(Engine.camera.zoom, Engine.camera.zoom);
|
|
31
|
+
this.ctx.translate(-Engine.camera.position.x, -Engine.camera.position.y);
|
|
32
|
+
// Sort entities by zIndex for correct draw order
|
|
33
|
+
const sorted = Array.from(entities).sort((a, b) => (a.zIndex > b.zIndex ? 1 : -1));
|
|
34
|
+
sorted.forEach((object) => {
|
|
35
|
+
// Frustum Culling
|
|
36
|
+
if (object.position.x < bounds.x + bounds.width &&
|
|
37
|
+
object.position.x + object.width > bounds.x &&
|
|
38
|
+
object.position.y < bounds.y + bounds.height &&
|
|
39
|
+
object.position.y + object.height > bounds.y) {
|
|
40
|
+
// Find any RenderComponent on the entity
|
|
41
|
+
const renderable = object.getComponent(RenderComponent);
|
|
42
|
+
if (renderable && object.visible) {
|
|
43
|
+
renderable.draw(this.ctx);
|
|
44
|
+
}
|
|
45
|
+
// Backward compatibility: call draw() if it exists on the object itself
|
|
46
|
+
const legacyObj = object;
|
|
47
|
+
if (legacyObj.draw && !(object.getComponent(RenderComponent))) {
|
|
48
|
+
legacyObj.draw(this.ctx);
|
|
49
|
+
}
|
|
50
|
+
// Draw debug overlays in world space
|
|
51
|
+
if (debug) {
|
|
52
|
+
this.ctx.save();
|
|
53
|
+
this.ctx.strokeStyle = object === Engine.selectedObject ? '#00ff00' : 'red';
|
|
54
|
+
this.ctx.lineWidth = (object === Engine.selectedObject ? 2 : 1) / Engine.camera.zoom;
|
|
55
|
+
this.ctx.strokeRect(object.position.x, object.position.y, object.width, object.height);
|
|
56
|
+
this.ctx.font = `${12 / Engine.camera.zoom}px monospace`;
|
|
57
|
+
this.ctx.fillStyle = this.ctx.strokeStyle;
|
|
58
|
+
this.ctx.fillText(object.tag || 'obj', object.position.x, object.position.y - (5 / Engine.camera.zoom));
|
|
59
|
+
this.ctx.restore();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
this.ctx.restore();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=RenderingSystem.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RenderingSystem.js","sourceRoot":"","sources":["../src/RenderingSystem.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AAEjC,OAAO,eAAe,MAAM,sBAAsB,CAAC;AACnD,OAAO,MAAM,MAAM,aAAa,CAAC;AAEjC;;;GAGG;AACH,MAAM,CAAC,OAAO,OAAO,eAAgB,SAAQ,MAAM;IAGjD,YAAY,GAA6B;QACvC,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;IACjB,CA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CAAC,MAAM,CAAC,IAAI,cAAc,CAAC;oBACzD,IAAI,CAAC,GAAG,CAAC,SAAS,GAAG,IAAI,CAAC,GAAG,CAAC,WAAW,CAAC;oBAC1C,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,MAAM,CAAC,GAAG,IAAI,KAAK,EAAE,MAAM,CAAC,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;oBACxG,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,CAAC;gBACrB,CAAC;YACH,CAAC;QACH,CAAC,CAAC,CAAC;QAEH,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,CAAC;IACrB,CAAC;CACF"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import RenderComponent from './RenderComponent.js';
|
|
2
|
+
/**
|
|
3
|
+
* Types of shapes supported by the ShapeComponent.
|
|
4
|
+
*/
|
|
5
|
+
export type ShapeType = 'rect' | 'circle';
|
|
6
|
+
/**
|
|
7
|
+
* Component that holds data for rendering a basic shape.
|
|
8
|
+
*/
|
|
9
|
+
export default class ShapeComponent extends RenderComponent {
|
|
10
|
+
/** The type of shape to draw. */
|
|
11
|
+
type: ShapeType;
|
|
12
|
+
/** Fill colour. */
|
|
13
|
+
colour: string;
|
|
14
|
+
/** Width (for rect) or radius (for circle). */
|
|
15
|
+
width: number;
|
|
16
|
+
/** Height (for rect) or unused (for circle). */
|
|
17
|
+
height: number;
|
|
18
|
+
constructor(type: ShapeType, colour: string, width: number, height?: number);
|
|
19
|
+
/**
|
|
20
|
+
* Draws the shape to the canvas.
|
|
21
|
+
* @param ctx The canvas 2D rendering context.
|
|
22
|
+
*/
|
|
23
|
+
draw(ctx: CanvasRenderingContext2D): void;
|
|
24
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import RenderComponent from './RenderComponent.js';
|
|
2
|
+
/**
|
|
3
|
+
* Component that holds data for rendering a basic shape.
|
|
4
|
+
*/
|
|
5
|
+
export default class ShapeComponent extends RenderComponent {
|
|
6
|
+
constructor(type, colour, width, height = 0) {
|
|
7
|
+
super();
|
|
8
|
+
this.type = type;
|
|
9
|
+
this.colour = colour;
|
|
10
|
+
this.width = width;
|
|
11
|
+
this.height = height;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Draws the shape to the canvas.
|
|
15
|
+
* @param ctx The canvas 2D rendering context.
|
|
16
|
+
*/
|
|
17
|
+
draw(ctx) {
|
|
18
|
+
if (!this.gameObject)
|
|
19
|
+
return;
|
|
20
|
+
const { position } = this.gameObject;
|
|
21
|
+
ctx.fillStyle = this.colour;
|
|
22
|
+
if (this.type === 'rect') {
|
|
23
|
+
ctx.fillRect(position.x, position.y, this.width, this.height);
|
|
24
|
+
}
|
|
25
|
+
else if (this.type === 'circle') {
|
|
26
|
+
ctx.beginPath();
|
|
27
|
+
const centerX = position.x + this.width; // width is radius
|
|
28
|
+
const centerY = position.y + this.width;
|
|
29
|
+
ctx.arc(centerX, centerY, this.width, 0, Math.PI * 2);
|
|
30
|
+
ctx.fill();
|
|
31
|
+
ctx.closePath();
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=ShapeComponent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ShapeComponent.js","sourceRoot":"","sources":["../src/ShapeComponent.ts"],"names":[],"mappings":"AAAA,OAAO,eAAe,MAAM,sBAAsB,CAAC;AAOnD;;GAEG;AACH,MAAM,CAAC,OAAO,OAAO,cAAe,SAAQ,eAAe;IAUzD,YAAY,IAAe,EAAE,MAAc,EAAE,KAAa,EAAE,SAAiB,CAAC;QAC5E,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAED;;;OAGG;IACH,IAAI,CAAC,GAA6B;QAChC,IAAI,CAAC,IAAI,CAAC,UAAU;YAAE,OAAO;QAE7B,MAAM,EAAE,QAAQ,EAAE,GAAG,IAAI,CAAC,UAAU,CAAC;QACrC,GAAG,CAAC,SAAS,GAAG,IAAI,CAAC,MAAM,CAAC;QAE5B,IAAI,IAAI,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;YACzB,GAAG,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC,EAAE,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;QAChE,CAAC;aAAM,IAAI,IAAI,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;YAClC,GAAG,CAAC,SAAS,EAAE,CAAC;YAChB,MAAM,OAAO,GAAG,QAAQ,CAAC,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,kBAAkB;YAC3D,MAAM,OAAO,GAAG,QAAQ,CAAC,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC;YACxC,GAAG,CAAC,GAAG,CAAC,OAAO,EAAE,OAAO,EAAE,IAAI,CAAC,KAAK,EAAE,CAAC,EAAE,IAAI,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC;YACtD,GAAG,CAAC,IAAI,EAAE,CAAC;YACX,GAAG,CAAC,SAAS,EAAE,CAAC;QAClB,CAAC;IACH,CAAC;CACF"}
|
package/dist/Sprite.d.ts
CHANGED
|
@@ -25,29 +25,38 @@ export interface SpriteProps {
|
|
|
25
25
|
* Represents an animated sprite using a spritesheet.
|
|
26
26
|
*/
|
|
27
27
|
export default class Sprite extends GameObject {
|
|
28
|
-
|
|
28
|
+
/** Internal sprite component for rendering and animation. */
|
|
29
|
+
private _spriteComponent;
|
|
29
30
|
/** The source image for the sprite. */
|
|
30
|
-
img: HTMLImageElement;
|
|
31
|
+
get img(): HTMLImageElement;
|
|
32
|
+
set img(val: HTMLImageElement);
|
|
31
33
|
/** Number of rows in the spritesheet. */
|
|
32
|
-
rows: number;
|
|
34
|
+
get rows(): number;
|
|
35
|
+
set rows(val: number);
|
|
33
36
|
/** Number of columns in the spritesheet. */
|
|
34
|
-
cols: number;
|
|
37
|
+
get cols(): number;
|
|
38
|
+
set cols(val: number);
|
|
35
39
|
/** The starting column for the current animation loop. */
|
|
36
|
-
startCol: number;
|
|
40
|
+
get startCol(): number;
|
|
41
|
+
set startCol(val: number);
|
|
37
42
|
/** The ending column for the current animation loop. */
|
|
38
|
-
endCol: number;
|
|
43
|
+
get endCol(): number;
|
|
44
|
+
set endCol(val: number);
|
|
39
45
|
/** The pixel width of a single animation frame (calculated automatically). */
|
|
40
|
-
frameWidth: number;
|
|
46
|
+
get frameWidth(): number;
|
|
41
47
|
/** The pixel height of a single animation frame (calculated automatically). */
|
|
42
|
-
frameHeight: number;
|
|
48
|
+
get frameHeight(): number;
|
|
43
49
|
/** Whether the sprite is currently registered with the engine. */
|
|
44
50
|
registered: boolean;
|
|
45
51
|
/** The current frame index being displayed. */
|
|
46
|
-
currentFrame: number;
|
|
52
|
+
get currentFrame(): number;
|
|
53
|
+
set currentFrame(val: number);
|
|
47
54
|
/** Whether the sprite is horizontally flipped. */
|
|
48
|
-
flip: boolean;
|
|
55
|
+
get flip(): boolean;
|
|
56
|
+
set flip(val: boolean);
|
|
49
57
|
/** Duration of each animation frame in milliseconds. */
|
|
50
|
-
frameDuration: number;
|
|
58
|
+
get frameDuration(): number;
|
|
59
|
+
set frameDuration(val: number);
|
|
51
60
|
/**
|
|
52
61
|
* Gets the display width of the sprite (scaled by 3).
|
|
53
62
|
*/
|
|
@@ -57,12 +66,6 @@ export default class Sprite extends GameObject {
|
|
|
57
66
|
*/
|
|
58
67
|
get height(): number;
|
|
59
68
|
constructor(props: SpriteProps);
|
|
60
|
-
/**
|
|
61
|
-
* Main rendering method for the sprite.
|
|
62
|
-
* Handles frame calculation and horizontal flipping.
|
|
63
|
-
* @param ctx The canvas 2D rendering context.
|
|
64
|
-
*/
|
|
65
|
-
draw(ctx: CanvasRenderingContext2D): void;
|
|
66
69
|
/**
|
|
67
70
|
* Registers the sprite with the engine and starts its animation loop.
|
|
68
71
|
*/
|
package/dist/Sprite.js
CHANGED
|
@@ -1,22 +1,39 @@
|
|
|
1
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
2
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
3
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
4
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
|
-
};
|
|
6
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
7
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
8
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
9
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
10
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
11
|
-
};
|
|
12
|
-
var _Sprite_lastFrameUpdate;
|
|
13
1
|
import Engine from './Engine.js';
|
|
14
2
|
import GameObject from './GameObject.js';
|
|
15
3
|
import ResourceLoader from './Loader.js';
|
|
4
|
+
import SpriteComponent from './SpriteComponent.js';
|
|
16
5
|
/**
|
|
17
6
|
* Represents an animated sprite using a spritesheet.
|
|
18
7
|
*/
|
|
19
|
-
class Sprite extends GameObject {
|
|
8
|
+
export default class Sprite extends GameObject {
|
|
9
|
+
/** The source image for the sprite. */
|
|
10
|
+
get img() { return this._spriteComponent.img; }
|
|
11
|
+
set img(val) { this._spriteComponent.img = val; }
|
|
12
|
+
/** Number of rows in the spritesheet. */
|
|
13
|
+
get rows() { return this._spriteComponent.rows; }
|
|
14
|
+
set rows(val) { this._spriteComponent.rows = val; }
|
|
15
|
+
/** Number of columns in the spritesheet. */
|
|
16
|
+
get cols() { return this._spriteComponent.cols; }
|
|
17
|
+
set cols(val) { this._spriteComponent.cols = val; }
|
|
18
|
+
/** The starting column for the current animation loop. */
|
|
19
|
+
get startCol() { return this._spriteComponent.startCol; }
|
|
20
|
+
set startCol(val) { this._spriteComponent.startCol = val; }
|
|
21
|
+
/** The ending column for the current animation loop. */
|
|
22
|
+
get endCol() { return this._spriteComponent.endCol; }
|
|
23
|
+
set endCol(val) { this._spriteComponent.endCol = val; }
|
|
24
|
+
/** The pixel width of a single animation frame (calculated automatically). */
|
|
25
|
+
get frameWidth() { return this._spriteComponent.frameWidth; }
|
|
26
|
+
/** The pixel height of a single animation frame (calculated automatically). */
|
|
27
|
+
get frameHeight() { return this._spriteComponent.frameHeight; }
|
|
28
|
+
/** The current frame index being displayed. */
|
|
29
|
+
get currentFrame() { return this._spriteComponent.currentFrame; }
|
|
30
|
+
set currentFrame(val) { this._spriteComponent.currentFrame = val; }
|
|
31
|
+
/** Whether the sprite is horizontally flipped. */
|
|
32
|
+
get flip() { return this._spriteComponent.flip; }
|
|
33
|
+
set flip(val) { this._spriteComponent.flip = val; }
|
|
34
|
+
/** Duration of each animation frame in milliseconds. */
|
|
35
|
+
get frameDuration() { return this._spriteComponent.frameDuration; }
|
|
36
|
+
set frameDuration(val) { this._spriteComponent.frameDuration = val; }
|
|
20
37
|
/**
|
|
21
38
|
* Gets the display width of the sprite (scaled by 3).
|
|
22
39
|
*/
|
|
@@ -34,84 +51,25 @@ class Sprite extends GameObject {
|
|
|
34
51
|
throw new Error('You must provide a tag for a Sprite');
|
|
35
52
|
}
|
|
36
53
|
super(props.tag, props.zIndex || 1);
|
|
37
|
-
/** The pixel width of a single animation frame (calculated automatically). */
|
|
38
|
-
this.frameWidth = 0;
|
|
39
|
-
/** The pixel height of a single animation frame (calculated automatically). */
|
|
40
|
-
this.frameHeight = 0;
|
|
41
54
|
/** Whether the sprite is currently registered with the engine. */
|
|
42
55
|
this.registered = false;
|
|
43
|
-
|
|
44
|
-
this.currentFrame = 0;
|
|
45
|
-
/** Whether the sprite is horizontally flipped. */
|
|
46
|
-
this.flip = false;
|
|
47
|
-
/** Duration of each animation frame in milliseconds. */
|
|
48
|
-
this.frameDuration = 100;
|
|
49
|
-
_Sprite_lastFrameUpdate.set(this, Date.now());
|
|
56
|
+
let img;
|
|
50
57
|
if (typeof props.img === 'string') {
|
|
51
|
-
|
|
58
|
+
img = ResourceLoader.getImage(props.img);
|
|
52
59
|
}
|
|
53
60
|
else {
|
|
54
|
-
|
|
61
|
+
img = props.img;
|
|
55
62
|
}
|
|
56
|
-
this.
|
|
57
|
-
this.
|
|
63
|
+
this._spriteComponent = new SpriteComponent(img, props.rows, props.cols, props.startCol, props.endCol);
|
|
64
|
+
this.addComponent(this._spriteComponent);
|
|
58
65
|
this.position = props.position;
|
|
59
|
-
this.startCol = props.startCol;
|
|
60
|
-
this.endCol = props.endCol;
|
|
61
|
-
this.currentFrame = props.startCol || 0;
|
|
62
|
-
const setDimensions = () => {
|
|
63
|
-
this.frameWidth = this.img.width / this.cols;
|
|
64
|
-
this.frameHeight = this.img.height / this.rows;
|
|
65
|
-
};
|
|
66
|
-
if (this.img.complete) {
|
|
67
|
-
setDimensions();
|
|
68
|
-
}
|
|
69
|
-
else {
|
|
70
|
-
this.img.addEventListener('load', setDimensions, { once: true });
|
|
71
|
-
}
|
|
72
66
|
this.registered = false;
|
|
73
67
|
}
|
|
74
|
-
/**
|
|
75
|
-
* Main rendering method for the sprite.
|
|
76
|
-
* Handles frame calculation and horizontal flipping.
|
|
77
|
-
* @param ctx The canvas 2D rendering context.
|
|
78
|
-
*/
|
|
79
|
-
draw(ctx) {
|
|
80
|
-
if (!this.visible || !this.frameWidth || !this.frameHeight)
|
|
81
|
-
return;
|
|
82
|
-
const now = Date.now();
|
|
83
|
-
if (this.registered && now - __classPrivateFieldGet(this, _Sprite_lastFrameUpdate, "f") > this.frameDuration) {
|
|
84
|
-
this.currentFrame += 1;
|
|
85
|
-
__classPrivateFieldSet(this, _Sprite_lastFrameUpdate, now, "f");
|
|
86
|
-
}
|
|
87
|
-
const { img, cols, frameWidth, frameHeight, position, startCol, endCol, } = this;
|
|
88
|
-
ctx.imageSmoothingEnabled = true;
|
|
89
|
-
ctx.imageSmoothingQuality = 'high';
|
|
90
|
-
const maxFrame = endCol - 1;
|
|
91
|
-
if (this.currentFrame < startCol) {
|
|
92
|
-
this.currentFrame = startCol;
|
|
93
|
-
}
|
|
94
|
-
if (this.currentFrame > maxFrame) {
|
|
95
|
-
this.currentFrame = startCol;
|
|
96
|
-
}
|
|
97
|
-
// Update rows and columns
|
|
98
|
-
const column = this.currentFrame % cols;
|
|
99
|
-
const row = Math.floor(this.currentFrame / cols);
|
|
100
|
-
ctx.save();
|
|
101
|
-
if (this.flip) {
|
|
102
|
-
ctx.translate(position.x + (frameWidth * 3), position.y);
|
|
103
|
-
ctx.scale(-1, 1);
|
|
104
|
-
ctx.drawImage(img, column * frameWidth, row * frameHeight, frameWidth, frameHeight, 0, 0, frameWidth * 3, frameHeight * 3);
|
|
105
|
-
}
|
|
106
|
-
else {
|
|
107
|
-
ctx.drawImage(img, column * frameWidth, row * frameHeight, frameWidth, frameHeight, position.x, position.y, frameWidth * 3, frameHeight * 3);
|
|
108
|
-
}
|
|
109
|
-
ctx.restore();
|
|
110
|
-
}
|
|
111
68
|
/**
|
|
112
69
|
* Registers the sprite with the engine and starts its animation loop.
|
|
113
70
|
*/
|
|
114
71
|
play() {
|
|
72
|
+
this._spriteComponent.playing = true;
|
|
115
73
|
if (!this.registered) {
|
|
116
74
|
Engine.registerObject(this);
|
|
117
75
|
this.registered = true;
|
|
@@ -121,9 +79,8 @@ class Sprite extends GameObject {
|
|
|
121
79
|
* Stops the sprite's animation and removes it from the engine.
|
|
122
80
|
*/
|
|
123
81
|
stop() {
|
|
82
|
+
this._spriteComponent.playing = false;
|
|
124
83
|
Engine.destroyObject(this);
|
|
125
84
|
}
|
|
126
85
|
}
|
|
127
|
-
_Sprite_lastFrameUpdate = new WeakMap();
|
|
128
|
-
export default Sprite;
|
|
129
86
|
//# sourceMappingURL=Sprite.js.map
|
package/dist/Sprite.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Sprite.js","sourceRoot":"","sources":["../src/Sprite.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Sprite.js","sourceRoot":"","sources":["../src/Sprite.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,UAAU,MAAM,iBAAiB,CAAC;AAEzC,OAAO,cAAc,MAAM,aAAa,CAAC;AACzC,OAAO,eAAe,MAAM,sBAAsB,CAAC;AAwBnD;;GAEG;AACH,MAAM,CAAC,OAAO,OAAO,MAAO,SAAQ,UAAU;IAI5C,uCAAuC;IACvC,IAAI,GAAG,KAAuB,OAAO,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,CAAC,CAAC;IACjE,IAAI,GAAG,CAAC,GAAqB,IAAI,IAAI,CAAC,gBAAgB,CAAC,GAAG,GAAG,GAAG,CAAC,CAAC,CAAC;IAEnE,yCAAyC;IACzC,IAAI,IAAI,KAAa,OAAO,IAAI,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAC,CAAC;IACzD,IAAI,IAAI,CAAC,GAAW,IAAI,IAAI,CAAC,gBAAgB,CAAC,IAAI,GAAG,GAAG,CAAC,CAAC,CAAC;IAE3D,4CAA4C;IAC5C,IAAI,IAAI,KAAa,OAAO,IAAI,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAC,CAAC;IACzD,IAAI,IAAI,CAAC,GAAW,IAAI,IAAI,CAAC,gBAAgB,CAAC,IAAI,GAAG,GAAG,CAAC,CAAC,CAA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,IAAI,CAAC,gBAAgB,CAAC,OAAO,GAAG,KAAK,CAAC;QACtC,MAAM,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC;IAC7B,CAAC;CACF"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import RenderComponent from './RenderComponent.js';
|
|
2
|
+
/**
|
|
3
|
+
* Component that holds data for rendering an animated sprite.
|
|
4
|
+
*/
|
|
5
|
+
export default class SpriteComponent extends RenderComponent {
|
|
6
|
+
#private;
|
|
7
|
+
/** The source image for the sprite. */
|
|
8
|
+
img: HTMLImageElement;
|
|
9
|
+
/** Number of rows in the spritesheet. */
|
|
10
|
+
rows: number;
|
|
11
|
+
/** Number of columns in the spritesheet. */
|
|
12
|
+
cols: number;
|
|
13
|
+
/** The starting column for the current animation loop. */
|
|
14
|
+
startCol: number;
|
|
15
|
+
/** The ending column for the current animation loop. */
|
|
16
|
+
endCol: number;
|
|
17
|
+
/** The pixel width of a single animation frame. */
|
|
18
|
+
frameWidth: number;
|
|
19
|
+
/** The pixel height of a single animation frame. */
|
|
20
|
+
frameHeight: number;
|
|
21
|
+
/** The current frame index being displayed. */
|
|
22
|
+
currentFrame: number;
|
|
23
|
+
/** Whether the sprite is horizontally flipped. */
|
|
24
|
+
flip: boolean;
|
|
25
|
+
/** Duration of each animation frame in milliseconds. */
|
|
26
|
+
frameDuration: number;
|
|
27
|
+
/** Whether the sprite is currently playing its animation. */
|
|
28
|
+
playing: boolean;
|
|
29
|
+
constructor(img: HTMLImageElement, rows: number, cols: number, startCol: number, endCol: number);
|
|
30
|
+
/**
|
|
31
|
+
* Draws the current frame of the sprite.
|
|
32
|
+
* @param ctx The canvas 2D rendering context.
|
|
33
|
+
*/
|
|
34
|
+
draw(ctx: CanvasRenderingContext2D): void;
|
|
35
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
2
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
3
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
4
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
|
+
};
|
|
6
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
7
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
10
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
11
|
+
};
|
|
12
|
+
var _SpriteComponent_lastFrameUpdate;
|
|
13
|
+
import RenderComponent from './RenderComponent.js';
|
|
14
|
+
/**
|
|
15
|
+
* Component that holds data for rendering an animated sprite.
|
|
16
|
+
*/
|
|
17
|
+
class SpriteComponent extends RenderComponent {
|
|
18
|
+
constructor(img, rows, cols, startCol, endCol) {
|
|
19
|
+
super();
|
|
20
|
+
/** The pixel width of a single animation frame. */
|
|
21
|
+
this.frameWidth = 0;
|
|
22
|
+
/** The pixel height of a single animation frame. */
|
|
23
|
+
this.frameHeight = 0;
|
|
24
|
+
/** The current frame index being displayed. */
|
|
25
|
+
this.currentFrame = 0;
|
|
26
|
+
/** Whether the sprite is horizontally flipped. */
|
|
27
|
+
this.flip = false;
|
|
28
|
+
/** Duration of each animation frame in milliseconds. */
|
|
29
|
+
this.frameDuration = 100;
|
|
30
|
+
/** Whether the sprite is currently playing its animation. */
|
|
31
|
+
this.playing = false;
|
|
32
|
+
_SpriteComponent_lastFrameUpdate.set(this, Date.now());
|
|
33
|
+
this.img = img;
|
|
34
|
+
this.rows = rows;
|
|
35
|
+
this.cols = cols;
|
|
36
|
+
this.startCol = startCol;
|
|
37
|
+
this.endCol = endCol;
|
|
38
|
+
this.currentFrame = startCol;
|
|
39
|
+
const setDimensions = () => {
|
|
40
|
+
this.frameWidth = this.img.width / this.cols;
|
|
41
|
+
this.frameHeight = this.img.height / this.rows;
|
|
42
|
+
};
|
|
43
|
+
if (this.img.complete) {
|
|
44
|
+
setDimensions();
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
this.img.addEventListener('load', setDimensions, { once: true });
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Draws the current frame of the sprite.
|
|
52
|
+
* @param ctx The canvas 2D rendering context.
|
|
53
|
+
*/
|
|
54
|
+
draw(ctx) {
|
|
55
|
+
if (!this.frameWidth || !this.frameHeight || !this.gameObject)
|
|
56
|
+
return;
|
|
57
|
+
const now = Date.now();
|
|
58
|
+
if (this.playing && now - __classPrivateFieldGet(this, _SpriteComponent_lastFrameUpdate, "f") > this.frameDuration) {
|
|
59
|
+
this.currentFrame += 1;
|
|
60
|
+
__classPrivateFieldSet(this, _SpriteComponent_lastFrameUpdate, now, "f");
|
|
61
|
+
}
|
|
62
|
+
const { img, cols, frameWidth, frameHeight, startCol, endCol, } = this;
|
|
63
|
+
const { position } = this.gameObject;
|
|
64
|
+
ctx.imageSmoothingEnabled = true;
|
|
65
|
+
ctx.imageSmoothingQuality = 'high';
|
|
66
|
+
const maxFrame = endCol - 1;
|
|
67
|
+
if (this.currentFrame < startCol) {
|
|
68
|
+
this.currentFrame = startCol;
|
|
69
|
+
}
|
|
70
|
+
if (this.currentFrame > maxFrame) {
|
|
71
|
+
this.currentFrame = startCol;
|
|
72
|
+
}
|
|
73
|
+
// Update rows and columns
|
|
74
|
+
const column = this.currentFrame % cols;
|
|
75
|
+
const row = Math.floor(this.currentFrame / cols);
|
|
76
|
+
ctx.save();
|
|
77
|
+
if (this.flip) {
|
|
78
|
+
ctx.translate(position.x + (frameWidth * 3), position.y);
|
|
79
|
+
ctx.scale(-1, 1);
|
|
80
|
+
ctx.drawImage(img, column * frameWidth, row * frameHeight, frameWidth, frameHeight, 0, 0, frameWidth * 3, frameHeight * 3);
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
ctx.drawImage(img, column * frameWidth, row * frameHeight, frameWidth, frameHeight, position.x, position.y, frameWidth * 3, frameHeight * 3);
|
|
84
|
+
}
|
|
85
|
+
ctx.restore();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
_SpriteComponent_lastFrameUpdate = new WeakMap();
|
|
89
|
+
export default SpriteComponent;
|
|
90
|
+
//# sourceMappingURL=SpriteComponent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SpriteComponent.js","sourceRoot":"","sources":["../src/SpriteComponent.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,eAAe,MAAM,sBAAsB,CAAC;AAEnD;;GAEG;AACH,MAAqB,eAAgB,SAAQ,eAAe;IA0B1D,YAAY,GAAqB,EAAE,IAAY,EAAE,IAAY,EAAE,QAAgB,EAAE,MAAc;QAC7F,KAAK,EAAE,CAAC;QAhBV,mDAAmD;QACnD,eAAU,GAAW,CAAC,CAAC;QACvB,oDAAoD;QACpD,gBAAW,GAAW,CAAC,CAAC;QACxB,+CAA+C;QAC/C,iBAAY,GAAW,CAAC,CAAC;QACzB,kDAAkD;QAClD,SAAI,GAAY,KAAK,CAAC;QACtB,wDAAwD;QACxD,kBAAa,GAAW,GAAG,CAAC;QAC5B,6DAA6D;QAC7D,YAAO,GAAY,KAAK,CAAC;QAEzB,2CAA2B,IAAI,CAAC,GAAG,EAAE,EAAC;QAIpC,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC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|
+
* Base class for all systems in the Entity Component System.
|
|
4
|
+
* Systems contain the logic that processes entities with specific components.
|
|
5
|
+
*/
|
|
6
|
+
export default abstract class System {
|
|
7
|
+
/**
|
|
8
|
+
* Called every frame during the main update loop.
|
|
9
|
+
* @param entities The set of entities to process.
|
|
10
|
+
* @param deltaTime Time passed since the last frame in seconds.
|
|
11
|
+
* @param debug Whether to draw debug information.
|
|
12
|
+
*/
|
|
13
|
+
update?(entities: Set<GameObject>, deltaTime?: number, debug?: boolean): void;
|
|
14
|
+
/**
|
|
15
|
+
* Called at a fixed interval for physics and consistent logic.
|
|
16
|
+
* @param entities The set of entities to process.
|
|
17
|
+
* @param fixedDelta The fixed time step in seconds.
|
|
18
|
+
*/
|
|
19
|
+
fixedUpdate?(entities: Set<GameObject>, fixedDelta: number): void;
|
|
20
|
+
}
|
package/dist/System.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"System.js","sourceRoot":"","sources":["../src/System.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,MAAM,CAAC,OAAO,OAAgB,MAAM;CAenC"}
|
package/dist/Text.d.ts
CHANGED
|
@@ -42,26 +42,36 @@ export interface TextProperties {
|
|
|
42
42
|
*/
|
|
43
43
|
export default class Text extends GameObject {
|
|
44
44
|
#private;
|
|
45
|
+
/** Internal text component. */
|
|
46
|
+
private _textComponent;
|
|
45
47
|
/** Text fill colour. */
|
|
46
|
-
colour: string;
|
|
48
|
+
get colour(): string;
|
|
49
|
+
set colour(val: string);
|
|
47
50
|
/** Background box colour. */
|
|
48
|
-
backgroundColour: string;
|
|
51
|
+
get backgroundColour(): string;
|
|
52
|
+
set backgroundColour(val: string);
|
|
49
53
|
/** Font size in pixels. */
|
|
50
54
|
fontSize: number;
|
|
51
55
|
/** Compiled font string (e.g., '25px Helvetica'). */
|
|
52
|
-
font: string;
|
|
56
|
+
get font(): string;
|
|
57
|
+
set font(val: string);
|
|
53
58
|
/** The text content. */
|
|
54
|
-
text: string;
|
|
59
|
+
get text(): string;
|
|
60
|
+
set text(val: string);
|
|
55
61
|
/** Number of characters in the text. */
|
|
56
62
|
length: number;
|
|
57
63
|
/** Horizontal alignment. */
|
|
58
|
-
horizontalAlign: HorizontalAlign;
|
|
64
|
+
get horizontalAlign(): HorizontalAlign;
|
|
65
|
+
set horizontalAlign(val: HorizontalAlign);
|
|
59
66
|
/** Vertical alignment. */
|
|
60
|
-
verticalAlign: VerticalAlign;
|
|
67
|
+
get verticalAlign(): VerticalAlign;
|
|
68
|
+
set verticalAlign(val: VerticalAlign);
|
|
61
69
|
/** Width of the background box or interaction area. */
|
|
62
|
-
width: number;
|
|
70
|
+
get width(): number;
|
|
71
|
+
set width(val: number);
|
|
63
72
|
/** Height of the background box or interaction area. */
|
|
64
|
-
height: number;
|
|
73
|
+
get height(): number;
|
|
74
|
+
set height(val: number);
|
|
65
75
|
/** Whether the object should be registered. */
|
|
66
76
|
register: boolean;
|
|
67
77
|
/** Whether the object is currently registered. */
|
|
@@ -69,8 +79,6 @@ export default class Text extends GameObject {
|
|
|
69
79
|
/** Callback for click events. */
|
|
70
80
|
onClick: () => void;
|
|
71
81
|
constructor(props: TextProperties);
|
|
72
|
-
/** Draws the text and its optional background onto the context. */
|
|
73
|
-
draw(ctx: CanvasRenderingContext2D): void;
|
|
74
82
|
/** Registers the text object with the engine for rendering and interaction. */
|
|
75
83
|
registerSelf(): void;
|
|
76
84
|
/** Removes the text object from the engine. */
|