@safe-engine/pixi 8.4.7 → 8.5.4

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 (117) hide show
  1. package/README.md +17 -25
  2. package/dist/app.d.ts.map +1 -1
  3. package/dist/app.js +0 -23
  4. package/dist/box2d-wasm/ContactListener.d.ts +1 -1
  5. package/dist/box2d-wasm/ContactListener.d.ts.map +1 -1
  6. package/dist/box2d-wasm/ContactListener.js +50 -18
  7. package/dist/box2d-wasm/PhysicsComponent.d.ts +27 -30
  8. package/dist/box2d-wasm/PhysicsComponent.d.ts.map +1 -1
  9. package/dist/box2d-wasm/PhysicsComponent.js +44 -11
  10. package/dist/box2d-wasm/PhysicsSprite.d.ts +3 -0
  11. package/dist/box2d-wasm/PhysicsSprite.d.ts.map +1 -1
  12. package/dist/box2d-wasm/PhysicsSprite.js +24 -3
  13. package/dist/box2d-wasm/PhysicsSystem.d.ts +5 -3
  14. package/dist/box2d-wasm/PhysicsSystem.d.ts.map +1 -1
  15. package/dist/box2d-wasm/PhysicsSystem.js +135 -45
  16. package/dist/collider/CollideComponent.d.ts +27 -10
  17. package/dist/collider/CollideComponent.d.ts.map +1 -1
  18. package/dist/collider/CollideComponent.js +10 -12
  19. package/dist/collider/helper/Intersection.d.ts +6 -6
  20. package/dist/collider/helper/Intersection.d.ts.map +1 -1
  21. package/dist/collider/helper/Intersection.js +4 -4
  22. package/dist/collider/index.d.ts +1 -0
  23. package/dist/collider/index.d.ts.map +1 -1
  24. package/dist/collider/index.js +1 -0
  25. package/dist/components/NodeComp.d.ts +62 -36
  26. package/dist/components/NodeComp.d.ts.map +1 -1
  27. package/dist/components/NodeComp.js +18 -99
  28. package/dist/core/Color.d.ts.map +1 -1
  29. package/dist/dragonbones/DragonBonesComponent.d.ts +3 -2
  30. package/dist/dragonbones/DragonBonesComponent.d.ts.map +1 -1
  31. package/dist/dragonbones/DragonBonesComponent.js +1 -1
  32. package/dist/dragonbones/DragonBonesSystem.js +2 -2
  33. package/dist/gui/GUISystem.d.ts.map +1 -1
  34. package/dist/gui/GUISystem.js +5 -6
  35. package/dist/helper/utils.d.ts +0 -6
  36. package/dist/helper/utils.d.ts.map +1 -1
  37. package/dist/helper/utils.js +0 -12
  38. package/dist/index.d.ts +0 -5
  39. package/dist/index.d.ts.map +1 -1
  40. package/dist/index.js +0 -5
  41. package/dist/norender/NoRenderComponent.d.ts +5 -1
  42. package/dist/norender/NoRenderComponent.d.ts.map +1 -1
  43. package/dist/norender/NoRenderSystem.d.ts.map +1 -1
  44. package/dist/norender/NoRenderSystem.js +8 -9
  45. package/dist/render/RenderComponent.d.ts +6 -17
  46. package/dist/render/RenderComponent.d.ts.map +1 -1
  47. package/dist/render/RenderComponent.js +26 -18
  48. package/dist/spine/lib/require-shim.d.ts.map +1 -1
  49. package/dist/spine/lib/require-shim.js +1 -1
  50. package/package.json +6 -7
  51. package/.github/workflows/npm-publish.yml +0 -35
  52. package/src/app.ts +0 -91
  53. package/src/base/EnhancedComponent.ts +0 -38
  54. package/src/base/gworld.ts +0 -20
  55. package/src/base/index.ts +0 -14
  56. package/src/base/utils.ts +0 -23
  57. package/src/box2d-wasm/ContactListener.ts +0 -66
  58. package/src/box2d-wasm/PhysicsComponent.ts +0 -83
  59. package/src/box2d-wasm/PhysicsSprite.ts +0 -42
  60. package/src/box2d-wasm/PhysicsSystem.ts +0 -142
  61. package/src/box2d-wasm/debugDraw.ts +0 -249
  62. package/src/box2d-wasm/index.ts +0 -19
  63. package/src/collider/CollideComponent.ts +0 -257
  64. package/src/collider/CollideSystem.ts +0 -166
  65. package/src/collider/helper/Intersection.ts +0 -139
  66. package/src/collider/helper/utils.ts +0 -37
  67. package/src/collider/index.ts +0 -16
  68. package/src/components/BaseComponent.ts +0 -17
  69. package/src/components/NodeComp.ts +0 -442
  70. package/src/components/Scene.ts +0 -17
  71. package/src/core/Color.ts +0 -7
  72. package/src/core/LoadingBar.ts +0 -63
  73. package/src/core/NodePool.ts +0 -28
  74. package/src/core/Size.ts +0 -21
  75. package/src/core/Vec2.ts +0 -108
  76. package/src/core/director.ts +0 -11
  77. package/src/core/index.ts +0 -7
  78. package/src/core/loader.ts +0 -14
  79. package/src/core/math.ts +0 -22
  80. package/src/dragonbones/DragonBonesComponent.ts +0 -32
  81. package/src/dragonbones/DragonBonesSystem.ts +0 -35
  82. package/src/dragonbones/index.ts +0 -11
  83. package/src/gui/GUIComponent.ts +0 -155
  84. package/src/gui/GUISystem.ts +0 -125
  85. package/src/helper/utils.ts +0 -50
  86. package/src/index.ts +0 -21
  87. package/src/norender/NoRenderComponent.ts +0 -60
  88. package/src/norender/NoRenderSystem.ts +0 -67
  89. package/src/norender/Touch.ts +0 -37
  90. package/src/planck/PhysicsComponent.ts +0 -83
  91. package/src/planck/PhysicsSprite.ts +0 -43
  92. package/src/planck/PhysicsSystem.ts +0 -201
  93. package/src/planck/index.ts +0 -3
  94. package/src/render/RenderComponent.ts +0 -132
  95. package/src/render/RenderSystem.ts +0 -64
  96. package/src/richtext/RichTextComp.ts +0 -50
  97. package/src/richtext/RichTextSystem.ts +0 -26
  98. package/src/richtext/html-text-parser.ts +0 -87
  99. package/src/richtext/index.ts +0 -8
  100. package/src/spine/SpineComponent.ts +0 -18
  101. package/src/spine/SpineSystem.ts +0 -30
  102. package/src/spine/index.ts +0 -11
  103. package/src/spine/lib/BatchableSpineSlot.ts +0 -138
  104. package/src/spine/lib/Spine.ts +0 -894
  105. package/src/spine/lib/SpineDebugRenderer.ts +0 -615
  106. package/src/spine/lib/SpinePipe.ts +0 -203
  107. package/src/spine/lib/SpineTexture.ts +0 -143
  108. package/src/spine/lib/assets/atlasLoader.ts +0 -158
  109. package/src/spine/lib/assets/skeletonLoader.ts +0 -81
  110. package/src/spine/lib/darktint/DarkTintBatchGeometry.ts +0 -92
  111. package/src/spine/lib/darktint/DarkTintBatcher.ts +0 -186
  112. package/src/spine/lib/darktint/DarkTintShader.ts +0 -74
  113. package/src/spine/lib/darktint/darkTintBit.ts +0 -77
  114. package/src/spine/lib/index.ts +0 -43
  115. package/src/spine/lib/require-shim.ts +0 -43
  116. package/tsconfig.json +0 -18
  117. /package/{src/@types → @types}/index.d.ts +0 -0
