summaryrefslogtreecommitdiffstats
path: root/src/renderer/runtime/controller/entitycontext.ts
blob: 434bf9bf9bb13f898da518c2c20c8e46eaff4713 (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
44
45
46
47
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) {
		this.renderer.setTranslation(this.pos);
		this.view.renderByTime(time);
	}

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

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

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