Add bogus entity query

This commit is contained in:
Ketchetwahmeegwun T. Southall 2022-01-30 11:02:39 -08:00
parent c968f7a9a6
commit a886c73047
2 changed files with 40 additions and 4 deletions

View File

@ -1,6 +1,10 @@
import { Entity } from "./Entity"
import * as planck from 'planck'
export interface WorldContext {
addEntity(entity: Entity, layerTitle: string, x: number, y: number): void
removeEntity(entity: Entity): void
queryAABB(aabb: planck.AABB, callback: (fixture: planck.Fixture) => boolean): void
rayCast(point1: planck.Vec2, point2: planck.Vec2, callback: (fixture: planck.Fixture, pointer: planck.Vec2, normal: planck.Vec2, fraction: number) => boolean): void
queryEntities(min: [number, number], max: [number, number]): Entity[]
}

View File

@ -254,10 +254,6 @@ export function GameState(ctx: ContextI, selectedAnimal: string, selectedSegment
let entity = new AnimalEntity(animals[animal])
setTimeout(() => {
entity.dead = true
entity.shouldGib = true
}, 1000+Math.random()*3000)
addEntity(entity, 'objects', x, y)
}
@ -373,6 +369,7 @@ export function GameState(ctx: ContextI, selectedAnimal: string, selectedSegment
shape = planck.Polygon(spriteShape.points.map(v=>planck.Vec2(v[0], v[1])))
}
if (shape !== undefined) {
entity.shape = shape
let body = world.createDynamicBody({
position: planck.Vec2(entity.x, entity.y),
fixedRotation: true,
@ -460,6 +457,41 @@ export function GameState(ctx: ContextI, selectedAnimal: string, selectedSegment
let worldContext: WorldContext = {
addEntity,
removeEntity,
queryAABB: (aabb: planck.AABB, callback: (fixture: planck.Fixture) => boolean) => {
world.queryAABB(aabb, callback)
},
rayCast: (point1: planck.Vec2, point2: planck.Vec2, callback: (fixture: planck.Fixture, point: planck.Vec2, normal: planck.Vec2, fraction: number) => boolean) => {
world.rayCast(point1, point2, callback)
},
queryEntities: (min: [number, number], max: [number, number]): Entity[] => {
let ret: Entity[] = []
for (let entity of entities) {
if (!entity.shape) {
continue
}
let p = entity.position
let r = entity.shape.getRadius()
let a = {
x: min[0],
w: max[0] - min[0],
y: min[1],
h: max[1] - min[1],
}
let b = {
x: p[0],
w: r,
y: p[1],
h: r,
}
console.log(a, 'vs', b)
if (
(Math.abs(min[0] - (p[0]-r)) * 2 < ((max[0]-min[0]) + r)) &&
(Math.abs(min[1] - (p[1]-r)) * 2 < ((max[1]-min[1]) + r))) {
ret.push(entity)
}
}
return ret
},
}
let desiredActions: Action[] = []