Skip to content

Commit

Permalink
feat: further improve generic type of system
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacek Pietal committed May 11, 2023
1 parent 6bdfbf2 commit 73a1498
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 18 deletions.
4 changes: 2 additions & 2 deletions dist/model.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ export interface BodyOptions {
/**
* system.raycast(from, to) result
*/
export interface RaycastHit {
export interface RaycastHit<TBody> {
point: Vector;
body: Body;
body: TBody;
}
/**
* potential vector
Expand Down
4 changes: 2 additions & 2 deletions dist/system.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export declare class System<TBody extends Body = Body> extends BaseSystem<TBody>
/**
* check one collider collisions with callback
*/
checkOne(body: Body, callback: (response: Response) => void | boolean, response?: Response): boolean;
checkOne(body: TBody, callback: (response: Response) => void | boolean, response?: Response): boolean;
/**
* check all colliders collisions with callback
*/
Expand All @@ -53,7 +53,7 @@ export declare class System<TBody extends Body = Body> extends BaseSystem<TBody>
/**
* raycast to get collider of ray from start to end
*/
raycast(start: Vector, end: Vector, allow?: (body: TBody) => boolean): RaycastHit | null;
raycast(start: Vector, end: Vector, allow?: (body: TBody) => boolean): RaycastHit<TBody> | null;
/**
* used to find body deep inside data with finder function returning boolean found or not
*/
Expand Down
6 changes: 3 additions & 3 deletions docs/classes/System.html
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ <h3 class="tsd-anchor-link"><span>check<wbr/>One</span><a href="#checkOne" aria-
<h4 class="tsd-parameters-title">Parameters</h4>
<ul class="tsd-parameter-list">
<li>
<h5><span class="tsd-kind-parameter">body</span>: <a href="../types/Body.html" class="tsd-signature-type tsd-kind-type-alias">Body</a></h5></li>
<h5><span class="tsd-kind-parameter">body</span>: <span class="tsd-signature-type tsd-kind-type-parameter">TBody</span></h5></li>
<li>
<h5><span class="tsd-kind-parameter">callback</span>: <span class="tsd-signature-symbol">(</span><span class="tsd-signature-symbol">(</span><span class="tsd-kind-parameter">response</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol"> =&gt; </span><span class="tsd-signature-type">boolean</span><span class="tsd-signature-symbol"> | </span><span class="tsd-signature-type">void</span><span class="tsd-signature-symbol">)</span></h5>
<ul class="tsd-parameters">
Expand Down Expand Up @@ -555,7 +555,7 @@ <h4 class="tsd-returns-title">Returns <a href="RBush.html" class="tsd-signature-
<section class="tsd-panel tsd-member"><a id="raycast" class="tsd-anchor"></a>
<h3 class="tsd-anchor-link"><span>raycast</span><a href="#raycast" aria-label="Permalink" class="tsd-anchor-icon"><svg class="icon icon-tabler icon-tabler-link" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><use href="#icon-anchor-a"></use><use href="#icon-anchor-b"></use><use href="#icon-anchor-c"></use></svg></a></h3>
<ul class="tsd-signatures">
<li class="tsd-signature tsd-anchor-link" id="raycast.raycast-1"><span class="tsd-kind-call-signature">raycast</span><span class="tsd-signature-symbol">(</span><span class="tsd-kind-parameter">start</span>, <span class="tsd-kind-parameter">end</span>, <span class="tsd-kind-parameter">allow</span><span class="tsd-signature-symbol">?</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">null</span><span class="tsd-signature-symbol"> | </span><a href="../interfaces/RaycastHit.html" class="tsd-signature-type tsd-kind-interface">RaycastHit</a><a href="#raycast.raycast-1" aria-label="Permalink" class="tsd-anchor-icon"><svg class="icon icon-tabler icon-tabler-link" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><use href="#icon-anchor-a"></use><use href="#icon-anchor-b"></use><use href="#icon-anchor-c"></use></svg></a></li>
<li class="tsd-signature tsd-anchor-link" id="raycast.raycast-1"><span class="tsd-kind-call-signature">raycast</span><span class="tsd-signature-symbol">(</span><span class="tsd-kind-parameter">start</span>, <span class="tsd-kind-parameter">end</span>, <span class="tsd-kind-parameter">allow</span><span class="tsd-signature-symbol">?</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">null</span><span class="tsd-signature-symbol"> | </span><a href="../interfaces/RaycastHit.html" class="tsd-signature-type tsd-kind-interface">RaycastHit</a><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type tsd-kind-type-parameter">TBody</span><span class="tsd-signature-symbol">&gt;</span><a href="#raycast.raycast-1" aria-label="Permalink" class="tsd-anchor-icon"><svg class="icon icon-tabler icon-tabler-link" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><use href="#icon-anchor-a"></use><use href="#icon-anchor-b"></use><use href="#icon-anchor-c"></use></svg></a></li>
<li class="tsd-description">
<div class="tsd-comment tsd-typography"><p>raycast to get collider of ray from start to end</p>
</div>
Expand All @@ -579,7 +579,7 @@ <h4 class="tsd-parameters-title">Parameters</h4>
<li>
<h5><span class="tsd-kind-parameter">body</span>: <span class="tsd-signature-type tsd-kind-type-parameter">TBody</span></h5></li></ul></div>
<h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">boolean</span></h4></li></ul></li></ul></li></ul></div>
<h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">null</span><span class="tsd-signature-symbol"> | </span><a href="../interfaces/RaycastHit.html" class="tsd-signature-type tsd-kind-interface">RaycastHit</a></h4><aside class="tsd-sources">
<h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">null</span><span class="tsd-signature-symbol"> | </span><a href="../interfaces/RaycastHit.html" class="tsd-signature-type tsd-kind-interface">RaycastHit</a><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type tsd-kind-type-parameter">TBody</span><span class="tsd-signature-symbol">&gt;</span></h4><aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/Prozi/detect-collisions/blob/master/src/system.ts#L220">src/system.ts:220</a></li></ul></aside></li></ul></section>
<section class="tsd-panel tsd-member"><a id="remove" class="tsd-anchor"></a>
Expand Down
11 changes: 8 additions & 3 deletions docs/interfaces/RaycastHit.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@
<ul class="tsd-breadcrumb">
<li><a href="../modules.html">Detect-Collisions</a></li>
<li><a href="RaycastHit.html">RaycastHit</a></li></ul>
<h1>Interface RaycastHit</h1></div>
<h1>Interface RaycastHit&lt;TBody&gt;</h1></div>
<section class="tsd-panel tsd-comment">
<div class="tsd-comment tsd-typography"><p>system.raycast(from, to) result</p>
</div></section>
</div></section>
<section class="tsd-panel">
<h4>Type Parameters</h4>
<ul class="tsd-type-parameter-list">
<li>
<h4><span class="tsd-kind-type-parameter">TBody</span></h4></li></ul></section>
<section class="tsd-panel tsd-hierarchy">
<h4>Hierarchy</h4>
<ul class="tsd-hierarchy">
Expand All @@ -38,7 +43,7 @@ <h3 class="tsd-index-heading">Properties</h3>
<h2>Properties</h2>
<section class="tsd-panel tsd-member"><a id="body" class="tsd-anchor"></a>
<h3 class="tsd-anchor-link"><span>body</span><a href="#body" aria-label="Permalink" class="tsd-anchor-icon"><svg class="icon icon-tabler icon-tabler-link" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none" id="icon-anchor-a"></path><path d="M10 14a3.5 3.5 0 0 0 5 0l4 -4a3.5 3.5 0 0 0 -5 -5l-.5 .5" id="icon-anchor-b"></path><path d="M14 10a3.5 3.5 0 0 0 -5 0l-4 4a3.5 3.5 0 0 0 5 5l.5 -.5" id="icon-anchor-c"></path></svg></a></h3>
<div class="tsd-signature"><span class="tsd-kind-property">body</span><span class="tsd-signature-symbol">:</span> <a href="../types/Body.html" class="tsd-signature-type tsd-kind-type-alias">Body</a></div><aside class="tsd-sources">
<div class="tsd-signature"><span class="tsd-kind-property">body</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type tsd-kind-type-parameter">TBody</span></div><aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/Prozi/detect-collisions/blob/master/src/model.ts#L88">src/model.ts:88</a></li></ul></aside></section>
<section class="tsd-panel tsd-member"><a id="point" class="tsd-anchor"></a>
Expand Down
2 changes: 1 addition & 1 deletion src/base-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class BaseSystem<TBody extends Body>
* draw exact bodies colliders outline
*/
draw(context: CanvasRenderingContext2D): void {
forEach(this.all(), (body: Body) => {
forEach(this.all(), (body: TBody) => {
body.draw(context);
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ export interface BodyOptions {
/**
* system.raycast(from, to) result
*/
export interface RaycastHit {
export interface RaycastHit<TBody> {
point: Vector;
body: Body;
body: TBody;
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class System<TBody extends Body = Body> extends BaseSystem<TBody> {
* check one collider collisions with callback
*/
checkOne(
body: Body,
body: TBody,
callback: (response: Response) => void | boolean,
response = this.response
): boolean {
Expand All @@ -126,7 +126,7 @@ export class System<TBody extends Body = Body> extends BaseSystem<TBody> {
const checkCollision = (candidate: TBody) => {
if (
candidate !== body &&
this.checkCollision(body as TBody, candidate, response)
this.checkCollision(body, candidate, response)
) {
return callback(response);
}
Expand Down Expand Up @@ -221,9 +221,9 @@ export class System<TBody extends Body = Body> extends BaseSystem<TBody> {
start: Vector,
end: Vector,
allow: (body: TBody) => boolean = () => true
): RaycastHit | null {
): RaycastHit<TBody> | null {
let minDistance = Infinity;
let result: RaycastHit | null = null;
let result: RaycastHit<TBody> | null = null;

if (!this.ray) {
this.ray = new Line(start, end, { isTrigger: true });
Expand All @@ -234,7 +234,7 @@ export class System<TBody extends Body = Body> extends BaseSystem<TBody> {

this.insert(this.ray as TBody);

this.checkOne(this.ray, ({ b: body }) => {
this.checkOne(this.ray as TBody, ({ b: body }) => {
if (!allow(body)) {
return false;
}
Expand Down

0 comments on commit 73a1498

Please sign in to comment.