Skip to content

Commit

Permalink
refactor: add common utilities under @fibbojs/util (#38)
Browse files Browse the repository at this point in the history
* feat: begin math refactor

* fix: import @fibbojs/core in @fibbojs/util

* feat: fix and use interpretPath
  • Loading branch information
Gugustinette authored Dec 17, 2024
1 parent 3cdcea3 commit b44a0ee
Show file tree
Hide file tree
Showing 36 changed files with 290 additions and 89 deletions.
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/2d/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"@dimforge/rapier2d": "0.14.0",
"@fibbojs/core": "0.0.13",
"@fibbojs/event": "0.0.13",
"@fibbojs/util": "0.0.13",
"pixi-viewport": "5.0.3",
"pixi.js": "8.3.4"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/2d/src/core/FCollider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as RAPIER from '@dimforge/rapier2d'
import type { FVector2 } from '../types/FVector2'
import type { FVector2 } from '@fibbojs/core'
import { FShapes } from '../types/FShapes'
import type { FComponent } from './FComponent'
import type { FRigidBody } from './FRigidBody'
Expand Down
3 changes: 1 addition & 2 deletions packages/2d/src/core/FComponent.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type { FComponentOptions as FComponentOptionsCore, OnCollisionWithData } from '@fibbojs/core'
import type { FComponentOptions as FComponentOptionsCore, FVector2, OnCollisionWithData } from '@fibbojs/core'
import { FComponent as FComponentCore } from '@fibbojs/core'
import * as PIXI from 'pixi.js'
import * as RAPIER from '@dimforge/rapier2d'
import type { FController } from '../controllers/FController'
import type { FVector2 } from '../types/FVector2'
import type { FScene } from './FScene'
import type { FColliderOptions } from './FCollider'
import { FCollider } from './FCollider'
Expand Down
2 changes: 1 addition & 1 deletion packages/2d/src/core/FRigidBody.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as RAPIER from '@dimforge/rapier2d'
import type { FVector2 } from '../types/FVector2'
import type { FVector2 } from '@fibbojs/core'
import { FRigidBodyType } from '../types/FRigidBodyType'
import { FShapes } from '../types/FShapes'
import type { FComponent } from './FComponent'
Expand Down
9 changes: 5 additions & 4 deletions packages/2d/src/core/FTransform.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { FVector2 } from '../types/FVector2'
import type { FVector2 } from '@fibbojs/core'
import { FMathUtil } from '@fibbojs/util'

export interface FTransformOptions {
position?: { x: number, y: number }
Expand Down Expand Up @@ -70,7 +71,7 @@ export class FTransform {

// Set the transform values
this.__POSITION__ = options.position
this.__ROTATION__ = options.rotationDegree !== undefined ? options.rotationDegree * (Math.PI / 180) : options.rotation || 0
this.__ROTATION__ = options.rotationDegree !== undefined ? FMathUtil.degreeToRad(options.rotationDegree) : options.rotation || 0
this.__SCALE__ = options.scale
}

Expand Down Expand Up @@ -197,15 +198,15 @@ export class FTransform {
* Get the rotation in degrees.
*/
get rotationDegree() {
return this.__ROTATION__ * (180 / Math.PI)
return FMathUtil.radToDegree(this.__ROTATION__)
}

/**
* Set the rotation in degrees.
* @param rotationDegree The new rotation in degrees.
*/
set rotationDegree(rotationDegree: number) {
this.setRotation(rotationDegree * (Math.PI / 180))
this.setRotation(FMathUtil.degreeToRad(rotationDegree))
}

/**
Expand Down
1 change: 0 additions & 1 deletion packages/2d/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,3 @@ export * from './sprite/FSprite'
// Types
export * from './types/FRigidBodyType'
export * from './types/FShapes'
export * from './types/FVector2'
2 changes: 1 addition & 1 deletion packages/2d/src/polygons/FRectangle.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as PIXI from 'pixi.js'
import type { FVector2 } from '@fibbojs/core'
import type { FScene } from '../core/FScene'
import type { FVector2 } from '../types/FVector2'
import type { FPolygonOptions } from './FPolygon'
import { FPolygon } from './FPolygon'

Expand Down
19 changes: 2 additions & 17 deletions packages/2d/src/sprite/FSprite.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as PIXI from 'pixi.js'
import { FAssetUtil } from '@fibbojs/util'
import type { FComponentOptions } from '../core/FComponent'
import { FComponent } from '../core/FComponent'
import type { FScene } from '../core/FScene'
Expand Down Expand Up @@ -49,24 +50,8 @@ export class FSprite extends FComponent {
* @param texture The path to the texture.
*/
async loadTexture(texture: string) {
// Interpret path function
function interpretPath(path: string) {
// Resource URL (if it starts http, treat as a URL)
if (path.startsWith('http')) {
return path
}
// Absolute path (if it starts with /), add the current domain + path
else if (path.startsWith('/')) {
return `${window.location.href}${path}`
}
// Otherwise, treat as a relative path starting to the assets folder
else {
return `${window.location.href}/assets/${path}`
}
}

// Interpret the path
const path = interpretPath(texture)
const path = FAssetUtil.interpretPath(texture)
// Load the texture
this.__TEXTURE__ = await PIXI.Assets.load(path)
this.__TEXTURE__.source.scaleMode = 'nearest'
Expand Down
1 change: 1 addition & 0 deletions packages/3d/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"@dimforge/rapier3d": "0.14.0",
"@fibbojs/core": "0.0.13",
"@fibbojs/event": "0.0.13",
"@fibbojs/util": "0.0.13",
"@types/three": "0.168.0",
"three": "0.168.0"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/3d/src/cameras/FAttachedCamera.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { FVector3 } from '@fibbojs/core'
import type { FComponent } from '../core/FComponent'
import type { FScene } from '../core/FScene'
import type { FVector3 } from '../types/FVector3'
import type { FCameraOptions } from './FCamera'
import { FCamera } from './FCamera'

Expand Down
2 changes: 1 addition & 1 deletion packages/3d/src/cameras/FCamera.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as THREE from 'three'
import { FCamera as FCameraCore } from '@fibbojs/core'
import type { FVector3 } from '@fibbojs/core'
import type { FTransformOptions } from '../core/FTransform'
import { FTransform } from '../core/FTransform'
import type { FScene } from '../core/FScene'
import type { FVector3 } from '../types/FVector3'

export interface FCameraOptions extends FTransformOptions {}

Expand Down
2 changes: 1 addition & 1 deletion packages/3d/src/cameras/FGameCamera.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { FVector3 } from '@fibbojs/core'
import type { FScene } from '../core/FScene'
import type { FVector3 } from '../types/FVector3'
import { FOrbitCamera } from './FOrbitCamera'
import type { FAttachedCameraOptions } from './FAttachedCamera'

Expand Down
2 changes: 1 addition & 1 deletion packages/3d/src/cameras/FOrbitCamera.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as THREE from 'three'
import { OrbitControls } from 'three/addons/controls/OrbitControls.js'
import type { FVector3 } from '@fibbojs/core'
import type { FComponent } from '../core/FComponent'
import type { FScene } from '../core/FScene'
import type { FVector3 } from '../types/FVector3'
import { FCamera } from './FCamera'
import type { FAttachedCameraOptions } from './FAttachedCamera'

Expand Down
2 changes: 1 addition & 1 deletion packages/3d/src/controllers/FCharacterControllerK.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as THREE from 'three'
import RAPIER from '@dimforge/rapier3d'
import { FKeyboard } from '@fibbojs/event'
import type { FVector3 } from '@fibbojs/core'
import type { FScene } from '../core/FScene'
import type { FVector3 } from '../types/FVector3'
import type { FCharacterControllerOptions } from './FCharacterController'
import { FCharacterController } from './FCharacterController'

Expand Down
5 changes: 3 additions & 2 deletions packages/3d/src/core/FCollider.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as THREE from 'three'
import * as RAPIER from '@dimforge/rapier3d'
import type { FVector3 } from '@fibbojs/core'
import { FMathUtil } from '@fibbojs/util'
import { FShapes } from '../types/FShapes'
import type { FVector3 } from '../types/FVector3'
import type { FComponent } from './FComponent'
import type { FRigidBody } from './FRigidBody'
import { FTransform } from './FTransform'
Expand Down Expand Up @@ -168,7 +169,7 @@ export class FCollider {
frame(_delta: number): void {
// As the collider should have moved, update the transform to sync with the collider
this.transform.__POSITION__ = this.__COLLIDER__.translation()
this.transform.__ROTATION__ = new THREE.Euler().setFromQuaternion(new THREE.Quaternion().copy(this.__COLLIDER__.rotation()))
this.transform.__ROTATION__ = FMathUtil.quaternionToRad(this.__COLLIDER__.rotation())
// Propagate the position and rotation update if the collider is attached to a component
if (this.component) {
// Propagate the position update
Expand Down
35 changes: 19 additions & 16 deletions packages/3d/src/core/FComponent.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import * as THREE from 'three'
import * as RAPIER from '@dimforge/rapier3d'
import type { OnCollisionWithData } from '@fibbojs/core'
import type { FVector3, OnCollisionWithData } from '@fibbojs/core'
import { FComponent as FComponentCore } from '@fibbojs/core'
import type { FController } from '../controllers/FController'
import type { FVector3 } from '../types/FVector3'
import type { FScene } from './FScene'
import type { FColliderOptions } from './FCollider'
import { FCollider } from './FCollider'
Expand Down Expand Up @@ -139,18 +138,18 @@ export abstract class FComponent extends FComponentCore {
if (this.rigidBody) {
// Move the component
this.__SET_POSITION__({
x: this.rigidBody.transform.position.x - this.rigidBody.offset.position.x,
y: this.rigidBody.transform.position.y - this.rigidBody.offset.position.y,
z: this.rigidBody.transform.position.z - this.rigidBody.offset.position.z,
x: this.rigidBody.transform.x - this.rigidBody.offset.x,
y: this.rigidBody.transform.y - this.rigidBody.offset.y,
z: this.rigidBody.transform.z - this.rigidBody.offset.z,
})
}
// If a collider exists, the propagation comes from the collider
else if (this.collider) {
// Move the component
this.__SET_POSITION__({
x: this.collider.transform.position.x - this.collider.offset.x,
y: this.collider.transform.position.y - this.collider.offset.y,
z: this.collider.transform.position.z - this.collider.offset.z,
x: this.collider.transform.x - this.collider.offset.x,
y: this.collider.transform.y - this.collider.offset.y,
z: this.collider.transform.z - this.collider.offset.z,
})
}
}
Expand Down Expand Up @@ -181,18 +180,18 @@ export abstract class FComponent extends FComponentCore {
if (this.rigidBody) {
// Rotate the component
this.__SET_ROTATION__({
x: this.rigidBody.transform.rotation.x - this.rigidBody.offset.rotationX,
y: this.rigidBody.transform.rotation.y - this.rigidBody.offset.rotationY,
z: this.rigidBody.transform.rotation.z - this.rigidBody.offset.rotationZ,
x: this.rigidBody.transform.rotationX - this.rigidBody.offset.rotationX,
y: this.rigidBody.transform.rotationY - this.rigidBody.offset.rotationY,
z: this.rigidBody.transform.rotationZ - this.rigidBody.offset.rotationZ,
})
}
// If a collider exists, the propagation comes from the collider
else if (this.collider) {
// Rotate the component
this.__SET_ROTATION__({
x: this.collider.transform.rotation.x - this.collider.offset.rotationX,
y: this.collider.transform.rotation.y - this.collider.offset.rotationY,
z: this.collider.transform.rotation.z - this.collider.offset.rotationZ,
x: this.collider.transform.rotationX - this.collider.offset.rotationX,
y: this.collider.transform.rotationY - this.collider.offset.rotationY,
z: this.collider.transform.rotationZ - this.collider.offset.rotationZ,
})
}
}
Expand Down Expand Up @@ -245,15 +244,19 @@ export abstract class FComponent extends FComponentCore {
if (this.__MESH__)
this.__MESH__.position.set(position.x, position.y, position.z)
// Update the transform
this.transform.__POSITION__ = position
this.transform.__POSITION__.x = position.x
this.transform.__POSITION__.y = position.y
this.transform.__POSITION__.z = position.z
}

__SET_ROTATION__(rotation: FVector3): void {
// Rotate the mesh
if (this.__MESH__)
this.__MESH__.rotation.set(rotation.x, rotation.y, rotation.z)
// Update the transform
this.transform.__ROTATION__ = rotation
this.transform.__ROTATION__.x = rotation.x
this.transform.__ROTATION__.y = rotation.y
this.transform.__ROTATION__.z = rotation.z
}

__SET_SCALE__(scale: FVector3): void {
Expand Down
5 changes: 3 additions & 2 deletions packages/3d/src/core/FRigidBody.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as THREE from 'three'
import * as RAPIER from '@dimforge/rapier3d'
import type { FVector3 } from '@fibbojs/core'
import { FMathUtil } from '@fibbojs/util'
import { FShapes } from '../types/FShapes'
import type { FVector3 } from '../types/FVector3'
import { FRigidBodyType } from '../types/FRigidBodyType'
import type { FComponent } from './FComponent'
import { FCollider } from './FCollider'
Expand Down Expand Up @@ -203,7 +204,7 @@ export class FRigidBody {
frame(_delta: number): void {
// As the rigidBody should have moved, update the transform to sync with the rigidBody
this.transform.__POSITION__ = this.__RIGIDBODY__.translation()
this.transform.__ROTATION__ = new THREE.Euler().setFromQuaternion(new THREE.Quaternion().copy(this.__RIGIDBODY__.rotation()))
this.transform.__ROTATION__ = FMathUtil.quaternionToRad(this.__RIGIDBODY__.rotation())
// Propagate the position and rotation update if the rigidBody is attached to a component
if (this.component) {
// Propagate the position update
Expand Down
3 changes: 1 addition & 2 deletions packages/3d/src/core/FScene.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import * as THREE from 'three'
import { FScene as FSceneCore } from '@fibbojs/core'
import type { FSceneOptions as FSceneOptionsCore } from '@fibbojs/core'
import type { FSceneOptions as FSceneOptionsCore, FVector3 } from '@fibbojs/core'
import type RAPIER from '@dimforge/rapier3d'
import type { FCamera } from '../cameras/FCamera'
import { FFixedCamera } from '../cameras/FFixedCamera'
import type { FLight } from '../lights/FLight'
import type { FVector3 } from '../types/FVector3'
import type { FComponent } from './FComponent'
import type { FRigidBody } from './FRigidBody'
import type { FCollider } from './FCollider'
Expand Down
Loading

0 comments on commit b44a0ee

Please sign in to comment.