Allow adding entity to specific layers
This commit is contained in:
parent
08d0232e37
commit
25f44c4dd9
|
@ -1,6 +1,6 @@
|
||||||
import { Entity } from "./Entity"
|
import { Entity } from "./Entity"
|
||||||
|
|
||||||
export interface WorldContext {
|
export interface WorldContext {
|
||||||
addEntity(entity: Entity, x: number, y: number): void
|
addEntity(entity: Entity, layerTitle: string, x: number, y: number): void
|
||||||
removeEntity(entity: Entity): void
|
removeEntity(entity: Entity): void
|
||||||
}
|
}
|
|
@ -35,8 +35,8 @@ export function GameState(ctx: ContextI): StateI {
|
||||||
//disableSound()
|
//disableSound()
|
||||||
let isNight = false
|
let isNight = false
|
||||||
let modeTimer = 0
|
let modeTimer = 0
|
||||||
let nightTime = 10 * 1000
|
let nightTime = 30 * 1000
|
||||||
let dayTime = 10 * 1000
|
let dayTime = 30 * 1000
|
||||||
let lastTime: number = performance.now()
|
let lastTime: number = performance.now()
|
||||||
|
|
||||||
let world: planck.World = planck.World({
|
let world: planck.World = planck.World({
|
||||||
|
@ -146,7 +146,7 @@ export function GameState(ctx: ContextI): StateI {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add bogus entity
|
// Add bogus entity
|
||||||
addEntity(new PlayerEntity('deer.animal.stand.west.0'), 300, 100)
|
addEntity(new PlayerEntity('deer.animal.stand.west.0'), 'objects', 300, 100)
|
||||||
|
|
||||||
ctx.app.stage.addChild(rootContainer)
|
ctx.app.stage.addChild(rootContainer)
|
||||||
}
|
}
|
||||||
|
@ -189,6 +189,8 @@ export function GameState(ctx: ContextI): StateI {
|
||||||
// FIXME: This isn't the right place for this.
|
// FIXME: This isn't the right place for this.
|
||||||
rootContainer.position.set(Math.round(ctx.app.renderer.width/2), Math.round(ctx.app.renderer.height/2))
|
rootContainer.position.set(Math.round(ctx.app.renderer.width/2), Math.round(ctx.app.renderer.height/2))
|
||||||
rootContainer.pivot.set(Math.max(rootContainer.width/6, Math.min(entity.x, rootContainer.width/3)), Math.max(rootContainer.height/6, Math.min(entity.y, rootContainer.height/3)))
|
rootContainer.pivot.set(Math.max(rootContainer.width/6, Math.min(entity.x, rootContainer.width/3)), Math.max(rootContainer.height/6, Math.min(entity.y, rootContainer.height/3)))
|
||||||
|
} else {
|
||||||
|
//console.log('hmm, we do be tickin', entity, entity.x, entity.y)
|
||||||
}
|
}
|
||||||
entity.update(realDelta, worldContext)
|
entity.update(realDelta, worldContext)
|
||||||
// I guess...
|
// I guess...
|
||||||
|
@ -198,11 +200,14 @@ export function GameState(ctx: ContextI): StateI {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let addEntity = (entity: Entity, x: number, y: number) => {
|
let addEntity = (entity: Entity, layerTitle: string, x: number, y: number) => {
|
||||||
|
console.log('add called with', x, y)
|
||||||
if (entities.find(v=>v===entity)) return
|
if (entities.find(v=>v===entity)) return
|
||||||
console.log('add somethin', entity)
|
console.log('add somethin', entity)
|
||||||
entities.push(entity)
|
entities.push(entity)
|
||||||
playLayer.container.addChild(entity.sprite.container)
|
let layer = layers.find(v=>v.title===layerTitle)
|
||||||
|
if (!layer) layer = playLayer
|
||||||
|
layer.container.addChild(entity.sprite.container)
|
||||||
// I guess this is a fair enough place to create physics and add it to the entity.
|
// I guess this is a fair enough place to create physics and add it to the entity.
|
||||||
let spriteShape = entity.sprite.getBodyShape()
|
let spriteShape = entity.sprite.getBodyShape()
|
||||||
if (spriteShape) {
|
if (spriteShape) {
|
||||||
|
@ -230,10 +235,11 @@ export function GameState(ctx: ContextI): StateI {
|
||||||
}
|
}
|
||||||
entity.x = x
|
entity.x = x
|
||||||
entity.y = y
|
entity.y = y
|
||||||
|
console.log('set entity xy', entity.x, entity.y)
|
||||||
}
|
}
|
||||||
let removeEntity = (entity: Entity) => {
|
let removeEntity = (entity: Entity) => {
|
||||||
entities = entities.filter(v=>v!==entity)
|
entities = entities.filter(v=>v!==entity)
|
||||||
playLayer.container.removeChild(entity.sprite.container)
|
entity.sprite.container.parent.removeChild(entity.sprite.container)
|
||||||
if (entity.body) {
|
if (entity.body) {
|
||||||
world.destroyBody(entity.body)
|
world.destroyBody(entity.body)
|
||||||
entity.body = undefined
|
entity.body = undefined
|
||||||
|
|
Loading…
Reference in New Issue
Block a user