kiwiengine 0.5.4 → 0.5.6

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 (69) hide show
  1. package/examples/auto-battle/objects/character.ts +1 -1
  2. package/examples/battle-benchmark-matterjs/objects/character.ts +1 -1
  3. package/examples/battle-benchmark-separation/objects/character.ts +1 -1
  4. package/examples/battle-benchmark-separation2/objects/character.ts +1 -1
  5. package/examples/simple-battle/objects/character.ts +1 -1
  6. package/lib/asset/preload.js +21 -19
  7. package/lib/asset/preload.js.map +1 -1
  8. package/lib/dom/dom-animated-sprite.js.map +1 -1
  9. package/lib/dom/dom-game-object.js.map +1 -1
  10. package/lib/dom/dom-sprite.js.map +1 -1
  11. package/lib/index.js +1 -1
  12. package/lib/index.js.map +1 -1
  13. package/lib/node/core/game-object.js.map +1 -1
  14. package/lib/node/core/renderable.js +2 -0
  15. package/lib/node/core/renderable.js.map +1 -1
  16. package/lib/node/core/transformable.js +5 -1
  17. package/lib/node/core/transformable.js.map +1 -1
  18. package/lib/node/ext/animated-sprite.js +16 -15
  19. package/lib/node/ext/animated-sprite.js.map +1 -1
  20. package/lib/node/ext/bitmap-text.js +29 -19
  21. package/lib/node/ext/bitmap-text.js.map +1 -1
  22. package/lib/node/ext/spine.js.map +1 -1
  23. package/lib/node/ext/sprite.js.map +1 -1
  24. package/lib/node/physics/physics-object.js +8 -1
  25. package/lib/node/physics/physics-object.js.map +1 -1
  26. package/lib/node/physics/physics-world.js.map +1 -1
  27. package/lib/types/asset/preload.d.ts +2 -0
  28. package/lib/types/asset/preload.d.ts.map +1 -1
  29. package/lib/types/dom/dom-animated-sprite.d.ts +1 -1
  30. package/lib/types/dom/dom-animated-sprite.d.ts.map +1 -1
  31. package/lib/types/dom/dom-game-object.d.ts +1 -1
  32. package/lib/types/dom/dom-game-object.d.ts.map +1 -1
  33. package/lib/types/dom/dom-sprite.d.ts +1 -1
  34. package/lib/types/dom/dom-sprite.d.ts.map +1 -1
  35. package/lib/types/index.d.ts +1 -1
  36. package/lib/types/index.d.ts.map +1 -1
  37. package/lib/types/node/core/game-object.d.ts +1 -1
  38. package/lib/types/node/core/game-object.d.ts.map +1 -1
  39. package/lib/types/node/core/renderable.d.ts +2 -0
  40. package/lib/types/node/core/renderable.d.ts.map +1 -1
  41. package/lib/types/node/core/transformable.d.ts +3 -0
  42. package/lib/types/node/core/transformable.d.ts.map +1 -1
  43. package/lib/types/node/ext/animated-sprite.d.ts +1 -1
  44. package/lib/types/node/ext/animated-sprite.d.ts.map +1 -1
  45. package/lib/types/node/ext/bitmap-text.d.ts +4 -2
  46. package/lib/types/node/ext/bitmap-text.d.ts.map +1 -1
  47. package/lib/types/node/ext/spine.d.ts +1 -1
  48. package/lib/types/node/ext/spine.d.ts.map +1 -1
  49. package/lib/types/node/ext/sprite.d.ts +1 -1
  50. package/lib/types/node/ext/sprite.d.ts.map +1 -1
  51. package/lib/types/node/physics/physics-object.d.ts +2 -1
  52. package/lib/types/node/physics/physics-object.d.ts.map +1 -1
  53. package/lib/types/node/physics/physics-world.d.ts +1 -1
  54. package/lib/types/node/physics/physics-world.d.ts.map +1 -1
  55. package/package.json +1 -1
  56. package/src/asset/preload.ts +18 -16
  57. package/src/dom/dom-animated-sprite.ts +1 -1
  58. package/src/dom/dom-game-object.ts +1 -1
  59. package/src/dom/dom-sprite.ts +1 -1
  60. package/src/index.ts +1 -1
  61. package/src/node/core/game-object.ts +1 -1
  62. package/src/node/core/renderable.ts +3 -0
  63. package/src/node/core/transformable.ts +6 -1
  64. package/src/node/ext/animated-sprite.ts +18 -17
  65. package/src/node/ext/bitmap-text.ts +37 -21
  66. package/src/node/ext/spine.ts +1 -1
  67. package/src/node/ext/sprite.ts +1 -1
  68. package/src/node/physics/physics-object.ts +9 -2
  69. package/src/node/physics/physics-world.ts +1 -1
