@rpgjs/client 3.3.2 → 4.0.0-beta.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.
Files changed (164) hide show
  1. package/LICENSE +19 -0
  2. package/lib/Components/AbstractComponent.d.ts +3 -2
  3. package/lib/Components/AbstractComponent.js +17 -28
  4. package/lib/Components/AbstractComponent.js.map +1 -1
  5. package/lib/Components/BarComponent.d.ts +2 -1
  6. package/lib/Components/BarComponent.js +32 -33
  7. package/lib/Components/BarComponent.js.map +1 -1
  8. package/lib/Components/Component.d.ts +3 -3
  9. package/lib/Components/Component.js +84 -96
  10. package/lib/Components/Component.js.map +1 -1
  11. package/lib/Components/DebugComponent.d.ts +2 -1
  12. package/lib/Components/DebugComponent.js +9 -10
  13. package/lib/Components/DebugComponent.js.map +1 -1
  14. package/lib/Components/ImageComponent.d.ts +2 -1
  15. package/lib/Components/ImageComponent.js +5 -7
  16. package/lib/Components/ImageComponent.js.map +1 -1
  17. package/lib/Components/ShapeComponent.d.ts +2 -1
  18. package/lib/Components/ShapeComponent.js +14 -14
  19. package/lib/Components/ShapeComponent.js.map +1 -1
  20. package/lib/Components/TextComponent.d.ts +3 -2
  21. package/lib/Components/TextComponent.js +10 -9
  22. package/lib/Components/TextComponent.js.map +1 -1
  23. package/lib/Components/TileComponent.d.ts +2 -1
  24. package/lib/Components/TileComponent.js +12 -16
  25. package/lib/Components/TileComponent.js.map +1 -1
  26. package/lib/Effects/Animation.d.ts +10 -9
  27. package/lib/Effects/Animation.js +36 -36
  28. package/lib/Effects/Animation.js.map +1 -1
  29. package/lib/Effects/AnimationCharacter.js +2 -5
  30. package/lib/Effects/AnimationCharacter.js.map +1 -1
  31. package/lib/Effects/Spinner.d.ts +3 -2
  32. package/lib/Effects/Spinner.js +2 -5
  33. package/lib/Effects/Spinner.js.map +1 -1
  34. package/lib/Effects/Timeline.d.ts +1 -1
  35. package/lib/Effects/Timeline.js +6 -10
  36. package/lib/Effects/Timeline.js.map +1 -1
  37. package/lib/Effects/TransitionScene.d.ts +2 -1
  38. package/lib/Effects/TransitionScene.js +3 -7
  39. package/lib/Effects/TransitionScene.js.map +1 -1
  40. package/lib/GameEngine.js +51 -26
  41. package/lib/GameEngine.js.map +1 -1
  42. package/lib/Interfaces/Character.js +1 -2
  43. package/lib/Interfaces/Scene.js +1 -2
  44. package/lib/KeyboardControls.d.ts +2 -1
  45. package/lib/KeyboardControls.js +47 -45
  46. package/lib/KeyboardControls.js.map +1 -1
  47. package/lib/Logger.js +1 -5
  48. package/lib/Logger.js.map +1 -1
  49. package/lib/Presets/AnimationSpritesheet.js +9 -13
  50. package/lib/Presets/AnimationSpritesheet.js.map +1 -1
  51. package/lib/Presets/Scene.js +2 -5
  52. package/lib/Presets/Scene.js.map +1 -1
  53. package/lib/Renderer.d.ts +5 -3
  54. package/lib/Renderer.js +68 -60
  55. package/lib/Renderer.js.map +1 -1
  56. package/lib/Resources.js +1 -5
  57. package/lib/Resources.js.map +1 -1
  58. package/lib/RpgClient.d.ts +3 -2
  59. package/lib/RpgClient.js +1 -2
  60. package/lib/RpgClientEngine.d.ts +2 -1
  61. package/lib/RpgClientEngine.js +192 -176
  62. package/lib/RpgClientEngine.js.map +1 -1
  63. package/lib/RpgGui.js +40 -41
  64. package/lib/RpgGui.js.map +1 -1
  65. package/lib/Scene/EventLayer.d.ts +4 -0
  66. package/lib/Scene/EventLayer.js +8 -0
  67. package/lib/Scene/EventLayer.js.map +1 -0
  68. package/lib/Scene/Map.d.ts +17 -3
  69. package/lib/Scene/Map.js +157 -113
  70. package/lib/Scene/Map.js.map +1 -1
  71. package/lib/Scene/Scene.d.ts +3 -3
  72. package/lib/Scene/Scene.js +24 -20
  73. package/lib/Scene/Scene.js.map +1 -1
  74. package/lib/Scene/SceneData.js +1 -5
  75. package/lib/Scene/SceneData.js.map +1 -1
  76. package/lib/Sound/RpgSound.js +8 -11
  77. package/lib/Sound/RpgSound.js.map +1 -1
  78. package/lib/Sound/Sound.d.ts +2 -2
  79. package/lib/Sound/Sound.js +1 -5
  80. package/lib/Sound/Sound.js.map +1 -1
  81. package/lib/Sound/Sounds.js +4 -8
  82. package/lib/Sound/Sounds.js.map +1 -1
  83. package/lib/Sprite/Character.d.ts +3 -2
  84. package/lib/Sprite/Character.js +15 -16
  85. package/lib/Sprite/Character.js.map +1 -1
  86. package/lib/Sprite/Player.js +2 -9
  87. package/lib/Sprite/Player.js.map +1 -1
  88. package/lib/Sprite/Spritesheet.d.ts +4 -4
  89. package/lib/Sprite/Spritesheet.js +1 -5
  90. package/lib/Sprite/Spritesheet.js.map +1 -1
  91. package/lib/Sprite/Spritesheets.js +4 -8
  92. package/lib/Sprite/Spritesheets.js.map +1 -1
  93. package/lib/Tilemap/CommonLayer.d.ts +2 -1
  94. package/lib/Tilemap/CommonLayer.js +7 -11
  95. package/lib/Tilemap/CommonLayer.js.map +1 -1
  96. package/lib/Tilemap/ImageLayer.js +5 -7
  97. package/lib/Tilemap/ImageLayer.js.map +1 -1
  98. package/lib/Tilemap/Tile.d.ts +5 -5
  99. package/lib/Tilemap/Tile.js +19 -21
  100. package/lib/Tilemap/Tile.js.map +1 -1
  101. package/lib/Tilemap/TileLayer.d.ts +0 -7
  102. package/lib/Tilemap/TileLayer.js +27 -29
  103. package/lib/Tilemap/TileLayer.js.map +1 -1
  104. package/lib/Tilemap/TileSet.d.ts +2 -1
  105. package/lib/Tilemap/TileSet.js +9 -12
  106. package/lib/Tilemap/TileSet.js.map +1 -1
  107. package/lib/Tilemap/index.d.ts +3 -11
  108. package/lib/Tilemap/index.js +22 -61
  109. package/lib/Tilemap/index.js.map +1 -1
  110. package/lib/clientEntryPoint.js +26 -28
  111. package/lib/clientEntryPoint.js.map +1 -1
  112. package/lib/index.js +21 -75
  113. package/lib/index.js.map +1 -1
  114. package/package.json +19 -19
  115. package/src/Components/AbstractComponent.ts +120 -0
  116. package/src/Components/BarComponent.ts +179 -0
  117. package/src/Components/Component.ts +506 -0
  118. package/src/Components/DebugComponent.ts +36 -0
  119. package/src/Components/ImageComponent.ts +30 -0
  120. package/src/Components/ShapeComponent.ts +64 -0
  121. package/src/Components/TextComponent.ts +33 -0
  122. package/src/Components/TileComponent.ts +43 -0
  123. package/src/Effects/Animation.ts +297 -0
  124. package/src/Effects/AnimationCharacter.ts +7 -0
  125. package/src/Effects/Spinner.ts +19 -0
  126. package/src/Effects/Timeline.ts +294 -0
  127. package/src/Effects/TransitionScene.ts +57 -0
  128. package/src/GameEngine.ts +284 -0
  129. package/src/Interfaces/Character.ts +7 -0
  130. package/src/Interfaces/Scene.ts +9 -0
  131. package/src/KeyboardControls.ts +552 -0
  132. package/src/Logger.ts +3 -0
  133. package/src/Presets/AnimationSpritesheet.ts +36 -0
  134. package/src/Presets/Scene.ts +3 -0
  135. package/src/Renderer.ts +263 -0
  136. package/src/Resources.ts +40 -0
  137. package/src/RpgClient.ts +333 -0
  138. package/src/RpgClientEngine.ts +709 -0
  139. package/src/RpgGui.ts +553 -0
  140. package/src/RpgGuiCompiled.ts +43 -0
  141. package/src/Scene/EventLayer.ts +9 -0
  142. package/src/Scene/Map.ts +393 -0
  143. package/src/Scene/Scene.ts +270 -0
  144. package/src/Scene/SceneData.ts +13 -0
  145. package/src/Sound/RpgSound.ts +50 -0
  146. package/src/Sound/Sound.ts +91 -0
  147. package/src/Sound/Sounds.ts +7 -0
  148. package/src/Sprite/Character.ts +149 -0
  149. package/src/Sprite/Player.ts +3 -0
  150. package/src/Sprite/Spritesheet.ts +392 -0
  151. package/src/Sprite/Spritesheets.ts +8 -0
  152. package/src/Tilemap/CommonLayer.ts +20 -0
  153. package/src/Tilemap/ImageLayer.ts +20 -0
  154. package/src/Tilemap/Tile.ts +80 -0
  155. package/src/Tilemap/TileLayer.ts +142 -0
  156. package/src/Tilemap/TileSet.ts +40 -0
  157. package/src/Tilemap/index.ts +173 -0
  158. package/src/clientEntryPoint.ts +141 -0
  159. package/src/index.ts +25 -0
  160. package/src/types/howler.d.ts +73 -0
  161. package/tsconfig.json +30 -0
  162. package/lib/Components/ColorComponent.d.ts +0 -9
  163. package/lib/Components/ColorComponent.js +0 -18
  164. package/lib/Components/ColorComponent.js.map +0 -1
