Skip to content

Commit f0aca9c

Browse files
committed
feat: improve internals + debug mode reactivity
1 parent 247df57 commit f0aca9c

File tree

14 files changed

+92
-68
lines changed

14 files changed

+92
-68
lines changed

docs/api/2d/classes/FComponent2d.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ It is a dictionary where the key is the class name or object id and the value is
443443

444444
#### Inherited from
445445

446-
`FComponent.CALLBACKS_ONCOLLISION`
446+
`FComponent.__CALLBACKS_ON_COLLISION__`
447447

448448
#### Defined in
449449

@@ -462,7 +462,7 @@ It is generated automatically.
462462

463463
#### Inherited from
464464

465-
`FComponent.ID`
465+
`FComponent.__ID__`
466466

467467
#### Defined in
468468

docs/api/2d/classes/FScene2d.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,9 @@ packages/core/dist/index.d.ts:123
323323

324324
***
325325

326-
### rapierToComponent
326+
### __RAPIER_TO_COMPONENT__
327327

328-
> **rapierToComponent**: `Map`\<`number`, [`FComponent2d`](FComponent2d.md)\>
328+
> **__RAPIER_TO_COMPONENT__**: `Map`\<`number`, [`FComponent2d`](FComponent2d.md)\>
329329
330330
#### Defined in
331331

docs/api/3d/classes/FCamera3d.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2176,7 +2176,7 @@ It is a dictionary where the key is the class name or object id and the value is
21762176

21772177
#### Implementation of
21782178

2179-
`FCamera.CALLBACKS_ONCOLLISION`
2179+
`FCamera.__CALLBACKS_ON_COLLISION__`
21802180

21812181
#### Defined in
21822182

@@ -2195,7 +2195,7 @@ It is generated automatically.
21952195

21962196
#### Implementation of
21972197

2198-
`FCamera.ID`
2198+
`FCamera.__ID__`
21992199

22002200
#### Defined in
22012201

docs/api/3d/classes/FComponent3d.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ It is a dictionary where the key is the class name or object id and the value is
496496

497497
#### Inherited from
498498

499-
`FComponent.CALLBACKS_ONCOLLISION`
499+
`FComponent.__CALLBACKS_ON_COLLISION__`
500500

501501
#### Defined in
502502

@@ -515,7 +515,7 @@ It is generated automatically.
515515

516516
#### Inherited from
517517

518-
`FComponent.ID`
518+
`FComponent.__ID__`
519519

520520
#### Defined in
521521

docs/api/3d/classes/FScene3d.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,9 @@ packages/core/dist/index.d.ts:123
287287

288288
***
289289

290-
### rapierToComponent
290+
### __RAPIER_TO_COMPONENT__
291291

292-
> **rapierToComponent**: `Map`\<`number`, [`FComponent3d`](FComponent3d.md)\>
292+
> **__RAPIER_TO_COMPONENT__**: `Map`\<`number`, [`FComponent3d`](FComponent3d.md)\>
293293
294294
#### Defined in
295295

packages/2d/src/FComponent2d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ export abstract class FComponent2d extends FComponent {
109109
this.container.position.set(this.position.x * 100, -this.position.y * 100)
110110
this.container.rotation = this.rotation
111111
}
112+
// Update position and rotation properties of the component
113+
this.position = {
114+
x: this.container.position.x / 100,
115+
y: -this.container.position.y / 100,
116+
}
117+
this.rotation = this.container.rotation
112118
}
113119

