From 360a6c9df6b36cdba2ae995bf7c86c198cbded98 Mon Sep 17 00:00:00 2001
From: Paolo Brasolin <paolo.brasolin@eurac.edu>
Date: Thu, 10 Mar 2022 11:29:36 +0100
Subject: [PATCH] feat: #fe clean up clue addition

---
 frontend/src/js/fight_scene.js | 15 +--------------
 frontend/src/js/foe.js         | 19 ++++++++++++++++---
 2 files changed, 17 insertions(+), 17 deletions(-)

diff --git a/frontend/src/js/fight_scene.js b/frontend/src/js/fight_scene.js
index b8f3c8b..d301a88 100644
--- a/frontend/src/js/fight_scene.js
+++ b/frontend/src/js/fight_scene.js
@@ -367,20 +367,7 @@ function gameStart(scene) {
 
 function dispatchEnemy(scene) {
   backend.post("GetImage", {}).then(function (response) {
-    let e = new Foe(scene, response.data);
-
-    scene.textures.addBase64(`WORD-${response.data.id}`, response.data.image);
-    scene.textures.once(
-      "addtexture",
-      function () {
-        e.run((v) => {
-          setTimeout(() => {
-            dispatchEnemy(scene);
-          }, Math.floor(Math.random() * 10000 + 3000));
-        });
-      },
-      scene,
-    );
+    new Foe(scene, response.data);
   });
 }
 
diff --git a/frontend/src/js/foe.js b/frontend/src/js/foe.js
index ea8619f..a45872b 100644
--- a/frontend/src/js/foe.js
+++ b/frontend/src/js/foe.js
@@ -6,9 +6,24 @@ class Foe {
     this.scene = scene;
     this.scene.foes.push(this);
     this.word = word;
+    this.addClue();
+    this.run();
   }
 
-  run(callback) {
+  addClue() {
+    this.scene.textures.addBase64(this.word.id, this.word.image);
+    this.scene.textures.once(
+      "addtexture",
+      this.addClueSprite.bind(this),
+      this.scene,
+    );
+  }
+
+  addClueSprite() {
+    this.scene.add.sprite(400, 300, this.word.id);
+  }
+
+  run() {
     let me = this;
     let randomEnemyType = enemies[Math.floor(Math.random() * 4 + 0)];
 
@@ -19,7 +34,6 @@ class Foe {
       scale = 3;
     }
 
-    let flag = this.scene.add.sprite(400, 300, `WORD-${this.word.id}`);
     let enemy = this.scene.physics.add
       .sprite(-100, this.scene.cameras.main.height - 100, randomEnemyType)
       .setScale(scale)
@@ -60,7 +74,6 @@ class Foe {
           duration: 2000,
           onComplete: function () {
             nemico.destroy();
-            callback(me);
           },
         });
       }, 3000);
-- 
GitLab