@safe-engine/pixi 8.4.7 → 8.5.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 (92) hide show
  1. package/README.md +14 -23
  2. package/dist/box2d-wasm/ContactListener.d.ts +1 -1
  3. package/dist/box2d-wasm/ContactListener.d.ts.map +1 -1
  4. package/dist/box2d-wasm/ContactListener.js +50 -18
  5. package/dist/box2d-wasm/PhysicsComponent.d.ts +27 -30
  6. package/dist/box2d-wasm/PhysicsComponent.d.ts.map +1 -1
  7. package/dist/box2d-wasm/PhysicsComponent.js +44 -11
  8. package/dist/box2d-wasm/PhysicsSprite.d.ts +3 -0
  9. package/dist/box2d-wasm/PhysicsSprite.d.ts.map +1 -1
  10. package/dist/box2d-wasm/PhysicsSprite.js +24 -3
  11. package/dist/box2d-wasm/PhysicsSystem.d.ts +4 -2
  12. package/dist/box2d-wasm/PhysicsSystem.d.ts.map +1 -1
  13. package/dist/box2d-wasm/PhysicsSystem.js +134 -44
  14. package/dist/collider/CollideComponent.d.ts +27 -10
  15. package/dist/collider/CollideComponent.d.ts.map +1 -1
  16. package/dist/collider/CollideComponent.js +10 -12
  17. package/dist/collider/helper/Intersection.d.ts +6 -6
  18. package/dist/collider/helper/Intersection.d.ts.map +1 -1
  19. package/dist/collider/helper/Intersection.js +4 -4
  20. package/dist/helper/utils.d.ts +0 -6
  21. package/dist/helper/utils.d.ts.map +1 -1
  22. package/dist/helper/utils.js +0 -12
  23. package/package.json +6 -2
  24. package/.github/workflows/npm-publish.yml +0 -35
  25. package/dist/@types/safex.d.ts +0 -15
  26. package/src/@types/index.d.ts +0 -2
  27. package/src/app.ts +0 -91
  28. package/src/base/EnhancedComponent.ts +0 -38
  29. package/src/base/gworld.ts +0 -20
  30. package/src/base/index.ts +0 -14
  31. package/src/base/utils.ts +0 -23
  32. package/src/box2d-wasm/ContactListener.ts +0 -66
  33. package/src/box2d-wasm/PhysicsComponent.ts +0 -83
  34. package/src/box2d-wasm/PhysicsSprite.ts +0 -42
  35. package/src/box2d-wasm/PhysicsSystem.ts +0 -142
  36. package/src/box2d-wasm/debugDraw.ts +0 -249
  37. package/src/box2d-wasm/index.ts +0 -19
  38. package/src/collider/CollideComponent.ts +0 -257
  39. package/src/collider/CollideSystem.ts +0 -166
  40. package/src/collider/helper/Intersection.ts +0 -139
  41. package/src/collider/helper/utils.ts +0 -37
  42. package/src/collider/index.ts +0 -16
  43. package/src/components/BaseComponent.ts +0 -17
  44. package/src/components/NodeComp.ts +0 -442
  45. package/src/components/Scene.ts +0 -17
  46. package/src/core/Color.ts +0 -7
  47. package/src/core/LoadingBar.ts +0 -63
  48. package/src/core/NodePool.ts +0 -28
  49. package/src/core/Size.ts +0 -21
  50. package/src/core/Vec2.ts +0 -108
  51. package/src/core/director.ts +0 -11
  52. package/src/core/index.ts +0 -7
  53. package/src/core/loader.ts +0 -14
  54. package/src/core/math.ts +0 -22
  55. package/src/dragonbones/DragonBonesComponent.ts +0 -32
  56. package/src/dragonbones/DragonBonesSystem.ts +0 -35
  57. package/src/dragonbones/index.ts +0 -11
  58. package/src/gui/GUIComponent.ts +0 -155
  59. package/src/gui/GUISystem.ts +0 -125
  60. package/src/helper/utils.ts +0 -50
  61. package/src/index.ts +0 -21
  62. package/src/norender/NoRenderComponent.ts +0 -60
  63. package/src/norender/NoRenderSystem.ts +0 -67
  64. package/src/norender/Touch.ts +0 -37
  65. package/src/planck/PhysicsComponent.ts +0 -83
  66. package/src/planck/PhysicsSprite.ts +0 -43
  67. package/src/planck/PhysicsSystem.ts +0 -201
  68. package/src/planck/index.ts +0 -3
  69. package/src/render/RenderComponent.ts +0 -132
  70. package/src/render/RenderSystem.ts +0 -64
  71. package/src/richtext/RichTextComp.ts +0 -50
  72. package/src/richtext/RichTextSystem.ts +0 -26
  73. package/src/richtext/html-text-parser.ts +0 -87
  74. package/src/richtext/index.ts +0 -8
  75. package/src/spine/SpineComponent.ts +0 -18
  76. package/src/spine/SpineSystem.ts +0 -30
  77. package/src/spine/index.ts +0 -11
  78. package/src/spine/lib/BatchableSpineSlot.ts +0 -138
  79. package/src/spine/lib/Spine.ts +0 -894
  80. package/src/spine/lib/SpineDebugRenderer.ts +0 -615
  81. package/src/spine/lib/SpinePipe.ts +0 -203
  82. package/src/spine/lib/SpineTexture.ts +0 -143
  83. package/src/spine/lib/assets/atlasLoader.ts +0 -158
  84. package/src/spine/lib/assets/skeletonLoader.ts +0 -81
  85. package/src/spine/lib/darktint/DarkTintBatchGeometry.ts +0 -92
  86. package/src/spine/lib/darktint/DarkTintBatcher.ts +0 -186
  87. package/src/spine/lib/darktint/DarkTintShader.ts +0 -74
  88. package/src/spine/lib/darktint/darkTintBit.ts +0 -77
  89. package/src/spine/lib/index.ts +0 -43
  90. package/src/spine/lib/require-shim.ts +0 -43
  91. package/tsconfig.json +0 -18
  92. /package/{dist/@types → @types}/index.d.ts +0 -0
