Skip to content

Commit

Permalink
fix: improve types to disallow invalid combo of collider args (#3024)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
mattjennings and eonarheim authored May 28, 2024
1 parent 01126d4 commit 887b6b6
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 40 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions sandbox/tests/physics/fastphysics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
102 changes: 64 additions & 38 deletions src/engine/Actor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
*/
Expand All @@ -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
*/
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -596,43 +619,46 @@ 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
}
}

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 {
Expand Down

0 comments on commit 887b6b6

Please sign in to comment.