Skip to content

Commit

Permalink
Make bodies share an interface
Browse files Browse the repository at this point in the history
  • Loading branch information
lorgan3 committed Dec 26, 2023
1 parent d47d0f3 commit be2dd8a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/data/collision/body.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { PhysicsBody } from ".";
import { CollisionMask } from "./collisionMask";

// Random value that determines how much gravity a body must receive before something is considered a collision with the ground/ceiling
Expand All @@ -22,7 +23,7 @@ interface Config {
roundness?: number;
}

export class Body {
export class Body implements PhysicsBody {
public active = 1;

private xVelocity = 0;
Expand Down
5 changes: 5 additions & 0 deletions src/data/collision/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface PhysicsBody {
addVelocity(x: number, y: number): void;
addAngularVelocity(power: number, direction: number): void;
tick(dt: number): boolean;
}
5 changes: 4 additions & 1 deletion src/data/collision/simpleBody.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { PhysicsBody } from ".";
import { CollisionMask } from "./collisionMask";

const GRAVITY = 0.3;
Expand All @@ -12,7 +13,7 @@ interface Config {
onCollide?: (x: number, y: number) => void;
}

export class SimpleBody {
export class SimpleBody implements PhysicsBody {
private xVelocity = 0;
private yVelocity = 0;

Expand Down Expand Up @@ -105,6 +106,8 @@ export class SimpleBody {
) {
this.onCollide(xCollision || x, yCollision || y);
}

return true;
}

get velocity() {
Expand Down

0 comments on commit be2dd8a

Please sign in to comment.