@@ -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
- }
@@ -1,249 +0,0 @@
1
- import { Graphics } from 'pixi.js'
2
-
3
- import { Color4B } from '..'
4
-
5
- /**
6
- * Forked from Box2D.js
7
- * @see https://github.com/kripken/box2d.js/blob/f75077b/helpers/embox2d-helpers.js
8
- * @author dmagunov + Huy Nguyen + fork contributions from Alex Birch
9
- * @see https://github.com/kripken/box2d.js/blob/49dddd6/helpers/embox2d-html5canvas-debugDraw.js
10
- * @author dmagunov + fork contributions from Alex Birch
11
- * @license Zlib https://opensource.org/licenses/Zlib
12
- * License evidence: https://github.com/kripken/box2d.js/blob/master/README.markdown#box2djs
13
- * "box2d.js is zlib licensed, just like Box2D."
14
- *
15
- * @typedef {import('box2d-wasm')} Box2DFactory
16
- * @param {CanvasRenderingContext2D} ctx
17
- * @param {number} pixelsPerMeter
18
- * @param {typeof Box2D & EmscriptenModule} box2D
19
- */
20
- export const makeDebugDraw = (graphics: Graphics, pixelsPerMeter, box2D: typeof Box2D) => {
21
- const {
22
- b2Color,
23
- b2Draw: { e_shapeBit },
24
- b2Transform,
25
- b2Vec2,
26
- JSDraw,
27
- wrapPointer,
28
- } = box2D
29
-
30
- /**
31
- * to replace original C++ operator =
32
- * @param {Box2D.b2Vec2} vec
33
- * @returns {Box2D.b2Vec2}
34
- */
35
- const copyVec2 = (vec: Box2D.b2Vec2) => new b2Vec2(vec.get_x(), vec.get_y())
36
-
37
- /**
38
- * to replace original C++ operator *= (float)
39
- * @param {Box2D.b2Vec2} vec
40
- * @param {number} scale
41
- * @returns {Box2D.b2Vec2}
42
- */
43
- const scaledVec2 = (vec: Box2D.b2Vec2, scale: number) => new b2Vec2(scale * vec.get_x(), scale * vec.get_y())
44
-
45
- /**
46
- * @param {Box2D.b2Color} color
47
- * @returns {string}
48
- */
49
- const getRgbStr = (color: Box2D.b2Color) => {
50
- const red = (color.get_r() * 255) | 0
51
- const green = (color.get_g() * 255) | 0
52
- const blue = (color.get_b() * 255) | 0
53
- return Color4B(red, green, blue, 0.1)
54
- }
55
-
56
- /**
57
- * @param {string} rgbStr
58
- * @returns {void}
59
- */
60
- const setCtxColor = (rgbStr: Color4B) => {
61
- graphics.fillStyle = rgbStr
62
- graphics.strokeStyle = { color: rgbStr }
63
- }
64
-
65
- /**
66
- * @param {Box2D.b2Vec2[]} vertices
67
- * @param {boolean} fill
68
- * @returns {void}
69
- */
70
- const drawPolygon = (vertices, fill) => {
71
- // console.log("drawPolygon", vertices[0].y, fill);
72
- // graphics.poly(vertices, fill)
73
- graphics.moveTo(vertices[vertices.length - 1].x * pixelsPerMeter, vertices[vertices.length - 1].y * pixelsPerMeter)
74
- vertices.forEach((v) => {
75
- graphics.lineTo(v.x * pixelsPerMeter, v.y * pixelsPerMeter)
76
- })
77
- graphics.fill()
78
- }
79
-
80
- /**
81
- * @param {Box2D.b2Vec2} center
82
- * @param {number} radius
83
- * @param {Box2D.b2Vec2} axis
84
- * @param {boolean} fill
85
- * @returns {void}
86
- */
87
- const drawCircle = (center, radius, axis, fill) => {
88
- // graphics.circle(center.x, center.y, radius)
89
- let angle = 0
90
- const angleStep = 32
91
- const n = 360 / angleStep
92
- let x = radius * Math.cos((angle * Math.PI) / 180)
93
- let y = radius * Math.sin((angle * Math.PI) / 180)
94
- graphics.moveTo(center.x + x, center.y + y)
95
- angle += angleStep
96
-
97
- for (let i = 0; i < n; i++) {
98
- x = radius * Math.cos((angle * Math.PI) / 180)
99
- y = radius * Math.sin((angle * Math.PI) / 180)
100
- graphics.lineTo(center.x + x, center.y + y)
101
- angle += angleStep
102
- }
103
- graphics.fill()
104
- }
105
-
106
- /**
107
- * @param {Box2D.b2Vec2} vert1
108
- * @param {Box2D.b2Vec2} vert2
109
- * @returns {void}
110
- */
111
- const drawSegment = (vert1, vert2) => {
112
- // console.log("drawSegment", vert1, vert2)
113
- graphics.moveTo(vert1.get_x(), vert1.get_y())
114
- graphics.lineTo(vert2.get_x(), vert2.get_y())
115
- graphics.stroke()
116
- }
117
-
118
- /**
119
- * @param {Box2D.b2Vec2} vertex
120
- * @param {number} sizeMetres
121
- * @returns {void}
122
- */
123
- const drawPoint = (vertex, sizeMetres) => {
124
- const sizePixels = sizeMetres / pixelsPerMeter
125
- graphics.rect(vertex.get_x() - sizePixels / 2, vertex.get_y() - sizePixels / 2, sizePixels, sizePixels)
126
- }
127
-
128
- /**
129
- * @param {Box2D.b2Transform} transform
130
- * @param {number} sizeMetres
131
- * @returns {void}
132
- */
133
- const drawTransform = (transform: Box2D.b2Transform) => {
134
- const pos = transform.get_p()
135
- const rot = transform.get_q()
136
-
137
- graphics.save()
138
- graphics.translateTransform(pos.get_x(), pos.get_y())
139
- graphics.scaleTransform(0.5, 0.5)
140
- graphics.rotateTransform(rot.GetAngle())
141
- // graphics.lineWidth *= 2;
142
- graphics.restore()
143
- }
144
-
145
- /** {@link Box2D.b2Vec2} is a struct of `float x, y` */
146
- const sizeOfB2Vec = Float32Array.BYTES_PER_ELEMENT * 2
147
-
148
- /**
149
- * @param {number} array_p pointer to {@link Box2D.b2Vec2}
150
- * @param {number} numElements length of array
151
- * @param {number} sizeOfElement size of an instance of the array element
152
- * @param {typeof Box2D.b2Vec2} ctor constructor for the array element
153
- * @return {Box2D.b2Vec2[]}
154
- */
155
- const reifyArray = (array_p, numElements, sizeOfElement, ctor) =>
156
- Array(numElements)
157
- .fill(undefined)
158
- .map((_, index) => wrapPointer(array_p + index * sizeOfElement, ctor))
159
-
160
- const debugDraw = Object.assign(new JSDraw(), {
161
- /**
162
- * @param {number} vert1_p pointer to {@link Box2D.b2Vec2}
163
- * @param {number} vert2_p pointer to {@link Box2D.b2Vec2}
164
- * @param {number} color_p pointer to {@link Box2D.b2Color}
165
- * @returns {void}
166
- */
167
- DrawSegment(vert1_p, vert2_p, color_p) {
168
- const color = wrapPointer(color_p, b2Color)
169
- setCtxColor(getRgbStr(color))
170
- const vert1 = wrapPointer(vert1_p, b2Vec2)
171
- const vert2 = wrapPointer(vert2_p, b2Vec2)
172
- drawSegment(vert1, vert2)
173
- },
174
- /**
175
- * @param {number} vertices_p pointer to Array<{@link Box2D.b2Vec2}>
176
- * @param {number} vertexCount
177
- * @param {number} color_p pointer to {@link Box2D.b2Color}
178
- * @returns {void}
179
- */
180
- DrawPolygon(vertices_p, vertexCount, color_p) {
181
- const color = wrapPointer(color_p, b2Color)
182
- setCtxColor(getRgbStr(color))
183
- const vertices = reifyArray(vertices_p, vertexCount, sizeOfB2Vec, b2Vec2)
184
- drawPolygon(vertices, false)
185
- // console.log(`DrawPolygon`, getRgbStr(color).green, vertices[0].y)
186
- },
187
- /**
188
- * @param {number} vertices_p pointer to Array<{@link Box2D.b2Vec2}>
189
- * @param {number} vertexCount
190
- * @param {number} color_p pointer to {@link Box2D.b2Color}
191
- * @returns {void}
192
- */
193
- DrawSolidPolygon(vertices_p, vertexCount, color_p) {
194
- const color = wrapPointer(color_p, b2Color)
195
- setCtxColor(getRgbStr(color))
196
- const vertices = reifyArray(vertices_p, vertexCount, sizeOfB2Vec, b2Vec2)
197
- drawPolygon(vertices, true)
198
- },
199
- /**
200
- * @param {number} center_p pointer to {@link Box2D.b2Vec2}
201
- * @param {number} radius
202
- * @param {number} color_p pointer to {@link Box2D.b2Color}
203
- * @returns {void}
204
- */
205
- DrawCircle(center_p, radius, color_p) {
206
- const color = wrapPointer(color_p, b2Color)
207
- setCtxColor(getRgbStr(color))
208
- const center = wrapPointer(center_p, b2Vec2)
209
- const dummyAxis = new b2Vec2(0, 0)
210
- drawCircle(center, radius, dummyAxis, false)
211
- },
212
- /**
213
- * @param {number} center_p pointer to {@link Box2D.b2Vec2}
214
- * @param {number} radius
215
- * @param {number} axis_p pointer to {@link Box2D.b2Vec2}
216
- * @param {number} color_p pointer to {@link Box2D.b2Color}
217
- * @returns {void}
218
- */
219
- DrawSolidCircle(center_p, radius, axis_p, color_p) {
220
- const color = wrapPointer(color_p, b2Color)
221
- setCtxColor(getRgbStr(color))
222
- const center = wrapPointer(center_p, b2Vec2)
223
- const axis = wrapPointer(axis_p, b2Vec2)
224
- drawCircle(center, radius, axis, true)
225
- },
226
- /**
227
- * @param {number} transform_p pointer to {@link Box2D.b2Transform}
228
- * @returns {void}
229
- */
230
- DrawTransform(transform_p) {
231
- const transform = wrapPointer(transform_p, b2Transform)
232
- drawTransform(transform)
233
- },
234
- /**
235
- * @param {number} vertex_p pointer to {@link Box2D.b2Vec2}
236
- * @param {number} sizeMetres
237
- * @param {number} pointer to {@link Box2D.b2Color}
238
- * @returns {void}
239
- */
240
- DrawPoint(vertex_p, sizeMetres, color_p) {
241
- const color = wrapPointer(color_p, b2Color)
242
- setCtxColor(getRgbStr(color))
243
- const vertex = wrapPointer(vertex_p, b2Vec2)
244
- drawPoint(vertex, sizeMetres)
245
- },
246
- })
247
- debugDraw.SetFlags(e_shapeBit)
248
- return debugDraw
249
- }