|
@@ -12,13 +12,12 @@ class Controller
|
12
|
12
|
initializeGame() {
|
13
|
13
|
let params = this.gatherMenuValues();
|
14
|
14
|
|
15
|
|
- this.game = new Game(params.TUBESNUMBERS, params.TUBESLEVELS);
|
16
|
|
- this.drawer = new Drawer(params.PADDING, params.SCALE);
|
|
15
|
+ this.#game = new Game(params.TUBESNUMBERS, params.TUBESLEVELS);
|
|
16
|
+ this.#drawer = new Drawer(params.PADDING, params.SCALE);
|
17
|
17
|
|
18
|
|
- this.drawer.computeGameParams(params.TUBESNUMBERS, params.TUBESLEVELS);
|
19
|
|
- this.width = 800
|
20
|
|
- this.height = 600
|
21
|
|
- this.ui = new UI(this.width, this.height);
|
|
18
|
+ this.#drawer.computeGameParams(params.TUBESNUMBERS, params.TUBESLEVELS);
|
|
19
|
+
|
|
20
|
+ this.ui = new UI(800, 600);
|
22
|
21
|
this.lastTime = 0;
|
23
|
22
|
|
24
|
23
|
this.initializeColorTubes(params.TUBESLEVELS - 1);
|
|
@@ -43,7 +42,7 @@ class Controller
|
43
|
42
|
|
44
|
43
|
initializeColorTubes(LayersPerTube)
|
45
|
44
|
{
|
46
|
|
- let nbTubes = this.game.tubesNumber() - 1;
|
|
45
|
+ let nbTubes = this.#game.tubesNumber() - 1;
|
47
|
46
|
let allColors = [];
|
48
|
47
|
|
49
|
48
|
for (let i = 0; i < nbTubes; i++)
|
|
@@ -59,7 +58,7 @@ class Controller
|
59
|
58
|
let randomIndex = Math.floor(Math.random() * (index + 1));
|
60
|
59
|
let tubeIndex = Math.floor(index / LayersPerTube);
|
61
|
60
|
|
62
|
|
- this.game.tubeAt(tubeIndex).addColorLayer(allColors[randomIndex]);
|
|
61
|
+ this.#game.tubeAt(tubeIndex).addColorLayer(allColors[randomIndex]);
|
63
|
62
|
|
64
|
63
|
allColors[randomIndex] = allColors[index];
|
65
|
64
|
}
|
|
@@ -67,18 +66,18 @@ class Controller
|
67
|
66
|
|
68
|
67
|
clickTube(tubeIndex)
|
69
|
68
|
{
|
70
|
|
- let _clickedTube = this.game.tubeAt(tubeIndex);
|
|
69
|
+ let _clickedTube = this.#game.tubeAt(tubeIndex);
|
71
|
70
|
if (_clickedTube == null) return;
|
72
|
71
|
|
73
|
|
- if (_clickedTube == this.game.selectedTube())
|
74
|
|
- this.game.unselectTube();
|
75
|
|
- else if (this.canPourInto(this.game.selectedTube(), _clickedTube))
|
|
72
|
+ if (_clickedTube == this.#game.selectedTube())
|
|
73
|
+ this.#game.unselectTube();
|
|
74
|
+ else if (this.canPourInto(this.#game.selectedTube(), _clickedTube))
|
76
|
75
|
{
|
77
|
|
- this.pourColorInto(this.game.selectedTube(), _clickedTube);
|
78
|
|
- this.game.unselectTube();
|
|
76
|
+ this.pourColorInto(this.#game.selectedTube(), _clickedTube);
|
|
77
|
+ this.#game.unselectTube();
|
79
|
78
|
}
|
80
|
79
|
else if (!_clickedTube.isEmpty())
|
81
|
|
- this.game.selectTubeAt(tubeIndex);
|
|
80
|
+ this.#game.selectTubeAt(tubeIndex);
|
82
|
81
|
}
|
83
|
82
|
|
84
|
83
|
canPourInto(sourceTube, targetTube)
|
|
@@ -86,7 +85,7 @@ class Controller
|
86
|
85
|
return sourceTube != null && targetTube != null
|
87
|
86
|
&& sourceTube != targetTube
|
88
|
87
|
&& (targetTube.isEmpty() || sourceTube.peekTopColor() == targetTube.peekTopColor())
|
89
|
|
- && !targetTube.isFull();
|
|
88
|
+ && !targetTube.isFull() && !targetTube.isComplete();
|
90
|
89
|
}
|
91
|
90
|
|
92
|
91
|
pourColorInto(sourceTube, targetTube)
|
|
@@ -104,22 +103,22 @@ class Controller
|
104
|
103
|
{
|
105
|
104
|
if (tube.isComplete())
|
106
|
105
|
{
|
107
|
|
- this.game.removeTube(tube);
|
|
106
|
+ //this.#game.removeTube(tube);
|
108
|
107
|
this.checkGameCompletion();
|
109
|
108
|
}
|
110
|
109
|
}
|
111
|
110
|
|
112
|
111
|
checkGameCompletion()
|
113
|
112
|
{
|
114
|
|
- if (this.game.tubesNumber() == 1 && this.game.tubeAt(0).isEmpty())
|
|
113
|
+ if (this.#game.tubesNumber() == 1 && this.#game.tubeAt(0).isEmpty())
|
115
|
114
|
{
|
116
|
|
- this.game.clean();
|
|
115
|
+ this.#game.clean();
|
117
|
116
|
}
|
118
|
117
|
}
|
119
|
118
|
|
120
|
119
|
mousePressed(e)
|
121
|
120
|
{
|
122
|
|
- let clickedTubeId = this.drawer.getTubeIdAt(mouseX, mouseY);
|
|
121
|
+ let clickedTubeId = this.#drawer.getTubeIdAt(mouseX, mouseY);
|
123
|
122
|
this.clickTube(clickedTubeId);
|
124
|
123
|
|
125
|
124
|
this.needUpdate = true;
|
|
@@ -127,21 +126,26 @@ class Controller
|
127
|
126
|
|
128
|
127
|
runLoop()
|
129
|
128
|
{
|
130
|
|
- const timeDelta = millis() - lastTime;
|
|
129
|
+ const timeDelta = millis() - this.lastTime;
|
131
|
130
|
// peut-être pas nécessaire de limiter le framerate
|
132
|
131
|
if (timeDelta < 50)
|
133
|
132
|
return;
|
134
|
133
|
this.lastTime = millis();
|
135
|
134
|
|
136
|
135
|
const steps = [
|
137
|
|
- (this.needUpdate || this.game.isCompleted) && drawGame,
|
138
|
|
- this.game.isCompleted && drawUI
|
|
136
|
+ (this.needUpdate || this.#game.isCompleted) && this.drawGame,
|
|
137
|
+ this.#game.isCompleted && this.drawUI
|
139
|
138
|
]
|
140
|
139
|
|
141
|
|
- steps.forEach(step => step && step());
|
|
140
|
+ steps.forEach(step => step && step.call(this));
|
|
141
|
+ }
|
|
142
|
+
|
|
143
|
+ drawGame()
|
|
144
|
+ {
|
|
145
|
+ this.#drawer.draw(this.#game);
|
142
|
146
|
}
|
143
|
147
|
|
144
|
|
- function drawUI()
|
|
148
|
+ drawUI()
|
145
|
149
|
{
|
146
|
150
|
this.ui.drawWinView();
|
147
|
151
|
}
|