package/src/app.ts DELETED
@@ -1,91 +0,0 @@
1
- import { actionManager } from 'pixi-action-ease'
2
- import { Application, AssetsClass, Renderer } from 'pixi.js'
3
-
4
- import { GameWorld } from './base'
5
- import { CollideSystem } from './collider'
6
- import { GUISystem } from './gui/GUISystem'
7
- import { NoRenderSystem } from './norender/NoRenderSystem'
8
- import { RenderSystem } from './render/RenderSystem'
9
-
10
- export async function startGame(
11
- defaultFont,
12
- designedResolution = { width: 720, height: 1280 },
13
- assetManager: AssetsClass,
14
- id = 'gameCanvas',
15
- ): Promise<Application<Renderer>> {
16
- const app = new Application()
17
- await app.init({
18
- antialias: true,
19
- resolution: window.devicePixelRatio,
20
- resizeTo: window,
21
- eventFeatures: {
22
- move: true,
23
- /** disables the global move events which can be very expensive in large scenes */
24
- globalMove: false,
25
- click: true,
26
- wheel: false,
27
- },
28
- canvas: document.getElementById(id) as HTMLCanvasElement,
29
- })
30
- // GameWorld.Instance.setup(NodeComp, app.stage)
31
- Object.assign(app.canvas.style, {
32
- width: `${window.innerWidth}px`,
33
- height: `${window.innerHeight}px`,
34
- overflow: 'visible',
35
- })
36
- const { width, height } = designedResolution
37
- app.renderer.resize(width, height)
38
- // app.stage.position.y = app.renderer.height / app.renderer.resolution
39
- // app.stage.scale.y = -1
40
- GameWorld.Instance.app = app
41
- GameWorld.Instance.assetManager = assetManager
42
- initWorld(defaultFont)
43
- startGameLoop(GameWorld.Instance)
44
- return app
45
- }
46
-
47
- function startGameLoop(world: GameWorld) {
48
- // Listen for frame updates
49
- world.app.ticker.add(() => {
50
- const dt = world.app.ticker.deltaMS * 0.001
51
- actionManager.update(dt)
52
- world.update(dt)
53
- })
54
- // app.ticker.speed = 0.5
55
- }
56
-
57
- // const systemsList = [RenderSystem, GUISystem, SpineSystem, DragonBonesSystem, CollideSystem, NoRenderSystem]
58
- // export function startGameSystems(list = []) {
59
- // const world = GameWorld.Instance
60
- // systemsList.forEach(system => {
61
- // world.systems.add(system)
62
- // world.systems.configureOnce(system)
63
- // })
64
- // world.listUpdate.push(CollideSystem)
65
- // // world.listUpdate.push(PhysicsSystem)
66
- // list.forEach(system => {
67
- // world.systems.add(system)
68
- // world.systems.configureOnce(system)
69
- // world.listUpdate.push(system)
70
- // })
71
- // startGameLoop(world)
72
- // // console.log('startGameLoop', world.listUpdate)
73
- // }
74
-
75
- function initWorld(defaultFont?: string) {
76
- const world = GameWorld.Instance
77
- world.systems.add(RenderSystem)
78
- world.systems.add(CollideSystem)
79
- world.systems.add(GUISystem)
80
- world.systems.add(NoRenderSystem)
81
- world.listUpdate.push(CollideSystem)
82
- world.systems.configureOnce(RenderSystem)
83
- world.systems.configureOnce(CollideSystem)
84
- world.systems.configureOnce(GUISystem)
85
- world.systems.configureOnce(NoRenderSystem)
86
- if (defaultFont) {
87
- const guiSystem = world.systems.get(GUISystem)
88
- guiSystem.defaultFont = defaultFont
89
- }
90
- // startGameLoop(world, app)
91
- }
@@ -1,38 +0,0 @@
1
- import { Constructor } from 'entityx-ts'
2
- import { BaseComponentProps } from '..'
3
- import { NodeComp } from '../components/NodeComp'
4
-
5
- export class EnhancedComponent<Props = {}, N extends NodeComp<any> = NodeComp<any>> {
6
- props: Props = {} as any
7
- enabled = true
8
- constructor(data?: BaseComponentProps<EnhancedComponent> & Props) {
9
- this.init(data)
10
- }
11
- init(data?: Props) {
12
- if (data) {
13
- // console.log('constructor', this.constructor.name, data)
14
- Object.keys(data).forEach((key) => {
15
- this.props[key] = data[key]
16
- })
17
- }
18
- }
19
- node: N
20
-
21
- addComponent<T extends EnhancedComponent>(instance): T {
22
- return this.node.addComponent(instance)
23
- }
24
- getComponent<T extends ComponentType>(component: Constructor<T>): T {
25
- return this.node.getComponent(component)
26
- }
27
- getComponentsInChildren<T extends ComponentType>(component: Constructor<T>): T[] {
28
- return this.node.getComponentsInChildren(component)
29
- }
30
- getComponentInChildren<T extends ComponentType>(component: Constructor<T>): T {
31
- return this.node.getComponentInChildren(component)
32
- }
33
- isEqual(other: EnhancedComponent) {
34
- return this.node.entity.id === other.node.entity.id
35
- }
36
- }
37
-
38
- export type ComponentType = EnhancedComponent | NodeComp
@@ -1,20 +0,0 @@
1
- import { Constructor, System, World } from 'entityx-ts'
2
- import { Application, AssetsClass } from 'pixi.js'
3
-
4
- export class GameWorld extends World {
5
- listUpdate: (System | Constructor<System>)[] = []
6
- app: Application
7
- assetManager: AssetsClass
8
- update(dt: number) {
9
- this.listUpdate.forEach((system: any) => {
10
- this.systems.update(system, dt)
11
- })
12
- }
13
-
14
- private static _instance: GameWorld
15
-
16
- public static get Instance() {
17
- // Do you need arguments? Make it a regular static method instead.
18
- return this._instance || (this._instance = new this())
19
- }
20
- }
package/src/base/index.ts DELETED
@@ -1,14 +0,0 @@
1
- import { NodeComp } from '../components/NodeComp'
2
-
3
- export * from './EnhancedComponent'
4
- export * from './gworld'
5
- export * from './utils'
6
-
7
- export interface BaseComponentProps<T> {
8
- $ref?: T
9
- $push?: T[]
10
- $refNode?: NodeComp
11
- $pushNode?: NodeComp[]
12
- node?: Partial<NodeComp>
13
- // [$key: `$${string}`]: string
14
- }
package/src/base/utils.ts DELETED
@@ -1,23 +0,0 @@
1
- import { Constructor } from 'entityx-ts'
2
-
3
- import { GameWorld } from '..'
4
- import { ComponentX, NoRenderComponentX } from '../components/BaseComponent'
5
-
6
- export type GetProps<T> = T extends ComponentX<infer P> ? P : T extends NoRenderComponentX<infer Q> ? Q : never
7
-
8
- export function instantiate<T extends ComponentX>(ComponentType: Constructor<T>, data?: GetProps<T>): T {
9
- const instance = new ComponentType()
10
- instance.init(data)
11
- // console.log(ComponentType.name, data, instance)
12
- if (!instance.render) {
13
- return instance
14
- }
15
- return instance.render()
16
- }
17
-
18
- export function loadScene<T extends ComponentX>(ComponentType: Constructor<T>) {
19
- const world = GameWorld.Instance
20
- world.entities.reset()
21
- const instance = new ComponentType()
22
- instance.render()
23
- }
@@ -1,66 +0,0 @@
1
- import { NodeComp } from "..";
2
- import { ColliderPhysics } from "./PhysicsComponent";
3
- type Meta = { [key: number]: NodeComp }
4
- export const makeContactListener = (world: Box2D.b2World, metadata: Meta, box2D: typeof Box2D) => {
5
- const { JSContactListener, getPointer, NULL } = box2D
6
- function getContactById(contact: number) {
7
- for (let contactList = world.GetContactList(); getPointer(contactList) !== getPointer(NULL); contactList = contactList.GetNext()) {
8
- if (getPointer(contactList) === contact) {
9
- console.log('contactBegin', contact, getPointer(contactList), getPointer(NULL));
10
- return contactList
11
- }
12
- }
13
- }
14
- const listener = Object.assign(new JSContactListener(), {
15
- BeginContact(contact: Box2D.b2Contact | number): void {
16
- if (typeof contact === 'number') {
17
- contact = getContactById(contact)
18
- }
19
- const ett1: NodeComp = metadata[getPointer(contact.GetFixtureA().GetBody())]
20
- const ett2: NodeComp = metadata[getPointer(contact.GetFixtureB().GetBody())]
21
- // world.addPostStepCallback(() => {
22
- // // log('addPostStepCallback');
23
- // listRemoveShape.forEach((s) => world.removeShape(s))
24
- // listRemoveBody.forEach((b) => world.removeBody(b))
25
- // listRemoveBody = []
26
- // listRemoveShape = []
27
- // })
28
- const phys1 = ett1.getComponent(ColliderPhysics)
29
- const phys2 = ett2.getComponent(ColliderPhysics)
30
- if (phys1 && phys2) {
31
- if (Object.prototype.hasOwnProperty.call(phys1, 'onCollisionEnter')) {
32
- phys1.props.onCollisionEnter(phys2)
33
- }
34
- if (Object.prototype.hasOwnProperty.call(phys2, 'onCollisionEnter')) {
35
- phys2.props.onCollisionEnter(phys1)
36
- }
37
- }
38
- },
39
- EndContact(contact: Box2D.b2Contact | number): void {
40
- if (typeof contact === 'number') {
41
- contact = getContactById(contact)
42
- }
43
- const ett1: NodeComp = metadata[getPointer(contact.GetFixtureA().GetBody())]
44
- const ett2: NodeComp = metadata[getPointer(contact.GetFixtureB().GetBody())]
45
- // const event1 = ett1.getComponent(NodeComp)
46
- const phys1 = ett1.getComponent(ColliderPhysics)
47
- const phys2 = ett2.getComponent(ColliderPhysics)
48
- // const event2 = ett2.getComponent(NodeComp)
49
- if (phys1 && phys2) {
50
- if (Object.prototype.hasOwnProperty.call(phys1, 'onCollisionExit')) {
51
- phys1.props.onCollisionExit(phys2)
52
- }
53
- if (Object.prototype.hasOwnProperty.call(phys2, 'onCollisionExit')) {
54
- phys2.props.onCollisionExit(phys1)
55
- }
56
- }
57
- },
58
- PreSolve(contact: Box2D.b2Contact | number, oldManifold: Box2D.b2Manifold | number): void {
59
- console.log('collisionSeparate');
60
- },
61
- PostSolve(contact: Box2D.b2Contact | number, impulse: Box2D.b2ContactImpulse | number): void {
62
- console.log('collisionPost');
63
- }
64
- });
65
- return listener
66
- }
@@ -1,83 +0,0 @@
1
- import { BodyType, Vec2 } from 'planck'
2
- import { NoRenderComponentX } from '..'
3
- import { PhysicsSprite } from './PhysicsSprite'
4
-
5
- interface RigidBodyProps {
6
- type?: BodyType
7
- density?: Float
8
- restitution?: Float
9
- friction?: Float
10
- gravityScale?: Float
11
- }
12
-
13
- export class RigidBody extends NoRenderComponentX<RigidBodyProps> {
14
- body: Box2D.b2Body
15
- // set linearVelocity(vel: Vec2) {
16
- // if (!this.node) {
17
- // return
18
- // }
19
- // const physics = this.node.instance
20
- // if (physics instanceof Sprite) {
21
- // physics.getBody().setVel(vel)
22
- // }
23
- // }
24
-
25
- // get linearVelocity() {
26
- // if (!this.node) {
27
- // return Vec2.ZERO
28
- // }
29
- // const physics = this.node.instance
30
- // const vel = (physics as Sprite).getBody().getVel()
31
- // return v2(vel)
32
- // }
33
- }
34
-
35
- interface PhysicsMaterialProps {
36
- friction?: number
37
- restitution?: number
38
- density?: number
39
- }
40
- export class PhysicsMaterial extends NoRenderComponentX<PhysicsMaterialProps> {
41
-
42
- }
43
-
44
- interface ColliderPhysicsProps {
45
- tag?: number
46
- group?: number
47
- offset?: Vec2
48
- onCollisionEnter?: (other: ColliderPhysics) => void
49
- onCollisionExit?: (other: ColliderPhysics) => void
50
- onCollisionStay?: (other: ColliderPhysics) => void
51
- }
52
-
53
- export class ColliderPhysics<T extends ColliderPhysicsProps = ColliderPhysicsProps> extends NoRenderComponentX<T, PhysicsSprite['node']> {
54
- enabled = true
55
- instance: PhysicsSprite
56
- }
57
-
58
- interface BoxColliderPhysicsProps extends ColliderPhysicsProps {
59
- width: number
60
- height: number
61
- }
62
- export class BoxColliderPhysics extends ColliderPhysics<BoxColliderPhysicsProps> {
63
-
64
- // set onCollisionEnter(val) {
65
- // const phys1 = this.getComponent(ColliderPhysics)
66
- // phys1._onCollisionEnter = val
67
- // }
68
-
69
- // get onCollisionEnter() {
70
- // const phys1 = this.getComponent(ColliderPhysics)
71
- // return phys1._onCollisionEnter
72
- // }
73
- }
74
- interface CircleColliderPhysicsProps extends ColliderPhysicsProps {
75
- radius: number
76
- }
77
- export class CircleColliderPhysics extends ColliderPhysics<CircleColliderPhysicsProps> {
78
- }
79
- interface PolygonColliderPhysicsProps extends ColliderPhysicsProps {
80
- points: Array<Vec2>
81
- }
82
- export class PolygonColliderPhysics extends ColliderPhysics<PolygonColliderPhysicsProps> {
83
- }
@@ -1,42 +0,0 @@
1
- import { Container } from 'pixi.js'
2
-
3
- export class PhysicsSprite {
4
- node: Container
5
- physicsBody: Box2D.b2Body
6
-
7
- constructor(node: Container, body: Box2D.b2Body) {
8
- this.node = node
9
- this.physicsBody = body
10
- }
11
-
12
- // set position(val: Box2D.b2Vec2) {
13
- // this.physicsBody.setPosition(val)
14
- // }
15
-
16
- // set x(val) {
17
- // this.physicsBody.setPosition(Vec2(val, this.y))
18
- // }
19
- // set y(val) {
20
- // this.physicsBody.setPosition(Vec2(this.x, val))
21
- // }
22
-
23
- get x() {
24
- return this.physicsBody.GetPosition().x
25
- }
26
-
27
- get y() {
28
- return this.physicsBody.GetPosition().x
29
- }
30
-
31
- // set angle(val: number) {
32
- // this.physicsBody.setAngle(val)
33
- // }
34
-
35
- get angle() {
36
- return this.physicsBody.GetAngle()
37
- }
38
-
39
- addChild(child: Container) {
40
- this.node.addChild(child)
41
- }
42
- }
@@ -1,142 +0,0 @@
1
- import Box2DFactory from 'box2d-wasm'
2
- import { EntityManager, EventManager, EventTypes, System } from 'entityx-ts'
3
- import { Graphics } from 'pixi.js'
4
-
5
- import { GameWorld, instantiate, NodeComp, Vec2 } from '..'
6
- import { makeContactListener } from './ContactListener'
7
- import { makeDebugDraw } from './debugDraw'
8
- import {
9
- BoxColliderPhysics,
10
- CircleColliderPhysics,
11
- ColliderPhysics,
12
- PhysicsMaterial,
13
- PolygonColliderPhysics,
14
- RigidBody,
15
- } from './PhysicsComponent'
16
- import { PhysicsSprite } from './PhysicsSprite'
17
-
18
- // Box2D.b2Fixture.prototype.shouldCollide = function (other) {
19
- // const nodeThis: NodeComp = this.getBody().getUserData()
20
- // const nodeOther = other.getBody().getUserData() as NodeComp
21
- // const { colliderMatrix } = GameWorld.Instance.systems.get(PhysicsSystem)
22
- // return colliderMatrix[nodeOther.group][nodeThis.group]
23
- // }
24
- export let box2D: typeof Box2D
25
- export async function initBox2d() {
26
- box2D = await Box2DFactory()
27
- }
28
-
29
- export function setColliderMatrix(colliderMatrix = [[true]]) {
30
- const physicsSystem = GameWorld.Instance.systems.get(PhysicsSystem)
31
- physicsSystem.colliderMatrix = colliderMatrix
32
- }
33
- const maxTimeStep = 1 / 60
34
- const velocityIterations = 1
35
- const positionIterations = 1
36
- const metadata: { [key: number]: NodeComp } = {}
37
- const pixelsPerMeter = 1
38
-
39
- export class PhysicsSystem implements System {
40
- world: Box2D.b2World
41
- listRemoveBody: Body[] = []
42
- listRemoveShape: Box2D.b2Shape[] = []
43
- colliderMatrix = [[true]]
44
- graphics: Graphics
45
-
46
- addDebug() {
47
- const debugDraw = makeDebugDraw(this.graphics, pixelsPerMeter, box2D)
48
- this.world.SetDebugDraw(debugDraw)
49
- }
50
-
51
- configure(event_manager: EventManager) {
52
- const { b2BodyDef, b2_dynamicBody, b2_staticBody, b2FixtureDef, b2PolygonShape, b2Vec2, b2World, getPointer, b2ContactListener } =
53
- box2D as typeof Box2D
54
- const gravity = new b2Vec2(0, 10)
55
- this.world = new b2World(gravity)
56
- console.log('configure world', (event_manager.world as GameWorld).app)
57
- // event_manager.world.physicsManager = this
58
- const graphics = new Graphics()
59
- this.graphics = graphics
60
- graphics.zIndex = 1000
61
- ;(event_manager.world as GameWorld).app.stage.addChild(graphics)
62
- // event_manager.subscribe(ComponentAddedEvent(RigidBody), this);
63
- event_manager.subscribe(EventTypes.ComponentAdded, BoxColliderPhysics, ({ entity, component }) => {
64
- console.log('ComponentAddedEvent BoxColliderPhysics', component)
65
- let rigidBody = entity.getComponent(RigidBody)
66
- if (!rigidBody) {
67
- rigidBody = instantiate(RigidBody)
68
- entity.assign(rigidBody)
69
- }
70
- const { type = 'static', gravityScale = 1 } = rigidBody.props
71
- const physicsMaterial = entity.getComponent(PhysicsMaterial)
72
- const { density = 1, friction = 0.5, restitution = 0.3 } = physicsMaterial?.props || {}
73
- const box = component
74
- const node = entity.getComponent(NodeComp)
75
- const { width, height, ...colliderProps } = box.props
76
- // ett.assign(instantiate(ColliderPhysics, { tag, offset }))
77
- const { x = 0, y = 0 } = colliderProps.offset || {}
78
- const zero = new b2Vec2(0, 0)
79
- const position = new b2Vec2(node.posX, node.posY)
80
- const offset = new b2Vec2(x, y)
81
-
82
- const bd = new b2BodyDef()
83
- bd.set_type(type === 'dynamic' ? b2_dynamicBody : b2_staticBody)
84
- bd.set_position(zero)
85
- bd.set_gravityScale(gravityScale)
86
- const body = this.world.CreateBody(bd)
87
- rigidBody.body = body
88
- // console.log('body', type, b2_dynamicBody, b2_staticBody, getPointer(body));
89
- // body.setMassData({ mass: 1 } as any)
90
- const physicsNode = new PhysicsSprite(node.instance, body)
91
- const square = new b2PolygonShape()
92
- square.SetAsBox(width / 2, height / 2)
93
- const fixtureDef = new b2FixtureDef()
94
- fixtureDef.set_shape(square)
95
- fixtureDef.set_density(density)
96
- fixtureDef.set_friction(friction)
97
- fixtureDef.set_restitution(restitution)
98
- body.CreateFixture(fixtureDef)
99
- body.SetTransform(position, 0)
100
- body.SetLinearVelocity(zero)
101
- body.SetAwake(true)
102
- body.SetEnabled(true)
103
- metadata[getPointer(body)] = node
104
-
105
- const physicsCollide = entity.assign(instantiate(ColliderPhysics, colliderProps))
106
- physicsCollide.instance = physicsNode
107
- physicsCollide.node = node
108
- box.node = node
109
- })
110
- event_manager.subscribe(EventTypes.ComponentAdded, CircleColliderPhysics, () => {})
111
- event_manager.subscribe(EventTypes.ComponentAdded, PolygonColliderPhysics, () => {})
112
- event_manager.subscribe(EventTypes.ComponentRemoved, NodeComp, () => {
113
- // log('ComponentRemovedEvent NodeComp', event);
114
- // const node = event.entity.getComponent(NodeComp)
115
- // if (node.instance instanceof Sprite) {
116
- // const body = node.instance.getBody()
117
- // this.listRemoveShape.push(...body.shapeList)
118
- // this.listRemoveBody.push(body)
119
- // }
120
- })
121
- const listener = makeContactListener(this.world, metadata, box2D)
122
- this.world.SetContactListener(listener)
123
- }
124
-
125
- update(entities: EntityManager, events: EventManager, dt: number) {
126
- if (this.world) {
127
- const clampedDelta = Math.min(dt, maxTimeStep)
128
- this.world.Step(clampedDelta, velocityIterations, positionIterations)
129
- this.graphics.clear()
130
- this.world.DebugDraw()
131
- this.graphics.fill()
132
- // this.graphics.stroke();
133
- // console.log('GetBodyCount', this.world.GetBodyCount());
134
- }
135
- }
136
-
137
- set gravity(val: Vec2) {
138
- this.world.SetGravity(new box2D.b2Vec2(val.x, val.y))
139
- // this.world.iterations = 60
140
- // this.world.collisionSlop = 0.5
141
- }
142
- }