Skip to content

Commit e6e0436

Browse files
committed
feat: normalize API 2d
1 parent 8397f20 commit e6e0436

File tree

3 files changed

+26
-24
lines changed

3 files changed

+26
-24
lines changed

apps/playground-2d/src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ import MySquare from './classes/MySquare'
9696
console.log('Sprite collided with the circle!')
9797
})
9898
character.onCollisionWith(deathZone, () => {
99-
character.setPosition(0, 5)
99+
character.setPosition({ x: 0, y: 5 })
100100
console.log('Sprite collided with the death zone!')
101101
})
102102
scene.addComponent(character)

packages/2d/src/FComponent.ts

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -158,48 +158,50 @@ export abstract class FComponent extends FComponentCore {
158158

159159
/**
160160
* @description Set the position of the component.
161-
* @param x The x position.
162-
* @param y The y position.
161+
* @param options The options for the position.
162+
* @param options.x The x position.
163+
* @param options.y The y position.
163164
* @example
164165
* ```ts
165-
* component.setPosition(0, 0)
166+
* component.setPosition({ x: 0, y: 0 })
166167
* ```
167168
*/
168-
setPosition(x: number, y: number): void {
169-
this.position = { x, y }
170-
this.container.position.set(x, y)
169+
setPosition(options: { x: number, y: number }): void {
170+
this.position = { x: options.x, y: options.y }
171+
this.container.position.set(options.x, options.y)
171172
// If a collider exists, update its translation
172173
if (this.collider)
173-
this.collider.collider.setTranslation(new RAPIER.Vector2(x, y))
174+
this.collider.collider.setTranslation(new RAPIER.Vector2(options.x, options.y))
174175
// If a rigid body exists, update its translation
175176
if (this.rigidBody)
176-
this.rigidBody.rigidBody.setTranslation(new RAPIER.Vector2(x, y), true)
177+
this.rigidBody.rigidBody.setTranslation(new RAPIER.Vector2(options.x, options.y), true)
177178
}
178179

179180
/**
180181
* @description Set the scale of the component.
181-
* @param x The x scale.
182-
* @param y The y scale.
182+
* @param options The options for the scale.
183+
* @param options.x The x scale.
184+
* @param options.y The y scale.
183185
* @example
184186
* ```ts
185-
* component.setScale(1, 1)
187+
* component.setScale({ x: 1, y: 1 })
186188
* ```
187189
*/
188-
setScale(x: number, y: number): void {
189-
this.scale = { x, y }
190-
this.container.height = y * 100
191-
this.container.width = x * 100
190+
setScale(options: { x: number, y: number }): void {
191+
this.scale = { x: options.x, y: options.y }
192+
this.container.height = options.y * 100
193+
this.container.width = options.x * 100
192194
// If a collider exists
193195
if (this.collider) {
194196
// If the collider is a cuboid, update its half extents
195197
if (this.collider.collider.shape.type === RAPIER.ShapeType.Cuboid) {
196-
this.collider.collider.setHalfExtents(new RAPIER.Vector2(x / 2, y / 2))
198+
this.collider.collider.setHalfExtents(new RAPIER.Vector2(options.x / 2, options.y / 2))
197199
}
198200
// If the collider is a ball, update its radius
199201
else if (this.collider.collider.shape.type === RAPIER.ShapeType.Ball) {
200202
this.collider.collider.setRadius(
201203
// Get the maximum value of x and y
202-
Math.max(x, y) / 2,
204+
Math.max(options.x, options.y) / 2,
203205
)
204206
}
205207
}
@@ -334,15 +336,15 @@ export abstract class FComponent extends FComponentCore {
334336
}
335337

336338
set x(x: number) {
337-
this.setPosition(x, this.position.y)
339+
this.setPosition({ x, y: this.position.y })
338340
}
339341

340342
get y(): number {
341343
return this.position.y
342344
}
343345

344346
set y(y: number) {
345-
this.setPosition(this.position.x, y)
347+
this.setPosition({ x: this.position.x, y })
346348
}
347349

348350
get rotationDegree(): number {
@@ -358,14 +360,14 @@ export abstract class FComponent extends FComponentCore {
358360
}
359361

360362
set scaleX(x: number) {
361-
this.setScale(x, this.scale.y)
363+
this.setScale({ x, y: this.scale.y })
362364
}
363365

364366
get scaleY(): number {
365367
return this.scale.y
366368
}
367369

368370
set scaleY(y: number) {
369-
this.setScale(this.scale.x, y)
371+
this.setScale({ x: this.scale.x, y })
370372
}
371373
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export class FSprite extends FComponent {
6464
* @param width The width of the sprite.
6565
*/
6666
setScaleWidth(width: number) {
67-
this.setScale(width, width * this.texture.height / this.texture.width)
67+
this.setScale({ x: width, y: width * this.texture.height / this.texture.width })
6868
}
6969

7070
/**
@@ -73,7 +73,7 @@ export class FSprite extends FComponent {
7373
* @param height The height of the sprite.
7474
*/
7575
setScaleHeight(height: number) {
76-
this.setScale(height * this.texture.width / this.texture.height, height)
76+
this.setScale({ x: height * this.texture.width / this.texture.height, y: height })
7777
}
7878

7979
onFrame(delta: number): void {

0 commit comments

Comments
 (0)