Skip to content

Commit

Permalink
change CollisionActor into CollisionAction
Browse files Browse the repository at this point in the history
  • Loading branch information
tassaron committed Apr 5, 2022
1 parent 03296a5 commit 6722547
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 53 deletions.
37 changes: 0 additions & 37 deletions src/actors/CollisionActor.ts

This file was deleted.

20 changes: 20 additions & 0 deletions src/actors/actions/CollisionAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import IActorAction from "../../interfaces/IActorAction";
import IActor from "../../interfaces/IActor";


export default class CollisionAction implements IActorAction {
actor: IActor;

constructor(actor: IActor) {
this.actor = actor;
}

collides(other: CollisionAction) {
return (
this.actor.x + this.actor.width > other.actor.x &&
this.actor.x < other.actor.x + other.actor.width &&
other.actor.y + other.actor.height > this.actor.y &&
other.actor.y < this.actor.y + this.actor.height
);
}
}
22 changes: 14 additions & 8 deletions src/examples/BallsScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,34 @@ import * as PIXI from "pixi.js";
import { logger } from "../core/logger";
import IGame from "../interfaces/IGame";
import Scene from "../scenes/Scene";
import EllipseActor from "../actors/EllipseActor";
import RectangleActor from "../actors/RectangleActor";
import CollisionActor from "../actors/CollisionActor";
import CollisionAction from "../actors/actions/CollisionAction";
import IKeyboard from "../interfaces/IKeyboard";
import MenuScene, { newBackButton } from "../scenes/MenuScene";


export class Ball extends CollisionActor {
export class Ball extends RectangleActor {
dx = 0.0;
dy = 0.0;
collision: CollisionAction;

constructor(game: IGame) {
super(game, 60, 60, 0x666666, null);
this.collision = new CollisionAction(this);
}

tick(delta: number, keyboard: IKeyboard) {
super.tick(delta, keyboard);
if (this.x < 0.0) {
this.dx = Math.abs(this.dx);
} else if (this.x + this.collisionWidth > this.game.width) {
} else if (this.x + this.width > this.game.width) {
this.dx = Math.abs(this.dx) * -1;
}
this.x += this.dx * delta;

if (this.y < 0.0) {
this.dy = Math.abs(this.dy);
} else if (this.y + this.collisionHeight > this.game.height) {
} else if (this.y + this.height > this.game.height) {
this.dy = Math.abs(this.dy) * -1;
}
this.y += this.dy * delta;
Expand All @@ -42,8 +47,9 @@ export default class BallsScene extends Scene {

// Create balls to bounce around the screen
this.balls = [
new Ball(game, new EllipseActor(game, 30, 30, 0x666666, null), 60, 60),
new Ball(game, new RectangleActor(game, 60, 60, 0x666666, null), 60, 60)
new Ball(game),
new Ball(game),
new Ball(game),
]
this.addActors(this.balls);
this.placeBalls();
Expand Down Expand Up @@ -91,7 +97,7 @@ export default class BallsScene extends Scene {
for (let j = 0; j < this.balls.length; j++) {
if (i == j || collisions.has(j)) continue;
collisions.add([i, j]);
if (this.balls[i].collides(this.balls[j])) {
if (this.balls[i].collision.collides(this.balls[j].collision)) {
this.balls[i].dx = -this.balls[i].dx;
this.balls[i].dy = -this.balls[i].dy;
}
Expand Down
8 changes: 0 additions & 8 deletions src/interfaces/ICollisionActor.ts

This file was deleted.

0 comments on commit 6722547

Please sign in to comment.