Skip to content

Commit

Permalink
feat: added fixture functions
Browse files Browse the repository at this point in the history
  • Loading branch information
KeSuave committed Nov 26, 2024
1 parent d9da8cd commit b24bc13
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/lib/components/Fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ export interface KPFixtureComp extends Comp {
* @type {(Fixture | null)}
*/
fixture: Fixture | null;

getDensity(): number;
getFriction(): number;
getRestitution(): number;
isSensor(): boolean;
setDensity(density: number): void;
setFriction(friction: number): void;
setRestitution(restitution: number): void;
setSensor(flag: boolean): void;
}

type FixtureThis = GameObj<KPFixtureComp & KPBodyComp & KPShapeComp>;
Expand All @@ -27,6 +36,63 @@ export default function fixture(
require: ["kpBody", "kpShape"],
fixture: null,

getDensity() {
if (!this.fixture) {
throw new Error("kpFixture is not initialized");
}

return this.fixture.getDensity();
},
getFriction() {
if (!this.fixture) {
throw new Error("kpFixture is not initialized");
}

return this.fixture.getFriction();
},
getRestitution() {
if (!this.fixture) {
throw new Error("kpFixture is not initialized");
}

return this.fixture.getRestitution();
},
isSensor() {
if (!this.fixture) {
throw new Error("kpFixture is not initialized");
}

return this.fixture.isSensor();
},
setDensity(density: number) {
if (!this.fixture) {
throw new Error("kpFixture is not initialized");
}

return this.fixture.setDensity(density);
},
setFriction(friction: number) {
if (!this.fixture) {
throw new Error("kpFixture is not initialized");
}

return this.fixture.setFriction(friction);
},
setRestitution(restitution: number) {
if (!this.fixture) {
throw new Error("kpFixture is not initialized");
}

return this.fixture.setRestitution(restitution);
},
setSensor(flag: boolean) {
if (!this.fixture) {
throw new Error("kpFixture is not initialized");
}

return this.fixture.setSensor(flag);
},

add(this: FixtureThis) {
if (!this.body) throw new Error("kpBody is required");

Expand Down
34 changes: 34 additions & 0 deletions src/lib/components/Fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ import { KPShapesComp } from "./Shapes";

export interface KPFixturesComp extends Comp {
fixtures: Fixture[];

getDensity(index: number): number;
getFriction(index: number): number;
getRestitution(index: number): number;
isSensor(index: number): boolean;
setDensity(index: number, density: number): void;
setFriction(index: number, friction: number): void;
setRestitution(index: number, restitution: number): void;
setSensor(index: number, flag: boolean): void;
}

type FixturesThis = GameObj<KPFixturesComp & KPBodyComp & KPShapesComp>;
Expand All @@ -17,6 +26,31 @@ export default function fixtures(defs: KPFixtureDef[]): KPFixturesComp {
require: ["kpBody", "kpShapes"],
fixtures: [],

getDensity(index: number) {
return this.fixtures[index].getDensity();
},
getFriction(index: number) {
return this.fixtures[index].getFriction();
},
getRestitution(index: number) {
return this.fixtures[index].getRestitution();
},
isSensor(index: number) {
return this.fixtures[index].isSensor();
},
setDensity(index: number, density: number) {
return this.fixtures[index].setDensity(density);
},
setFriction(index: number, friction: number) {
return this.fixtures[index].setFriction(friction);
},
setRestitution(index: number, restitution: number) {
return this.fixtures[index].setRestitution(restitution);
},
setSensor(index: number, flag: boolean) {
return this.fixtures[index].setSensor(flag);
},

add(this: FixturesThis) {
if (!this.body) throw new Error("kpBody is required");
if (!this.shapes) throw new Error("kpShapes is required");
Expand Down

0 comments on commit b24bc13

Please sign in to comment.