summaryrefslogtreecommitdiffstats
path: root/src/renderer/runtime/controller/entitycontext.ts
blob: 33d7b040f04f1c0208dfdd45cc6feb223a2617ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { EntityView } from '../view/entity';
import { Renderer } from '../view/renderer/renderer';

import { Collidable } from '../math/collision';

import { CollidableGroup, mkCollision } from './collision';

import { vec2 } from 'gl-matrix';

export class EntityContext implements CollidableGroup {
	public static async load(renderer: Renderer, name: string): Promise<EntityContext> {
		return new EntityContext(renderer, name, await EntityView.load(renderer, name));
	}

	public readonly pos: vec2 = vec2.create();

	private readonly collision: Collidable[];

	private constructor(
		private readonly renderer: Renderer,
		private readonly name: string,
		private readonly view: EntityView,
	) {
		this.collision = mkCollision(view.data.collision);
	}

	public render(time: number): void {
		this.renderer.setTranslation(this.pos);
		this.view.renderByTime(time);
	}

	public getTranslation(): vec2 {
		return this.pos;
	}

	public getCollidables(): Collidable[] {
		return this.collision;
	}

	public interact(): void {
		alert(`You've interacted with ${this.name}!`);
	}
}