summaryrefslogtreecommitdiffstats
path: root/src/controller/entitycontext.ts
blob: 6932227507dcdd5865dad44afb02eb7b99d47aef (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { EntityAnimation, 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<EntityContext> {
		const entity = new EntityData(await getJSON(`resources/entity/${name}.json`));

		return new EntityContext(
			renderer,
			name,
			await loadEntity(renderer, entity),
			mkCollision(entity.collision),
			entity.animation,
		);
	}

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

	private readonly totalTime: number;

	private constructor(
		private readonly renderer: Renderer,
		private readonly name: string,
		private readonly sprites: SpriteView[],
		private readonly collision: Collidable[],
		private readonly animation?: EntityAnimation,
	) {
		if (animation)
			this.totalTime = animation.sequence.reduce((a, s) => a + s[0], 0);
		else
			this.totalTime = 0;
	}

	public render(time: number) {
		this.renderer.setTranslation(this.pos);
		this.getSprite(time).render();
	}

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

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

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

	private getSprite(time: number): SpriteView {
		time %= this.totalTime;

		if (this.animation) {
			for (const [len, sprite] of this.animation.sequence) {
				time -= len;
				if (time < 0)
					return this.sprites[sprite];
			}
		}

		return this.sprites[0];
	}
}