import { EntityData } from '../model/data/entity'; import { loadEntity } from '../view/entity'; import { Renderer } from '../view/renderer/renderer'; import { SpriteView } from '../view/sprite'; import { Collidable } from '../math/collision'; import { getJSON } from '../util'; import { CollidableGroup, mkCollision } from './collision'; import { vec2 } from 'gl-matrix'; export class EntityContext implements CollidableGroup { public static async load(renderer: Renderer, name: string): Promise { const entity = new EntityData(await getJSON(`resources/entity/${name}.json`)); return new EntityContext( renderer, name, await loadEntity(renderer, entity), mkCollision(entity.collision), ); } public readonly pos: vec2 = vec2.create(); private constructor( private readonly renderer: Renderer, private readonly name: string, private readonly view: SpriteView, private readonly collision: Collidable[], ) {} public render() { this.renderer.setTranslation(this.pos); this.view.render(); } public getTranslation(): vec2 { return this.pos; } public getCollidables(): Collidable[] { return this.collision; } public interact() { alert(`You've interacted with ${this.name}!`); } }