Skip to content

Commit

Permalink
rename CollisionAction. Add EllipseCollisionAction
Browse files Browse the repository at this point in the history
  • Loading branch information
tassaron committed Apr 5, 2022
1 parent 6722547 commit feb220c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
21 changes: 21 additions & 0 deletions src/actors/actions/EllipseCollisionAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import IActorAction from "../../interfaces/IActorAction";
import IActor from "../../interfaces/IActor";


export default class EllipseCollisionAction implements IActorAction {
actor: IActor;
squareRadius: number;
overlap = 0;

constructor(actor: IActor) {
this.actor = actor;
this.squareRadius = Math.pow((actor.width / 3) * 2, 2);
}

collides(other: EllipseCollisionAction) {
this.overlap = (Math.pow(other.actor.x - this.actor.x, 2) + Math.pow(other.actor.y - this.actor.y, 2)) - this.squareRadius
return (
this.overlap < 0
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import IActorAction from "../../interfaces/IActorAction";
import IActor from "../../interfaces/IActor";


export default class CollisionAction implements IActorAction {
export default class RectangleCollisionAction implements IActorAction {
actor: IActor;

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

collides(other: CollisionAction) {
collides(other: RectangleCollisionAction) {
return (
this.actor.x + this.actor.width > other.actor.x &&
this.actor.x < other.actor.x + other.actor.width &&
Expand Down
6 changes: 3 additions & 3 deletions src/examples/BallsScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ import { logger } from "../core/logger";
import IGame from "../interfaces/IGame";
import Scene from "../scenes/Scene";
import RectangleActor from "../actors/RectangleActor";
import CollisionAction from "../actors/actions/CollisionAction";
import RectangleCollisionAction from "../actors/actions/RectangleCollisionAction";
import IKeyboard from "../interfaces/IKeyboard";
import MenuScene, { newBackButton } from "../scenes/MenuScene";


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

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

tick(delta: number, keyboard: IKeyboard) {
Expand Down

0 comments on commit feb220c

Please sign in to comment.