Skip to content

Commit

Permalink
fix: [#3098] Make collision events consistent targets (#3238)
Browse files Browse the repository at this point in the history
* fix: consistent events

* fix tests

* fix lint

* fix sandbox

* fix docs

* fix docs
  • Loading branch information
eonarheim authored Oct 23, 2024
1 parent 83f53df commit 591ff8e
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 119 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Breaking Changes

- Collision events now only target `ex.Collider` types, this previously would sometimes emit an `ex.Entity` if you attached to the `ex.ColliderComponent`
* `ex.PreCollisionEvent`
* `ex.PostCollisionEvent`
* `ex.ContactStartEvent`
* `ex.ContactEndEvent`
* `ex.CollisionPreSolveEvent`
* `ex.CollisionPostSolveEvent`
* `ex.CollisionStartEvent`
* `ex.CollisionEndEvent`
- `System.priority` is refactored to be static.
- `ex.Timer` now only takes the option bag constructor
- `PreDrawEvent`, `PostDrawEvent`, `PreTransformDrawEvent`, `PostTransformDrawEvent`, `PreUpdateEvent`, `PostUpdateEvent` now use `elapsedMs` instead of `delta` for the elapsed milliseconds between the last frame.460696
Expand Down Expand Up @@ -118,6 +127,7 @@ are doing mtv adjustments during precollision.

### Fixed

- Fixed issue where Collision events ahd inconsistent targets, sometimes they were Colliders and sometimes they were Entities
- Fixed issue where `ex.Engine.screenshot()` images may not yet be loaded in time for use in `ex.Transition`s
- Fixed issue where there would be an incorrect background color for 1 frame when transitioning to a new scene
- Fixed issue where `blockInput: true` on scene transition only blocked input events, not accessors like `wasHeld(...)` etc.
Expand Down
8 changes: 4 additions & 4 deletions sandbox/src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ player.on('collisionstart', () => {
});

player.on('collisionend', (e) => {
console.log('collision end', e.other.collider);
console.log('collision end', e.other);
});

// Health bar example
Expand Down Expand Up @@ -952,8 +952,8 @@ player.on('postcollision', (data: ex.PostCollisionEvent) => {
game.input.keyboard.isHeld(ex.Keys.Down)
)
) {
player.vel.x = data.other.vel.x;
player.vel.y = data.other.vel.y;
player.vel.x = data.other.owner.vel.x;
player.vel.y = data.other.owner.vel.y;
}

if (!data.other) {
Expand All @@ -964,7 +964,7 @@ player.on('postcollision', (data: ex.PostCollisionEvent) => {

if (data.side === ex.Side.Top) {
if (data.other) {
player.vel.y = data.other.vel.y - player.vel.y;
player.vel.y = data.other.owner.vel.y - player.vel.y;
} else {
player.vel.y = 0;
}
Expand Down
2 changes: 1 addition & 1 deletion sandbox/tests/collision/touching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ ball.on('collisionstart', (evt: ex.CollisionStartEvent) => {

ball.on('collisionend', (evt: ex.CollisionEndEvent) => {
console.log('Ball was being touched on frame:', game.stats.currFrame.id);
evt.actor.vel = ex.vec(0, -speed);
evt.self.owner.vel = ex.vec(0, -speed);
});
ball.on('postupdate', function () {
if (ball.pos.y < 0) {
Expand Down
12 changes: 6 additions & 6 deletions sandbox/tests/high-gravity-arcade/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ class TouchingComponent extends ex.Component {
this.origin = new ex.Vector(owner.pos.x, owner.pos.y);
// collect up all of the collisionstart/end events for each frame
owner.on('collisionstart', (ev) => {
if (ev.other.collider) {
if (ev.other) {
// console.log(ev.contact.colliderA.worldPos, ev.contact.colliderB.worldPos)
if (ev.other.body?.collisionType === ex.CollisionType.Passive) {
this.passives.add(ev.other);
if (ev.other.owner.body?.collisionType === ex.CollisionType.Passive) {
this.passives.add(ev.other.owner);
} else {
const side = ev.side;

this.contacts.set(ev.contact.id, {
contact: ev.contact,
actor: ev.other,
actor: ev.other.owner,
side
});
this.updateSides();
Expand All @@ -58,8 +58,8 @@ class TouchingComponent extends ex.Component {
});

owner.on('collisionend', (ev) => {
if (ev.other.body?.collisionType === ex.CollisionType.Passive) {
this.passives.delete(ev.other);
if (ev.other.owner.body?.collisionType === ex.CollisionType.Passive) {
this.passives.delete(ev.other.owner);
} else {
this.contacts.delete(ev.lastContact.id);
this.updateSides();
Expand Down
8 changes: 4 additions & 4 deletions site/docs/00-your-first-game.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -304,15 +304,15 @@ declare const game: ex.Engine;
declare const ball: ex.Actor;
declare const ballSpeed: ex.Vector;
declare type Actor = any;
declare const bricks: ex.Actor[];
declare const bricks: ex.Entity[];
// ---cut---
// On collision remove the brick, bounce the ball
let colliding = false;
ball.on("collisionstart", function (ev) {
if (bricks.indexOf(ev.other) > -1) {
if (bricks.indexOf(ev.other.owner) > -1) {
// kill removes an actor from the current scene
// therefore it will no longer be drawn or updated
ev.other.kill();
ev.other.owner.kill();
}

// reverse course after any collision
Expand Down Expand Up @@ -349,7 +349,7 @@ declare const game: ex.Engine;
declare const ball: ex.Actor;
declare const ballSpeed: ex.Vector;
declare type Actor = any;
declare const bricks: ex.Actor[];
declare const bricks: ex.Entity[];
// ---cut---
// Loss condition
ball.on("exitviewport", () => {
Expand Down
24 changes: 9 additions & 15 deletions src/engine/Collision/ColliderComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,46 +157,40 @@ export class ColliderComponent extends Component {
const precollision = evt as PreCollisionEvent<Collider>;
entity.events.emit(
'precollision',
new PreCollisionEvent(
precollision.target.owner,
precollision.other.owner,
precollision.side,
precollision.intersection,
precollision.contact
)
new PreCollisionEvent(precollision.self, precollision.other, precollision.side, precollision.intersection, precollision.contact)
);
if (entity instanceof Actor) {
entity.onPreCollisionResolve(precollision.target, precollision.other, precollision.side, precollision.contact);
entity.onPreCollisionResolve(precollision.self, precollision.other, precollision.side, precollision.contact);
}
});
this.events.on('postcollision', (evt: any) => {
const postcollision = evt as PostCollisionEvent<Collider>;
entity.events.emit(
'postcollision',
new PostCollisionEvent(
postcollision.target.owner,
postcollision.other.owner,
postcollision.self,
postcollision.other,
postcollision.side,
postcollision.intersection,
postcollision.contact
)
);
if (entity instanceof Actor) {
entity.onPostCollisionResolve(postcollision.target, postcollision.other, postcollision.side, postcollision.contact);
entity.onPostCollisionResolve(postcollision.self, postcollision.other, postcollision.side, postcollision.contact);
}
});
this.events.on('collisionstart', (evt: any) => {
const start = evt as CollisionStartEvent<Collider>;
entity.events.emit('collisionstart', new CollisionStartEvent(start.target.owner, start.other.owner, start.side, start.contact));
entity.events.emit('collisionstart', new CollisionStartEvent(start.self, start.other, start.side, start.contact));
if (entity instanceof Actor) {
entity.onCollisionStart(start.target, start.other, start.side, start.contact);
entity.onCollisionStart(start.self, start.other, start.side, start.contact);
}
});
this.events.on('collisionend', (evt: any) => {
const end = evt as CollisionEndEvent<Collider>;
entity.events.emit('collisionend', new CollisionEndEvent(end.target.owner, end.other.owner, end.side, end.lastContact));
entity.events.emit('collisionend', new CollisionEndEvent(end.self, end.other, end.side, end.lastContact));
if (entity instanceof Actor) {
entity.onCollisionEnd(end.target, end.other, end.side, end.lastContact);
entity.onCollisionEnd(end.self, end.other, end.side, end.lastContact);
}
});
}
Expand Down
Loading

0 comments on commit 591ff8e

Please sign in to comment.