2020-02-19 22:07:52 +00:00
|
|
|
import { Entity, ComponentConstructor, Component, World } from "ecsy"
|
|
|
|
|
import { Loader, interaction } from "pixi.js"
|
|
|
|
|
import { Human } from "./Components/human"
|
|
|
|
|
import { Name } from "./Components/name"
|
|
|
|
|
import { Appearance } from "./Components/appearance"
|
|
|
|
|
import { InCabin } from "./Components/inCabin"
|
|
|
|
|
import { OrderZ } from "./Components/rendering/orderZ"
|
|
|
|
|
import { WalkRandomly } from "./Components/walkRandomly"
|
|
|
|
|
import { Position } from "./Components/position"
|
2020-02-20 20:12:55 +00:00
|
|
|
import { roomBounds, FirstNames, LastNames } from "./constants"
|
2020-02-19 22:07:52 +00:00
|
|
|
import { Clickable } from "./Components/clickable"
|
2020-02-20 20:12:55 +00:00
|
|
|
import globals from "./globals"
|
2020-02-06 10:57:43 +00:00
|
|
|
|
|
|
|
|
|
2020-02-17 11:56:41 +00:00
|
|
|
export function addOrSetComponent(entity: Entity, Component: ComponentConstructor<Component>, values: any) {
|
2020-02-06 10:57:43 +00:00
|
|
|
if(entity.hasComponent(Component)){
|
|
|
|
|
//component exists, copy values into it
|
|
|
|
|
let component: any = entity.getMutableComponent(Component)
|
|
|
|
|
if(component.copy){
|
|
|
|
|
component.copy(values)
|
|
|
|
|
} else {
|
2020-02-19 22:07:52 +00:00
|
|
|
for (let name in values) {
|
2020-02-17 11:56:41 +00:00
|
|
|
component[name]= values[name]
|
2020-02-06 10:57:43 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
//component doesn't exist, add new one
|
|
|
|
|
entity.addComponent(Component, values)
|
|
|
|
|
}
|
2020-02-17 11:56:41 +00:00
|
|
|
}
|
|
|
|
|
|
2020-02-19 22:07:52 +00:00
|
|
|
export function randomArrayValue(array: any[]):any{
|
2020-02-20 20:12:55 +00:00
|
|
|
let randomIndex = Math.floor(Math.random() * array.length)
|
2020-02-19 22:07:52 +00:00
|
|
|
return array[randomIndex]
|
|
|
|
|
}
|
2020-02-17 11:56:41 +00:00
|
|
|
|
2020-02-19 22:07:52 +00:00
|
|
|
export function createRandomHuman(world: World){
|
|
|
|
|
let resources = Loader.shared.resources;
|
|
|
|
|
let entity = world.createEntity()
|
|
|
|
|
entity
|
|
|
|
|
.addComponent(Human)
|
2020-02-20 20:12:55 +00:00
|
|
|
.addComponent(Name, <Name>{first: randomArrayValue(FirstNames), last:randomArrayValue(LastNames)})
|
2020-02-19 22:07:52 +00:00
|
|
|
.addComponent(Appearance, <Appearance>{idleTexture: resources["Human"].texture}) //Todo: generate appearance from body traits instead?
|
|
|
|
|
.addComponent(InCabin)
|
|
|
|
|
.addComponent(Position, <Position>{value: roomBounds.randomPoint()})
|
|
|
|
|
.addComponent(OrderZ)
|
|
|
|
|
.addComponent(WalkRandomly)
|
|
|
|
|
//todo: change this into selecing the human instead of killing it...
|
2020-02-20 20:12:55 +00:00
|
|
|
.addComponent(Clickable, { actions: { "click": (event: interaction.InteractionEvent) => {globals.selected.set(entity)} }})
|
2020-02-19 22:07:52 +00:00
|
|
|
}
|