-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
186 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Component } from "src/lib/ecs/Component"; | ||
|
||
type Sprite = Texture; | ||
|
||
export default class SpriteComponent implements Component { | ||
sprite: Sprite; | ||
|
||
constructor(sprite: Sprite) { | ||
this.sprite = sprite; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { angle, distance, normalizeAngle } from "src/lib/utils"; | ||
import Entity from "../Entity"; | ||
import PositionComponent from "../components/PositionComponent"; | ||
import CircleComponent from "../components/CircleComponent"; | ||
|
||
type Radius = number; | ||
type Angle = number; | ||
|
||
export interface PolarPosition { | ||
distance: Radius; | ||
angleFrom: Angle; | ||
angleTo: Angle; | ||
entity: Entity; | ||
} | ||
|
||
export default class PolarMap { | ||
polarEntities: PolarPosition[] = []; | ||
|
||
constructor(center: Entity, entities: Entity[]) { | ||
this.calculatePolarEntities(center, entities); | ||
} | ||
|
||
public select(distanceTo: number, angleFrom: number, angleTo: number) { | ||
|
||
angleFrom = normalizeAngle(angleFrom); | ||
angleTo = normalizeAngle(angleTo); | ||
|
||
return this.polarEntities | ||
.filter((polarEntity) => { | ||
if (distanceTo <= polarEntity.distance) { | ||
return false | ||
} | ||
|
||
const a1 = 0; | ||
const a2 = normalizeAngle(polarEntity.angleTo - polarEntity.angleFrom); | ||
const b1 = normalizeAngle(angleFrom - polarEntity.angleFrom); | ||
const b2 = normalizeAngle(angleTo - polarEntity.angleFrom); | ||
|
||
return ( | ||
a1 <= b1 && b1 <= a2 && | ||
a1 <= b2 && b2 <= a2 | ||
) | ||
}) | ||
.sort((pe1, pe2) => pe2.distance - pe1.distance) | ||
} | ||
|
||
protected calculatePolarEntities(center: Entity, entities: Entity[]) { | ||
const centerPosition = center.getComponent(PositionComponent); | ||
|
||
this.polarEntities = entities.map(entity => { | ||
const pointCircle = entity.getComponent(CircleComponent); | ||
const pointPosition = entity.getComponent(PositionComponent); | ||
const a = angle( | ||
centerPosition.x, | ||
centerPosition.y, | ||
pointPosition.x, | ||
pointPosition.y, | ||
); | ||
|
||
const d = distance( | ||
centerPosition.x, | ||
centerPosition.y, | ||
pointPosition.x, | ||
pointPosition.y, | ||
); | ||
|
||
const ta = Math.asin(pointCircle.radius / (d)) * (180 / Math.PI); | ||
|
||
return { | ||
distance: d, | ||
angleFrom: normalizeAngle(a - ta), | ||
angleTo: normalizeAngle(a + ta), | ||
entity | ||
} | ||
}).filter(polarEntity => !isNaN(polarEntity.angleFrom)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.