@@ -0,0 +1,50 @@
1
+ import { Howl, Howler } from 'howler'
2
+ import { log } from '../Logger'
3
+ import { sounds } from './Sounds'
4
+
5
+ class RpgSoundClass {
6
+ private sounds: Map<string, Howl> = new Map()
7
+
8
+ get(id: string): Howl {
9
+ if (this.sounds.has(id)) {
10
+ return this.sounds.get(id) as Howl
11
+ }
12
+ const resource = sounds.get(id)
13
+ if (!resource) {
14
+ throw log(`Impossible to find the ${id} sound. Did you put the right name or create the sound?`)
15
+ }
16
+ const howl = new Howl({
17
+ src: [resource.sound],
18
+ loop: resource.loop,
19
+ autoplay: resource.autoplay,
20
+ volume: resource.volume,
21
+ sprite: resource.sprite
22
+ })
23
+ this.sounds.set(id, howl)
24
+ return howl
25
+ }
26
+
27
+ stop(id: string) {
28
+ this.get(id).stop()
29
+ }
30
+
31
+ play(id: string): boolean {
32
+ const sound = this.get(id)
33
+ if (!sound.playing()) {
34
+ sound.play()
35
+ return true
36
+ }
37
+ return false
38
+ }
39
+
40
+ clear() {
41
+ this.sounds.clear()
42
+ this.global.stop()
43
+ }
44
+
45
+ get global(): typeof Howler {
46
+ return Howler
47
+ }
48
+ }
49
+
50
+ export const RpgSound = new RpgSoundClass()
@@ -0,0 +1,91 @@
1
+ interface SoundOptions {
2
+ /**
3
+ * The link to the sound
4
+ *
5
+ * > Do not use the `images` property
6
+ * > Remember to wrap the link to the image with the `require` function.
7
+ *
8
+ * ```ts
9
+ * sound: require('./assets/sound.ogg')
10
+ * ```
11
+ *
12
+ * @prop {string} [sound]
13
+ * @memberof Sound
14
+ * */
15
+ sound?: string
16
+
17
+ /**
18
+ * The sound will restart at the beginning when it is finished.
19
+ *
20
+ * @prop {boolean} [loop]
21
+ * @memberof Sound
22
+ * */
23
+ loop?: boolean,
24
+
25
+ /**
26
+ * Volume
27
+ *
28
+ * @prop {number} [volume]
29
+ * @memberof Sound
30
+ * */
31
+ volume?: number
32
+ }
33
+
34
+ type SoundIdOptions = SoundOptions & {
35
+ /**
36
+ * Sound identifier.
37
+ *
38
+ * > Do not use the `sounds` property
39
+ *
40
+ * @prop {string} [id]
41
+ * @memberof Sound
42
+ * */
43
+ id: string
44
+
45
+ /**
46
+ * The link to the sound
47
+ *
48
+ * > Do not use the `images` property
49
+ * > Remember to wrap the link to the image with the `require` function.
50
+ *
51
+ * ```ts
52
+ * sound: require('./assets/sound.ogg')
53
+ * ```
54
+ *
55
+ * @prop {string} [sound]
56
+ * @memberof Sound
57
+ * */
58
+ sound: string
59
+ }
60
+ type SoundIdsOptions = SoundOptions & {
61
+ /**
62
+ * Put the different sounds that are concerned by the properties below.
63
+ * The key of the object is the identifier of the sound and the value and the link to the sound.
64
+ *
65
+ * > Remember to wrap the link to the sound with the `require` function.
66
+ *
67
+ * ```ts
68
+ * sounds: {
69
+ * hero: require('./assets/sound.ogg')
70
+ * }
71
+ * ```
72
+ *
73
+ * @prop { { [id: string]: string } } [sounds]
74
+ * @memberof Sound
75
+ * */
76
+ sounds?: {
77
+ [id: string]: string
78
+ }
79
+ }
80
+
81
+ export function Sound(options: SoundIdOptions)
82
+ export function Sound(options: SoundIdsOptions)
83
+ export function Sound(options: SoundIdOptions | SoundIdsOptions) {
84
+ return (target: Function) => {
85
+ if ('sounds' in options) target['sounds'] = options.sounds
86
+ if ('id' in options) target['id'] = options.id
87
+ for (let key in options) {
88
+ target.prototype[key] = options[key]
89
+ }
90
+ }
91
+ }
@@ -0,0 +1,7 @@
1
+ import { _initResource } from '../Resources'
2
+
3
+ export const sounds = new Map()
4
+
5
+ export function _initSound(_sounds, engine) {
6
+ return _initResource(sounds, _sounds, 'sound', engine)
7
+ }
@@ -0,0 +1,149 @@
1
+ import { Utils, RpgPlugin, HookClient } from '@rpgjs/common'
2
+ import { spritesheets } from './Spritesheets'
3
+ import { Animation } from '../Effects/Animation'
4
+ import { Animation as AnimationEnum } from '../Effects/AnimationCharacter'
5
+ import { RpgComponent } from '../Components/Component'
6
+ import { TransformOptions } from './Spritesheet'
7
+ import { Sprite } from 'pixi.js'
8
+
9
+ const { capitalize } = Utils
10
+
11
+ export default class Character extends Sprite {
12
+ static readonly id: string = 'graphic'
13
+
14
+ private spritesheet: any
15
+ private playStandardAnimation: boolean = true
16
+ public animation: Animation
17
+ private objSaved: object = {}
18
+ private data: any = {}
19
+
20
+ /** @internal */
21
+ h: number = 1
22
+ /** @internal */
23
+ w: number = 1
24
+
25
+ /** @internal */
26
+ anim: Animation
27
+
28
+ constructor(private component: RpgComponent, private graphic: string) {
29
+ super()
30
+ this.data = component.logic
31
+ this.setGraphic(graphic)
32
+ }
33
+
34
+ /** @internal */
35
+ showAnimation(graphic: string | string[], animationName: string): Animation {
36
+ const refreshAnimation = (graphic: string) => {
37
+ this.removeChild(this.animation)
38
+ this.animation = new Animation(graphic)
39
+ this.addChild(this.animation)
40
+ this.setAnimationAnchor()
41
+ }
42
+ const memoryGraphic = this.graphic
43
+ let graphicId: string = ''
44
+
45
+ if (Utils.isArray(graphic)) {
46
+ graphicId = (graphic as string[]).find(id => id == this.graphic) as string
47
+ }
48
+ else {
49
+ graphicId = graphic as string
50
+ }
51
+
52
+ refreshAnimation(graphicId)
53
+
54
+ this.animation.onFinish = () => {
55
+ this.playStandardAnimation = true
56
+ refreshAnimation(memoryGraphic)
57
+ this.update(this.objSaved)
58
+ }
59
+
60
+ this.playStandardAnimation = false
61
+ this.playAnimation(animationName)
62
+ return this.animation
63
+ }
64
+
65
+ /** @internal */
66
+ setGraphic(graphic: string) {
67
+ (this.children as Animation[]).forEach((graphic: Animation, index: number) => {
68
+ if (graphic.id == this.graphic) {
69
+ this.removeChildAt(index)
70
+ }
71
+ })
72
+ this.graphic = graphic
73
+ this.spritesheet = spritesheets.get(this.graphic)
74
+ this.animation = new Animation(this.graphic)
75
+ this.addChild(this.animation)
76
+ this.setAnimationAnchor()
77
+ }
78
+
79
+ getGraphicHeight(): number {
80
+ return this.animation.getSpriteHeight()
81
+ }
82
+
83
+ getGraphicWidth(): number {
84
+ return this.animation.getSpriteWidth()
85
+ }
86
+
87
+ animationSprite() {
88
+ return this.animation.animation$
89
+ }
90
+
91
+ private setAnimationAnchor(): void {
92
+ this.animation.hitbox = { h: this.data.hHitbox, w: this.data.wHitbox }
93
+ this.animation.applyTransform = (frame, animation, spritesheet) => {
94
+ const { spriteWidth, spriteHeight } = animation
95
+ const prop: keyof TransformOptions = 'spriteRealSize'
96
+ const currentAnchor = frame[prop] || animation[prop] || spritesheet[prop]
97
+ if (currentAnchor) {
98
+ return {}
99
+ }
100
+ return {
101
+ spriteRealSize: {
102
+ width: spriteWidth,
103
+ height: spriteHeight
104
+ }
105
+ }
106
+ }
107
+ }
108
+
109
+ /** @internal */
110
+ update(obj, options: any = {}, deltaRatio: number = 1): {
111
+ moving: boolean
112
+ instance: Character
113
+ } {
114
+ const { moving } = options
115
+ this.data = obj
116
+ if (this.anim) this.anim.update(deltaRatio)
117
+ if (this.animation) this.animation.update(deltaRatio)
118
+
119
+ if (this.playStandardAnimation) {
120
+ if (moving) {
121
+ RpgPlugin.emit(HookClient.SpriteMove, this)
122
+ this.playAnimation(AnimationEnum.Walk)
123
+ }
124
+ else {
125
+ this.playAnimation(AnimationEnum.Stand)
126
+ }
127
+ }
128
+
129
+ this.objSaved = obj
130
+
131
+ return {
132
+ moving,
133
+ instance: this
134
+ }
135
+ }
136
+
137
+ /** @internal */
138
+ playAnimation(name: string) {
139
+ const hook = `onCharacter${capitalize(name)}`
140
+ if (!this.spritesheet) return
141
+ if (this.spritesheet[hook]) {
142
+ this.spritesheet[hook](this)
143
+ }
144
+ else if (this.animation.has(name)) {
145
+ this.animation.play(name, [this.data.direction])
146
+ }
147
+ }
148
+
149
+ }
@@ -0,0 +1,3 @@
1
+ import Character from './Character'
2
+
3
+ export class RpgSprite extends Character {}
@@ -0,0 +1,392 @@
1
+ import { constructor } from "@rpgjs/types"
2
+ import { ClassDeclaration } from "typescript"
3
+
4
+ export interface TransformOptions {
5
+ /**
6
+ * The global value of opacity (between 0 and 1)
7
+ *
8
+ * @prop {number} [opacity]
9
+ * @memberof Spritesheet
10
+ * */
11
+ opacity?: number
12
+ /**
13
+ * The global value of pivot.
14
+ *
15
+ * Invariant under rotation, scaling, and skewing. The projection of into the parent's space of the pivot is equal to position, regardless of the other three transformations. In other words, It is the center of rotation, scaling, and skewing.
16
+ *
17
+ * The array has two elements: [x, y]. If y is omitted, both x and y will be set to x.
18
+ *
19
+ * ```ts
20
+ * pivot: [0.5, 0.8]
21
+ * ```
22
+ *
23
+ * @prop {Array<number>} [pivot]
24
+ * @memberof Spritesheet
25
+ * */
26
+ pivot?: number[]
27
+
28
+ /**
29
+ * The global value of anchor.
30
+ *
31
+ * Position of the origin point
32
+ *
33
+ * The array has two elements: [x, y]. If y is omitted, both x and y will be set to x.
34
+ *
35
+ * ```ts
36
+ * anchor: [0.5, 0.8]
37
+ * ```
38
+ *
39
+ * @prop {Array<number>} [anchor]
40
+ * @memberof Spritesheet
41
+ * */
42
+ anchor?: number[]
43
+
44
+ /**
45
+ * Defines the actual size of the sprite that is inside a larger rectangle.
46
+ * For example, if the texture rectangle is 192x192 while the character, which is in the center, is only 64x64 then set `spriteRealSize: 64`. This way the character will be well positioned in relation to the animations that have a different rectangle
47
+ *
48
+ * > You can also put `spriteRealSize: { width: 64, height: 64 }` but be aware that the width is not concerned because it will always be centered while the height depends on the hitbox
49
+ *
50
+ * @prop {{ width: number, height: number } | number} [spriteRealSize]
51
+ * @since 3.2.0
52
+ * @memberof Spritesheet
53
+ * */
54
+ spriteRealSize?: { width: number, height: number } | number
55
+
56
+ /**
57
+ * The global value of rotation
58
+ *
59
+ * Rotation. This will rotate the display object's projection by this angle (in radians).
60
+ *
61
+ * @prop {number} [angle]
62
+ * @memberof Spritesheet
63
+ * */
64
+ angle?: number
65
+
66
+ /**
67
+ * The global value of rotation
68
+ *
69
+ * Rotation. This is an alias for rotation, but in degrees.
70
+ *
71
+ * @prop {number} [rotation]
72
+ * @memberof Spritesheet
73
+ * */
74
+ rotation?: number
75
+
76
+ /**
77
+ * The global value of scale.
78
+ *
79
+ * Scaling. This will stretch (or compress) the display object's projection. The scale factors are along the local coordinate axes. In other words, the display object is scaled before rotated or skewed. The center of scaling is the pivot.
80
+ *
81
+ * The array has two elements: [x, y]. If y is omitted, both x and y will be set to x.
82
+ *
83
+ * ```ts
84
+ * scale: [0.5, 0.8]
85
+ * ```
86
+ *
87
+ * @prop {Array<number>} [scale]
88
+ * @memberof Spritesheet
89
+ * */
90
+ scale?: number[]
91
+
92
+ /**
93
+ * The global value of skew.
94
+ *
95
+ * Skewing. This can be used to deform a rectangular display object into a parallelogram.
96
+ *
97
+ * In PixiJS, skew has a slightly different behaviour than the conventional meaning. It can be thought of the net rotation applied to the coordinate axes (separately). For example, if "skew.x" is ⍺ and "skew.y" is β, then the line x = 0 will be rotated by ⍺ (y = -x*cot⍺) and the line y = 0 will be rotated by β (y = x*tanβ). A line y = x*tanϴ (i.e. a line at angle ϴ to the x-axis in local-space) will be rotated by an angle between ⍺ and β.
98
+ *
99
+ * It can be observed that if skew is applied equally to both axes, then it will be equivalent to applying a rotation. Indeed, if "skew.x" = -ϴ and "skew.y" = ϴ, it will produce an equivalent of "rotation" = ϴ.
100
+ *
101
+ * Another quite interesting observation is that "skew.x", "skew.y", rotation are communtative operations. Indeed, because rotation is essentially a careful combination of the two.
102
+ *
103
+ * The array has two elements: [x, y]. If y is omitted, both x and y will be set to x.
104
+ *
105
+ * @prop {Array<number>} [skew]
106
+ * @memberof Spritesheet
107
+ * */
108
+ skew?: number[]
109
+
110
+ /**
111
+ * The global value of X translation
112
+ *
113
+ * @prop {number} [x]
114
+ * @memberof Spritesheet
115
+ * */
116
+ x?: number
117
+
118
+ /**
119
+ * The global value of Y translation
120
+ *
121
+ * @prop {number} [y]
122
+ * @memberof Spritesheet
123
+ * */
124
+ y?: number
125
+
126
+ /**
127
+ * The global value of visible
128
+ *
129
+ * @prop {boolean} [visible]
130
+ * @memberof Spritesheet
131
+ * */
132
+ visible?: boolean
133
+
134
+ /**
135
+ * Define the sound that will be played for all animations in the spritesheet. Remember to create the sound before with the @Sound decorator
136
+ *
137
+ * @prop {string} [sound]
138
+ * @memberof Spritesheet
139
+ * */
140
+ sound?: string
141
+ }
142
+
143
+ export interface FrameOptions extends TransformOptions {
144
+ time: number
145
+ frameX?: number
146
+ frameY?: number
147
+ }
148
+
149
+ export interface TextureOptions {
150
+ /**
151
+ * The number of frames on the width
152
+ *
153
+ * @prop {number} framesWidth
154
+ * @memberof Spritesheet
155
+ * */
156
+ framesWidth?: number,
157
+
158
+ /**
159
+ * The number of frames on the height
160
+ *
161
+ * @prop {number} framesHeight
162
+ * @memberof Spritesheet
163
+ * */
164
+ framesHeight?: number,
165
+
166
+ /**
167
+ * The width of the image (in pixels)
168
+ *
169
+ * @prop {number} width
170
+ * @memberof Spritesheet
171
+ * */
172
+ width?: number,
173
+
174
+ /**
175
+ * The height of the image (in pixels)
176
+ *
177
+ * @prop {number} height
178
+ * @memberof Spritesheet
179
+ * */
180
+ height?: number
181
+
182
+ /**
183
+ * Takes a width of a rectangle in the image. Equivalent to `width / framesWidth`
184
+ *
185
+ * @prop {number} [rectWidth]
186
+ * @memberof Spritesheet
187
+ * */
188
+ rectWidth?: number,
189
+
190
+ /**
191
+ * Takes a height of a rectangle in the image. Equivalent to `height / framesHeight`
192
+ *
193
+ * @prop {number} [rectHeight]
194
+ * @memberof Spritesheet
195
+ * */
196
+ rectHeight?: number,
197
+
198
+ /**
199
+ * To take the texture, start at a well defined X and Y position. Otherwise, it starts at 0,0
200
+ *
201
+ * @prop {number} [offset]
202
+ * @memberof Spritesheet
203
+ * */
204
+ offset?: { x: number, y: number }
205
+ }
206
+
207
+ export type AnimationFrames = FrameOptions[][] | ((...args: any) => FrameOptions[][])
208
+
209
+ export interface TexturesOptions extends TextureOptions, TransformOptions {
210
+ animations: AnimationFrames
211
+ }
212
+
213
+ export interface SpritesheetOptions extends TransformOptions, TextureOptions {
214
+ /**
215
+ * Object containing all animations.
216
+ * The key to the object is the name of the animation. The value is a two-dimensional array
217
+ *
218
+ * ```ts
219
+ * textures: {
220
+ * myanim: {
221
+ * animations: [
222
+ * [ { time: 0, frameX: 0, frameY: 0 } ]
223
+ * ]
224
+ * }
225
+ * }
226
+ * ```
227
+ *
228
+ * The first array represents an animation group. You can put several of them together to create an animation cluster. For example, several explosions with the same spritesheet
229
+ * The second array represents the animation itself which will animate over time. The object indicates, over a period of time (in frame), which part of the spritesheet will be taken (`frameX`, `frameY`)
230
+ *
231
+ * Here are the properties:
232
+ *
233
+ * * `time`: Time in frame
234
+ * * `frameX`: Retrieve a frame from the spritesheet on the X-axis
235
+ * * `frameY`: Retrieve a frame from the spritesheet on the Y-axis
236
+ * * `opacity`
237
+ * * `pivot`
238
+ * * `anchor`
239
+ * * `rotation`
240
+ * * `angle`
241
+ * * `scale`
242
+ * * `skew`
243
+ * * `x`
244
+ * * `y`
245
+ * * `visible`
246
+ * * `sound`: The sound that will be played during the frame
247
+ *
248
+ * ---
249
+ * **Extract Animation of Spritesheet**
250
+ *
251
+ * Sometimes the animation is part of the image
252
+ *
253
+ * ```ts
254
+ * textures: {
255
+ * myanim: {
256
+ * rectWidth: 64,
257
+ * rectHeight: 64,
258
+ * framesWidth: 10,
259
+ * framesHeight: 2,
260
+ * offset: {x: 0, y: 230},
261
+ * sound: 'my-sound-id', // You can put a sound just for the animation
262
+ * animations: [
263
+ * [ { time: 0, frameX: 0, frameY: 0 } ]
264
+ * ]
265
+ * }
266
+ * }
267
+ * ```
268
+ *
269
+ * Above, we can specify which part we want to recover
270
+ *
271
+ * 1. We go to the position {0, 230} of the image (`offset`)
272
+ * 2. We recover cells of 64px (`rectWidth` and `rectHeight`)
273
+ * 3. And we get 20 cells (10 on the width, 2 on the height) (`frameX` and `frameY`)
274
+ *
275
+ * ---
276
+ *
277
+ * **Advanced**
278
+ *
279
+ * You can create an animation that will be linked to a data. For example, different animation according to a direction of the character.
280
+ *
281
+ * Full example:
282
+ *
283
+ * ```ts
284
+ * import { Spritesheet, Animation, Direction } from '@rpgjs/client'
285
+ *
286
+ * @Spritesheet({
287
+ * id: 'chest',
288
+ * image: require('./assets/chest.png'),
289
+ * width: 124,
290
+ * height: 61,
291
+ * framesHeight: 2,
292
+ * framesWidth: 4,
293
+ * textures: {
294
+ * [Animation.Stand]: {
295
+ * animations: direction => [[ {time: 0, frameX: 3, frameY: direction == Direction.Up ? 0 : 1 } ]]
296
+ * }
297
+ * })
298
+ * })
299
+ * export class Chest { }
300
+ * ```
301
+ *
302
+ * > It is important to know that `Animation.Stand` animation is called if it exists. it only works in the case of an event that doesn't move. The direction is then sent
303
+ *
304
+ * As you can see, the property contains a function that returns the array for the animation. Here, it is the direction but the parameters depend on the call of the animation. Example:
305
+ *
306
+ * ```ts
307
+ * import { Spritesheet, Animation, Direction, RpgSprite, ISpriteCharacter } from '@rpgjs/client'
308
+ *
309
+ * @Spritesheet({
310
+ * id: 'chest',
311
+ * image: require('./assets/chest.png'),
312
+ * width: 124,
313
+ * height: 61,
314
+ * framesHeight: 2,
315
+ * framesWidth: 4,
316
+ * textures: {
317
+ * [Animation.Stand]: {
318
+ * animations: str => [[ {time: 0, frameX: 3, frameY: str == 'hello' ? 0 : 1 } ]]
319
+ * }
320
+ * }
321
+ * })
322
+ * export class Chest implements ISpriteCharacter {
323
+ * onCharacterStand(sprite: RpgSprite) {
324
+ * sprite.animation.play(Animation.Stand, ['hello'])
325
+ * }
326
+ * }
327
+ * ```
328
+ *
329
+ * @prop { { [animName: string]: { animations: Array<Array<FrameOptions>> | Function, ...other } } } [textures]
330
+ * @memberof Spritesheet
331
+ * */
332
+ textures?: {
333
+ [animationName: string]: Partial<TexturesOptions> & Pick<TexturesOptions, 'animations'>
334
+ }
335
+ }
336
+
337
+ type SpritesheetImageOptions = SpritesheetOptions & {
338
+ /**
339
+ * The link to the image
340
+ *
341
+ * > Do not use the `images` property
342
+ * > Remember to wrap the link to the image with the `require` function.
343
+ *
344
+ * ```ts
345
+ * image: require('./assets/hero.png')
346
+ * ```
347
+ *
348
+ * @prop {string} [image]
349
+ * @memberof Spritesheet
350
+ * */
351
+ image: string,
352
+ /**
353
+ * Spritesheet identifier.
354
+ *
355
+ * > Do not use the `images` property
356
+ *
357
+ * @prop {string} [id]
358
+ * @memberof Spritesheet
359
+ * */
360
+ id: string
361
+ }
362
+ type SpritesheetImagesOptions = SpritesheetOptions & {
363
+ /**
364
+ * Put the different images that are concerned by the properties below.
365
+ * The key of the object is the identifier of the spritesheet and the value and the link to the image.
366
+ *
367
+ * > Remember to wrap the link to the image with the `require` function.
368
+ *
369
+ * ```ts
370
+ * images: {
371
+ * hero: require('./assets/hero.png')
372
+ * }
373
+ * ```
374
+ *
375
+ * @prop { { [id: string]: string } } [images]
376
+ * @memberof Spritesheet
377
+ * */
378
+ images?: { [id: string]: string }
379
+ }
380
+
381
+ export function Spritesheet(options: SpritesheetImageOptions)
382
+ export function Spritesheet(options: SpritesheetImagesOptions)
383
+ export function Spritesheet(options: SpritesheetImageOptions | SpritesheetImagesOptions) {
384
+ return (target: Function) => {
385
+ if ('images' in options) target['images'] = options.images
386
+ if ('id' in options) target['id'] = options.id
387
+ for (let key in options) {
388
+ target.prototype[key] = options[key]
389
+ }
390
+ return
391
+ }
392
+ }
@@ -0,0 +1,8 @@
1
+ import { RpgClientEngine } from '..'
2
+ import { _initResource } from '../Resources'
3
+
4
+ export const spritesheets: Map<string, any> = new Map()
5
+
6
+ export function _initSpritesheet(_spritesheets, engine: RpgClientEngine) {
7
+ return _initResource(spritesheets, _spritesheets, 'image', engine)
8
+ }