@rpgjs/common 4.3.0 → 5.0.0-alpha.1

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 (148) hide show
  1. package/dist/Physic.d.ts +619 -0
  2. package/dist/Player.d.ts +198 -0
  3. package/{lib → dist}/Utils.d.ts +19 -2
  4. package/dist/database/Item.d.ts +10 -0
  5. package/dist/database/index.d.ts +1 -0
  6. package/dist/index.d.ts +9 -0
  7. package/dist/index.js +16741 -0
  8. package/dist/index.js.map +1 -0
  9. package/dist/modules.d.ts +92 -0
  10. package/dist/movement/MovementManager.d.ts +84 -0
  11. package/dist/movement/MovementStrategy.d.ts +39 -0
  12. package/dist/movement/index.d.ts +12 -0
  13. package/dist/movement/strategies/CompositeMovement.d.ts +76 -0
  14. package/dist/movement/strategies/Dash.d.ts +52 -0
  15. package/dist/movement/strategies/IceMovement.d.ts +87 -0
  16. package/dist/movement/strategies/Knockback.d.ts +50 -0
  17. package/dist/movement/strategies/LinearMove.d.ts +43 -0
  18. package/dist/movement/strategies/LinearRepulsion.d.ts +55 -0
  19. package/dist/movement/strategies/Oscillate.d.ts +60 -0
  20. package/dist/movement/strategies/PathFollow.d.ts +78 -0
  21. package/dist/movement/strategies/ProjectileMovement.d.ts +138 -0
  22. package/dist/movement/strategies/SeekAvoid.d.ts +27 -0
  23. package/dist/rooms/Map.d.ts +109 -0
  24. package/dist/services/updateMap.d.ts +7 -0
  25. package/package.json +17 -17
  26. package/src/Physic.ts +1644 -0
  27. package/src/Player.ts +262 -26
  28. package/src/{gui/PrebuiltGui.ts → PrebuiltGui.ts} +1 -1
  29. package/src/Utils.ts +184 -123
  30. package/src/database/Item.ts +19 -0
  31. package/src/database/index.ts +1 -0
  32. package/src/index.ts +9 -25
  33. package/src/modules.ts +230 -0
  34. package/src/movement/MovementManager.ts +142 -0
  35. package/src/movement/MovementStrategy.ts +42 -0
  36. package/src/movement/index.ts +15 -0
  37. package/src/movement/strategies/CompositeMovement.ts +173 -0
  38. package/src/movement/strategies/Dash.ts +82 -0
  39. package/src/movement/strategies/IceMovement.ts +158 -0
  40. package/src/movement/strategies/Knockback.ts +81 -0
  41. package/src/movement/strategies/LinearMove.ts +58 -0
  42. package/src/movement/strategies/LinearRepulsion.ts +128 -0
  43. package/src/movement/strategies/Oscillate.ts +144 -0
  44. package/src/movement/strategies/PathFollow.ts +156 -0
  45. package/src/movement/strategies/ProjectileMovement.ts +322 -0
  46. package/src/movement/strategies/SeekAvoid.ts +123 -0
  47. package/src/rooms/Map.ts +272 -0
  48. package/src/services/updateMap.ts +9 -0
  49. package/tests/physic.spec.ts +454 -0
  50. package/tsconfig.json +8 -3
  51. package/vite.config.ts +21 -0
  52. package/CHANGELOG.md +0 -160
  53. package/LICENSE +0 -19
  54. package/browser/manifest.json +0 -7
  55. package/browser/rpg.common.js +0 -11357
  56. package/browser/rpg.common.umd.cjs +0 -11358
  57. package/lib/AbstractObject.d.ts +0 -322
  58. package/lib/AbstractObject.js +0 -872
  59. package/lib/AbstractObject.js.map +0 -1
  60. package/lib/Color.d.ts +0 -1
  61. package/lib/Color.js +0 -25
  62. package/lib/Color.js.map +0 -1
  63. package/lib/DefaultInput.d.ts +0 -2
  64. package/lib/DefaultInput.js +0 -26
  65. package/lib/DefaultInput.js.map +0 -1
  66. package/lib/Event.d.ts +0 -3
  67. package/lib/Event.js +0 -4
  68. package/lib/Event.js.map +0 -1
  69. package/lib/EventEmitter.d.ts +0 -10
  70. package/lib/EventEmitter.js +0 -61
  71. package/lib/EventEmitter.js.map +0 -1
  72. package/lib/Game.d.ts +0 -28
  73. package/lib/Game.js +0 -127
  74. package/lib/Game.js.map +0 -1
  75. package/lib/Hit.d.ts +0 -16
  76. package/lib/Hit.js +0 -65
  77. package/lib/Hit.js.map +0 -1
  78. package/lib/Inject.d.ts +0 -9
  79. package/lib/Inject.js +0 -17
  80. package/lib/Inject.js.map +0 -1
  81. package/lib/Logger.d.ts +0 -2
  82. package/lib/Logger.js +0 -7
  83. package/lib/Logger.js.map +0 -1
  84. package/lib/Map.d.ts +0 -174
  85. package/lib/Map.js +0 -263
  86. package/lib/Map.js.map +0 -1
  87. package/lib/Module.d.ts +0 -16
  88. package/lib/Module.js +0 -139
  89. package/lib/Module.js.map +0 -1
  90. package/lib/Player.d.ts +0 -26
  91. package/lib/Player.js +0 -19
  92. package/lib/Player.js.map +0 -1
  93. package/lib/Plugin.d.ts +0 -67
  94. package/lib/Plugin.js +0 -92
  95. package/lib/Plugin.js.map +0 -1
  96. package/lib/Scheduler.d.ts +0 -26
  97. package/lib/Scheduler.js +0 -90
  98. package/lib/Scheduler.js.map +0 -1
  99. package/lib/Shape.d.ts +0 -127
  100. package/lib/Shape.js +0 -261
  101. package/lib/Shape.js.map +0 -1
  102. package/lib/Utils.js +0 -181
  103. package/lib/Utils.js.map +0 -1
  104. package/lib/Vector2d.d.ts +0 -20
  105. package/lib/Vector2d.js +0 -63
  106. package/lib/Vector2d.js.map +0 -1
  107. package/lib/VirtualGrid.d.ts +0 -26
  108. package/lib/VirtualGrid.js +0 -68
  109. package/lib/VirtualGrid.js.map +0 -1
  110. package/lib/Worker.d.ts +0 -7
  111. package/lib/Worker.js +0 -13
  112. package/lib/Worker.js.map +0 -1
  113. package/lib/WorldMaps.d.ts +0 -105
  114. package/lib/WorldMaps.js +0 -184
  115. package/lib/WorldMaps.js.map +0 -1
  116. package/lib/gui/PrebuiltGui.js +0 -29
  117. package/lib/gui/PrebuiltGui.js.map +0 -1
  118. package/lib/index.d.ts +0 -25
  119. package/lib/index.js +0 -26
  120. package/lib/index.js.map +0 -1
  121. package/lib/transports/io.d.ts +0 -22
  122. package/lib/transports/io.js +0 -82
  123. package/lib/transports/io.js.map +0 -1
  124. package/lib/workers/move.d.ts +0 -1
  125. package/lib/workers/move.js +0 -57
  126. package/lib/workers/move.js.map +0 -1
  127. package/rpg.toml +0 -11
  128. package/src/AbstractObject.ts +0 -973
  129. package/src/Color.ts +0 -29
  130. package/src/DefaultInput.ts +0 -26
  131. package/src/Event.ts +0 -3
  132. package/src/EventEmitter.ts +0 -65
  133. package/src/Game.ts +0 -159
  134. package/src/Hit.ts +0 -70
  135. package/src/Inject.ts +0 -22
  136. package/src/Logger.ts +0 -7
  137. package/src/Map.ts +0 -335
  138. package/src/Module.ts +0 -144
  139. package/src/Plugin.ts +0 -100
  140. package/src/Scheduler.ts +0 -95
  141. package/src/Shape.ts +0 -302
  142. package/src/Vector2d.ts +0 -70
  143. package/src/VirtualGrid.ts +0 -78
  144. package/src/Worker.ts +0 -17
  145. package/src/WorldMaps.ts +0 -204
  146. package/src/transports/io.ts +0 -91
  147. package/src/workers/move.ts +0 -61
  148. /package/{lib/gui → dist}/PrebuiltGui.d.ts +0 -0
