@dryanovski/gamefoo 0.0.1 → 0.2.1
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/core/animate.d.ts +129 -0
- package/dist/core/animate.d.ts.map +1 -0
- package/dist/core/asset.d.ts +59 -0
- package/dist/core/asset.d.ts.map +1 -0
- package/dist/core/behaviour.d.ts +118 -0
- package/dist/core/behaviour.d.ts.map +1 -1
- package/dist/core/behaviours/collidable.d.ts +203 -4
- package/dist/core/behaviours/collidable.d.ts.map +1 -1
- package/dist/core/behaviours/control.d.ts +51 -3
- package/dist/core/behaviours/control.d.ts.map +1 -1
- package/dist/core/behaviours/healtkit.d.ts +120 -3
- package/dist/core/behaviours/healtkit.d.ts.map +1 -1
- package/dist/core/behaviours/sprite_render.d.ts +141 -0
- package/dist/core/behaviours/sprite_render.d.ts.map +1 -0
- package/dist/core/camera.d.ts +101 -0
- package/dist/core/camera.d.ts.map +1 -1
- package/dist/core/engine.d.ts +377 -15
- package/dist/core/engine.d.ts.map +1 -1
- package/dist/core/fonts/font_bitmap.d.ts +156 -0
- package/dist/core/fonts/font_bitmap.d.ts.map +1 -0
- package/dist/core/fonts/font_bitmap_prebuild.d.ts +102 -0
- package/dist/core/fonts/font_bitmap_prebuild.d.ts.map +1 -0
- package/dist/core/fonts/internal/font_3x5.d.ts +76 -0
- package/dist/core/fonts/internal/font_3x5.d.ts.map +1 -0
- package/dist/core/fonts/internal/font_4x6.d.ts +76 -0
- package/dist/core/fonts/internal/font_4x6.d.ts.map +1 -0
- package/dist/core/fonts/internal/font_5x5.d.ts +79 -0
- package/dist/core/fonts/internal/font_5x5.d.ts.map +1 -0
- package/dist/core/fonts/internal/font_6x8.d.ts +76 -0
- package/dist/core/fonts/internal/font_6x8.d.ts.map +1 -0
- package/dist/core/fonts/internal/font_8x13.d.ts +76 -0
- package/dist/core/fonts/internal/font_8x13.d.ts.map +1 -0
- package/dist/core/fonts/internal/font_8x8.d.ts +76 -0
- package/dist/core/fonts/internal/font_8x8.d.ts.map +1 -0
- package/dist/core/game_object_register.d.ts +101 -1
- package/dist/core/game_object_register.d.ts.map +1 -1
- package/dist/core/input.d.ts +131 -0
- package/dist/core/input.d.ts.map +1 -1
- package/dist/core/sprite.d.ts +232 -0
- package/dist/core/sprite.d.ts.map +1 -0
- package/dist/core/utils/perlin_noise.d.ts +136 -0
- package/dist/core/utils/perlin_noise.d.ts.map +1 -0
- package/dist/core/world.d.ts +147 -0
- package/dist/core/world.d.ts.map +1 -1
- package/dist/debug/monitor.d.ts +12 -0
- package/dist/debug/monitor.d.ts.map +1 -0
- package/dist/decorators/index.d.ts +2 -0
- package/dist/decorators/index.d.ts.map +1 -0
- package/dist/decorators/log.d.ts +33 -0
- package/dist/decorators/log.d.ts.map +1 -0
- package/dist/entities/dynamic_entity.d.ts +82 -0
- package/dist/entities/dynamic_entity.d.ts.map +1 -1
- package/dist/entities/entity.d.ts +216 -11
- package/dist/entities/entity.d.ts.map +1 -1
- package/dist/entities/player.d.ts +76 -0
- package/dist/entities/player.d.ts.map +1 -1
- package/dist/entities/text.d.ts +52 -0
- package/dist/entities/text.d.ts.map +1 -0
- package/dist/index.d.ts +29 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +23 -604
- package/dist/index.js.map +3 -15
- package/dist/subsystems/camera_system.d.ts +25 -0
- package/dist/subsystems/camera_system.d.ts.map +1 -0
- package/dist/subsystems/collision_system.d.ts +16 -0
- package/dist/subsystems/collision_system.d.ts.map +1 -0
- package/dist/subsystems/monitor_system.d.ts +17 -0
- package/dist/subsystems/monitor_system.d.ts.map +1 -0
- package/dist/subsystems/object_system.d.ts +18 -0
- package/dist/subsystems/object_system.d.ts.map +1 -0
- package/dist/subsystems/types.d.ts +40 -0
- package/dist/subsystems/types.d.ts.map +1 -0
- package/dist/types.d.ts +140 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +25 -11
|
@@ -1,17 +1,134 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import type Entity from "../../entities/entity";
|
|
2
2
|
import { Behaviour } from "../behaviour";
|
|
3
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Health-tracking behaviour for a {@link Entity}.
|
|
5
|
+
*
|
|
6
|
+
* `HealthKit` manages a current and maximum HP value, provides damage
|
|
7
|
+
* and healing methods, and exposes queries for health percentage and
|
|
8
|
+
* death state.
|
|
9
|
+
*
|
|
10
|
+
* @category Behaviours
|
|
11
|
+
* @since 0.1.0
|
|
12
|
+
*
|
|
13
|
+
* @example Attaching to a player
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { HealthKit, Player } from "gamefoo";
|
|
16
|
+
*
|
|
17
|
+
* const player = new Player("hero", 400, 300, 50, 50);
|
|
18
|
+
* player.attachBehaviour(new HealthKit(player, 100));
|
|
19
|
+
*
|
|
20
|
+
* player.healthkit?.takeDamage(25);
|
|
21
|
+
* console.log(player.healthkit?.getHealth()); // 75
|
|
22
|
+
* console.log(player.healthkit?.getHealthPercent()); // 0.75
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @example Custom max HP
|
|
26
|
+
* ```ts
|
|
27
|
+
* const hk = new HealthKit(entity, 50, 200);
|
|
28
|
+
* // starts at 50 HP, max is 200
|
|
29
|
+
* hk.heal(999);
|
|
30
|
+
* console.log(hk.getHealth()); // 200 (clamped to max)
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* @see {@link Behaviour} — abstract base class
|
|
34
|
+
* @see {@link Player} — has a convenience getter for this behaviour
|
|
35
|
+
*/
|
|
36
|
+
export declare class HealthKit extends Behaviour<Entity> {
|
|
37
|
+
/** @inheritDoc */
|
|
4
38
|
readonly type = "healthkit";
|
|
39
|
+
/** Current health points. */
|
|
5
40
|
private health;
|
|
41
|
+
/** Maximum health points (healing cap). */
|
|
6
42
|
private maxHP;
|
|
7
|
-
|
|
43
|
+
/**
|
|
44
|
+
* Creates a new health behaviour.
|
|
45
|
+
*
|
|
46
|
+
* @param owner - The entity this behaviour is attached to.
|
|
47
|
+
* @param health - Starting health value.
|
|
48
|
+
* @param maxHP - Maximum health cap. If omitted, defaults to the
|
|
49
|
+
* initial `health` value.
|
|
50
|
+
*/
|
|
51
|
+
constructor(owner: Entity, health: number, maxHP?: number);
|
|
52
|
+
/**
|
|
53
|
+
* No-op — health does not change passively each frame.
|
|
54
|
+
*
|
|
55
|
+
* @param _deltaTime - Unused.
|
|
56
|
+
*/
|
|
8
57
|
update(_deltaTime: number): void;
|
|
58
|
+
/**
|
|
59
|
+
* Reduces health by the given amount, clamping at zero.
|
|
60
|
+
*
|
|
61
|
+
* @param amount - Damage to apply (positive number).
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```ts
|
|
65
|
+
* healthkit.takeDamage(30);
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
9
68
|
takeDamage(amount: number): void;
|
|
69
|
+
/**
|
|
70
|
+
* Increases health by the given amount, clamping at
|
|
71
|
+
* {@link HealthKit.maxHP}.
|
|
72
|
+
*
|
|
73
|
+
* @param amount - Health to restore (positive number).
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* healthkit.heal(50);
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
10
80
|
heal(amount: number): void;
|
|
81
|
+
/**
|
|
82
|
+
* Returns the current health value.
|
|
83
|
+
*
|
|
84
|
+
* @returns Current HP.
|
|
85
|
+
*/
|
|
11
86
|
getHealth(): number;
|
|
87
|
+
/**
|
|
88
|
+
* Returns the maximum health cap.
|
|
89
|
+
*
|
|
90
|
+
* @returns Maximum HP.
|
|
91
|
+
*/
|
|
12
92
|
getMaxHealth(): number;
|
|
93
|
+
/**
|
|
94
|
+
* Updates the maximum health cap.
|
|
95
|
+
*
|
|
96
|
+
* If the current health exceeds the new cap it is clamped down.
|
|
97
|
+
*
|
|
98
|
+
* @param value - The new maximum HP.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```ts
|
|
102
|
+
* healthkit.setMaxHealth(150);
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
13
105
|
setMaxHealth(value: number): void;
|
|
106
|
+
/**
|
|
107
|
+
* Whether the entity is dead (health is zero or below).
|
|
108
|
+
*
|
|
109
|
+
* @returns `true` if `health <= 0`.
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```ts
|
|
113
|
+
* if (healthkit.isDead()) {
|
|
114
|
+
* entity.destroy();
|
|
115
|
+
* }
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
14
118
|
isDead(): boolean;
|
|
119
|
+
/**
|
|
120
|
+
* Returns health as a normalised ratio in the range `[0, 1]`.
|
|
121
|
+
*
|
|
122
|
+
* Useful for rendering health bars.
|
|
123
|
+
*
|
|
124
|
+
* @returns `health / maxHP`, or `0` if `maxHP` is zero.
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* ```ts
|
|
128
|
+
* const barWidth = 100 * healthkit.getHealthPercent();
|
|
129
|
+
* ctx.fillRect(x, y, barWidth, 8);
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
15
132
|
getHealthPercent(): number;
|
|
16
133
|
}
|
|
17
134
|
//# sourceMappingURL=healtkit.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"healtkit.d.ts","sourceRoot":"","sources":["../../../core/behaviours/healtkit.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"healtkit.d.ts","sourceRoot":"","sources":["../../../src/core/behaviours/healtkit.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,uBAAuB,CAAC;AAChD,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,qBAAa,SAAU,SAAQ,SAAS,CAAC,MAAM,CAAC;IAC9C,kBAAkB;IAClB,QAAQ,CAAC,IAAI,eAAe;IAE5B,6BAA6B;IAC7B,OAAO,CAAC,MAAM,CAAS;IAEvB,2CAA2C;IAC3C,OAAO,CAAC,KAAK,CAAS;IAEtB;;;;;;;OAOG;gBACS,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM;IAMzD;;;;OAIG;IACH,MAAM,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI;IAEhC;;;;;;;;;OASG;IACH,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAIhC;;;;;;;;;;OAUG;IACH,IAAI,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAI1B;;;;OAIG;IACH,SAAS,IAAI,MAAM;IAInB;;;;OAIG;IACH,YAAY,IAAI,MAAM;IAItB;;;;;;;;;;;OAWG;IACH,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAOjC;;;;;;;;;;;OAWG;IACH,MAAM,IAAI,OAAO;IAIjB;;;;;;;;;;;;OAYG;IACH,gBAAgB,IAAI,MAAM;CAG3B"}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import type Entity from "../../entities/entity";
|
|
2
|
+
import type { Vector2 } from "../../types";
|
|
3
|
+
import { Behaviour } from "../behaviour";
|
|
4
|
+
import type Sprite from "../sprite";
|
|
5
|
+
/**
|
|
6
|
+
* Sprite animation renderer that can be attached to any {@link Entity}.
|
|
7
|
+
*
|
|
8
|
+
* `SpriteRender` plays named animations defined in a {@link Sprite}
|
|
9
|
+
* sheet, advancing frames based on each animation's `duration` and
|
|
10
|
+
* `loop` settings. It supports horizontal flipping for left/right
|
|
11
|
+
* facing and an optional pixel offset for fine-tuning draw position.
|
|
12
|
+
*
|
|
13
|
+
* @category Behaviours
|
|
14
|
+
* @since 0.1.0
|
|
15
|
+
*
|
|
16
|
+
* @example Attaching and playing an animation
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { Asset, Sprite, SpriteRender, Player } from "gamefoo";
|
|
19
|
+
*
|
|
20
|
+
* const image = await Asset.load("hero.png");
|
|
21
|
+
* const sheet = new Sprite(image, 32, 32, {
|
|
22
|
+
* idle: { frames: [0, 1], duration: 0.25, loop: true },
|
|
23
|
+
* run: { frames: [2, 3, 4, 5], duration: 0.1, loop: true },
|
|
24
|
+
* });
|
|
25
|
+
*
|
|
26
|
+
* const player = new Player("hero", 100, 100, 32, 32);
|
|
27
|
+
* const sr = new SpriteRender(player, sheet);
|
|
28
|
+
* player.attachBehaviour(sr);
|
|
29
|
+
*
|
|
30
|
+
* sr.play("idle");
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* @example Flipping for directional facing
|
|
34
|
+
* ```ts
|
|
35
|
+
* if (velocity.x < 0) {
|
|
36
|
+
* spriteRender.setFlipX(true);
|
|
37
|
+
* } else if (velocity.x > 0) {
|
|
38
|
+
* spriteRender.setFlipX(false);
|
|
39
|
+
* }
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* @see {@link Sprite} — spritesheet metadata
|
|
43
|
+
* @see {@link Asset} — image loader
|
|
44
|
+
* @see {@link Behaviour} — abstract base class
|
|
45
|
+
*/
|
|
46
|
+
export default class SpriteRender extends Behaviour<Entity> {
|
|
47
|
+
/** @inheritDoc */
|
|
48
|
+
readonly type = "sprite";
|
|
49
|
+
/** The spritesheet this renderer draws from. */
|
|
50
|
+
private sheet;
|
|
51
|
+
/**
|
|
52
|
+
* Name of the currently playing animation, or `null` if stopped.
|
|
53
|
+
*
|
|
54
|
+
* @defaultValue `null`
|
|
55
|
+
*/
|
|
56
|
+
private currentFrame;
|
|
57
|
+
/**
|
|
58
|
+
* Index into the current animation's `frames` array.
|
|
59
|
+
*
|
|
60
|
+
* @defaultValue `0`
|
|
61
|
+
*/
|
|
62
|
+
private currentFrameIndex;
|
|
63
|
+
/**
|
|
64
|
+
* Seconds accumulated towards the next frame advance.
|
|
65
|
+
*
|
|
66
|
+
* @defaultValue `0`
|
|
67
|
+
*/
|
|
68
|
+
private elapsedTime;
|
|
69
|
+
/**
|
|
70
|
+
* Whether the sprite is drawn mirrored horizontally.
|
|
71
|
+
*
|
|
72
|
+
* @defaultValue `false`
|
|
73
|
+
*/
|
|
74
|
+
private flipX;
|
|
75
|
+
/**
|
|
76
|
+
* Pixel offset applied to the draw position, relative to the
|
|
77
|
+
* entity's origin.
|
|
78
|
+
*
|
|
79
|
+
* @defaultValue `{ x: 0, y: 0 }`
|
|
80
|
+
*/
|
|
81
|
+
offset: Vector2;
|
|
82
|
+
/**
|
|
83
|
+
* Creates a sprite renderer bound to the given entity and sheet.
|
|
84
|
+
*
|
|
85
|
+
* @param owner - The entity whose position determines where the
|
|
86
|
+
* sprite is drawn.
|
|
87
|
+
* @param sheet - A {@link Sprite} containing the image and animation
|
|
88
|
+
* definitions.
|
|
89
|
+
*/
|
|
90
|
+
constructor(owner: Entity, sheet: Sprite);
|
|
91
|
+
/**
|
|
92
|
+
* Starts (or switches to) the named animation.
|
|
93
|
+
*
|
|
94
|
+
* If the requested animation is already playing, the call is a no-op
|
|
95
|
+
* so the current playback position is preserved.
|
|
96
|
+
*
|
|
97
|
+
* @param animation - Name matching a key in
|
|
98
|
+
* {@link Sprite.animations}.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```ts
|
|
102
|
+
* spriteRender.play("run");
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
play(animation: string): void;
|
|
106
|
+
/**
|
|
107
|
+
* Stops the current animation and resets playback state.
|
|
108
|
+
*
|
|
109
|
+
* After calling `stop`, nothing is drawn until {@link SpriteRender.play}
|
|
110
|
+
* is called again.
|
|
111
|
+
*/
|
|
112
|
+
stop(): void;
|
|
113
|
+
/**
|
|
114
|
+
* Enables or disables horizontal flipping.
|
|
115
|
+
*
|
|
116
|
+
* Useful for mirroring a character sprite when facing left.
|
|
117
|
+
*
|
|
118
|
+
* @param flip - `true` to mirror horizontally, `false` for normal.
|
|
119
|
+
*/
|
|
120
|
+
setFlipX(flip: boolean): void;
|
|
121
|
+
/**
|
|
122
|
+
* Advances the animation clock and moves to the next frame when the
|
|
123
|
+
* animation's `duration` has elapsed.
|
|
124
|
+
*
|
|
125
|
+
* If the animation does not loop, it holds on the last frame.
|
|
126
|
+
*
|
|
127
|
+
* @param deltaTime - Seconds elapsed since the previous frame.
|
|
128
|
+
*/
|
|
129
|
+
update(deltaTime: number): void;
|
|
130
|
+
/**
|
|
131
|
+
* Draws the current animation frame to the canvas.
|
|
132
|
+
*
|
|
133
|
+
* Respects {@link SpriteRender.flipX} by temporarily mirroring the
|
|
134
|
+
* canvas transform, and applies {@link SpriteRender.offset} to the
|
|
135
|
+
* draw position.
|
|
136
|
+
*
|
|
137
|
+
* @param ctx - The canvas 2-D rendering context.
|
|
138
|
+
*/
|
|
139
|
+
render(ctx: CanvasRenderingContext2D): void;
|
|
140
|
+
}
|
|
141
|
+
//# sourceMappingURL=sprite_render.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sprite_render.d.ts","sourceRoot":"","sources":["../../../src/core/behaviours/sprite_render.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,uBAAuB,CAAC;AAChD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,KAAK,MAAM,MAAM,WAAW,CAAC;AAEpC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,MAAM,CAAC,OAAO,OAAO,YAAa,SAAQ,SAAS,CAAC,MAAM,CAAC;IACzD,kBAAkB;IAClB,QAAQ,CAAC,IAAI,YAAY;IAEzB,gDAAgD;IAChD,OAAO,CAAC,KAAK,CAAS;IAEtB;;;;OAIG;IACH,OAAO,CAAC,YAAY,CAAuB;IAE3C;;;;OAIG;IACH,OAAO,CAAC,iBAAiB,CAAa;IAEtC;;;;OAIG;IACH,OAAO,CAAC,WAAW,CAAa;IAEhC;;;;OAIG;IACH,OAAO,CAAC,KAAK,CAAkB;IAE/B;;;;;OAKG;IACI,MAAM,EAAE,OAAO,CAAkB;IAExC;;;;;;;OAOG;gBACS,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM;IAKxC;;;;;;;;;;;;;OAaG;IACH,IAAI,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI;IAS7B;;;;;OAKG;IACH,IAAI,IAAI,IAAI;IAMZ;;;;;;OAMG;IACH,QAAQ,CAAC,IAAI,EAAE,OAAO,GAAG,IAAI;IAI7B;;;;;;;OAOG;IACH,MAAM,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI;IAwB/B;;;;;;;;OAQG;IACM,MAAM,CAAC,GAAG,EAAE,wBAAwB,GAAG,IAAI;CA0BrD"}
|
package/dist/core/camera.d.ts
CHANGED
|
@@ -1,19 +1,120 @@
|
|
|
1
1
|
import type { Vector2 } from "../types";
|
|
2
|
+
/**
|
|
3
|
+
* A 2-D viewport camera that tracks a target position within the game
|
|
4
|
+
* world.
|
|
5
|
+
*
|
|
6
|
+
* The camera stores its own `(x, y)` centre and viewport dimensions.
|
|
7
|
+
* Each frame the {@link Engine} calls {@link Camera.follow} with the
|
|
8
|
+
* player's position so the viewport stays centred on the action.
|
|
9
|
+
*
|
|
10
|
+
* @category Core
|
|
11
|
+
* @since 0.1.0
|
|
12
|
+
*
|
|
13
|
+
* @example Basic usage inside the engine
|
|
14
|
+
* ```ts
|
|
15
|
+
* const camera = new Camera(800, 600);
|
|
16
|
+
* camera.follow(player.getPosition());
|
|
17
|
+
*
|
|
18
|
+
* const view = camera.getViewRect();
|
|
19
|
+
* // view → { x: px - 400, y: py - 300, width: 800, height: 600 }
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @example Manual camera control
|
|
23
|
+
* ```ts
|
|
24
|
+
* const camera = new Camera(800, 600);
|
|
25
|
+
* camera.moveTo({ x: 0, y: 0 }); // jump to origin
|
|
26
|
+
* console.log(camera.getPosition()); // { x: 0, y: 0 }
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @see {@link Engine} — owns and drives the camera each frame
|
|
30
|
+
*/
|
|
2
31
|
export default class Camera {
|
|
32
|
+
/** Current X coordinate of the camera centre. */
|
|
3
33
|
private x;
|
|
34
|
+
/** Current Y coordinate of the camera centre. */
|
|
4
35
|
private y;
|
|
36
|
+
/** Viewport width in pixels. */
|
|
5
37
|
private width;
|
|
38
|
+
/** Viewport height in pixels. */
|
|
6
39
|
private height;
|
|
40
|
+
/**
|
|
41
|
+
* Creates a camera with the given viewport dimensions.
|
|
42
|
+
*
|
|
43
|
+
* @param width - Viewport width in pixels.
|
|
44
|
+
* @param height - Viewport height in pixels.
|
|
45
|
+
*/
|
|
7
46
|
constructor(width: number, height: number);
|
|
47
|
+
/**
|
|
48
|
+
* Centres the camera on a target position.
|
|
49
|
+
*
|
|
50
|
+
* Called by the engine every frame when a player is set.
|
|
51
|
+
*
|
|
52
|
+
* @param target - The world-space position to track.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* camera.follow(player.getPosition());
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
8
59
|
follow(target: Vector2): void;
|
|
60
|
+
/**
|
|
61
|
+
* Instantly teleports the camera to a specific position.
|
|
62
|
+
*
|
|
63
|
+
* Functionally identical to {@link Camera.follow} but communicates
|
|
64
|
+
* intent more clearly for one-shot repositioning (e.g. scene
|
|
65
|
+
* transitions).
|
|
66
|
+
*
|
|
67
|
+
* @param target - The world-space position to jump to.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```ts
|
|
71
|
+
* camera.moveTo({ x: 500, y: 300 });
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
9
74
|
moveTo(target: Vector2): void;
|
|
75
|
+
/**
|
|
76
|
+
* Returns the current centre position of the camera.
|
|
77
|
+
*
|
|
78
|
+
* @returns A new {@link Vector2} copy of the camera centre.
|
|
79
|
+
*/
|
|
10
80
|
getPosition(): Vector2;
|
|
81
|
+
/**
|
|
82
|
+
* Computes the axis-aligned rectangle that represents the visible
|
|
83
|
+
* area in world-space.
|
|
84
|
+
*
|
|
85
|
+
* The rectangle is centred on the camera's current position.
|
|
86
|
+
*
|
|
87
|
+
* @returns An object with `x` (left edge), `y` (top edge), `width`,
|
|
88
|
+
* and `height`.
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* ```ts
|
|
92
|
+
* const rect = camera.getViewRect();
|
|
93
|
+
* // Use rect to cull off-screen objects
|
|
94
|
+
* if (entity.x < rect.x || entity.x > rect.x + rect.width) {
|
|
95
|
+
* return; // off-screen — skip rendering
|
|
96
|
+
* }
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
11
99
|
getViewRect(): {
|
|
12
100
|
x: number;
|
|
13
101
|
y: number;
|
|
14
102
|
width: number;
|
|
15
103
|
height: number;
|
|
16
104
|
};
|
|
105
|
+
/**
|
|
106
|
+
* Updates the viewport dimensions, e.g. after a window resize.
|
|
107
|
+
*
|
|
108
|
+
* @param width - New viewport width in pixels.
|
|
109
|
+
* @param height - New viewport height in pixels.
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```ts
|
|
113
|
+
* window.addEventListener("resize", () => {
|
|
114
|
+
* camera.resize(window.innerWidth, window.innerHeight);
|
|
115
|
+
* });
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
17
118
|
resize(width: number, height: number): void;
|
|
18
119
|
}
|
|
19
120
|
//# sourceMappingURL=camera.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"camera.d.ts","sourceRoot":"","sources":["../../core/camera.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAExC,MAAM,CAAC,OAAO,OAAO,MAAM;IACzB,OAAO,CAAC,CAAC,CAAa;
|
|
1
|
+
{"version":3,"file":"camera.d.ts","sourceRoot":"","sources":["../../src/core/camera.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAExC;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAM,CAAC,OAAO,OAAO,MAAM;IACzB,iDAAiD;IACjD,OAAO,CAAC,CAAC,CAAa;IAEtB,iDAAiD;IACjD,OAAO,CAAC,CAAC,CAAa;IAEtB,gCAAgC;IAChC,OAAO,CAAC,KAAK,CAAS;IAEtB,iCAAiC;IACjC,OAAO,CAAC,MAAM,CAAS;IAEvB;;;;;OAKG;gBACS,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;IAKzC;;;;;;;;;;;OAWG;IACH,MAAM,CAAC,MAAM,EAAE,OAAO,GAAG,IAAI;IAK7B;;;;;;;;;;;;;OAaG;IACH,MAAM,CAAC,MAAM,EAAE,OAAO,GAAG,IAAI;IAK7B;;;;OAIG;IACH,WAAW,IAAI,OAAO;IAItB;;;;;;;;;;;;;;;;;OAiBG;IACH,WAAW,IAAI;QAAE,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE;IAStE;;;;;;;;;;;;OAYG;IACH,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI;CAI5C"}
|