From 887b6b6c9fa57a9d0f1991ee353bdce65d04bb28 Mon Sep 17 00:00:00 2001 From: Matt Jennings Date: Tue, 28 May 2024 09:15:15 -0500 Subject: [PATCH] fix: improve types to disallow invalid combo of collider args (#3024) This causes type errors in a few tests. Not sure if those were just mistyped actors or if those were legitimate usage of mixing width/height/radius/collider. ## Changes: - improve types to disallow invalid combo of collider/width/height/radius in actor args - only add default color graphic for the respective collider used --------- Co-authored-by: Erik Onarheim --- CHANGELOG.md | 2 + sandbox/tests/physics/fastphysics.ts | 4 +- src/engine/Actor.ts | 102 +++++++++++++++++---------- 3 files changed, 68 insertions(+), 40 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cce79ba88..bf9a460ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Fixed +- improve types to disallow invalid combo of collider/width/height/radius in actor args +- only add default color graphic for the respective collider used - Fixed issue where `ex.SpriteFont` did not respect scale when measuring text - Fixed issue where negative transforms would cause collision issues because polygon winding would change. - Fixed issue where removing and re-adding an actor would cause subsequent children added not to function properly with regards to their parent/child transforms diff --git a/sandbox/tests/physics/fastphysics.ts b/sandbox/tests/physics/fastphysics.ts index 684c564cc..c1bf763fc 100644 --- a/sandbox/tests/physics/fastphysics.ts +++ b/sandbox/tests/physics/fastphysics.ts @@ -12,14 +12,14 @@ game.debug.collider.showGeometry = true; game.debug.motion.showAll = true; game.debug.collider.showBounds = true; ex.Physics.acc.setTo(0, 300); -//ex.Physics.dynamicTreeVelocityMultiplyer = 1; +//ex.Physics.dynamicTreeVelocityMultiplayer = 1; game.currentScene.camera.zoom = 0.5; game.toggleDebug(); var rocketTex = new ex.ImageSource('missile.png'); var loader = new ex.Loader([rocketTex]); function spawnRocket(direction) { - var rocket = new ex.Actor({ x: 300, y: 200, radius: 48, height: 16 }); + var rocket = new ex.Actor({ x: 300, y: 200, radius: 48 }); rocket.body.canSleep = true; rocket.on('preupdate', () => { diff --git a/src/engine/Actor.ts b/src/engine/Actor.ts index c71564b02..99398db55 100644 --- a/src/engine/Actor.ts +++ b/src/engine/Actor.ts @@ -59,7 +59,7 @@ export function isActor(x: any): x is Actor { /** * Actor constructor options */ -export interface ActorArgs { +export type ActorArgs = ColliderArgs & { /** * Optionally set the name of the actor, default is 'anonymous' */ @@ -80,18 +80,6 @@ export interface ActorArgs { * Optionally set the coordinate plane of the actor, default is [[CoordPlane.World]] meaning actor is subject to camera positioning */ coordPlane?: CoordPlane; - /** - * Optionally set the width of a box collider for the actor - */ - width?: number; - /** - * Optionally set the height of a box collider for the actor - */ - height?: number; - /** - * Optionally set the radius of the circle collider for the actor - */ - radius?: number; /** * Optionally set the velocity of the actor in pixels/sec */ @@ -142,15 +130,50 @@ export interface ActorArgs { * Optionally set the collision type */ collisionType?: CollisionType; - /** - * Optionally supply a collider for an actor, if supplied ignores any supplied width/height - */ - collider?: Collider; + /** * Optionally supply a [[CollisionGroup]] */ collisionGroup?: CollisionGroup; -} +}; + +type ColliderArgs = + | // custom collider + { + /** + * Optionally supply a collider for an actor, if supplied ignores any supplied width/height + */ + collider?: Collider; + + width?: undefined; + height?: undefined; + radius?: undefined; + } + // box collider + | { + /** + * Optionally set the width of a box collider for the actor + */ + width?: number; + /** + * Optionally set the height of a box collider for the actor + */ + height?: number; + + collider?: undefined; + radius?: undefined; + } + // circle collider + | { + /** + * Optionally set the radius of the circle collider for the actor + */ + radius?: number; + + collider?: undefined; + width?: undefined; + height?: undefined; + }; export type ActorEvents = EntityEvents & { collisionstart: CollisionStartEvent; @@ -596,16 +619,39 @@ export class Actor extends Entity implements Eventable, PointerEvents, CanInitia this.body.group = collisionGroup; } + if (color) { + this.color = color; + } + if (collider) { this.collider = new ColliderComponent(collider); this.addComponent(this.collider); } else if (radius) { this.collider = new ColliderComponent(Shape.Circle(radius)); this.addComponent(this.collider); + + if (color) { + this.graphics.add( + new Circle({ + color: color, + radius + }) + ); + } } else { if (width > 0 && height > 0) { this.collider = new ColliderComponent(Shape.Box(width, height, this.anchor)); this.addComponent(this.collider); + + if (color && width && height) { + this.graphics.add( + new Rectangle({ + color: color, + width, + height + }) + ); + } } else { this.collider = new ColliderComponent(); this.addComponent(this.collider); // no collider @@ -613,26 +659,6 @@ export class Actor extends Entity implements Eventable, PointerEvents, CanInitia } this.graphics.visible = visible ?? true; - - if (color) { - this.color = color; - if (width && height) { - this.graphics.add( - new Rectangle({ - color: color, - width, - height - }) - ); - } else if (radius) { - this.graphics.add( - new Circle({ - color: color, - radius - }) - ); - } - } } public clone(): Actor {