rapid-render 0.1.1 → 0.1.2-1.3

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/index.d.ts CHANGED
@@ -1,10 +1,16 @@
1
- import { graphicAttributes } from "./regions/graphic_region";
2
- import { spriteAttributes } from "./regions/sprite_region";
3
- import Rapid from "./render";
4
- import GLShader from "./webgl/glshader";
5
- import { Text } from "./texture";
6
- export { Text, Rapid, GLShader, spriteAttributes, graphicAttributes, };
7
- export * from "./math";
8
- export * from "./interface";
9
- export * from "./texture";
10
- export * from "./render";
1
+ import { default as Rapid } from './render';
2
+ import { default as GLShader } from './webgl/glshader';
3
+ import { Text } from './texture';
4
+ import { TileMapRender, TileSet } from './tilemap';
5
+ import { Uniform } from './webgl/uniform';
6
+ import { spriteAttributes, graphicAttributes } from './regions/attributes';
7
+ import { ParticleEmitter } from './particle';
8
+ export { Text, Rapid, GLShader, TileMapRender, TileSet, Uniform, ParticleEmitter, graphicAttributes, spriteAttributes, };
9
+ export * from './math';
10
+ export * from './interface';
11
+ export * from './texture';
12
+ export * from './render';
13
+ export * from './particle';
14
+ export * from './game';
15
+ export * from './input';
16
+ export * from './assets';
@@ -0,0 +1,95 @@
1
+ import { Vec2 } from './math';
2
+ import { default as Rapid } from './render';
3
+ import { Entity } from './game';
4
+ /**
5
+ * Manages user input for keyboard and mouse events.
6
+ * Tracks key and button states, including pressed/released states for the current and previous frames.
7
+ */
8
+ export declare class InputManager {
9
+ private rapid;
10
+ private canvas;
11
+ mousePosition: Vec2;
12
+ private keysDown;
13
+ private keysDownLastFrame;
14
+ private buttonsDown;
15
+ private buttonsDownLastFrame;
16
+ /**
17
+ * Creates an instance of InputManager.
18
+ * @param rapid - The Rapid instance for rendering and coordinate conversion.
19
+ */
20
+ constructor(rapid: Rapid);
21
+ /**
22
+ * Attaches all necessary event listeners for keyboard and mouse input.
23
+ * @private
24
+ */
25
+ private attachEventListeners;
26
+ /**
27
+ * Updates the state of keys and buttons for the next frame.
28
+ */
29
+ updateNextFrame(): void;
30
+ /**
31
+ * Checks if a key is currently pressed (continuous detection).
32
+ * @param key - The key code (e.g., "KeyW", "Space").
33
+ * @returns True if the key is pressed, false otherwise.
34
+ */
35
+ isKeyDown(key: string): boolean;
36
+ /**
37
+ * Checks if a key is currently released.
38
+ * @param key - The key code (e.g., "KeyW", "Space").
39
+ * @returns True if the key is released, false otherwise.
40
+ */
41
+ isKeyUp(key: string): boolean;
42
+ /**
43
+ * Checks if a key was pressed in the current frame (single trigger).
44
+ * @param key - The key code (e.g., "KeyW", "Space").
45
+ * @returns True if the key was just pressed, false otherwise.
46
+ */
47
+ wasKeyPressed(key: string): boolean;
48
+ /**
49
+ * Checks if a key was released in the current frame (single trigger).
50
+ * @param key - The key code (e.g., "KeyW", "Space").
51
+ * @returns True if the key was just released, false otherwise.
52
+ */
53
+ wasKeyReleased(key: string): boolean;
54
+ /**
55
+ * Checks if a mouse button is currently pressed (continuous detection).
56
+ * @param button - The mouse button number (0: left, 1: middle, 2: right).
57
+ * @returns True if the button is pressed, false otherwise.
58
+ */
59
+ isButtonDown(button: number): boolean;
60
+ /**
61
+ * Checks if a mouse button is currently released.
62
+ * @param button - The mouse button number (0: left, 1: middle, 2: right).
63
+ * @returns True if the button is released, false otherwise.
64
+ */
65
+ isButtonUp(button: number): boolean;
66
+ /**
67
+ * Checks if a mouse button was pressed in the current frame (single trigger).
68
+ * @param button - The mouse button number (0: left, 1: middle, 2: right).
69
+ * @returns True if the button was just pressed, false otherwise.
70
+ */
71
+ wasButtonPressed(button: number): boolean;
72
+ /**
73
+ * Checks if a mouse button was released in the current frame (single trigger).
74
+ * @param button - The mouse button number (0: left, 1: middle, 2: right).
75
+ * @returns True if the button was just released, false otherwise.
76
+ */
77
+ wasButtonReleased(button: number): boolean;
78
+ /**
79
+ * Converts the mouse position to local coordinates relative to an entity.
80
+ * @param entity - The entity to convert coordinates for.
81
+ * @returns The mouse position in the entity's local coordinate system.
82
+ */
83
+ getMouseLocal(entity: Entity): Vec2;
84
+ getAxis(negativeAction: string, positiveAction: string): number;
85
+ getVector(negativeX: string, positiveX: string, negativeY: string, positiveY: string): Vec2;
86
+ /**
87
+ * Removes all event listeners and cleans up resources.
88
+ */
89
+ destroy(): void;
90
+ private handleMouseMove;
91
+ private handleKeyDown;
92
+ private handleKeyUp;
93
+ private handleMouseDown;
94
+ private handleMouseUp;
95
+ }
@@ -1,12 +1,58 @@
1
- import { Color, Vec2 } from "./math";
2
- import GLShader from "./webgl/glshader";
1
+ import { AudioPlayer } from './audio';
2
+ import { Entity } from './game';
3
+ import { Color, Vec2 } from './math';
4
+ import { Texture } from './texture';
5
+ import { TileSet } from './tilemap';
6
+ import { default as GLShader } from './webgl/glshader';
7
+ import { Uniform } from './webgl/uniform';
8
+ /**
9
+ * @ignore
10
+ */
3
11
  export type WebGLContext = WebGL2RenderingContext | WebGLRenderingContext;
