Skip to content

Commit

Permalink
🎳 Add group colliding functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ferdodo committed May 31, 2024
1 parent d73d87f commit 7156788
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export { CENTERED_UNIT_BLOCK } from "./centered-unit-block";
export { findClosestBlock } from "./find-closest-block";
export { isBlockColliding } from "./is-block-colliding";
export { isBlockEqual } from "./is-block-equal";
export { isBlockGroupColliding } from "./is-block-group-colliding";
export { isBlockGroupCollidingBlockGroup } from "./is-block-group-colliding-block-group";
export { isBlockIncluding } from "./is-block-including";
export { isBlockNotEqual } from "./is-block-not-equal";
export { isBlockPositionEqual } from "./is-block-position-equal";
Expand Down
15 changes: 15 additions & 0 deletions src/is-block-group-colliding-block-group.example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Block, isBlockGroupCollidingBlockGroup } from "blockwise";

const blocksA: Block[] = [
{ x: 0, y: 0, w: 10, h: 10 },
{ x: 20, y: 20, w: 10, h: 10 },
{ x: 10, y: 10, w: 10, h: 10 }
];

const blocksB: Block[] = [
{ x: 5, y: 5, w: 10, h: 10 },
{ x: 25, y: 25, w: 10, h: 10 },
{ x: 15, y: 15, w: 10, h: 10 }
];

console.log(isBlockGroupCollidingBlockGroup(blocksA, blocksB)); // true
30 changes: 30 additions & 0 deletions src/is-block-group-colliding-block-group.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { test, expect } from "vitest";
import { isBlockGroupCollidingBlockGroup } from "./is-block-group-colliding-block-group";

test("should return true when block group collides block group", function() {
const blocksA = [
{ x: 0, y: 0, w: 10, h: 10 },
{ x: 20, y: 20, w: 10, h: 10 },
{ x: 10, y: 10, w: 10, h: 10 }
];

const blocksB = [
{ x: 5, y: 5, w: 10, h: 10 }
];

expect(isBlockGroupCollidingBlockGroup(blocksA, blocksB)).toEqual(true);
});

test("should return false when block group does not collide block group", function() {
const blocksA = [
{ x: 0, y: 0, w: 10, h: 10 },
{ x: 20, y: 20, w: 10, h: 10 },
{ x: 10, y: 10, w: 10, h: 10 }
];

const blocksB = [
{ x: 30, y: 30, w: 10, h: 10 }
];

expect(isBlockGroupCollidingBlockGroup(blocksA, blocksB)).toEqual(false);
});
10 changes: 10 additions & 0 deletions src/is-block-group-colliding-block-group.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Block, isBlockGroupColliding } from "blockwise";

/**
* Check if a group of block is colliding with a group of blocks.
*
* @includeExample ./src/is-block-group-colliding-block-group.example.ts
*/
export function isBlockGroupCollidingBlockGroup(a: Block[], b: Block[]): boolean {
return a.some((block) => isBlockGroupColliding(b, block));
}
11 changes: 11 additions & 0 deletions src/is-block-group-colliding.example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Block, isBlockGroupColliding } from "blockwise";

const blocks: Block[] = [
{ x: 0, y: 0, w: 10, h: 10 },
{ x: 20, y: 20, w: 10, h: 10 },
{ x: 10, y: 10, w: 10, h: 10 }
];

const block: Block = { x: 5, y: 5, w: 10, h: 10 };

console.log(isBlockGroupColliding(blocks, block)); // true
14 changes: 14 additions & 0 deletions src/is-block-group-colliding.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { test, expect } from "vitest";
import { isBlockGroupColliding } from "./is-block-group-colliding";

test("should return true when block group collides some of block group", function() {
const blocksA = [
{ x: 0, y: 0, w: 10, h: 10 },
{ x: 20, y: 20, w: 10, h: 10 },
{ x: 10, y: 10, w: 10, h: 10 }
];

const blocksB = { x: 5, y: 5, w: 10, h: 10 };

expect(isBlockGroupColliding(blocksA, blocksB)).toEqual(true);
});
10 changes: 10 additions & 0 deletions src/is-block-group-colliding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Block, isBlockColliding } from "blockwise";

/**
* Check if a block is colliding with any block in a group of blocks.
*
* @includeExample ./src/is-block-group-colliding.example.ts
*/
export function isBlockGroupColliding(blocks: Block[], block: Block): boolean {
return blocks.some((b) => isBlockColliding(b, block));
}

0 comments on commit 7156788

Please sign in to comment.