Skip to content

Commit

Permalink
feat: added collisionIgnore to Fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
KeSuave committed Nov 22, 2024
1 parent a1e624b commit 734acc6
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 121 deletions.
67 changes: 7 additions & 60 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,15 @@

A KaPlay plugin that integrates Planck, while keeping the simple/fun API of KaPlay.

## Installation
[Read the docs to learn more](https://kesuave.github.io/KaPlanck)

```shell
npm i kaplanck
```
## Contributing

## Usage
Pull requests are welcome. For major changes, please open an issue first
to discuss what you would like to change.

<sub><sup>For more example check the examples folder</sup></sub>
Please make sure to update tests as appropriate.

```ts
import KaPlanckPlugin from "kaplanck";
import kaplay from "kaplay";
import { Vec2 } from "planck";
## License

const k = kaplay({
global: false,
background: [20, 20, 20],
plugins: [
KaPlanckPlugin({
lengthUnitsPerMeter: 20,
}),
],
debug: true,
debugKey: "d",
});

const worldContainer = k.add([
k.kpWorld({
gravity: new Vec2(0, 10),
}),
]);

worldContainer.add([
k.color(100, 100, 100),
k.kpPos(k.kpCenter()),
k.kpRotate(Math.PI * 0.1),
k.kpEdgeShape({
v1: new Vec2(-10, 0),
v2: new Vec2(10, 0),
draw: true,
}),
k.kpBody({
type: "static",
}),
k.kpFixture(),
]);

worldContainer.add([
k.color(200, 200, 200),
k.kpPos(k.kpCenter().sub({ x: k.rand(-10, 10), y: k.rand(10, 15) })),
k.kpRotate(),
k.kpCircleShape({
radius: 1,
draw: true,
}),
k.kpBody({ type: "dynamic" }),
k.kpFixture({ density: 1, friction: 0.3 }),
k.offscreen({ destroy: true }),
]);
```

## Documentation

WIP
[MIT](https://choosealicense.com/licenses/mit/)
Binary file added docssrc/public/suave.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@
"docs:build": "vocs build",
"docs:preview": "vocs preview",
"typedocs": "typedoc --options typedoc.json",
"predocs:dev": "npm run typedocs && node utils/docLinks.mjs",
"predocs:build": "npm run typedocs && node utils/docLinks.mjs",
"predocs:dev": "npm run typedocs",
"predocs:build": "npm run typedocs",
"posttypedocs": "node utils/docLinks.mjs",
"postdocs:build": "node utils/docsFolder.mjs"
},
"devDependencies": {
Expand Down
13 changes: 11 additions & 2 deletions src/lib/components/Body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,18 @@ import type { KPPosComp } from "./Position";
import type { KPRotateComp } from "./Rotate";

export interface KPBodyComp extends Comp {
/**
* The physics body associated with the game object.
*
* @type {(Body | null)}
*/
body: Body | null;

// the following are just for drawInspect and debug purposes
/**
* @internal
* The color to use for debugging the physics body.
*
* @type {{ r: number; g: number; b: number }}
*/
inspectColor: { r: number; g: number; b: number };
}

Expand Down
67 changes: 65 additions & 2 deletions src/lib/components/Fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import type {
KAPLAYCtx,
KEventController,
MouseButton,
Tag,
} from "kaplay";
import type { Fixture, FixtureDef } from "planck";

import { k2pVec2 } from "../utils";
import type { KPBodyComp } from "./Body";
import type { KPShapeComp } from "./Shape";
import { k2pVec2 } from "../utils";

export interface KPFixtureUserData {
gameObj: GameObj;
Expand All @@ -21,13 +22,59 @@ export interface KPFixtureDef extends Omit<FixtureDef, "shape"> {
}

export interface KPFixtureComp extends Comp {
/**
* The underlying Fixture object.
*
* @type {(Fixture | null)}
*/
fixture: Fixture | null;

/**
* Tags of other components that this fixture should ignore collisions with.
*
* @type {Tag[]}
*/
collisionIgnore: Tag[];

/**
* Check if the mouse is hovering over this fixture.
*
* @returns {boolean}
*/
isHovering(): boolean;
/**
* Check if the mouse has clicked on this fixture.
*
* @returns {boolean}
*/
isClicked(): boolean;
/**
* Add an action to be executed when the mouse starts hovering over this fixture.
*
* @param {() => void} action - The action to execute.
* @returns {KEventController} - A controller for managing the event.
*/
onHover(action: () => void): KEventController;
/**
* Add an action to be executed when the mouse updates its position while hovering over this fixture.
*
* @param {() => void} action - The action to execute.
* @returns {KEventController} - A controller for managing the event.
*/
onHoverUpdate(action: () => void): KEventController;
/**
* Add an action to be executed when the mouse stops hovering over this fixture.
*
* @param {() => void} action - The action to execute.
* @returns {KEventController} - A controller for managing the event.
*/
onHoverEnd(action: () => void): KEventController;
/**
* Add an action to be executed when this fixture is clicked.
*
* @param {() => void} action - The action to execute.
* @param {MouseButton} [btn] - The mouse button that was used to click (default: MouseButton.LEFT).
* @returns {KEventController} - A controller for managing the event.
*/
onClick(action: () => void, btn?: MouseButton): KEventController;
}

Expand All @@ -36,11 +83,13 @@ type FixtureThis = GameObj<KPFixtureComp & KPBodyComp & KPShapeComp>;
export default function fixture(
k: KAPLAYCtx,
def?: KPFixtureDef,
collisionIgnore?: Tag[],
): KPFixtureComp {
return {
id: "kpFixture",
require: ["kpBody", "kpShape"],
fixture: null,
collisionIgnore: collisionIgnore ?? [],

isHovering() {
if (!this.fixture) return false;
Expand Down Expand Up @@ -108,6 +157,20 @@ export default function fixture(
gameObj: this,
},
});

this.fixture.shouldCollide = (that: Fixture) => {
if (this.collisionIgnore.length === 0) return true;

const userData = that.getUserData() as KPFixtureUserData;

const thatGameObj = userData.gameObj;

if (thatGameObj.tags.length === 0) return true;

return !this.collisionIgnore.some((tag) => {
return thatGameObj.tags.includes(tag);
});
};
},
};
}
Loading

0 comments on commit 734acc6

Please sign in to comment.