4
- export interface IRapiadOptions {
12
+ /**
13
+ * Defines the required mathematical operations for an object to be tweenable.
14
+ */
15
+ export interface IMathObject<T> {
16
+ /**
17
+ * Creates a new object that is a copy of the current instance.
18
+ * @returns A new instance of the object.
19
+ */
20
+ clone(): IMathObject<T>;
21
+ /**
22
+ * Adds another object's values to this one.
23
+ * @param other - The object to add.
24
+ * @returns A new object with the result of the addition.
25
+ */
26
+ add(other: IMathObject<T>): IMathObject<T>;
27
+ /**
28
+ * Subtracts another object's values from this one.
29
+ * @param other - The object to subtract.
30
+ * @returns A new object with the result of the subtraction.
31
+ */
32
+ subtract(other: IMathObject<T>): IMathObject<T>;
33
+ /**
34
+ * Multiplies the object's values by a scalar.
35
+ * @param scalar - The number to multiply by.
36
+ * @returns A new object with the result of the multiplication.
37
+ */
38
+ multiply(scalar: number | IMathObject<T>): IMathObject<T>;
39
+ }
40
+ export declare enum ScaleRadio {
41
+ KEEP = "keep",
42
+ KEEP_H = "keep_h",
43
+ KEEP_W = "keep_w",
44
+ IGNORE = "ignore",
45
+ EXPAND = "expand"
46
+ }
47
+ export interface IRapidOptions {
5
48
  canvas: HTMLCanvasElement;
6
- pixelDensity?: number;
7
49
  width?: number;
8
50
  height?: number;
9
51
  backgroundColor?: Color;
52
+ antialias?: boolean;
53
+ devicePixelRatio?: number;
54
+ scaleEnable?: boolean;
55
+ scaleRadio?: ScaleRadio;
10
56
  }
11
57
  export interface IAttribute {
12
58
  name: string;
@@ -16,16 +62,44 @@ export interface IAttribute {
16
62
  stride: number;
17
63
  offset?: number;
18
64
  }
19
- export interface IRenderSpriteOptions {
65
+ export interface IEntityTransformOptions {
66
+ position?: Vec2;
67
+ scale?: Vec2 | number;
68
+ rotation?: number;
69
+ x?: number;
70
+ y?: number;
71
+ tags?: string[];
72
+ }
73
+ export interface ITilemapEntityOptions extends IEntityTransformOptions {
74
+ tileset: TileSet;
75
+ }
76
+ export interface ITransformOptions {
77
+ restoreTransform?: boolean;
78
+ saveTransform?: boolean;
79
+ position?: Vec2;
80
+ scale?: Vec2 | number;
81
+ rotation?: number;
82
+ x?: number;
83
+ y?: number;
84
+ offset?: Vec2;
85
+ offsetX?: number;
86
+ offsetY?: number;
87
+ origin?: Vec2 | number;
88
+ afterSave?(): unknown;
89
+ beforRestore?(): unknown;
90
+ }
91
+ export interface ISpriteRenderOptions extends ITransformOptions, IShaderRenderOptions {
20
92
  color?: Color;
21
- shader?: GLShader;
22
- uniforms?: UniformType;
93
+ texture?: Texture;
94
+ offset?: Vec2;
95
+ flipX?: boolean;
96
+ flipY?: boolean;
23
97
  }
24
- export interface ITextOptions {
98
+ export interface ITextTextureOptions {
25
99
  /**
26
100
  * The text string to be rendered.
27
101
  */
28
- text: string;
102
+ text?: string;
29
103
  /**
30
104
  * The font size for the text.
31
105
  * Default is 16.
@@ -40,7 +114,7 @@ export interface ITextOptions {
40
114
  * The color of the text.
41
115
  * Default is '#000000' (black).
42
116
  */
43
- color?: string;
117
+ color?: Color;
44
118
  /**
45
119
  * The alignment of the text.
46
120
  * Possible values: 'left', 'right', 'center', 'start', 'end'.
@@ -53,36 +127,283 @@ export interface ITextOptions {
53
127
  * Default is 'top'.
54
128
  */
55
129
  textBaseline?: CanvasTextBaseline;
56
- /**
57
- * Whether to apply antialiasing to the texture.
58
- * Default is `false`.
59
- */
60
- antialias?: boolean;
61
130
  }
62
- export declare enum JoinTyps {
63
- BEVEL = 0,
64
- ROUND = 1,
65
- MITER = 2
131
+ export interface ILineStyleOptions extends IGraphicRenderOptions {
132
+ width?: number;
133
+ closed?: boolean;
134
+ roundCap?: boolean;
135
+ textureMode?: LineTextureMode;
136
+ points: Vec2[];
66
137
  }
67
- export declare enum CapTyps {
68
- BUTT = 0,
69
- ROUND = 1,
70
- SQUARE = 2
138
+ export declare enum LineTextureMode {
139
+ STRETCH = "stretch",
140
+ REPEAT = "repeat"
71
141
  }
72
- export interface ILineOptions {
73
- cap?: CapTyps;
74
- join?: JoinTyps;
75
- width?: number;
76
- miterLimit?: number;
142
+ export declare enum TextureWrapMode {
143
+ REPEAT = "repeat",
144
+ CLAMP = "clamp",
145
+ MIRROR = "mirror"
77
146
  }
78
- export interface IRenderLineOptions extends ILineOptions {
79
- points: Vec2[];
80
- color?: Color;
147
+ export interface IRenderLineOptions extends ILineStyleOptions, ITransformOptions {
81
148
  }
82
- export interface IGraphicOptions {
83
- points: Vec2[];
84
- color?: Color;
149
+ export interface IGraphicRenderOptions extends ITransformOptions, IShaderRenderOptions {
150
+ color?: Color | Color[];
85
151
  drawType?: number;
152
+ uv?: Vec2[];
153
+ texture?: Texture;
154
+ }
155
+ export interface IPolygonGraphicRenderOptions extends IGraphicRenderOptions {
156
+ points: Vec2[];
157
+ }
158
+ export interface ICircleRenderOptions extends IGraphicRenderOptions {
159
+ radius: number;
160
+ segments?: number;
161
+ }
162
+ export interface IRectRenderOptions extends IGraphicRenderOptions {
163
+ width: number;
164
+ height: number;
86
165
  }
87
- export type UniformType = Record<string, number | Array<any>>;
166
+ export declare enum MaskType {
167
+ Include = "normal",
168
+ Exclude = "inverse"
169
+ }
170
+ export type UniformType = Record<string, number | Array<any> | boolean | Texture>;
88
171
  export type Images = ImageBitmap | ImageData | HTMLImageElement | HTMLCanvasElement | HTMLVideoElement | OffscreenCanvas;
172
+ export interface IRegisterTileOptions extends ISpriteRenderOptions {
173
+ texture: Texture;
174
+ offsetX?: number;
175
+ offsetY?: number;
176
+ ySortOffset?: number;
177
+ }
178
+ export interface YSortCallback {
179
+ ySort: number;
180
+ render?: () => void;
181
+ entity?: Entity;
182
+ renderSprite?: ISpriteRenderOptions;
183
+ }
184
+ export interface IEntityTilemapLayerOptions extends ITilemapLayerOptions, IEntityTransformOptions {
185
+ enableYsort: boolean;
186
+ }
187
+ export interface ITilemapLayerOptions {
188
+ error?: number | Vec2;
189
+ errorX?: number;
190
+ errorY?: number;
191
+ ySortCallback?: Array<YSortCallback>;
192
+ shape?: TilemapShape;
193
+ tileSet: TileSet;
194
+ eachTile?: (tileId: string | number, mapX: number, mapY: number) => ISpriteRenderOptions | undefined | void;
195
+ }
196
+ export interface ILayerRenderOptions extends ITransformOptions, ITilemapLayerOptions {
197
+ }
198
+ export interface IShaderRenderOptions {
199
+ shader?: GLShader;
200
+ uniforms?: Uniform;
201
+ }
202
+ export declare enum TilemapShape {
203
+ SQUARE = "square",
204
+ ISOMETRIC = "isometric"
205
+ }
206
+ export declare enum ShaderType {
207
+ SPRITE = "sprite",
208
+ GRAPHIC = "graphic"
209
+ }
210
+ export declare enum BlendMode {
211
+ Additive = "additive",
212
+ Subtractive = "subtractive",
213
+ Mix = "mix"
214
+ }
215
+ /**
216
+ * Interface for light rendering options
217
+ */
218
+ export interface ILightRenderOptions {
219
+ /** Position of the light source */
220
+ lightSource: Vec2;
221
+ /** Array of vertex arrays for occlusion objects, each occlusion object is defined by a set of vertices */
222
+ occlusion: Vec2[][];
223
+ /** Base projection length that controls shadow length */
224
+ baseProjectionLength?: number;
225
+ /** Type of mask to apply */
226
+ type?: MaskType;
227
+ }
228
+ export interface ICameraOptions extends ITransformOptions {
229
+ center?: boolean;
230
+ enable?: boolean;
231
+ positionSmoothingSpeed?: number;
232
+ rotationSmoothingSpeed?: number;
233
+ }
234
+ /**
235
+ * Defines particle emitter shape types
236
+ */
237
+ export declare enum ParticleShape {
238
+ /**
239
+ * Point emitter, emits particles from a single point
240
+ */
241
+ POINT = "point",
242
+ /**
243
+ * Circle emitter, emits particles randomly from a circular area
244
+ */
245
+ CIRCLE = "circle",
246
+ /**
247
+ * Rectangle emitter, emits particles randomly from a rectangular area
248
+ */
249
+ RECT = "rect"
250
+ }
251
+ /**
252
+ * Defines particle attribute animation
253
+ * @template T Attribute type, can be number, vector or color
254
+ */
255
+ export interface ParticleAttribute<T extends number | Vec2 | Color> {
256
+ /**
257
+ * Damping coefficient, controls attribute decay rate over time
258
+ */
259
+ damping?: number;
260
+ /**
261
+ * Initial attribute value
262
+ */
263
+ start: T;
264
+ /**
265
+ * Final attribute value, uses initial value if not specified
266
+ */
267
+ end?: T;
268
+ /**
269
+ * Attribute change rate, automatically calculated from start and end if not specified
270
+ */
271
+ delta?: T;
272
+ }
273
+ /**
274
+ * Particle system configuration options
275
+ */
276
+ export interface IParticleOptions extends ITransformOptions, IShaderRenderOptions {
277
+ /**
278
+ * Particle texture, can be a single texture, array of textures, or weighted texture array
279
+ */
280
+ texture: Texture | Texture[] | [Texture, number][];
281
+ /**
282
+ * Particle emission rate (particles per second)
283
+ */
284
+ emitRate?: number;
285
+ /**
286
+ * Emission time interval in seconds
287
+ */
288
+ emitTime?: number;
289
+ /**
290
+ * Maximum number of particles limit
291
+ */
292
+ maxParticles?: number;
293
+ /**
294
+ * Particle lifetime in seconds, can be fixed value or range
295
+ */
296
+ life?: number | [number, number];
297
+ /**
298
+ * Particle animation properties collection
299
+ */
300
+ animation: {
301
+ /**
302
+ * Velocity vector, controls particle movement direction and speed
303
+ */
304
+ velocity?: ParticleAttribute<Vec2>;
305
+ /**
306
+ * Acceleration vector, controls particle velocity changes
307
+ */
308
+ acceleration?: ParticleAttribute<Vec2>;
309
+ /**
310
+ * Speed scalar, used in combination with rotation direction
311
+ */
312
+ speed?: ParticleAttribute<number>;
313
+ /**
314
+ * Scale factor, controls particle size
315
+ */
316
+ scale?: ParticleAttribute<number>;
317
+ /**
318
+ * Rotation angle (in radians)
319
+ */
320
+ rotation?: ParticleAttribute<number>;
321
+ /**
322
+ * Color and transparency
323
+ */
324
+ color?: ParticleAttribute<Color>;
325
+ };
326
+ /**
327
+ * Emitter shape
328
+ */
329
+ emitShape?: ParticleShape;
330
+ /**
331
+ * Circular emitter radius
332
+ */
333
+ emitRadius?: number;
334
+ /**
335
+ * Rectangular emitter dimensions
336
+ */
337
+ emitRect?: {
338
+ width: number;
339
+ height: number;
340
+ };
341
+ /**
342
+ * Whether to use local coordinate system, true means particles are relative to emitter position,
343
+ * false means using global coordinates
344
+ */
345
+ localSpace?: boolean;
346
+ }
347
+ /**
348
+ * Particle attribute data types
349
+ */
350
+ export type ParticleAttributeTypes = number | Vec2 | Color;
351
+ /**
352
+ * Particle attribute runtime data
353
+ * @template T Attribute type
354
+ */
355
+ export type ParticleAttributeData<T extends ParticleAttributeTypes> = {
356
+ /**
357
+ * Attribute change rate per second
358
+ */
359
+ delta?: T;
360
+ /**
361
+ * Current attribute value
362
+ */
363
+ value: T;
364
+ /**
365
+ * Damping coefficient
366
+ */
367
+ damping?: number;
368
+ };
369
+ export interface IParticleEmitterOptions extends IParticleOptions, IEntityTransformOptions {
370
+ }
371
+ export interface IGameOptions extends IRapidOptions {
372
+ }
373
+ /**
374
+ * Interface for an asset to be loaded.
375
+ */
376
+ export interface IAsset {
377
+ type: 'json' | 'audio' | 'image';
378
+ name: string;
379
+ url: string;
380
+ }
381
+ /**
382
+ * Interface for the assets storage structure.
383
+ */
384
+ export interface IAssets {
385
+ json: {
386
+ [key: string]: any;
387
+ };
388
+ audio: {
389
+ [key: string]: AudioPlayer;
390
+ };
391
+ images: {
392
+ [key: string]: Texture;
393
+ };
394
+ }
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
+ }
@@ -0,0 +1,7 @@
1
+ import { Vec2 } from './math';
2
+ import { default as Rapid } from './render';
3
+ export declare class LightManager {
4
+ render: Rapid;
5
+ constructor(render: Rapid);
6
+ createLightShadowMaskPolygon(occlusion: Vec2[][], lightSource: Vec2, baseProjectionLength?: number): Vec2[][];
7
+ }
package/dist/line.d.ts CHANGED
@@ -1,3 +1,16 @@
1
- import { ILineOptions } from "./interface";
2
- import { Vec2 } from "./math";
3
- export declare const getStrokeGeometry: (points: Vec2[], attrs: ILineOptions) => Vec2[];
1
+ import { ILineStyleOptions } from './interface';
2
+ import { Vec2 } from './math';
3
+ /**
4
+ * @ignore
5
+ */
6
+ export declare const getLineNormal: (points: Vec2[], closed?: boolean) => {
7
+ normals: {
8
+ normal: Vec2;
9
+ miters: number;
10
+ }[];
11
+ length: number;
12
+ };
13
+ export declare const getLineGeometry: (options: ILineStyleOptions) => {
14
+ vertices: Vec2[];
15
+ uv: Vec2[];
16
+ };
package/dist/log.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ declare const warn: (text: string) => void;
2
+ export default warn;