Add/use spawn zones; reduce physics step

This commit is contained in:
Ketchetwahmeegwun T. Southall 2022-01-30 03:23:41 -08:00
parent 479973f9d6
commit a5207b7f81

View File

@ -101,6 +101,7 @@ export function GameState(ctx: ContextI): StateI {
let layers: Layer[] = []
let entities: Entity[] = []
let zones: Zone[] = []
let spawnZones: Record<string, Zone[]> = {}
let enter = () => {
lastTime = performance.now()
@ -174,18 +175,33 @@ export function GameState(ctx: ContextI): StateI {
addZone(new Zone(z))
}
// Add player entity
player = new AnimalEntity(animals.deer)
player.isPlayer = true
addEntity(player, 'objects', 300, 100)
let playerSpawn = getSpawnZone('spawn')
if (!playerSpawn) {
// return to menu with an error?
} else {
let bounds = playerSpawn.bounds
let x = bounds[0] + Math.floor(Math.random() * bounds[2])
let y = bounds[1] + Math.floor(Math.random() * bounds[3])
// Add player entity
player = new AnimalEntity(animals.deer)
player.isPlayer = true
addEntity(player, 'objects', x, y)
}
// Add fake others
addEntity(new AnimalEntity(animals.turkey), 'objects', 200, 200)
addEntity(new AnimalEntity(animals.nutria), 'objects', Math.random()*w.width, Math.random()*w.height)
addEntity(new AnimalEntity(animals.deer), 'objects', Math.random()*w.width, Math.random()*w.height)
addEntity(new AnimalEntity(animals.salamander), 'objects', Math.random()*w.width, Math.random()*w.height)
addEntity(new AnimalEntity(animals.rabbit), 'objects', Math.random()*w.width, Math.random()*w.height)
// MOVE ME
for (let animal of ['turkey','nutria','deer','salamander','rabbit']) {
let count = 2 + Math.floor(Math.random() * 8)
for (let i = 0; i < count; i++) {
let spawn = getSpawnZone(animal)
if (!spawn) break
let bounds = spawn.bounds
let x = bounds[0] + Math.floor(Math.random() * bounds[2])
let y = bounds[1] + Math.floor(Math.random() * bounds[3])
addEntity(new AnimalEntity(animals[animal]), 'objects', x, y)
}
}
ctx.app.stage.addChild(rootContainer)
}
let leave = () => {
@ -211,9 +227,9 @@ export function GameState(ctx: ContextI): StateI {
nightfall(true)
}
// Run world sim.
while (elapsed >= 1/60) {
world.step(1/60)
elapsed -= 1/60
while (elapsed >= 1/5) {
world.step(1/5)
elapsed -= 1/5
}
// Update/render.
for (let layer of layers) {
@ -243,9 +259,7 @@ export function GameState(ctx: ContextI): StateI {
}
let addEntity = (entity: Entity, layerTitle: string, x: number, y: number) => {
console.log('add called with', x, y)
if (entities.find(v=>v===entity)) return
console.log('add somethin', entity)
entities.push(entity)
let layer = layers.find(v=>v.title===layerTitle)
if (!layer) layer = playLayer
@ -280,23 +294,26 @@ export function GameState(ctx: ContextI): StateI {
defaultLong = entity.def.animal.scent
defaultShort = entity.def.animal.sight
}
// Create a short sensor (vision).
let senseFixture = body.createFixture({
shape: planck.Circle(planck.Vec2(entity.x, entity.y), defaultShort),
isSensor: true,
})
senseFixture.setUserData(new Sensor(entity, senseFixture, 'short'))
// Create a long sensor (scent).
let longSenseFixture = body.createFixture({
shape: planck.Circle(planck.Vec2(entity.x, entity.y), defaultLong),
isSensor: true,
})
longSenseFixture.setUserData(new Sensor(entity, longSenseFixture, 'long'))
if (entity instanceof AnimalEntity && !entity.isPlayer) {
// Create a short sensor (vision).
let senseFixture = body.createFixture({
shape: planck.Circle(planck.Vec2(entity.x, entity.y), defaultShort),
isSensor: true,
filterGroupIndex: -8
})
senseFixture.setUserData(new Sensor(entity, senseFixture, 'short'))
// Create a long sensor (scent).
let longSenseFixture = body.createFixture({
shape: planck.Circle(planck.Vec2(entity.x, entity.y), defaultLong),
isSensor: true,
filterGroupIndex: -8
})
longSenseFixture.setUserData(new Sensor(entity, longSenseFixture, 'long'))
}
}
}
entity.x = x
entity.y = y
console.log('set entity xy', entity.x, entity.y)
}
let removeEntity = (entity: Entity) => {
entities = entities.filter(v=>v!==entity)
@ -310,14 +327,23 @@ export function GameState(ctx: ContextI): StateI {
// Zonage
let addZone = (zone: Zone) => {
if (zones.find(v=>v===zone)) return
let shape = planck.Polygon(zone.points.map(v=>planck.Vec2(v[0],v[1])))
zone.fixture = worldBody.createFixture({
shape: shape,
})
if (zone.type === 'fluid') {
zone.fixture.setSensor(true)
// We're using triggers for spawning due to laziness.
if (zone.type === 'trigger') {
if (!spawnZones[zone.event]) {
spawnZones[zone.event] = []
}
spawnZones[zone.event].push(zone)
} else {
let shape = planck.Polygon(zone.points.map(v=>planck.Vec2(v[0],v[1])))
zone.fixture = worldBody.createFixture({
shape: shape,
})
if (zone.type === 'fluid') {
zone.fixture.setSensor(true)
}
zone.fixture.setUserData(zone)
}
zone.fixture.setUserData(zone)
zones.push(zone)
}
let removeZone = (zone: Zone) => {
zones = zones.filter(v=>v!==zone)
@ -325,6 +351,12 @@ export function GameState(ctx: ContextI): StateI {
worldBody.destroyFixture(zone.fixture)
}
}
let getSpawnZone = (name: string): Zone|undefined => {
if (!spawnZones[name] || !spawnZones[name].length) {
return undefined
}
return spawnZones[name][Math.floor(Math.random()*spawnZones[name].length)]
}
let worldContext: WorldContext = {
addEntity,