From be2dd8aba4e4f7c10c9fedfee91e3e9cb07369dd Mon Sep 17 00:00:00 2001 From: Lennert Claeys Date: Tue, 26 Dec 2023 20:49:07 +0100 Subject: [PATCH] Make bodies share an interface --- src/data/collision/body.ts | 3 ++- src/data/collision/index.ts | 5 +++++ src/data/collision/simpleBody.ts | 5 ++++- 3 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 src/data/collision/index.ts diff --git a/src/data/collision/body.ts b/src/data/collision/body.ts index 11676df..fbada22 100644 --- a/src/data/collision/body.ts +++ b/src/data/collision/body.ts @@ -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 @@ -22,7 +23,7 @@ interface Config { roundness?: number; } -export class Body { +export class Body implements PhysicsBody { public active = 1; private xVelocity = 0; diff --git a/src/data/collision/index.ts b/src/data/collision/index.ts new file mode 100644 index 0000000..2859afd --- /dev/null +++ b/src/data/collision/index.ts @@ -0,0 +1,5 @@ +export interface PhysicsBody { + addVelocity(x: number, y: number): void; + addAngularVelocity(power: number, direction: number): void; + tick(dt: number): boolean; +} diff --git a/src/data/collision/simpleBody.ts b/src/data/collision/simpleBody.ts index 104160d..07e4e0f 100644 --- a/src/data/collision/simpleBody.ts +++ b/src/data/collision/simpleBody.ts @@ -1,3 +1,4 @@ +import { PhysicsBody } from "."; import { CollisionMask } from "./collisionMask"; const GRAVITY = 0.3; @@ -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; @@ -105,6 +106,8 @@ export class SimpleBody { ) { this.onCollide(xCollision || x, yCollision || y); } + + return true; } get velocity() {