Skip to content

Commit d902197

Browse files
committed
audio
1 parent 973b457 commit d902197

File tree

11 files changed

+1024
-4038
lines changed

11 files changed

+1024
-4038
lines changed

dist/assets.d.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { AudioPlayer } from './audio';
22
import { Game } from './game';
3-
import { IAsset as IAsset, IAssets } from './interface';
3+
import { IAsset, IAssets } from './interface';
44
import { default as EventEmitter } from 'eventemitter3';
55
/**
66
* Events emitted by AssetsLoader.
@@ -35,17 +35,18 @@ declare class AssetsLoader extends EventEmitter<AssetsLoaderEvents> {
3535
*/
3636
loadJson(name: string, url: string): void;
3737
/**
38-
* Loads an audio file asynchronously.
38+
* Loads an audio file asynchronously using the AudioManager.
39+
* The AudioManager will handle fetching and caching.
3940
* @param name - The unique identifier for the audio asset.
4041
* @param url - The URL of the audio file.
4142
*/
42-
loadAudio(name: string, url: string): void;
43+
loadAudio(name: string, url: string): Promise<void>;
4344
/**
44-
* Loads an image file asynchronously.
45+
* Loads an image file asynchronously using the TextureManager.
4546
* @param name - The unique identifier for the image asset.
4647
* @param url - The URL of the image file.
4748
*/
48-
loadImage(name: string, url: string): void;
49+
loadImage(name: string, url: string): Promise<void>;
4950
/**
5051
* Loads multiple assets from a list.
5152
* @param assetList - Array of assets to load.

dist/audio.d.ts

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,54 +6,76 @@ interface AudioPlayerEvents {
66
ended: () => void;
77
}
88
/**
9-
* Audio class for managing individual audio elements.
10-
* Encapsulates an HTMLAudioElement and its Web Audio API nodes.
9+
* Represents a single, playable audio instance.
10+
* Created by the AudioManager.
1111
*/
1212
export declare class AudioPlayer extends EventEmitter<AudioPlayerEvents> {
13-
element: HTMLAudioElement;
14-
source: AudioNode | null;
15-
gainNode: GainNode;
1613
private audioContext;
14+
private audioBuffer;
15+
private sourceNode;
16+
private gainNode;
17+
private _volume;
18+
private _loop;
19+
private _isPlaying;
20+
constructor(audioContext: AudioContext, audioBuffer: AudioBuffer);
1721
/**
18-
* Creates an instance of Audio.
19-
* @param audioElement - The HTMLAudioElement to manage.
20-
* @param audioContext - The Web Audio API context.
22+
* Plays the audio. If already playing, it will stop the current playback and start over.
2123
*/
22-
constructor(audioElement: HTMLAudioElement, audioContext: AudioContext);
24+
play(): void;
2325
/**
24-
* Handles the 'ended' event when audio playback completes.
26+
* Stops the audio playback immediately.
2527
*/
26-
private handleEnded;
28+
stop(): void;
2729
/**
28-
* Plays the audio with optional looping.
29-
* @param loop - Whether the audio should loop (default: false).
30+
* Gets the volume of the audio, from 0.0 to 1.0.
3031
*/
31-
play(loop?: boolean): Promise<void>;
32+
get volume(): number;
3233
/**
33-
* Pauses the audio.
34+
* Sets the volume of the audio.
35+
* @param value - The volume, from 0.0 (silent) to 1.0 (full).
3436
*/
35-
pause(): void;
37+
set volume(value: number);
3638
/**
37-
* Stops the audio and resets playback position.
39+
* Gets whether the audio will loop.
3840
*/
39-
stop(): void;
41+
get loop(): boolean;
4042
/**
41-
* Sets the volume for the audio.
42-
* @param volume - Volume level (0.0 to 1.0).
43+
* Sets whether the audio should loop.
44+
* Can be changed while the audio is playing.
4345
*/
44-
setVolume(volume: number): void;
46+
set loop(value: boolean);
4547
/**
46-
* Cleans up resources associated with the audio.
48+
* Gets whether the audio is currently playing.
49+
*/
50+
get isPlaying(): boolean;
51+
/**
52+
* Stops playback, disconnects audio nodes, and removes all event listeners.
53+
* This makes the AudioPlayer instance unusable.
4754
*/
4855
destroy(): void;
4956
}
5057
/**
51-
* AudioManager class for managing game audio, including background music (BGM) and sound effects (SFX).
52-
* Delegates audio-specific responsibilities to AudioPlayer instances.
58+
* Manages loading, decoding, and caching of audio assets.
59+
* This is analogous to your TextureCache.
5360
*/
54-
declare class AudioManager {
55-
audioContext: AudioContext;
61+
export declare class AudioManager {
62+
private audioContext;
63+
private cache;
5664
constructor();
65+
/**
66+
* Creates an AudioPlayer from a URL.
67+
* It fetches, decodes, and caches the audio data. If the URL is already cached,
68+
* it skips the network request and uses the cached data.
69+
*
70+
* @param url - The URL of the audio file.
71+
* @returns A Promise that resolves to a new AudioPlayer instance.
72+
*/
73+
audioFromUrl(url: string): Promise<AudioPlayer>;
74+
/**
75+
* Destroys the AudioManager, clears the audio cache, and closes the AudioContext.
76+
* This releases all associated audio resources. After calling this, the AudioManager
77+
* and any AudioPlayers created by it will be unusable.
78+
*/
5779
destroy(): void;
5880
}
5981
export default AudioManager;

dist/game.d.ts

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { default as AssetsLoader } from './assets';
22
import { default as AudioManager } from './audio';
33
import { InputManager } from './input';
4-
import { ICameraOptions, IEntityTransformOptions, IGameOptions, IMathObject } from './interface';
5-
import { MatrixStack, Vec2 } from './math';
4+
import { IAnimation, ICameraOptions, IEntityTransformOptions, IGameOptions, ILabelEntityOptions, IMathObject, ISpriteOptions } from './interface';
5+
import { Color, MatrixStack, Vec2 } from './math';
66
import { default as Rapid } from './render';
7-
import { TextureCache } from './texture';
7+
import { Text, Texture, TextureCache } from './texture';
88
import { EasingFunction, Timer, Tween } from './utils';
99
/**
1010
* Base class for game entities with transform and rendering capabilities.
@@ -31,7 +31,7 @@ export declare class Entity {
3131
* @param options - Configuration options for position, scale, rotation, and tags.
3232
*/
3333
constructor(game: Game, options?: IEntityTransformOptions);
34-
getRoot(): Scene | null;
34+
getScene(): Scene | null;
3535
/**
3636
* Gets the parent transform or the renderer's matrix stack if no parent exists.
3737
* @returns The transform matrix stack.
@@ -100,6 +100,8 @@ export declare class Entity {
100100
* Hook for custom cleanup logic before disposal.
101101
*/
102102
protected postDispose(): void;
103+
getMouseLocalPosition(): Vec2;
104+
getMouseGlobalPosition(): Vec2;
103105
}
104106
/**
105107
* A layer that renders its children directly to the screen, ignoring any camera transforms.
@@ -109,7 +111,7 @@ export declare class Entity {
109111
* 非常适合用于UI元素,如HUD(状态栏)、菜单和分数显示。
110112
*/
111113
export declare class CanvasLayer extends Entity {
112-
constructor(game: Game);
114+
constructor(game: Game, options: IEntityTransformOptions);
113115
updateTransform(): void;
114116
}
115117
/**
@@ -164,6 +166,7 @@ export declare class Game {
164166
private timers;
165167
mainCamera: Camera | null;
166168
renderQueue: Entity[];
169+
worldTransform: MatrixStack;
167170
/**
168171
* Creates a new game instance.
169172
* @param options - Configuration options for the game.
@@ -247,3 +250,75 @@ export declare class Game {
247250
*/
248251
private updateTweens;
249252
}
253+
/**
254+
* An entity that displays a texture (a "sprite") and can play animations.
255+
* Animations are defined as a sequence of textures.
256+
*/
257+
export declare class Sprite extends Entity {
258+
/** The current texture being displayed. This can be a static texture or a frame from an animation. */
259+
texture: Texture | null;
260+
/** Whether the sprite is flipped horizontally. */
261+
flipX: boolean;
262+
/** Whether the sprite is flipped vertically. */
263+
flipY: boolean;
264+
color: Color;
265+
offset: Vec2;
266+
private animations;
267+
private currentAnimation;
268+
private currentFrame;
269+
private frameTimer;
270+
private isPlaying;
271+
/**
272+
* Creates a new Sprite entity.
273+
* @param game - The game instance.
274+
* @param options - Configuration for the sprite's transform and initial texture.
275+
*/
276+
constructor(game: Game, options?: ISpriteOptions);
277+
setAnimations(animations: IAnimation): void;
278+
/**
279+
* Defines a new animation sequence.
280+
* @param name - A unique name for the animation (e.g., "walk", "jump").
281+
* @param frames - An array of Textures to use as frames.
282+
* @param fps - The playback speed in frames per second.
283+
* @param loop - Whether the animation should repeat.
284+
*/
285+
addAnimation(name: string, frames: Texture[], fps?: number, loop?: boolean): void;
286+
/**
287+
* Plays an animation that has been previously defined with `addAnimation`.
288+
* @param name - The name of the animation to play.
289+
* @param forceRestart - If true, the animation will restart from the first frame even if it's already playing.
290+
*/
291+
play(name: string, forceRestart?: boolean): void;
292+
/**
293+
* Stops the currently playing animation.
294+
* The sprite will remain on the current frame.
295+
*/
296+
stop(): void;
297+
/**
298+
* Updates the animation frame based on the elapsed time.
299+
* @param deltaTime - Time in seconds since the last frame.
300+
* @override
301+
*/
302+
onUpdate(deltaTime: number): void;
303+
/**
304+
* Renders the sprite's current texture to the screen.
305+
* @param render - The Rapid rendering instance.
306+
* @override
307+
*/
308+
onRender(render: Rapid): void;
309+
}
310+
/**
311+
* An entity designed specifically to display text on the screen.
312+
* It encapsulates a `Text` texture, giving it position, scale, rotation,
313+
* and other entity-based properties.
314+
*/
315+
export declare class Label extends Entity {
316+
text: Text;
317+
constructor(game: Game, options: ILabelEntityOptions);
318+
onRender(render: Rapid): void;
319+
/**
320+
* Updates the displayed text. Re-renders the texture if the text has changed.
321+
* @param text - The new text to display.
322+
*/
323+
setText(text: string): void;
324+
}

