Browse Source

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

plutôt qu'une référence de l'objet
master
Figg 10 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
 class Game {
1
 class Game {
2
 	#tubes;
2
 	#tubes;
3
-	#selectedTube;
3
+	#selectedTubeIndex;
4
 	
4
 	
5
 	constructor(nbTubes, tubeLevel)
5
 	constructor(nbTubes, tubeLevel)
6
 	{
6
 	{
7
 		this.#tubes = [];
7
 		this.#tubes = [];
8
-		this.#selectedTube = null;
8
+		this.#selectedTubeIndex = -1;
9
 
9
 
10
 		for (let i = 0; i < nbTubes; i++)
10
 		for (let i = 0; i < nbTubes; i++)
11
 		{
11
 		{
20
 		return this.#tubes[tubeIndex];
20
 		return this.#tubes[tubeIndex];
21
 	}
21
 	}
22
 	
22
 	
23
+	selectedTubeIndex()
24
+	{
25
+		return this.#selectedTubeIndex;
26
+	}
27
+
23
 	selectedTube()
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
 	selectTubeAt(index)
35
 	selectTubeAt(index)
29
 	{
36
 	{
30
 		if (this.isInRange(index))
37
 		if (this.isInRange(index))
31
 		{
38
 		{
32
-			this.#selectedTube = this.#tubes[index];
39
+			this.#selectedTubeIndex = index;
33
 		}
40
 		}
34
 	}
41
 	}
35
 	
42
 	
39
 		
46
 		
40
 		if (tubeIndex != -1)
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
 	
59
 	
53
 	unselectTube()
60
 	unselectTube()
54
 	{
61
 	{
55
-		this.#selectedTube = null;
62
+		this.#selectedTubeIndex = -1;
56
 	}
63
 	}
57
 	
64
 	
58
 	tubesNumber()
65
 	tubesNumber()

Loading…
Cancel
Save