114120
/**

packages/2d/src/FScene2d.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,24 @@ import { FSprite } from './sprite/FSprite'
2525
* ```
2626
*/
2727
export class FScene2d extends FScene {
28-
components: FComponent2d[]
28+
// Components can be declared as it will be initialized by the parent class
29+
declare components: FComponent2d[]
2930
// Pixi.js
3031
app: PIXI.Application
3132
viewport?: Viewport
3233
// Rapier
3334
gravity: { x: number, y: number, z: number } = { x: 0, y: -9.81, z: 0 }
3435
declare world?: World
3536
declare eventQueue: RAPIER.EventQueue
36-
rapierToComponent: Map<number, FComponent2d> = new Map()
37+
__RAPIER_TO_COMPONENT__: Map<number, FComponent2d> = new Map()
3738
// onReadyCallbacks
3839
public onReadyCallbacks: (() => void)[] = []
3940
// Debug
4041
DEBUG_LINES: PIXI.Graphics[] = []
41-
DEBUG_MODE: boolean = false
42+
__DEBUG_MODE__: boolean = false
4243

4344
constructor(options: { debug?: boolean } = { debug: false }) {
4445
super()
45-
this.components = []
4646

4747
// Verify window and document are available
4848
if (typeof window === 'undefined' || typeof document === 'undefined')
@@ -52,7 +52,7 @@ export class FScene2d extends FScene {
5252
this.app = new PIXI.Application()
5353

5454
// Store the debug mode
55-
this.DEBUG_MODE = options.debug || false
55+
this.__DEBUG_MODE__ = options.debug || false
5656
}
5757

5858
/**
@@ -104,7 +104,7 @@ export class FScene2d extends FScene {
104104
this.viewport.setZoom(0.8, true)
105105

106106
// Add help grid
107-
if (this.DEBUG_MODE) {
107+
if (this.__DEBUG_MODE__) {
108108
const helpGrid = new PIXI.Graphics()
109109
// Draw the grid
110110
for (let i = -1000; i <= 1000; i += 100) {
@@ -132,7 +132,7 @@ export class FScene2d extends FScene {
132132
})
133133

134134
// Debug
135-
if (this.DEBUG_MODE)
135+
if (this.__DEBUG_MODE__)
136136
this.debug()
137137
})
138138

@@ -175,8 +175,8 @@ export class FScene2d extends FScene {
175175
*/
176176
handleCollision(handle1: RAPIER.ColliderHandle, handle2: RAPIER.ColliderHandle, start: boolean) {
177177
// Get the components from the handles
178-
const collider1 = this.rapierToComponent.get(handle1)
179-
const collider2 = this.rapierToComponent.get(handle2)
178+
const collider1 = this.__RAPIER_TO_COMPONENT__.get(handle1)
179+
const collider2 = this.__RAPIER_TO_COMPONENT__.get(handle2)
180180
// If both colliders are undefined, return
181181
if (collider1 === undefined && collider2 === undefined)
182182
return
@@ -200,7 +200,7 @@ export class FScene2d extends FScene {
200200
}
201201

202202
addComponent(component: FComponent2d) {
203-
this.components.push(component)
203+
super.addComponent(component)
204204

205205
// Detect if the FComponent2d is a FSprite instance
206206
if (component instanceof FSprite) {
@@ -216,9 +216,9 @@ export class FScene2d extends FScene {
216216
this.viewport?.addChild(component.container)
217217
}
218218

219-
// If a collider is defined, add it's handle to the rapierToComponent map
219+
// If a collider is defined, add it's handle to the __RAPIER_TO_COMPONENT__ map
220220
if (component.collider?.handle !== undefined)
221-
this.rapierToComponent.set(component.collider?.handle, component)
221+
this.__RAPIER_TO_COMPONENT__.set(component.collider?.handle, component)
222222
})
223223
}
224224
else {
@@ -234,9 +234,9 @@ export class FScene2d extends FScene {
234234
}
235235
}
236236

237-
// If a collider is defined, add it's handle to the rapierToComponent map
237+
// If a collider is defined, add it's handle to the __RAPIER_TO_COMPONENT__ map
238238
if (component.collider?.handle !== undefined)
239-
this.rapierToComponent.set(component.collider?.handle, component)
239+
this.__RAPIER_TO_COMPONENT__.set(component.collider?.handle, component)
240240
}
241241

242242
onReady(callback: () => void) {

packages/2d/src/sprite/FSprite.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class FSprite extends FComponent2d {
2323
/**
2424
* Callbacks for when the texture is loaded
2525
*/
26-
public CALLBACKS_ONLOADED: (() => void)[] = []
26+
public __CALLBACKS_ON_LOADED__: (() => void)[] = []
2727

2828
constructor(scene: FScene2d, texture: string, options?: {
2929
position?: PIXI.PointData
@@ -86,17 +86,17 @@ export class FSprite extends FComponent2d {
8686

8787
/**
8888
* @description Add a callback to be called when the texture is loaded.
89-
* @param fn The callback function.
89+
* @param callback The callback function.
9090
*/
91-
onLoaded(fn: () => void) {
92-
this.CALLBACKS_ONLOADED.push(fn)
91+
onLoaded(callback: () => void) {
92+
this.__CALLBACKS_ON_LOADED__.push(callback)
9393
}
9494

9595
/**
9696
* @description Emit the onLoaded callbacks.
9797
*/
9898
emitOnLoaded() {
99-
this.CALLBACKS_ONLOADED.forEach((callback) => {
99+
this.__CALLBACKS_ON_LOADED__.forEach((callback) => {
100100
callback()
101101
})
102102
}

packages/3d/src/FScene3d.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ import { FFixedCamera } from './cameras/FFixedCamera'
3939
* ```
4040
*/
4141
export class FScene3d extends FScene {
42-
components: FComponent3d[]
42+
// Components can be declared as it will be initialized by the parent class
43+
declare components: FComponent3d[]
4344
// Three.js
4445
declare scene: THREE.Scene
4546
declare renderer: THREE.WebGLRenderer
@@ -50,15 +51,13 @@ export class FScene3d extends FScene {
5051
gravity: { x: number, y: number, z: number } = { x: 0, y: -9.81, z: 0 }
5152
declare world: RAPIER.World
5253
declare eventQueue: RAPIER.EventQueue
53-
rapierToComponent: Map<number, FComponent3d> = new Map()
54+
__RAPIER_TO_COMPONENT__: Map<number, FComponent3d> = new Map()
5455
// Debug
55-
DEBUG_MODE: boolean
56+
__DEBUG_MODE__: boolean
5657

5758
constructor(options: { debug?: boolean } = { debug: false }) {
5859
super()
59-
// Initialize components array
60-
this.components = []
61-
this.DEBUG_MODE = options.debug || false
60+
this.__DEBUG_MODE__ = options.debug || false
6261

6362
// Verify window and document are available
6463
if (typeof window === 'undefined' || typeof document === 'undefined')
@@ -80,7 +79,7 @@ export class FScene3d extends FScene {
8079
this.scene.add(light)
8180

8281
// Debug mode
83-
if (this.DEBUG_MODE) {
82+
if (this.__DEBUG_MODE__) {
8483
// Grid helper
8584
const gridHelper = new THREE.GridHelper(10, 10)
8685
this.scene.add(gridHelper)
@@ -106,7 +105,7 @@ export class FScene3d extends FScene {
106105
this.components.forEach(component => component.onFrame(delta))
107106

108107
// Debug mode
109-
if (this.DEBUG_MODE) {
108+
if (this.__DEBUG_MODE__) {
110109
// Update controls
111110
this.controls?.update()
112111

@@ -134,7 +133,7 @@ export class FScene3d extends FScene {
134133
// onFrame loop
135134
this.onFrame((delta) => {
136135
// Debug mode
137-
if (this.DEBUG_MODE) {
136+
if (this.__DEBUG_MODE__) {
138137
// Remove previous debug lines
139138
const previousLines = this.scene.getObjectByName('DEBUG_LINES')
140139
if (previousLines)
@@ -169,8 +168,8 @@ export class FScene3d extends FScene {
169168
*/
170169
handleCollision(handle1: RAPIER.ColliderHandle, handle2: RAPIER.ColliderHandle, start: boolean) {
171170
// Get the components from the handles
172-
const collider1 = this.rapierToComponent.get(handle1)
173-
const collider2 = this.rapierToComponent.get(handle2)
171+
const collider1 = this.__RAPIER_TO_COMPONENT__.get(handle1)
172+
const collider2 = this.__RAPIER_TO_COMPONENT__.get(handle2)
174173
// If both colliders are undefined, return
175174
if (collider1 === undefined && collider2 === undefined)
176175
return
@@ -194,7 +193,7 @@ export class FScene3d extends FScene {
194193
}
195194

196195
addComponent(component: FComponent3d) {
197-
this.components.push(component)
196+
super.addComponent(component)
198197

199198
// Detect if the FComponent3d is a FGLTF instance
200199
if (component instanceof FGLTF) {
@@ -209,9 +208,9 @@ export class FScene3d extends FScene {
209208
this.scene.add(component.mesh)
210209
}
211210

212-
// If a collider is defined, add it's handle to the rapierToComponent map
211+
// If a collider is defined, add it's handle to the __RAPIER_TO_COMPONENT__ map
213212
if (component.collider?.handle !== undefined)
214-
this.rapierToComponent.set(component.collider?.handle, component)
213+
this.__RAPIER_TO_COMPONENT__.set(component.collider?.handle, component)
215214
}
216215

217216
private addDebugPanel() {

packages/3d/src/cameras/FCamera3d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { FCamera } from '@fibbojs/core'
66
* @category Camera
77
*/
88
export abstract class FCamera3d extends THREE.PerspectiveCamera implements FCamera {
9-
declare public ID: number
10-
public CALLBACKS_ONCOLLISION: { [key: string]: (() => void)[] } = {}
9+
declare public __ID__: number
10+
public __CALLBACKS_ON_COLLISION__: { [key: string]: (() => void)[] } = {}
1111

1212
constructor() {
1313
super(75, window.innerWidth / window.innerHeight, 0.1, 1000)

0 commit comments

Comments
 (0)