diff --git a/src/actors/CollisionActor.ts b/src/actors/CollisionActor.ts deleted file mode 100644 index 6bdeede..0000000 --- a/src/actors/CollisionActor.ts +++ /dev/null @@ -1,37 +0,0 @@ -import IActor from "../interfaces/IActor"; -import IGame from "../interfaces/IGame"; -import Actor from "./Actor"; -import ICollisionActor from "../interfaces/ICollisionActor"; -import { keyboard } from "../core/ui"; -import IKeyboard from "../interfaces/IKeyboard"; - - -export default class CollisionActor extends Actor implements ICollisionActor { - collisionWidth: number; - collisionHeight: number; - graphic: IActor; - - constructor(game: IGame, graphic: IActor, w: number, h: number) { - super(game); - this.collisionWidth = w; - this.collisionHeight = h; - this.graphic = graphic; - this.addChild(graphic); - graphic.x = 0; - graphic.y = 0; - } - - collides(other: ICollisionActor) { - return ( - this.x + this.collisionWidth > other.x && - this.x < other.x + other.collisionWidth && - other.y + other.collisionHeight > this.y && - other.y < this.y + this.collisionHeight - ); - } - - tick(delta: number, keyboard: IKeyboard) { - super.tick(delta, keyboard); - this.graphic.tick(delta, keyboard); - } -} diff --git a/src/actors/actions/CollisionAction.ts b/src/actors/actions/CollisionAction.ts new file mode 100644 index 0000000..1b987a3 --- /dev/null +++ b/src/actors/actions/CollisionAction.ts @@ -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 + ); + } +} diff --git a/src/examples/BallsScene.ts b/src/examples/BallsScene.ts index 89cde34..e1c748a 100644 --- a/src/examples/BallsScene.ts +++ b/src/examples/BallsScene.ts @@ -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; @@ -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(); @@ -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; } diff --git a/src/interfaces/ICollisionActor.ts b/src/interfaces/ICollisionActor.ts deleted file mode 100644 index 3ba6c43..0000000 --- a/src/interfaces/ICollisionActor.ts +++ /dev/null @@ -1,8 +0,0 @@ -import IActor from "./IActor"; - - -export default interface ICollisionActor extends IActor { - collisionWidth: number; - collisionHeight: number; - collides(other: ICollisionActor): boolean; -} \ No newline at end of file