Skip to content
Snippets Groups Projects
Commit fa4d695a authored by Paolo.Brasolin's avatar Paolo.Brasolin
Browse files

reafactor: #fe reintroduce Foe as coordinator

parent 642b40ad
No related branches found
No related tags found
No related merge requests found
import "phaser"; import "phaser";
import Critter from "./critter";
import Spear from "./spear"; import Spear from "./spear";
import backend from "./backend"; import backend from "./backend";
// TODO: write interfaces // TODO: write interfaces
import levenshtein from "damerau-levenshtein"; import levenshtein from "damerau-levenshtein";
import Clue from "./clue";
import * as Types from "../../../backend/src/types"; import * as Types from "../../../backend/src/types";
import Foe from "./foe";
export default class FightScene extends Phaser.Scene { export default class FightScene extends Phaser.Scene {
foes: Array<Critter>; foes: Array<Foe>;
ground: Phaser.Types.Physics.Arcade.ImageWithStaticBody; ground: Phaser.Types.Physics.Arcade.ImageWithStaticBody;
player: Phaser.Types.Physics.Arcade.SpriteWithDynamicBody; player: Phaser.Types.Physics.Arcade.SpriteWithDynamicBody;
cluesGroup: Phaser.Physics.Arcade.Group; cluesGroup: Phaser.Physics.Arcade.Group;
...@@ -197,18 +196,18 @@ export default class FightScene extends Phaser.Scene { ...@@ -197,18 +196,18 @@ export default class FightScene extends Phaser.Scene {
}); });
} }
shootSpear(enemy: Critter, hit: boolean) { shootSpear(foe: Foe | null, hit: boolean) {
const scene = this; const scene = this;
if (!hit) { if (foe === null || !hit) {
this.showMissMessage(); this.showMissMessage();
new Spear(this, this.player, undefined);
} else { } else {
this.showHitMessage(); this.showHitMessage();
// TODO: ew. // TODO: ew.
enemy.clue.delete(); foe.clue.delete();
scene.foes.splice(scene.foes.indexOf(enemy), 1); // FIXME // scene.foes.splice(scene.foes.indexOf(foe), 1); // FIXME
new Spear(this, this.player, foe.critter);
} }
new Spear(this, this.player, hit ? enemy : undefined);
} }
initCluesGroup() { initCluesGroup() {
...@@ -226,6 +225,25 @@ export default class FightScene extends Phaser.Scene { ...@@ -226,6 +225,25 @@ export default class FightScene extends Phaser.Scene {
}); });
this.physics.add.collider(this.cluesGroup, this.cluesGroup); this.physics.add.collider(this.cluesGroup, this.cluesGroup);
} }
findMatchingFoe(transcription: string) {
let result: { score: number; match: Foe | null } = {
score: -1,
match: null,
};
if (this.foes.length < 1) return result;
this.foes.forEach((foe) => {
const similarity = levenshtein(
transcription.toLowerCase(),
foe.beWord.ocr_transcript.toLowerCase(),
).similarity;
if (similarity < result.score) return;
result = { score: similarity, match: foe };
});
// match ??= scene.foes[0]; // TODO: remove this
// console.log(similarity, match.beWord.ocr_transcript);
return result;
}
} }
// TODO: remove any // TODO: remove any
...@@ -307,50 +325,22 @@ function initAndBindGuessPreview(scene: FightScene) { ...@@ -307,50 +325,22 @@ function initAndBindGuessPreview(scene: FightScene) {
} }
function submitTranscription(transcription: string, scene: FightScene) { function submitTranscription(transcription: string, scene: FightScene) {
if (scene.foes.length < 1) return; const { score, match } = scene.findMatchingFoe(transcription);
scene.shootSpear(match, score >= 0.9);
let similarity = 0;
let match = null;
scene.foes.forEach((critter) => {
const s = levenshtein(
transcription.toLowerCase(),
critter.clue.word.ocr_transcript.toLowerCase(),
).similarity;
if (s < similarity) return;
similarity = s;
match = critter;
});
match ??= scene.foes[0]; // TODO: remove this
console.log(similarity, match.clue.word.ocr_transcript);
// TODO: we can have near misses depending on similarity!
const hit = similarity >= 0.9;
const enemy = match;
scene.shootSpear(enemy, hit);
} }
function gameStart(scene: any) { function gameStart(scene: any) {
spawn(scene); spawn(scene);
// dispatchEnemy(scene);
} }
function spawn(scene: any) { async function spawn(scene: any) {
dispatchEnemy(scene); await dispatchEnemy(scene);
scene.time.now; scene.time.now;
const delay = const delay =
(8 * 1000 * (60 * 1000 - scene.time.now)) / 60 / 1000 + 2 * 1000; (8 * 1000 * (60 * 1000 - scene.time.now)) / 60 / 1000 + 2 * 1000;
setTimeout(() => spawn(scene), Math.max(delay, 2000)); setTimeout(() => spawn(scene), Math.max(delay, 2000));
} }
function dispatchEnemy(scene: any) { async function dispatchEnemy(scene: any) {
backend.getWord().then(function (response) { await new Foe(scene).initialize();
const clue = new Clue(scene, response.data);
const critter = new Critter(scene, clue);
// TODO: clue
scene.foes.push(critter);
});
} }
import "phaser";
import Clue from "./clue";
import Critter from "./critter";
import FightScene from "./fight_scene";
import backend from "./backend";
import * as Types from "../../../backend/src/types";
class Foe {
beWord: Types.Word;
beClue: Types.Clue;
scene: FightScene;
critter: Critter;
clue: Clue;
constructor(scene: FightScene) {
this.scene = scene;
}
async initialize() {
this.beWord = (await backend.getWord()).data;
this.clue = new Clue(this.scene, this.beWord);
this.critter = new Critter(this.scene, this.clue);
this.scene.foes.push(this);
}
}
export default Foe;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment