@safe-engine/pixi 8.4.6 → 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.
- package/README.md +14 -23
- package/dist/box2d-wasm/ContactListener.d.ts +1 -1
- package/dist/box2d-wasm/ContactListener.d.ts.map +1 -1
- package/dist/box2d-wasm/ContactListener.js +50 -18
- package/dist/box2d-wasm/PhysicsComponent.d.ts +27 -30
- package/dist/box2d-wasm/PhysicsComponent.d.ts.map +1 -1
- package/dist/box2d-wasm/PhysicsComponent.js +44 -11
- package/dist/box2d-wasm/PhysicsSprite.d.ts +3 -0
- package/dist/box2d-wasm/PhysicsSprite.d.ts.map +1 -1
- package/dist/box2d-wasm/PhysicsSprite.js +24 -3
- package/dist/box2d-wasm/PhysicsSystem.d.ts +4 -2
- package/dist/box2d-wasm/PhysicsSystem.d.ts.map +1 -1
- package/dist/box2d-wasm/PhysicsSystem.js +134 -44
- package/dist/collider/CollideComponent.d.ts +27 -10
- package/dist/collider/CollideComponent.d.ts.map +1 -1
- package/dist/collider/CollideComponent.js +10 -12
- package/dist/collider/helper/Intersection.d.ts +6 -6
- package/dist/collider/helper/Intersection.d.ts.map +1 -1
- package/dist/collider/helper/Intersection.js +4 -4
- package/dist/components/NodeComp.js +1 -1
- package/dist/helper/utils.d.ts +0 -6
- package/dist/helper/utils.d.ts.map +1 -1
- package/dist/helper/utils.js +0 -12
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/{src/spine/lib/require-shim.ts → dist/spine/lib/assets/atlasLoader.d.ts} +5 -14
- package/dist/spine/lib/assets/atlasLoader.d.ts.map +1 -0
- package/dist/spine/lib/assets/atlasLoader.js +122 -0
- package/{src/spine/lib/darktint/DarkTintShader.ts → dist/spine/lib/assets/skeletonLoader.d.ts} +3 -47
- package/dist/spine/lib/assets/skeletonLoader.d.ts.map +1 -0
- package/dist/spine/lib/assets/skeletonLoader.js +70 -0
- package/package.json +6 -2
- package/.github/workflows/npm-publish.yml +0 -35
- package/dist/@types/safex.d.ts +0 -15
- package/src/@types/index.d.ts +0 -2
- package/src/app.ts +0 -91
- package/src/base/EnhancedComponent.ts +0 -38
- package/src/base/gworld.ts +0 -20
- package/src/base/index.ts +0 -14
- package/src/base/utils.ts +0 -23
- package/src/box2d-wasm/ContactListener.ts +0 -66
- package/src/box2d-wasm/PhysicsComponent.ts +0 -83
- package/src/box2d-wasm/PhysicsSprite.ts +0 -42
- package/src/box2d-wasm/PhysicsSystem.ts +0 -142
- package/src/box2d-wasm/debugDraw.ts +0 -249
- package/src/box2d-wasm/index.ts +0 -19
- package/src/collider/CollideComponent.ts +0 -257
- package/src/collider/CollideSystem.ts +0 -166
- package/src/collider/helper/Intersection.ts +0 -139
- package/src/collider/helper/utils.ts +0 -37
- package/src/collider/index.ts +0 -16
- package/src/components/BaseComponent.ts +0 -17
- package/src/components/NodeComp.ts +0 -442
- package/src/components/Scene.ts +0 -17
- package/src/core/Color.ts +0 -7
- package/src/core/LoadingBar.ts +0 -63
- package/src/core/NodePool.ts +0 -28
- package/src/core/Size.ts +0 -21
- package/src/core/Vec2.ts +0 -108
- package/src/core/director.ts +0 -11
- package/src/core/index.ts +0 -7
- package/src/core/loader.ts +0 -14
- package/src/core/math.ts +0 -22
- package/src/dragonbones/DragonBonesComponent.ts +0 -32
- package/src/dragonbones/DragonBonesSystem.ts +0 -35
- package/src/dragonbones/index.ts +0 -11
- package/src/gui/GUIComponent.ts +0 -155
- package/src/gui/GUISystem.ts +0 -125
- package/src/helper/utils.ts +0 -50
- package/src/index.ts +0 -19
- package/src/norender/NoRenderComponent.ts +0 -60
- package/src/norender/NoRenderSystem.ts +0 -67
- package/src/norender/Touch.ts +0 -37
- package/src/planck/PhysicsComponent.ts +0 -83
- package/src/planck/PhysicsSprite.ts +0 -43
- package/src/planck/PhysicsSystem.ts +0 -201
- package/src/planck/index.ts +0 -3
- package/src/render/RenderComponent.ts +0 -132
- package/src/render/RenderSystem.ts +0 -64
- package/src/richtext/RichTextComp.ts +0 -50
- package/src/richtext/RichTextSystem.ts +0 -26
- package/src/richtext/html-text-parser.ts +0 -87
- package/src/richtext/index.ts +0 -8
- package/src/spine/SpineComponent.ts +0 -18
- package/src/spine/SpineSystem.ts +0 -30
- package/src/spine/index.ts +0 -11
- package/src/spine/lib/BatchableSpineSlot.ts +0 -138
- package/src/spine/lib/Spine.ts +0 -894
- package/src/spine/lib/SpineDebugRenderer.ts +0 -615
- package/src/spine/lib/SpinePipe.ts +0 -203
- package/src/spine/lib/SpineTexture.ts +0 -143
- package/src/spine/lib/darktint/DarkTintBatchGeometry.ts +0 -92
- package/src/spine/lib/darktint/DarkTintBatcher.ts +0 -186
- package/src/spine/lib/darktint/darkTintBit.ts +0 -77
- package/src/spine/lib/index.ts +0 -43
- package/tsconfig.json +0 -18
- /package/{dist/@types → @types}/index.d.ts +0 -0
|
@@ -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
|
-
}
|
package/src/box2d-wasm/index.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { GameWorld, Vec2 } from '..'
|
|
2
|
-
import { PhysicsSystem } from './PhysicsSystem'
|
|
3
|
-
|
|
4
|
-
export * from './PhysicsComponent'
|
|
5
|
-
export * from './PhysicsSprite'
|
|
6
|
-
export * from './PhysicsSystem'
|
|
7
|
-
|
|
8
|
-
export function setupPhysics(world = GameWorld.Instance, isDebugDraw?: boolean, gravity?: Vec2) {
|
|
9
|
-
console.log('app world', world.app)
|
|
10
|
-
world.systems.add(PhysicsSystem)
|
|
11
|
-
world.systems.configureOnce(PhysicsSystem)
|
|
12
|
-
world.listUpdate.push(PhysicsSystem)
|
|
13
|
-
if (isDebugDraw) {
|
|
14
|
-
world.systems.get(PhysicsSystem).addDebug()
|
|
15
|
-
}
|
|
16
|
-
if (gravity) {
|
|
17
|
-
world.systems.get(PhysicsSystem).gravity = gravity
|
|
18
|
-
}
|
|
19
|
-
}
|