@rpgjs/client 4.0.0-beta.1 → 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 (48) hide show
  1. package/package.json +8 -10
  2. package/src/Components/AbstractComponent.ts +120 -0
  3. package/src/Components/BarComponent.ts +179 -0
  4. package/src/Components/Component.ts +506 -0
  5. package/src/Components/DebugComponent.ts +36 -0
  6. package/src/Components/ImageComponent.ts +30 -0
  7. package/src/Components/ShapeComponent.ts +64 -0
  8. package/src/Components/TextComponent.ts +33 -0
  9. package/src/Components/TileComponent.ts +43 -0
  10. package/src/Effects/Animation.ts +297 -0
  11. package/src/Effects/AnimationCharacter.ts +7 -0
  12. package/src/Effects/Spinner.ts +19 -0
  13. package/src/Effects/Timeline.ts +294 -0
  14. package/src/Effects/TransitionScene.ts +57 -0
  15. package/src/GameEngine.ts +284 -0
  16. package/src/Interfaces/Character.ts +7 -0
  17. package/src/Interfaces/Scene.ts +9 -0
  18. package/src/KeyboardControls.ts +552 -0
  19. package/src/Logger.ts +3 -0
  20. package/src/Presets/AnimationSpritesheet.ts +36 -0
  21. package/src/Presets/Scene.ts +3 -0
  22. package/src/Renderer.ts +263 -0
  23. package/src/Resources.ts +40 -0
  24. package/src/RpgClient.ts +333 -0
  25. package/src/RpgClientEngine.ts +709 -0
  26. package/src/RpgGui.ts +553 -0
  27. package/src/RpgGuiCompiled.ts +43 -0
  28. package/src/Scene/EventLayer.ts +9 -0
  29. package/src/Scene/Map.ts +393 -0
  30. package/src/Scene/Scene.ts +270 -0
  31. package/src/Scene/SceneData.ts +13 -0
  32. package/src/Sound/RpgSound.ts +50 -0
  33. package/src/Sound/Sound.ts +91 -0
  34. package/src/Sound/Sounds.ts +7 -0
  35. package/src/Sprite/Character.ts +149 -0
  36. package/src/Sprite/Player.ts +3 -0
  37. package/src/Sprite/Spritesheet.ts +392 -0
  38. package/src/Sprite/Spritesheets.ts +8 -0
  39. package/src/Tilemap/CommonLayer.ts +20 -0
  40. package/src/Tilemap/ImageLayer.ts +20 -0
  41. package/src/Tilemap/Tile.ts +80 -0
  42. package/src/Tilemap/TileLayer.ts +142 -0
  43. package/src/Tilemap/TileSet.ts +40 -0
  44. package/src/Tilemap/index.ts +173 -0
  45. package/src/clientEntryPoint.ts +141 -0
  46. package/src/index.ts +25 -0
  47. package/src/types/howler.d.ts +73 -0
  48. package/tsconfig.json +30 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpgjs/client",
3
- "version": "4.0.0-beta.1",
3
+ "version": "4.0.0-beta.3",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "types": "./lib/index.d.ts",
@@ -8,20 +8,17 @@
8
8
  "access": "public"
9
9
  },
10
10
  "scripts": {
11
- "build": "tsc",
11
+ "build": "tsc && tsc-esm-fix --target='lib'",
12
12
  "watch": "tsc -w"
13
13
  },
14
- "files": [
15
- "lib"
16
- ],
17
14
  "keywords": [],
18
15
  "author": "Samuel Ronce",
19
16
  "license": "MIT",
20
17
  "dependencies": {
21
18
  "@pixi/tilemap": "^4.0.0",
22
- "@rpgjs/common": "^4.0.0-beta.1",
23
- "@rpgjs/tiled": "^4.0.0-beta.1",
24
- "@rpgjs/types": "^4.0.0-beta.1",
19
+ "@rpgjs/common": "^4.0.0-beta.3",
20
+ "@rpgjs/tiled": "^4.0.0-beta.3",
21
+ "@rpgjs/types": "^4.0.0-beta.3",
25
22
  "@types/howler": "2.2.7",
26
23
  "howler": "2.2.3",
27
24
  "lodash.get": "^4.4.2",
@@ -32,10 +29,11 @@
32
29
  "simple-room-client": "^2.0.3",
33
30
  "vue": "^3.2.47"
34
31
  },
