Browse Source

La classe game stocke l'index du tube sélectionné

plutôt qu'une référence de l'objet
master
Figg 9 months ago
parent
commit
5692c75824
1 changed files with 14 additions and 7 deletions
  1. 14
    7
      js/model/game.js

+ 14
- 7
js/model/game.js View File

@@ -1,11 +1,11 @@
1 1
 class Game {
2 2
 	#tubes;
3
-	#selectedTube;
3
+	#selectedTubeIndex;
4 4
 	
5 5
 	constructor(nbTubes, tubeLevel)
6 6
 	{
7 7
 		this.#tubes = [];
8
-		this.#selectedTube = null;
8
+		this.#selectedTubeIndex = -1;
9 9
 
10 10
 		for (let i = 0; i < nbTubes; i++)
11 11
 		{
@@ -20,16 +20,23 @@ class Game {
20 20
 		return this.#tubes[tubeIndex];
21 21
 	}
22 22
 	
23
+	selectedTubeIndex()
24
+	{
25
+		return this.#selectedTubeIndex;
26
+	}
27
+
23 28
 	selectedTube()
24 29
 	{
25
-		return this.#selectedTube;
30
+		if (this.#selectedTubeIndex == -1) return null;
31
+		
32
+		return this.#tubes[this.#selectedTubeIndex];
26 33
 	}
27 34
 	
28 35
 	selectTubeAt(index)
29 36
 	{
30 37
 		if (this.isInRange(index))
31 38
 		{
32
-			this.#selectedTube = this.#tubes[index];
39
+			this.#selectedTubeIndex = index;
33 40
 		}
34 41
 	}
35 42
 	
@@ -39,8 +46,8 @@ class Game {
39 46
 		
40 47
 		if (tubeIndex != -1)
41 48
 		{
42
-			let removed = this.#tubes.splice(tubeIndex, 1)[0];
43
-			if (removed = this.#selectedTube) this.unselectTube();
49
+			this.#tubes.splice(tubeIndex, 1)[0];
50
+			if (tubeIndex == this.#selectedTubeIndex) this.unselectTube();
44 51
 		}
45 52
 	}
46 53
 	
@@ -52,7 +59,7 @@ class Game {
52 59
 	
53 60
 	unselectTube()
54 61
 	{
55
-		this.#selectedTube = null;
62
+		this.#selectedTubeIndex = -1;
56 63
 	}
57 64
 	
58 65
 	tubesNumber()

Loading…
Cancel
Save