@@ -69,4 +69,7 @@ export abstract class RenderableNode<C extends PixiContainer, E extends EventMap
69
69
 
70
70
  set tint(t) { this._pixiContainer.tint = t }
71
71
  get tint() { return this._pixiContainer.tint }
72
+
73
+ hide() { this._pixiContainer.visible = false }
74
+ show() { this._pixiContainer.visible = true }
72
75
  }
@@ -13,6 +13,7 @@ export type TransformableNodeOptions = {
13
13
  pivotX?: number
14
14
  pivotY?: number
15
15
  rotation?: number
16
+ drawOrder?: number
16
17
 
17
18
  alpha?: number
18
19
  layer?: string
@@ -38,6 +39,7 @@ export abstract class TransformableNode<C extends PixiContainer, E extends Event
38
39
  if (options.pivotY !== undefined) this.pivotY = options.pivotY
39
40
  if (options.rotation !== undefined) this.rotation = options.rotation
40
41
  if (options.alpha !== undefined) this.alpha = options.alpha
42
+ if (options.drawOrder !== undefined) this.drawOrder = options.drawOrder
41
43
 
42
44
  this.#layer = options.layer
43
45
  this.#useYSort = options.useYSort ?? false
@@ -75,7 +77,7 @@ export abstract class TransformableNode<C extends PixiContainer, E extends Event
75
77
  } else {
76
78
  const lt = this.localTransform
77
79
  pc.position.set(lt.x, lt.y)
78
- if (this.#useYSort) pc.zIndex = lt.y
80
+ if (this.#useYSort) this.drawOrder = lt.y
79
81
  pc.pivot.set(lt.pivotX, lt.pivotY)
80
82
  pc.scale.set(lt.scaleX, lt.scaleY)
81
83
  pc.rotation = lt.rotation
@@ -108,4 +110,7 @@ export abstract class TransformableNode<C extends PixiContainer, E extends Event
108
110
 
109
111
  set rotation(v) { this.localTransform.rotation = v }
110
112
  get rotation() { return this.localTransform.rotation }
113
+
114
+ set drawOrder(v) { this._pixiContainer.zIndex = v }
115
+ get drawOrder() { return this._pixiContainer.zIndex }
111
116
  }
@@ -10,7 +10,7 @@ export type AnimatedSpriteNodeOptions = {
10
10
  animation: string
11
11
  } & GameObjectOptions
12
12
 
13
- export class AnimatedSpriteNode<E extends EventMap = EventMap> extends GameObject<E & {
13
+ export class AnimatedSpriteNode<E extends EventMap = {}> extends GameObject<E & {
14
14
  animationend: (animation: string) => void
15
15
  }> {
16
16
  #src: string
@@ -46,25 +46,26 @@ export class AnimatedSpriteNode<E extends EventMap = EventMap> extends GameObjec
46
46
  this.#sprite?.destroy()
47
47
  this.#sprite = undefined
48
48
 
49
- if (this.#sheet) {
50
- if (!this.#sheet.animations[this.#animation]) {
51
- console.error(`Animation not found: ${this.#animation}`)
52
- return
53
- }
49
+ if (!this.#sheet) return
54
50
 
55
- const a = this.#atlas.animations[this.#animation]
56
- const s = new PixiAnimatedSprite(this.#sheet.animations[this.#animation])
51
+ if (!this.#sheet.animations[this.#animation]) {
52
+ console.error(`Animation not found: ${this.#animation}`)
53
+ return
54
+ }
57
55
 
58
- s.anchor.set(0.5, 0.5)
59
- s.loop = a.loop
60
- s.animationSpeed = a.fps / 60
61
- s.play()
62
- s.onLoop = () => (this as any).emit('animationend', this.#animation)
63
- s.onComplete = () => (this as any).emit('animationend', this.#animation)
56
+ const a = this.#atlas.animations[this.#animation]
57
+ const s = new PixiAnimatedSprite(this.#sheet.animations[this.#animation])
64
58
 
65
- this._pixiContainer.addChild(s)
66
- this.#sprite = s
67
- }
59
+ s.anchor.set(0.5, 0.5)
60
+ s.zIndex = -999999
61
+ s.loop = a.loop
62
+ s.animationSpeed = a.fps / 60
63
+ s.play()
64
+ s.onLoop = () => (this as any).emit('animationend', this.#animation)
65
+ s.onComplete = () => (this as any).emit('animationend', this.#animation)
66
+
67
+ this._pixiContainer.addChild(s)
68
+ this.#sprite = s
68
69
  }
69
70
 
70
71
  set src(src) {
@@ -1,18 +1,21 @@
1
1
  import { EventMap } from '@webtaku/event-emitter'
2
2
  import { Rectangle as PixiRectangle, Sprite as PixiSprite, Texture as PixiTexture } from 'pixi.js'
3
3
  import { bitmapFontLoader } from '../../asset/loaders/bitmap-font'
4
+ import { BitmapFont } from '../../types/bitmap-font'
4
5
  import { GameObject, GameObjectOptions } from '../core/game-object'
5
6
 
6
7
  export type BitmapTextNodeOptions = {
7
8
  fnt: string
8
9
  src: string
9
- text: string
10
+ text?: string
10
11
  } & GameObjectOptions
11
12
 
12
- export class BitmapTextNode<E extends EventMap = EventMap> extends GameObject<E> {
13
+ export class BitmapTextNode<E extends EventMap = {}> extends GameObject<E> {
13
14
  #fnt: string
14
15
  #src: string
15
- #text: string
16
+ #text?: string
17
+
18
+ #font?: BitmapFont
16
19
  #sprites: PixiSprite[] = []
17
20
 
18
21
  constructor(options: BitmapTextNodeOptions) {
@@ -23,21 +26,14 @@ export class BitmapTextNode<E extends EventMap = EventMap> extends GameObject<E>
23
26
  this.#load()
24
27
  }
25
28
 
26
- async #load() {
27
- let font
28
- if (bitmapFontLoader.checkCached(this.#fnt)) {
29
- font = bitmapFontLoader.getCached(this.#fnt)
30
- } else {
31
- console.info(`Bitmap font not preloaded. Loading now: ${this.#fnt}`)
32
- font = await bitmapFontLoader.load(this.#fnt, this.#src)
33
- }
34
- if (!font) return
35
-
29
+ #updateText() {
36
30
  for (const sprite of this.#sprites) {
37
31
  sprite.destroy()
38
32
  }
39
33
  this.#sprites = []
40
34
 
35
+ if (!this.#font || !this.#text) return
36
+
41
37
  let xPos = 0, yPos = 0
42
38
  let minX = Infinity, minY = Infinity
43
39
  let maxX = -Infinity, maxY = -Infinity
@@ -47,16 +43,16 @@ export class BitmapTextNode<E extends EventMap = EventMap> extends GameObject<E>
47
43
 
48
44
  if (charCode === 10) {
49
45
  xPos = 0
50
- yPos += font.lineHeight
46
+ yPos += this.#font.lineHeight
51
47
  continue
52
48
  }
53
49
 
54
- const c = font.chars[charCode]
50
+ const c = this.#font.chars[charCode]
55
51
  if (!c) continue
56
52
 
57
53
  const frame = new PixiRectangle(c.x, c.y, c.width, c.height)
58
- const texture = new PixiTexture({ source: font.texture.source, frame })
59
- const sprite = new PixiSprite(texture)
54
+ const texture = new PixiTexture({ source: this.#font.texture.source, frame })
55
+ const sprite = new PixiSprite({ texture, zIndex: -999999 })
60
56
 
61
57
  const x0 = xPos + c.xoffset
62
58
  const y0 = yPos + c.yoffset
@@ -87,16 +83,27 @@ export class BitmapTextNode<E extends EventMap = EventMap> extends GameObject<E>
87
83
  maxY = 0
88
84
  }
89
85
 
90
- const width = maxX - minX
91
- const height = maxY - minY
86
+ const cx = (minX + maxX) / 2
87
+ const cy = (minY + maxY) / 2
92
88
 
93
89
  for (const s of this.#sprites) {
94
- s.x -= width / 2
95
- s.y -= height / 2
90
+ s.x -= cx
91
+ s.y -= cy
96
92
  this._pixiContainer.addChild(s)
97
93
  }
98
94
  }
99
95
 
96
+ async #load() {
97
+ if (bitmapFontLoader.checkCached(this.#fnt)) {
98
+ this.#font = bitmapFontLoader.getCached(this.#fnt)
99
+ } else {
100
+ console.info(`Bitmap font not preloaded. Loading now: ${this.#fnt}`)
101
+ this.#font = await bitmapFontLoader.load(this.#fnt, this.#src)
102
+ }
103
+
104
+ this.#updateText()
105
+ }
106
+
100
107
  changeFont(fnt: string, src: string) {
101
108
  if (this.#fnt !== fnt || this.#src !== src) {
102
109
  bitmapFontLoader.release(this.#fnt)
@@ -106,6 +113,15 @@ export class BitmapTextNode<E extends EventMap = EventMap> extends GameObject<E>
106
113
  }
107
114
  }
108
115
 
116
+ set text(text: string | undefined) {
117
+ this.#text = text
118
+ this.#updateText()
119
+ }
120
+
121
+ get text() {
122
+ return this.#text
123
+ }
124
+
109
125
  override remove() {
110
126
  bitmapFontLoader.release(this.#fnt)
111
127
  super.remove()
@@ -19,7 +19,7 @@ export type SpineNodeOptions = {
19
19
  loop?: boolean
20
20
  } & GameObjectOptions
21
21
 
22
- export class SpineNode<E extends EventMap = EventMap> extends GameObject<E & {
22
+ export class SpineNode<E extends EventMap = {}> extends GameObject<E & {
23
23
  animationend: (animation: string) => void
24
24
  }> {
25
25
  #atlas: string
@@ -7,7 +7,7 @@ export type SpriteNodeOptions = {
7
7
  src: string
8
8
  } & GameObjectOptions
9
9
 
10
- export class SpriteNode<E extends EventMap = EventMap> extends GameObject<E> {
10
+ export class SpriteNode<E extends EventMap = {}> extends GameObject<E> {
11
11
  #src: string
12
12
  #sprite?: PixiSprite
13
13
 
@@ -21,11 +21,12 @@ export type PhysicsObjectOptions = {
21
21
  useYSort?: boolean
22
22
  }
23
23
 
24
- export class PhysicsObject<E extends EventMap = EventMap> extends RenderableNode<PixiContainer, E> {
24
+ export class PhysicsObject<E extends EventMap = {}> extends RenderableNode<PixiContainer, E> {
25
25
  #localTransform = new LocalTransform()
26
26
  #matterBody: Matter.Body
27
27
 
28
28
  #useYSort = false
29
+ #collisionsEnabled = true
29
30
 
30
31
  constructor(options: PhysicsObjectOptions) {
31
32
  super(new PixiContainer({ sortableChildren: true }))
@@ -65,7 +66,7 @@ export class PhysicsObject<E extends EventMap = EventMap> extends RenderableNode
65
66
  : parent.constructor?.name ?? typeof parent
66
67
  throw new Error(`PhysicsObject parent must be PhysicsWorld, but got ${actual}`)
67
68
  }
68
- parent.addBody(this.#matterBody)
69
+ if (this.#collisionsEnabled) parent.addBody(this.#matterBody)
69
70
  super.parent = parent
70
71
  }
71
72
 
@@ -122,6 +123,12 @@ export class PhysicsObject<E extends EventMap = EventMap> extends RenderableNode
122
123
  get isStatic() { return this.#matterBody.isStatic }
123
124
 
124
125
  disableCollisions() {
126
+ this.#collisionsEnabled = false
125
127
  this.#removeFromWorld()
126
128
  }
129
+
130
+ enableCollisions() {
131
+ this.#collisionsEnabled = true;
132
+ (this.parent as PhysicsWorld)?.addBody(this.#matterBody)
133
+ }
127
134
  }
@@ -7,7 +7,7 @@ export type PhysicsWorldOptions = {
7
7
  gravity?: number
8
8
  }
9
9
 
10
- export class PhysicsWorld<E extends EventMap = EventMap> extends RenderableNode<PixiContainer, E> {
10
+ export class PhysicsWorld<E extends EventMap = {}> extends RenderableNode<PixiContainer, E> {
11
11
  #matterEngine = Matter.Engine.create()
12
12
 
13
13
  constructor(options?: PhysicsWorldOptions) {