35
- "gitHead": "b12826ef87716da2691da3bae02d7d7c66772789",
32
+ "gitHead": "952bdc2cecd04ad8dd9341ca0d51bf64efcd2bab",
36
33
  "devDependencies": {
37
34
  "@types/css-font-loading-module": "^0.0.8",
38
35
  "@types/node": "^18.16.0",
39
36
  "typescript": "^5.0.4"
40
- }
37
+ },
38
+ "type": "module"
41
39
  }
@@ -0,0 +1,120 @@
1
+ import { ComponentObject } from "@rpgjs/types"
2
+ import { Subject, filter, takeUntil } from "rxjs"
3
+ import { RpgComponent } from "./Component"
4
+ import get from 'lodash.get'
5
+ import { GameEngineClient } from "../GameEngine"
6
+ import { Container, Graphics, Sprite } from "pixi.js"
7
+
8
+ const REGEXP_VAR = /{([^\}]+)}/g
9
+
10
+ export type CellInfo = { x?: number, y?: number, width: number, height: number }
11
+
12
+ export abstract class AbstractComponent<
13
+ TypeComponent extends ComponentObject<any>,
14
+ ContainerType extends Container | Text | Sprite | Graphics
15
+ > extends Container {
16
+ private _onRender$: Subject<AbstractComponent<TypeComponent, ContainerType>> = new Subject()
17
+ private _onDestroy$: Subject<void> = new Subject()
18
+ readonly onRender$ = this._onRender$.asObservable()
19
+ protected readonly game: GameEngineClient = this.component.game
20
+ protected firstRender: boolean = true
21
+ private style = this.value?.style
22
+ private cacheText: {
23
+ [key: string]: string
24
+ } = {}
25
+ protected cell?: CellInfo
26
+
27
+ constructor(protected component: RpgComponent, protected value: TypeComponent['value']) {
28
+ super()
29
+ }
30
+
31
+ getStyle<T>(): T {
32
+ return this.style || {}
33
+ }
34
+
35
+ protected parseTextAndCache(text: string): string[] {
36
+ // parse text to get varariable in {} format et cache it
37
+ const matches = text.matchAll(REGEXP_VAR)
38
+ this.cacheParams = [
39
+ ...this.cacheParams,
40
+ ...Array.from(matches).map(match => match[1])
41
+ ]
42
+ return this.cacheParams
43
+ }
44
+
45
+ protected replaceText(object: any, text: string): string {
46
+ return text.replace(REGEXP_VAR, (match, key) => {
47
+ const value = get(object, key)
48
+ if (value !== undefined) {
49
+ this.cacheText[key] = value
50
+ return value ?? ''
51
+ }
52
+ return value ?? this.cacheText[key] ?? ''
53
+ })
54
+ }
55
+
56
+ protected getValue(object: any, expression: any): any {
57
+ if (typeof expression === 'string') {
58
+ const value = get(object, expression)
59
+ if (value !== undefined) {
60
+ if (this.cacheParams.indexOf(expression) === -1) this.cacheParams.push(expression)
61
+ return value
62
+ }
63
+ }
64
+ return expression
65
+ }
66
+
67
+ private verifyParams(): void | never {
68
+ const params = this.component.logic
69
+ for (const param of this.cacheParams) {
70
+ if (get(params, param) === undefined) {
71
+ throw new Error(`Param ${param} not found in object ${this.component.logic?.id}`)
72
+ }
73
+ }
74
+ }
75
+
76
+ public onInit(cell: CellInfo): void {
77
+ this.cell = cell
78
+
79
+ this.verifyParams()
80
+
81
+ const render= (object) => {
82
+ const opacity = this.getValue(object, this.getStyle<{ opacity: number | undefined }>().opacity || this.value.opacity)
83
+
84
+ if (opacity !== undefined) {
85
+ this.alpha = Math.min(opacity, 1)
86
+ }
87
+ }
88
+
89
+ render(this.component.logic)
90
+
91
+ const objectId = this.component.logic?.id
92
+
93
+ this.game.listenObject(objectId)
94
+ .pipe(
95
+ takeUntil(this._onDestroy$),
96
+ filter(object => {
97
+ const params = object?.paramsChanged
98
+ if (!params) return false
99
+ for (const param of this.cacheParams) {
100
+ if (get(params, param)) return true
101
+ }
102
+ return false
103
+ })
104
+ )
105
+ .subscribe(({ object }) => {
106
+ this.updateRender(object, this.firstRender)
107
+ render(object)
108
+ this.firstRender = false
109
+ this._onRender$.next(this)
110
+ })
111
+ }
112
+
113
+ abstract updateRender(object: any, firstRender: boolean): void
114
+ abstract cacheParams: string[]
115
+
116
+ onRemove() {
117
+ this._onDestroy$.next()
118
+ this._onDestroy$.complete()
119
+ }
120
+ }
@@ -0,0 +1,179 @@
1
+ import { BarComponentObject } from "@rpgjs/types"
2
+ import { Utils, transitionColor } from "@rpgjs/common"
3
+ import { AbstractComponent, CellInfo } from "./AbstractComponent"
4
+ import get from 'lodash.get'
5
+ import { Subject, takeUntil } from "rxjs"
6
+ import { Container, Graphics, Text } from "pixi.js"
7
+
8
+ const DEFAULT_COLOR = '#000000'
9
+
10
+ export class BarComponent extends AbstractComponent<BarComponentObject, Container> {
11
+ static readonly id: string = 'bar'
12
+ private barContainer: Graphics = new Graphics();
13
+ private barFill: Graphics = new Graphics();
14
+ private textContainer: Text = new Text('')
15
+ private barHeight: number = this.value.style?.height || 7;
16
+ private text: string = this.value.text || ''
17
+ private barStyle = this.getStyle<BarComponentObject['value']['style']>()
18
+ private currentValue: number = 0;
19
+ private maxValue: number = 0;
20
+ private nextValue: number = 0;
21
+ private notifier: Subject<void> = new Subject()
22
+ cacheParams: string[] = []
23
+
24
+ private get barWidth() {
25
+ return this.barStyle?.width || this.cell?.width || 0
26
+ }
27
+
28
+ onInit(cell: CellInfo) {
29
+ if (!this.value.style) {
30
+ this.value.style = {
31
+ fillColor: '#ffffff',
32
+ }
33
+ }
34
+ const { bgColor = DEFAULT_COLOR, borderColor = DEFAULT_COLOR, borderWidth = 1, borderRadius = 0 } = this.barStyle || {}
35
+ this.cell = cell
36
+ const { value: color, alpha } = Utils.hexaToNumber(bgColor)
37
+ this.barContainer.beginFill(color, alpha)
38
+ const paramsRect: [number, number, number, number] = [0, 0, this.barWidth, this.barHeight]
39
+ if (borderWidth) {
40
+ const { value: color, alpha } = Utils.hexaToNumber(borderColor)
41
+ this.barContainer.lineStyle(borderWidth, color, alpha);
42
+ }
43
+ if (borderRadius) {
44
+ this.barContainer.drawRoundedRect(...paramsRect, borderRadius);
45
+ }
46
+ else {
47
+ this.barContainer.drawRect(...paramsRect);
48
+ }
49
+ this.barContainer.endFill();
50
+ this.textContainer.style = {
51
+ fontSize: 10,
52
+ fill: '#ffffff',
53
+ fontWeight: 'bold'
54
+ }
55
+ // 5 is the padding
56
+ this.textContainer.y -= this.barHeight + this.textContainer.height - 5
57
+ if (this.text) this.addChild(this.textContainer)
58
+ this.addChild(this.barContainer);
59
+ this.barContainer.addChild(this.barFill);
60
+ this.cacheParams = [this.value.current, this.value.max]
61
+ super.onInit(cell)
62
+ }
63
+
64
+ updateRender(object: any, firstRender: boolean) {
65
+ this.currentValue = this.nextValue;
66
+ this.nextValue = get(object, this.value.current) ?? this.nextValue ?? 0;
67
+ this.maxValue = get(object, this.value.max) ?? this.maxValue;
68
+ const style = this.barStyle
69
+ const borderRadius = style?.borderRadius ?? 0
70
+ const borderWidth = style?.borderWidth ?? 0
71
+
72
+ // first render
73
+ if (firstRender) {
74
+ this.currentValue = this.nextValue;
75
+ }
76
+
77
+ const getColor = (value: number) => {
78
+ let determineLastColor = DEFAULT_COLOR
79
+ const percent = Math.max(0, (value / this.maxValue) * 100);
80
+ const perPercent = (style as any).perPercent;
81
+ if (perPercent) {
82
+ for (const p in perPercent) {
83
+ if (percent <= +p) {
84
+ determineLastColor = perPercent[p].fillColor;
85
+ break;
86
+ }
87
+ }
88
+ } else {
89
+ determineLastColor = (this.value.style as any).fillColor;
90
+ }
91
+ return determineLastColor
92
+ }
93
+
94
+ let colors: string[] = []
95
+ if (style) {
96
+ // TODO: add transition color
97
+ colors = transitionColor(getColor(this.currentValue), getColor(this.nextValue), 1)
98
+ }
99
+ else {
100
+ colors = transitionColor(DEFAULT_COLOR, DEFAULT_COLOR, 1)
101
+ }
102
+
103
+ const render = (up = false) => {
104
+ let currentValue = ~~this.currentValue
105
+ if (currentValue < 0) currentValue = 0
106
+ if (currentValue > this.maxValue) currentValue = this.maxValue
107
+ const percentBetween = ~~Math.max(0, ((currentValue - this.nextValue) * 100) / this.nextValue)
108
+ const colorIndex = Math.max(Math.floor((100 - percentBetween) / (100 / (colors.length - 1))), 0)
109
+ let fillColor = colors[colorIndex]
110
+ this.barFill.clear()
111
+ const { value: color, alpha } = Utils.hexaToNumber(fillColor ?? DEFAULT_COLOR)
112
+ this.barFill.beginFill(color, alpha)
113
+ const percent = Math.max(0, (currentValue / this.maxValue))
114
+ const bWidth = borderWidth / 4
115
+ const paramsRect: [number, number, number, number] = [bWidth, bWidth, percent * this.barWidth - bWidth, this.barHeight - bWidth]
116
+ if (percent > 0) {
117
+ if (borderRadius) {
118
+ this.barFill.drawRoundedRect(...paramsRect, borderRadius)
119
+ }
120
+ else {
121
+ this.barFill.drawRect(...paramsRect)
122
+ }
123
+ }
124
+ this.textContainer.text = this.replaceText({
125
+ ...object,
126
+ $current: currentValue,
127
+ $percent: Math.round(percent * 100),
128
+ $max: this.maxValue
129
+ }, this.text)
130
+ this.barFill.endFill();
131
+ }
132
+
133
+ if (firstRender) {
134
+ render();
135
+ return;
136
+ }
137
+
138
+ this.notifier.next()
139
+
140
+ this.game.clientEngine.tick
141
+ .pipe(
142
+ takeUntil(this.notifier)
143
+ )
144
+ .subscribe(() => {
145
+ // speed of animation, calculate the difference between the current value and the next value to determine the speed
146
+ const speed = Math.abs(this.currentValue - this.nextValue) / 10;
147
+ let up: boolean = false;
148
+
149
+ // if the current value is less than the next value, add the speed to the current value
150
+ if (this.currentValue < this.nextValue) {
151
+ this.currentValue += speed
152
+ up = true;
153
+ }
154
+
155
+ // if the current value is greater than the next value, subtract the speed from the current value
156
+ else if (this.currentValue > this.nextValue) {
157
+ this.currentValue -= speed
158
+ up = false;
159
+ }
160
+
161
+ render(up)
162
+
163
+ const currentValue = Math.round(this.currentValue)
164
+
165
+ if (!up && (~~currentValue <= ~~this.nextValue || currentValue <= 0)) {
166
+ this.notifier.next()
167
+ }
168
+ else if (up && (~~currentValue >= ~~this.nextValue || currentValue >= this.maxValue)) {
169
+ this.notifier.next()
170
+ }
171
+ })
172
+ }
173
+
174
+ onRemove() {
175
+ this.notifier.next()
176
+ this.notifier.complete()
177
+ super.onRemove()
178
+ }
179
+ }