dist/input.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { Entity } from './game';
88
export declare class InputManager {
99
private rapid;
1010
private canvas;
11-
private mousePosition;
11+
mousePosition: Vec2;
1212
private keysDown;
1313
private keysDownLastFrame;
1414
private buttonsDown;
@@ -81,6 +81,8 @@ export declare class InputManager {
8181
* @returns The mouse position in the entity's local coordinate system.
8282
*/
8383
getMouseLocal(entity: Entity): Vec2;
84+
getAxis(negativeAction: string, positiveAction: string): number;
85+
getVector(negativeX: string, positiveX: string, negativeY: string, positiveY: string): Vec2;
8486
/**
8587
* Removes all event listeners and cleans up resources.
8688
*/

dist/interface.d.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export interface ITextTextureOptions {
114114
* The color of the text.
115115
* Default is '#000000' (black).
116116
*/
117-
color?: string;
117+
color?: Color;
118118
/**
119119
* The alignment of the text.
120120
* Possible values: 'left', 'right', 'center', 'start', 'end'.
@@ -370,11 +370,6 @@ export interface IParticleEmitterOptions extends IParticleOptions, IEntityTransf
370370
}
371371
export interface IGameOptions extends IRapidOptions {
372372
}
373-
export interface ISound {
374-
element: HTMLAudioElement;
375-
source: MediaElementAudioSourceNode | null;
376-
gainNode: GainNode;
377-
}
378373
/**
379374
* Interface for an asset to be loaded.
380375
*/
@@ -397,3 +392,18 @@ export interface IAssets {
397392
[key: string]: Texture;
398393
};
399394
}
395+
export interface IAnimation {
396+
/** The name of the animation, used to play it. */
397+
name: string;
398+
/** An array of Textures that make up the frames of the animation. */
399+
frames: Texture[];
400+
/** The speed of the animation in frames per second (FPS). */
401+
fps: number;
402+
/** Whether the animation should loop back to the first frame when it ends. */
403+
loop: boolean;
404+
}
405+
export interface ISpriteOptions extends IEntityTransformOptions, ISpriteRenderOptions {
406+
animations?: IAnimation;
407+
}
408+
export interface ILabelEntityOptions extends ITextTextureOptions, IEntityTransformOptions {
409+
}

dist/math.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,12 @@ export declare class Color implements IMathObject<Color> {
265265
* @returns A new `Color` instance with the same RGBA values.
266266
*/
267267
clone(): Color;
268+
/**
269+
* Converts the color to a hexadecimal string representation (e.g., '#RRGGBBAA').
270+
* @param includeAlpha - Whether to include the alpha channel in the hex string.
271+
* @returns The hexadecimal string representation of the color.
272+
*/
273+
toHexString(includeAlpha?: boolean): string;
268274
/**
269275
* Checks if the current color is equal to another color.
270276
* @param color - The color to compare with.

dist/rapid.global.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)