Skip to content

Commit 439e274

Browse files
author
Your Name
committed
update documentation
1 parent c188d60 commit 439e274

28 files changed

+1039
-130
lines changed

Parts/Children/PolygonCollider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class PolygonCollider extends Collider {
4141
super.act(delta);
4242
}
4343

44-
override _updateVerticesAfterMerge(polygons: Vector[][][]): void {
44+
override _updateVerticesAfterMerge(polygons: Vector[][]): void {
4545
const newCollider = new MultiPolygonCollider({ polygons, tag: this.tag });
4646

4747
newCollider.active = this.active;

Parts/Layer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import type { Collider } from "./Children/Collider";
77
export class Layer extends Part {
88
spatialGrid: SpatialGrid;
99

10-
constructor({ name }: { name: string }) {
10+
constructor({ name, spatialGridDefinition }: { name: string, spatialGridDefinition?: number }) {
1111
super({ name });
1212
this.type = "Layer";
1313
this.id = generateUID();
1414
this.debugEmoji = "🗂️"; // Default emoji for debugging the layer
15-
this.spatialGrid = new SpatialGrid(50);
15+
this.spatialGrid = new SpatialGrid(spatialGridDefinition || 100);
1616
}
1717

1818
addChild(part: Part) {

Parts/PhysicsEngine.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@ import { Vector } from "../Math/Vector";
55
export class PhysicsEngine extends Part {
66
engine: Engine;
77
world: World;
8+
gravity: { x: number, y: number };
9+
scale: number;
810

9-
constructor({ gravity }: { gravity?: { x: number, y: number, scale: number } }) {
11+
constructor({ gravity, scale }: { gravity?: Vector, scale: number }) {
1012
super({ name: 'PhysicsEngine' });
13+
this.gravity = gravity?.toObject() || new Vector(0, 1).toObject();
14+
this.scale = scale || 0.001;
15+
1116
this.engine = Engine.create({
12-
gravity: gravity || {
13-
x: 0,
14-
y: 1, // Default gravity pointing downwards
15-
scale: 0.001 // Scale for the gravity vector
17+
gravity: {
18+
x: this.gravity.x,
19+
y: this.gravity.y,
20+
scale: this.scale
1621
}
1722
});
1823
this.world = this.engine.world;
@@ -26,7 +31,8 @@ export class PhysicsEngine extends Part {
2631
}
2732

2833
const clonedEngine = new PhysicsEngine({
29-
gravity: this.engine.gravity // Pass original gravity settings to constructor
34+
gravity: new Vector(this.gravity.x, this.gravity.y),
35+
scale: this.scale
3036
});
3137

3238
memo.set(this, clonedEngine);

docs/_sidebar.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525
- [Collider](api/Collider.md)
2626
- [BoxCollider](api/BoxCollider.md)
2727
- [PolygonCollider](api/PolygonCollider.md)
28+
- [MultiPolygonCollider](api/MultiPolygonCollider.md)
2829
- **Utility Components**
2930
- [CharacterMovement](api/CharacterMovement.md)
31+
- [GravityCharacterMovement](api/GravityCharacterMovement.md)
3032
- [Rotator](api/Rotator.md)
3133
- [Scaler](api/Scaler.md)
3234
- [Projectile](api/Projectile.md)
@@ -46,6 +48,8 @@
4648
- **Math**
4749
- [Vector](api/Vector.md)
4850
- [Engine Internals](engine-internals.md)
51+
- **Tools**
52+
- [Sprite Builder](spriteBuilder.md)
4953
- **Tutorials**
5054
- [Setting up the Physics Engine](tutorials/physics-engine.md)
5155
- [Integrating Physics with PhysicsBody](tutorials/physics-body.md)

docs/api-reference.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ This section provides a detailed reference for the core classes and components i
3333
- [Collider](./api/Collider.md) (Base class)
3434
- [BoxCollider](./api/BoxCollider.md)
3535
- [PolygonCollider](./api/PolygonCollider.md)
36+
- [MultiPolygonCollider](./api/MultiPolygonCollider.md)
3637
- [AreaTrigger](./api/AreaTrigger.md)
3738

3839

@@ -41,6 +42,7 @@ This section provides a detailed reference for the core classes and components i
4142

4243
- [CameraShake](./api/CameraShake.md)
4344
- [CharacterMovement](./api/CharacterMovement.md)
45+
- [GravityCharacterMovement](./api/GravityCharacterMovement.md)
4446
- [Follow](./api/Follow.md)
4547
- [Health](./api/Health.md)
4648
- [HealthBar](./api/HealthBar.md)

docs/api/AnimatedSprite.md

Lines changed: 142 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,148 @@ The `AnimatedSprite` component renders an animated sprite from a spritesheet. It
66

77
## Constructor
88

9-
`new AnimatedSprite({ spritesheet, spritesheetImage, width, height, startingAnimation, disableAntiAliasing, onAnimationComplete, webEngine })`
9+
# AnimatedSprite
10+
11+
**Extends:** [Renderer](./Renderer.md)
12+
13+
The `AnimatedSprite` component renders an animated sprite from a spritesheet. It requires a JSON file (typically generated by a tool like TexturePacker) that defines the frames and animations.
14+
15+
## Constructor
16+
17+
`new AnimatedSprite({ spritesheet, spritesheetImage, width, height, startingAnimation, disableAntiAliasing, onAnimationComplete, webEngine, loop, bounce })`
18+
19+
- `spritesheet: string`
20+
The path to the JSON file that defines the spritesheet data.
21+
22+
- `spritesheetImage?: string`
23+
The path to the image file for the spritesheet.
24+
25+
- `width: number`
26+
The width of the sprite.
27+
28+
- `height: number`
29+
The height of the sprite.
30+
31+
- `startingAnimation?: string`
32+
The name of the animation to play when the sprite is created. (Or "default" to preserve spritesheet data's starting animation but force the `bounce` and `loop` properties)
33+
34+
- `disableAntiAliasing?: boolean`
35+
If `true`, disables anti-aliasing for this sprite. Defaults to `false`.
36+
37+
- `onAnimationComplete?: (animationName: string, sprite: AnimatedSprite) => void`
38+
A callback function that is called when an animation completes.
39+
40+
- `webEngine?: boolean`
41+
This property is used internally by the engine and should not be modified.
42+
43+
- `loop?: boolean`
44+
Override spritesheet data and loop immediately.
45+
46+
- `bounce?: boolean`
47+
Override spritesheet data and bounce immediately.
48+
49+
## Properties
50+
51+
- `spritesheet: string`
52+
The path to the JSON data for the spritesheet.
53+
54+
- `spritesheetData?: SpriteSheetData`
55+
The parsed spritesheet data.
56+
57+
- `loadedSheet?: HTMLImageElement`
58+
The image element for the spritesheet.
59+
60+
- `frames: Record<string, HTMLImageElement[]>`
61+
A dictionary of all the frames for each animation.
62+
63+
- `currentFrameIndex: number`
64+
The index of the current frame in the current animation.
65+
66+
- `width: number` & `height: number`
67+
The dimensions of the sprite.
68+
69+
- `bouncing: boolean`
70+
A flag to indicate if the sprite animation is in reverse (bouncing).
71+
72+
- `currentAnimation: string`
73+
The name of the animation that is currently playing.
74+
75+
- `disableAntiAliasing: boolean`
76+
If `true`, disables anti-aliasing for this sprite.
77+
78+
- `onAnimationComplete?: (animationName: string, sprite: AnimatedSprite) => void`
79+
A callback function that is called when an animation completes.
80+
81+
- `lastFrameTime`
82+
Timestamp of the last frame update (as performance.now(), not Date.now())
83+
84+
## Methods
85+
86+
- `setAnimation(animationName: string, { loop, bounce }: { loop?: boolean, bounce?: boolean } = {})`
87+
Changes the current animation.
88+
89+
## Spritesheet Format
90+
91+
The engine expects a specific JSON format for the spritesheet data. See the `convertTexturePackerToSpriteSheetData` helper function and the `SpriteSheetData` type in the source code for details.
92+
93+
## Examples
94+
95+
### Creating an Animated Sprite
96+
97+
```javascript
98+
import { GameObject } from './Parts/GameObject';
99+
import { Transform } from './Parts/Children/Transform';
100+
import { AnimatedSprite } from './Parts/Children/AnimatedSprite';
101+
import { Vector } from './Math/Vector';
102+
103+
const monster = new GameObject({ name: 'Monster' });
104+
105+
monster.addChildren(
106+
new Transform({ position: new Vector(400, 300) }),
107+
new AnimatedSprite({
108+
spritesheet: './assets/monster.json', // Path to the JSON data
109+
spritesheetImage: './assets/monster.png', // Path to the image
110+
width: 32,
111+
height: 32,
112+
startingAnimation: 'walk'
113+
})
114+
);
115+
116+
myLayer.addChild(monster);
117+
```
118+
119+
### Controlling Animations from another Part
120+
121+
This example shows a script that changes the monster's animation based on its state.
122+
123+
```javascript
124+
import { Part } from './Parts/Part';
125+
import { AnimatedSprite } from './Parts/Children/AnimatedSprite';
126+
127+
class MonsterAI extends Part {
128+
constructor() {
129+
super();
130+
this.name = 'MonsterAI';
131+
this.isAngry = false;
132+
}
133+
134+
act() {
135+
const animator = this.sibling<AnimatedSprite>('AnimatedSprite');
136+
if (!animator) return;
137+
138+
// If the monster is angry and not already playing the attack animation...
139+
if (this.isAngry && animator.currentAnimation !== 'attack') {
140+
animator.setAnimation('attack');
141+
} else if (!this.isAngry && animator.currentAnimation !== 'walk') {
142+
animator.setAnimation('walk');
143+
}
144+
}
145+
}
146+
147+
// Add the AI to the monster GameObject
148+
monster.addChild(new MonsterAI());
149+
```
150+
10151

11152
- `spritesheet: string`
12153
The path to the JSON file that defines the spritesheet data.

docs/api/BoxCollider.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ A `BoxCollider` is a rectangular-shaped collider. It's the most common and compu
66

77
## Constructor
88

9-
`new BoxCollider({ width, height })`
9+
`new BoxCollider({ width, height, tag })`
1010

1111
- `width: number`
1212
The width of the collision box.
1313

1414
- `height: number`
1515
The height of the collision box.
1616

17+
- `tag?: string`
18+
An optional tag for the collider. Defaults to `"<Untagged>"`.
19+
1720
## Properties
1821

1922
- `start: Vector`

docs/api/Button.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ The `Button` component creates a clickable UI button with different visual state
66

77
## Constructor
88

9-
`new Button({ label, onClick, styles, clickSound, hoverSound, activeSound })`
9+
`new Button({ label, onClick, styles, clickSound, hoverSound, activeSound, width, height, backgroundColor, color, font, borderRadius, borderWidth, borderColor, hoverBackground, hoverColor, activeBackground, activeColor })`
1010

1111
- `label: string`
1212
The text to display on the button.
@@ -26,6 +26,9 @@ The `Button` component creates a clickable UI button with different visual state
2626
- `activeSound?: Sound`
2727
A `Sound` to play when the button is pressed.
2828

29+
- `width?: number`, `height?: number`, `backgroundColor?: string`, `color?: string`, `font?: string`, `borderRadius?: number`, `borderWidth?: number`, `borderColor?: string`, `hoverBackground?: string`, `hoverColor?: string`, `activeBackground?: string`, `activeColor?: string`
30+
Optional individual style properties that can be provided instead of a `styles` object.
31+
2932
## Properties
3033

3134
- `styles?: ButtonStyles`
@@ -89,4 +92,4 @@ startButton.addChildren(
8992
);
9093

9194
myMenuScene.addChild(startButton);
92-
```
95+
```

docs/api/Follow.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ The `Follow` component is used to make a `GameObject` follow another `Part` in t
66

77
## Constructor
88

9-
`new Follow({ target, offset, interpolationSpeed })`
9+
`new Follow({ name, target, offset, interpolationSpeed })`
10+
11+
- `name?: string`
12+
The name of the follow component.
1013

1114
- `target: Transform`
1215
The `Transform` component of the `Part` to follow.

docs/api/Game.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ The `Game` class is the heart of your application. It's the top-level container
66

77
## Constructor
88

9-
`new Game({ name, canvas, devmode, width, height, disableAntiAliasing, showtoolTips })`
9+
`new Game({ name, canvas, devmode, width, height, disableAntiAliasing, showtoolTips, showFrameStats })`
1010

1111
- `name: string`
1212
The name of the game.
@@ -29,6 +29,9 @@ The `Game` class is the heart of your application. It's the top-level container
2929
- `showtoolTips?: boolean`
3030
When set to `true`, shows tooltips in devmode. Defaults to `false`.
3131

32+
- `showFrameStats?: "BASIC" | "EXTENDED" | "ADVANCED" | "PERFORMANCE_HUD"`
33+
Shows frame rate statistics. Defaults to `"BASIC"`.
34+
3235
## Properties
3336

3437
- `canvas: HTMLCanvasElement`
@@ -129,4 +132,4 @@ myGame.addChild(myFirstScene);
129132
myGame.start(myFirstScene);
130133
// or by name:
131134
// myGame.start('Level 1');
132-
```
135+
```

0 commit comments

Comments
 (0)