summaryrefslogtreecommitdiffstats
path: root/src/renderer/runtime/controller
diff options
context:
space:
mode:
Diffstat (limited to 'src/renderer/runtime/controller')
-rw-r--r--src/renderer/runtime/controller/collision.ts53
-rw-r--r--src/renderer/runtime/controller/entitycontext.ts10
-rw-r--r--src/renderer/runtime/controller/gamecontext.ts53
3 files changed, 48 insertions, 68 deletions
diff --git a/src/renderer/runtime/controller/collision.ts b/src/renderer/runtime/controller/collision.ts
index a44ffa4..3c12945 100644
--- a/src/renderer/runtime/controller/collision.ts
+++ b/src/renderer/runtime/controller/collision.ts
@@ -12,26 +12,25 @@ export function mkCollision(collision: Collision[]): Collidable[] {
for (const c of collision) {
switch (c.type) {
- case 'polygon':
- if (!c.vertices.length)
- continue;
-
- let prev = c.vertices[c.vertices.length - 1];
-
- for (const v of c.vertices) {
- ret.push(LineSegment.fromPoints(vec2.clone(prev), vec2.clone(v)));
- prev = v;
- }
-
- for (const v of c.vertices) {
- ret.push(new Point(vec2.clone(v)));
- prev = v;
- }
- break;
-
- case 'circle':
- ret.push(new Circle(vec2.clone(c.center), c.radius));
- break;
+ case 'polygon':
+ if (!c.vertices.length) continue;
+
+ let prev = c.vertices[c.vertices.length - 1];
+
+ for (const v of c.vertices) {
+ ret.push(LineSegment.fromPoints(vec2.clone(prev), vec2.clone(v)));
+ prev = v;
+ }
+
+ for (const v of c.vertices) {
+ ret.push(new Point(vec2.clone(v)));
+ prev = v;
+ }
+ break;
+
+ case 'circle':
+ ret.push(new Circle(vec2.clone(c.center), c.radius));
+ break;
}
}
@@ -39,24 +38,20 @@ export function mkCollision(collision: Collision[]): Collidable[] {
}
export interface CollidableGroup {
- getTranslation(): vec2|null;
+ getTranslation(): vec2 | null;
getCollidables(): Collidable[];
}
export function collide(collision: CollidableGroup, out: vec2, move: Movement, radius: number): boolean {
const t = collision.getTranslation();
- if (t)
- move = move.translate(vec2.negate(vec2.create(), t));
+ if (t) move = move.translate(vec2.negate(vec2.create(), t));
for (const c of collision.getCollidables()) {
- if (!c.collide(out, move, radius))
- continue;
+ if (!c.collide(out, move, radius)) continue;
- if (vec2.squaredDistance(move.src, out) >= vec2.squaredDistance(move.src, move.dest))
- continue;
+ if (vec2.squaredDistance(move.src, out) >= vec2.squaredDistance(move.src, move.dest)) continue;
- if (t)
- vec2.add(out, out, t);
+ if (t) vec2.add(out, out, t);
return true;
}
diff --git a/src/renderer/runtime/controller/entitycontext.ts b/src/renderer/runtime/controller/entitycontext.ts
index 434bf9b..33d7b04 100644
--- a/src/renderer/runtime/controller/entitycontext.ts
+++ b/src/renderer/runtime/controller/entitycontext.ts
@@ -9,11 +9,7 @@ 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),
- );
+ return new EntityContext(renderer, name, await EntityView.load(renderer, name));
}
public readonly pos: vec2 = vec2.create();
@@ -28,7 +24,7 @@ export class EntityContext implements CollidableGroup {
this.collision = mkCollision(view.data.collision);
}
- public render(time: number) {
+ public render(time: number): void {
this.renderer.setTranslation(this.pos);
this.view.renderByTime(time);
}
@@ -41,7 +37,7 @@ export class EntityContext implements CollidableGroup {
return this.collision;
}
- public interact() {
+ public interact(): void {
alert(`You've interacted with ${this.name}!`);
}
}
diff --git a/src/renderer/runtime/controller/gamecontext.ts b/src/renderer/runtime/controller/gamecontext.ts
index 9a68e25..1b17314 100644
--- a/src/renderer/runtime/controller/gamecontext.ts
+++ b/src/renderer/runtime/controller/gamecontext.ts
@@ -1,7 +1,7 @@
import { CollidableGroup, collide, mkCollision } from './collision';
import { EntityContext } from './entitycontext';
-import { MapData } from '../model/data/map';
+import { MapData, MapDataInput } from '../model/data/map';
import { ButtonCode, GameInputHandler } from '../view/input/gameinput';
import { MapView } from '../view/map';
@@ -26,17 +26,11 @@ export class GameContext implements CollidableGroup {
vec2.set(player.pos, 7, 6);
vec2.set(entity.pos, 4, 3);
- return new GameContext(
- renderer,
- mapView,
- player,
- [entity],
- mapCollision,
- );
+ return new GameContext(renderer, mapView, player, [entity], mapCollision);
}
private static async loadMap(renderer: Renderer, name: string): Promise<[MapView, Collidable[]]> {
- const map = new MapData(await getJSON(`resources/map/${name}.json`));
+ const map = new MapData((await getJSON(`resources/map/${name}.json`)) as MapDataInput);
return [await MapView.load(renderer, map), mkCollision(map.collision)];
}
@@ -50,7 +44,7 @@ export class GameContext implements CollidableGroup {
private readonly input: GameInputHandler;
private readonly playerDir: vec2 = vec2.fromValues(0, 1);
- private speed: number = 0;
+ private speed = 0;
private readonly collisionRadius = 15 / 32;
private readonly interactLength = 1 / 32;
@@ -65,19 +59,18 @@ export class GameContext implements CollidableGroup {
this.input = new GameInputHandler();
this.input.addListener((input) => {
switch (input.type) {
- case 'button':
- if (input.button === ButtonCode.Action)
- this.interact();
- break;
-
- case 'direction':
- if (vec2.sqrLen(input.direction) > 0) {
- vec2.copy(this.playerDir, input.direction);
- this.speed = this.maxSpeed;
- } else {
- this.speed = 0;
- }
- break;
+ case 'button':
+ if (input.button === ButtonCode.Action) this.interact();
+ break;
+
+ case 'direction':
+ if (vec2.sqrLen(input.direction) > 0) {
+ vec2.copy(this.playerDir, input.direction);
+ this.speed = this.maxSpeed;
+ } else {
+ this.speed = 0;
+ }
+ break;
}
});
@@ -108,8 +101,7 @@ export class GameContext implements CollidableGroup {
private interact(): void {
for (const e of this.entities) {
- if (!this.canInteract(e))
- continue;
+ if (!this.canInteract(e)) continue;
e.interact();
break;
@@ -136,8 +128,7 @@ export class GameContext implements CollidableGroup {
const newDest = vec2.create();
while (this.updateStepCollide(newDest, dest)) {
- if (vec2.equals(newDest, this.player.pos))
- return;
+ if (vec2.equals(newDest, this.player.pos)) return;
vec2.copy(dest, newDest);
}
@@ -153,21 +144,19 @@ export class GameContext implements CollidableGroup {
return;
}
- for (let i = 0; i < diff; i++)
- this.updateStep();
+ for (let i = 0; i < diff; i++) this.updateStep();
}
private render(): void {
this.renderer.setCenter(this.player.pos);
this.renderer.clear();
- for (const r of [this.mapView, ...this.entities, this.player])
- r.render(this.time);
+ for (const r of [this.mapView, ...this.entities, this.player]) r.render(this.time);
}
private async renderLoop(): Promise<void> {
while (true) {
- this.update(await nextAnimationFrame() - this.initTime);
+ this.update((await nextAnimationFrame()) - this.initTime);
this.render();
}
}