package/src/Shape.ts DELETED
@@ -1,302 +0,0 @@
1
- import { RpgCommonPlayer } from './Player'
2
- import { Hit, HitType } from './Hit'
3
- import { TiledObjectClass } from '@rpgjs/tiled'
4
- import { PlayerType } from '@rpgjs/types'
5
- import { Vector2d } from './Vector2d'
6
- import { AbstractObject } from './AbstractObject'
7
-
8
- export enum ShapePositioning {
9
- Default = 'default',
10
- Center = 'center'
11
- }
12
-
13
- type ShapeObject = TiledObjectClass & {
14
- onIn?(player: RpgCommonPlayer)
15
- onOut?(player: RpgCommonPlayer)
16
- fixEvent?: RpgCommonPlayer,
17
- positioning?: ShapePositioning
18
- }
19
-
20
- export class RpgShape extends TiledObjectClass {
21
- _hitbox: any
22
- type: string = HitType.Box
23
- class: string = ''
24
- /**
25
- * Get/Set name
26
- * @title name
27
- * @prop { string } name
28
- * @memberof Shape
29
- */
30
- name: string = ''
31
- fixEvent?: RpgCommonPlayer
32
- private playersIn: {
33
- [playerid: string]: boolean
34
- } = {}
35
- private onIn: (player: RpgCommonPlayer) => void
36
- private onOut: (player: RpgCommonPlayer) => void
37
- clientContainer: any = null
38
- /**
39
- * Get/Set positioning
40
- * @title positioning
41
- * @prop { ShapePositioning } positioning
42
- * @default default
43
- * @memberof Shape
44
- */
45
- positioning?: ShapePositioning = ShapePositioning.Default
46
- components: any[] = []
47
-
48
- constructor(obj: ShapeObject) {
49
- super()
50
- Reflect.deleteProperty(obj, 'id')
51
- this.set(obj)
52
- }
53
-
54
- private setPos(type: string, val: number) {
55
- if (!this.hitbox.pos) return
56
- if (this.isShapePosition()) {
57
- this.hitbox[type] = val
58
- }
59
- else {
60
- this.hitbox.pos[type] = val
61
- }
62
- }
63
-
64
- get hasCollision(): boolean {
65
- return this.getProperty<boolean, boolean>('collision', false)
66
- }
67
-
68
- // alias
69
- get id(): any {
70
- return this.name
71
- }
72
-
73
- get hitbox() {
74
- if (this.fixEvent) {
75
- this._hitbox.pos.x = this.fixEvent.position.x
76
- this._hitbox.pos.y = this.fixEvent.position.y
77
- switch (this.positioning) {
78
- case ShapePositioning.Center:
79
- this._hitbox.pos.x -= this._hitbox.w / 2 - this.fixEvent.hitbox.w / 2
80
- this._hitbox.pos.y -= this._hitbox.h / 2 - this.fixEvent.hitbox.h / 2
81
- break
82
- }
83
- }
84
- return this._hitbox
85
- }
86
-
87
- set hitbox(val) {
88
- this._hitbox = val
89
- }
90
-
91
- /**
92
- * Get/Set width
93
- * @title width
94
- * @prop { number } width
95
- * @since 3.0.0-beta.5
96
- * @memberof Shape
97
- */
98
- get width(): number {
99
- return this.hitbox.w || 0
100
- }
101
-
102
- set width(val: number) {
103
- this.setPos('w', val)
104
- }
105
-
106
- /**
107
- * Get/Set height
108
- * @title height
109
- * @prop { number } height
110
- * @since 3.0.0-beta.5
111
- * @memberof Shape
112
- */
113
- get height(): number {
114
- return this.hitbox.h || 0
115
- }
116
-
117
- set height(val: number) {
118
- this.setPos('h', val)
119
- }
120
-
121
- /**
122
- * Get/Set x
123
- * @title x
124
- * @prop { number } x
125
- * @memberof Shape
126
- */
127
- get x(): number {
128
- return this.hitbox.x || this.hitbox.pos.x
129
- }
130
-
131
- set x(val: number) {
132
- this.setPos('x', val)
133
- }
134
-
135
- get z(): number | undefined {
136
- return this.getProperty<number>('z')
137
- }
138
-
139
- /**
140
- * Get/Set y
141
- * @title y
142
- * @prop { number } y
143
- * @memberof Shape
144
- */
145
- get y(): number {
146
- return this.hitbox.y || this.hitbox.pos.y
147
- }
148
-
149
- set y(val: number) {
150
- this.setPos('y', val)
151
- }
152
-
153
- get position(): Vector2d {
154
- return new Vector2d(this.x, this.y, this.z)
155
- }
156
-
157
- /**
158
- * Get/Set properties
159
-
160
- * @title Properties
161
- * @prop { object } Properties
162
- * @memberof Shape
163
- */
164
-
165
- isEvent(): boolean {
166
- return this.type == PlayerType.Event
167
- }
168
-
169
- set(obj: ShapeObject) {
170
- const hit = Hit.getHitbox(obj)
171
- Object.assign(this, hit)
172
- const objClone = { ...obj };
173
- // Delete dimension and position because already managed and given by the hitbox above
174
- ['width', 'height', 'x', 'y'].forEach((prop) => Reflect.deleteProperty(objClone, prop))
175
- Object.assign(this, objClone)
176
- const findPoint = (prop: string, isMin: boolean) => {
177
- return this.hitbox.points.sort((a, b) => isMin ? a[prop] - b[prop] : b[prop] - a[prop])[0][prop]
178
- }
179
- if (this.type == HitType.Polygon) {
180
- this.hitbox.minX = findPoint('x', true)
181
- this.hitbox.maxX = findPoint('x', false)
182
- this.hitbox.minY = findPoint('y', true)
183
- this.hitbox.maxY = findPoint('y', false)
184
- }
185
- this.positioning = obj.positioning
186
- this.fixEvent = obj.fixEvent
187
- this.setComponent()
188
- }
189
-
190
- setComponent() {
191
- const color = this.getProperty<string>('color')
192
- const image = this.getProperty<string>('image')
193
- if (color) {
194
- this.components = [{
195
- id: 'shape', value: {
196
- fill: color
197
- }
198
- }]
199
- return
200
- }
201
- if (image) {
202
- this.components = [{ id: 'image', value: image }]
203
- return
204
- }
205
- if (this.text) {
206
- this.components = [{ id: 'text', value: this.text.text }]
207
- return
208
- }
209
- if (this.gid) {
210
- this.components = [{ id: 'tile', value: this.gid }]
211
- return
212
- }
213
- }
214
-
215
- getType() {
216
- return this.class || this.type
217
- }
218
-
219
- async in(player: AbstractObject): Promise<boolean> {
220
- if (!this.playerIsIn(player)) {
221
- this.playersIn[player.id] = true
222
- player.inShapes[this.name] = this
223
- await player.execMethod('onInShape', [this])
224
- await this.fixEvent?.execMethod('onDetectInShape', [player, this], this.fixEvent)
225
- await player.execMethod('onIn', [player], this)
226
- return true
227
- }
228
- return false
229
- }
230
-
231
- async out(player: AbstractObject): Promise<boolean> {
232
- if (this.playerIsIn(player)) {
233
- delete this.playersIn[player.id]
234
- delete player.inShapes[this.name]
235
- await player.execMethod('onOutShape', [this])
236
- await this.fixEvent?.execMethod('onDetectOutShape', [player, this], this.fixEvent)
237
- await player.execMethod('onOut', [player], this)
238
- return true
239
- }
240
- return false
241
- }
242
-
243
- /**
244
- * Whether the player is in this shape
245
- *
246
- * @title Player is in this shape ?
247
- * @method shape.playerIsIn(player)
248
- * @returns {boolean}
249
- * @memberof Shape
250
- */
251
- playerIsIn(player: AbstractObject): boolean {
252
- return !!this.playersIn[player.id]
253
- }
254
-
255
- isShapePosition(): boolean {
256
- return this.type !== HitType.Box && this.type !== HitType.Circle && this.type !== HitType.Polygon
257
- }
258
-
259
- /**
260
- * Recover the player with the shape. You must have used the `attachShape()` method on the player
261
- *
262
- * @title Get Player Owner
263
- * @method shape.getPlayerOwner()
264
- * @returns {RpgPlayer | undefined}
265
- * @memberof Shape
266
- */
267
- getPlayerOwner(): RpgCommonPlayer | undefined {
268
- return this.fixEvent
269
- }
270
-
271
- /**
272
- * We get the rectangle of a shape (box, circle and polygon). We use in the grid system to recover a shape.
273
- * Generally we add a margin (size of a tile) to detect if the player enters or leaves a shape
274
- * @param margin
275
- * @returns { minX: number, minY: number, maxX: number, maxY: number }
276
- */
277
- getSizeBox(margin: number = 0): { minX: number, minY: number, maxX: number, maxY: number } {
278
- if (this.type == HitType.Circle) {
279
- const radius = this.hitbox.r
280
- return {
281
- minX: this.x - radius - margin,
282
- maxX: this.x + radius + margin,
283
- minY: this.y - radius - margin,
284
- maxY: this.y + radius + margin
285
- }
286
- }
287
- if (this.type == HitType.Polygon) {
288
- return {
289
- minX: this.x + this.hitbox.minX - margin,
290
- maxX: this.x + this.hitbox.maxX + margin,
291
- minY: this.y + this.hitbox.minY - margin,
292
- maxY: this.y + this.hitbox.maxY + margin
293
- }
294
- }
295
- return {
296
- minX: this.x - margin,
297
- maxX: this.x + this.width + margin,
298
- minY: this.y - margin,
299
- maxY: this.y + this.height + margin
300
- }
301
- }
302
- }
package/src/Vector2d.ts DELETED
@@ -1,70 +0,0 @@
1
- export class Vector2d {
2
- constructor(public x: number, public y: number, public z: number = 0) {}
3
-
4
- set(vector: Vector2d) {
5
- this.x = vector.x
6
- this.y = vector.y
7
- this.z = vector.z
8
- return this
9
- }
10
-
11
- add(vector: Vector2d) {
12
- this.x += vector.x
13
- this.y += vector.y
14
- return this
15
- }
16
-
17
- subtract(vector: Vector2d) {
18
- this.x -= vector.x
19
- this.y -= vector.y
20
- return this
21
- }
22
-
23
- multiply(scalar: number) {
24
- this.x *= scalar
25
- this.y *= scalar
26
- return this
27
- }
28
-
29
- divide(scalar: number) {
30
- this.x /= scalar
31
- this.y /= scalar
32
- return this
33
- }
34
-
35
- distanceWith(vector: Vector2d): number {
36
- const dx = this.x - vector.x
37
- const dy = this.y - vector.y
38
- return Math.sqrt(dx ** 2 + dy ** 2)
39
- }
40
-
41
- magnitude(): number {
42
- return Math.sqrt(this.x * this.x + this.y * this.y)
43
- }
44
-
45
- copy(): Vector2d {
46
- return new Vector2d(this.x, this.y, this.z)
47
- }
48
-
49
- normalize() {
50
- return this.divide(this.magnitude())
51
- }
52
-
53
- isEqual(vector: Vector2d): boolean {
54
- return this.x === vector.x &&
55
- this.y === vector.y &&
56
- this.z === vector.z
57
- }
58
-
59
- hasDifferentValues(vector: Vector2d): boolean {
60
- return this.x !== vector.x ||
61
- this.y !== vector.y ||
62
- this.z !== vector.z
63
- }
64
- }
65
-
66
- export class Vector2dZero extends Vector2d {
67
- constructor() {
68
- super(0, 0)
69
- }
70
- }
@@ -1,78 +0,0 @@
1
- export type Box = { minX: number, minY: number, maxX: number, maxY: number }
2
-
3
- export class VirtualGrid {
4
- private cells: Map<number, Set<string>> = new Map()
5
- private inverseCells: Map<string, Set<number>> = new Map()
6
-
7
- constructor(private nbCellWidth: number, private cellWidth: number, private cellHeight: number) {}
8
-
9
- zoom(nbCell: number): VirtualGrid {
10
- this.nbCellWidth = Math.ceil(this.nbCellWidth / nbCell)
11
- this.cellWidth *= nbCell
12
- this.cellHeight *= nbCell
13
- return this
14
- }
15
-
16
- getCellIndex(x: number, y: number) {
17
- return this.nbCellWidth * Math.floor(y / this.cellHeight) + Math.floor(x / this.cellWidth)
18
- }
19
-
20
- getCells(box: Box, cb: (index: number) => void) {
21
- const {
22
- minX,
23
- minY,
24
- maxX,
25
- maxY
26
- } = box
27
- const topLeft = this.getCellIndex(minX, minY)
28
- const topRight = this.getCellIndex(maxX, minY)
29
- const bottomLeft = this.getCellIndex(minX, maxY)
30
- const nbLines = (bottomLeft - topLeft) / this.nbCellWidth + 1
31
- for (let j=0 ; j < nbLines ; j++) {
32
- for (let i = topLeft ; i <= topRight ; i++) {
33
- const index = i + (j * this.nbCellWidth)
34
- cb(index)
35
- }
36
- }
37
- }
38
-
39
- getObjectsByBox(box: Box): Set<string> {
40
- let objects: string[] = []
41
- this.getCells(box, (index) => {
42
- objects = [...objects, ...this.cells.get(index) || []]
43
- })
44
- return new Set(objects)
45
- }
46
-
47
- getObjectsById(id: string): Set<string> {
48
- let objects: string[] = []
49
- const cells = this.inverseCells.get(id)
50
- cells?.forEach((index) => {
51
- objects = [...objects, ...this.cells.get(index) || []]
52
- })
53
- return new Set(objects)
54
- }
55
-
56
- clearObjectInCells(id: string) {
57
- if (this.inverseCells.has(id)) {
58
- this.inverseCells.get(id)?.forEach((cellIndex: number) => {
59
- this.cells.get(cellIndex)?.delete(id)
60
- })
61
- this.inverseCells.delete(id)
62
- }
63
- }
64
-
65
- insertInCells(id: string, box: { minX: number, minY: number, maxX: number, maxY: number }) {
66
- this.clearObjectInCells(id)
67
- const cells: Set<number> = new Set()
68
- this.getCells(box, (index) => {
69
- cells.add(index)
70
- const memoryCells = this.cells.get(index)
71
- if (!memoryCells) {
72
- this.cells.set(index, new Set())
73
- }
74
- this.cells.get(index)?.add(id)
75
- })
76
- this.inverseCells.set(id, cells)
77
- }
78
- }
package/src/Worker.ts DELETED
@@ -1,17 +0,0 @@
1
- import workerpool from 'workerpool'
2
-
3
- export class GameWorker {
4
- pool: any
5
-
6
- constructor(private options = {}) {
7
- //this.pool = workerpool.pool(__dirname + '/workers/move.js', options)
8
- }
9
-
10
- load() {
11
- return this
12
- }
13
-
14
- call(methodName: string, data: any) {
15
- return this.pool.exec(methodName, [data])
16
- }
17
- }
package/src/WorldMaps.ts DELETED
@@ -1,204 +0,0 @@
1
- import RBush from 'rbush'
2
- import { TiledWorldMap } from '@rpgjs/tiled'
3
- import { RpgCommonMap } from './Map'
4
- import { Direction } from '@rpgjs/types'
5
-
6
- export interface RpgClassMap<T> {
7
- id?: string
8
- new (server: any): T,
9
- }
10
-
11
- export interface RpgTiledWorldMap extends TiledWorldMap{
12
- properties?: {
13
- [key: string]: any
14
- }
15
- }
16
-
17
- type PositionBox = { minX: number, minY: number, maxX: number, maxY: number }
18
- type MapTree = { map: RpgClassMap<RpgCommonMap> } & PositionBox
19
-
20
- export class RpgCommonWorldMaps {
21
- private mapsTree: RBush = new RBush(500)
22
- private maps: Map<string, RpgTiledWorldMap> = new Map()
23
-
24
- constructor(public id: string) {}
25
-
26
- /**
27
- * Adding information from the map to the world
28
- *
29
- * > Maximum maps in world: 500
30
- *
31
- * @title Add Map in world
32
- * @method world.addMap(wordMapInfo,map)
33
- * @param {object} wordMapInfo
34
- * Object file:
35
- * ```ts
36
- * {
37
- * fileName: string;
38
- height: number;
39
- width: number;
40
- x: number;
41
- y: number;
42
- * }
43
- ```
44
- `fileName` represents a file to the JSON file (TMX transformed) or directly the Tiled Map Editor object
45
- *
46
- * @param {class of RpgMap} map
47
- * @since 3.0.0-beta.8
48
- * @memberof RpgWorldMaps
49
- */
50
- addMap(wordMapInfo: RpgTiledWorldMap, map: RpgClassMap<RpgCommonMap>) {
51
- const { x, y, height, width } = wordMapInfo
52
- map.prototype.worldMapParent = this
53
- this.maps.set(map.id as string, wordMapInfo)
54
- this.mapsTree.insert<MapTree>({
55
- minX: x,
56
- minY: y,
57
- maxX: x + width,
58
- maxY: y + height,
59
- map
60
- })
61
- }
62
-
63
- updateMap(mapId: string, wordMapInfo: RpgTiledWorldMap): boolean {
64
- const map = this.maps.get(mapId)
65
- if (map) {
66
- const item = (this.mapsTree.all() as MapTree[]).find(item => item.map.id == mapId)
67
- if (!item) return false
68
- this.maps.set(mapId, wordMapInfo)
69
- item.map.prototype.worldMapParent = this
70
- item.minX = wordMapInfo.x
71
- item.minY = wordMapInfo.y
72
- item.maxX = wordMapInfo.x + wordMapInfo.width
73
- item.maxY = wordMapInfo.y + wordMapInfo.height
74
- return true
75
- }
76
- return false
77
- }
78
-
79
- /**
80
- * Remove map of the world
81
- * @title Remove map of the world
82
- * @method world.removeMap(mapId)
83
- * @param {string} mapId
84
- * @returns {boolean}
85
- * @since 3.0.0-beta.8
86
- * @memberof RpgWorldMaps
87
- */
88
- removeMap(mapId: string): boolean {
89
- const map = this.maps.get(mapId)
90
- if (map) {
91
- const item = (this.mapsTree.all() as MapTree[]).find(item => item.map.id == mapId)
92
- if (!item) return false
93
- this.maps.delete(mapId)
94
- item.map.prototype.worldMapParent = undefined
95
- this.mapsTree.remove(item)
96
- return true
97
- }
98
- return false
99
- }
100
-
101
- removeAllMaps() {
102
- this.maps.forEach((map, id) => {
103
- this.removeMap(id)
104
- })
105
- }
106
-
107
- /**
108
- * Retrieve information from the world
109
- *
110
- * @title Retrieve information from the world
111
- * @method world.getMapInfo(id)
112
- * @param {string} id map id
113
- * @return {RpgTiledWorldMap | undefined}
114
- * {
115
- * id?: string
116
- * properties?: object
117
- * fileName: string;
118
- height: number;
119
- width: number;
120
- x: number;
121
- y: number;
122
- * }
123
- * @since 3.0.0-beta.8
124
- * @memberof RpgWorldMaps
125
- */
126
- getMapInfo(id: string): RpgTiledWorldMap | undefined {
127
- return this.maps.get(id)
128
- }
129
-
130
- /**
131
- * Retrieves neighboring maps according to positions or direction
132
- *
133
- * @title Retrieves neighboring maps
134
- * @method world.getAdjacentMaps(map,search)
135
- * @param {RpgMap} map The source map. We want to find the neighboring maps of the source map
136
- * @param { PositionBox | Direction | { x: number, y: number } } search Research method
137
- * * PositionBox. An object of the following form:
138
- * `{ minX: number, minY: number, maxX: number, maxY: number }`
139
- * * Direction. Collect all the maps in the given direction (e.g. the maps at the top)
140
- * * Point: { x: number, y: number }
141
- * @return { {class of RpgMap}[] }
142
- * @since 3.0.0-beta.8
143
- * @example
144
- * ```ts
145
- * world.getAdjacentMaps(mymap, Direction.Up) // returns [class of RpgMap]
146
- * ```
147
- * @memberof RpgWorldMaps
148
- */
149
- getAdjacentMaps(map: RpgCommonMap, search: PositionBox | Direction | { x: number, y: number }): RpgClassMap<RpgCommonMap>[] {
150
- let position: PositionBox = {} as PositionBox
151
- const point = search as { x: number, y: number }
152
- if (typeof search == 'number') {
153
- const padding = 1
154
- switch (search) {
155
- case Direction.Up:
156
- position = {
157
- minX: map.worldX + padding,
158
- maxX: map.worldX + map.widthPx - padding,
159
- minY: map.worldY - padding - 1,
160
- maxY: map.worldY - padding
161
- }
162
- break;
163
- case Direction.Right:
164
- position = {
165
- minX: map.worldX + map.widthPx + padding,
166
- maxX: map.worldX + map.widthPx + padding + 1,
167
- minY: map.worldY + padding,
168
- maxY: map.worldY + map.heightPx - padding
169
- }
170
- break;
171
- case Direction.Down:
172
- position = {
173
- minX: map.worldX + padding,
174
- maxX: map.worldX + map.widthPx - padding,
175
- minY: map.worldY + map.heightPx + padding,
176
- maxY: map.worldY + map.heightPx + padding + 1
177
- }
178
- break;
179
- case Direction.Left:
180
- position = {
181
- minX: map.worldX - padding,
182
- maxX: map.worldX - padding - 1,
183
- minY: map.worldY + padding,
184
- maxY: map.worldY + map.heightPx - padding
185
- }
186
- break;
187
- }
188
-
189
- }
190
- else if (point.x) {
191
- position = {
192
- minX: point.x,
193
- maxX: point.x,
194
- minY: point.y,
195
- maxY: point.y
196
- }
197
- }
198
- else {
199
- position = search as PositionBox
200
- }
201
- const result = this.mapsTree.search(position)
202
- return result.map(ret => ret.map)
203
- }
204
- }