|
@@ -1,95 +1,64 @@
|
1
|
1
|
class Drawer
|
2
|
2
|
{
|
3
|
|
- #canvas;
|
|
3
|
+ #tubeDrawObjects;
|
|
4
|
+ #scale;
|
4
|
5
|
|
5
|
|
- constructor(_padding, _scale)
|
|
6
|
+ constructor(_padding, _scale, _game)
|
6
|
7
|
{
|
7
|
|
- this.padding = _padding;
|
8
|
|
- this.scale = _scale;
|
|
8
|
+ this.#tubeDrawObjects = []
|
|
9
|
+ this.#scale = _scale;
|
9
|
10
|
|
10
|
|
- this.#canvas = createCanvas(0, 0);
|
|
11
|
+ this.#initializeTubeDrawObjects(_game, _padding);
|
|
12
|
+ this.#computeCanvasSize(_padding);
|
11
|
13
|
}
|
12
|
|
-
|
13
|
|
- computeGameParams(_tubesNumbers, _tubeLevels)
|
|
14
|
+
|
|
15
|
+ applySelectedEffect(index)
|
14
|
16
|
{
|
15
|
|
- let canvasWidth = _tubesNumbers * (this.scale + this.padding) + this.padding;
|
16
|
|
- let canvasHeight = 2 * this.padding + (_tubeLevels+1) * this.scale;
|
17
|
|
-
|
18
|
|
- resizeCanvas(canvasWidth, canvasHeight);
|
|
17
|
+ this.#tubeDrawObjects[index].y -= this.#scale;
|
19
|
18
|
}
|
20
|
|
-
|
21
|
|
- draw(game)
|
22
|
|
- {
|
23
|
|
- clear();
|
24
|
19
|
|
25
|
|
- this.drawTubes(this.padding, this.padding+this.scale, game);
|
|
20
|
+ removeSelectedEffect(index)
|
|
21
|
+ {
|
|
22
|
+ this.#tubeDrawObjects[index].y += this.#scale;
|
26
|
23
|
}
|
27
|
|
-
|
28
|
|
- drawTubes(x, y, game)
|
|
24
|
+
|
|
25
|
+ #initializeTubeDrawObjects(_game, _padding)
|
29
|
26
|
{
|
30
|
|
- let tubeX = x;
|
31
|
|
- let tubeY = y;
|
32
|
|
- let selectionOffset = 0;
|
33
|
|
-
|
34
|
|
- for (let i = 0; i < game.tubesNumber(); i++)
|
|
27
|
+ let _tubeX = _padding;
|
|
28
|
+ let _tubeY = _padding + this.#scale;
|
|
29
|
+
|
|
30
|
+ for (let tbIndex = 0; tbIndex < _game.tubesNumber(); tbIndex++)
|
35
|
31
|
{
|
36
|
|
- let tube = game.tubeAt(i);
|
37
|
|
- selectionOffset = (tube == game.selectedTube() ? this.scale : 0);
|
38
|
|
-
|
39
|
|
- this.drawTube(tubeX, tubeY - selectionOffset, tube);
|
40
|
|
- tubeX += this.padding + this.scale;
|
|
32
|
+ let tubeDrawObject = new TubeDrawObject(_tubeX, _tubeY, _game.tubeAt(tbIndex), this.#scale);
|
|
33
|
+ this.#tubeDrawObjects.push(tubeDrawObject);
|
|
34
|
+
|
|
35
|
+ _tubeX += this.#scale + _padding;
|
41
|
36
|
}
|
42
|
37
|
}
|
43
|
38
|
|
44
|
|
- drawTube(x, y, tube)
|
|
39
|
+ #computeCanvasSize(_padding)
|
45
|
40
|
{
|
46
|
|
- let tubeLevels = tube.height();
|
47
|
|
-
|
48
|
|
- y += (tubeLevels-1) * this.scale;
|
49
|
|
- strokeWeight(2);
|
50
|
|
-
|
51
|
|
- for(let i = 0; i < tubeLevels; i++)
|
52
|
|
- {
|
53
|
|
- let layerY = y - i * this.scale;
|
|
41
|
+ let w = this.#tubeDrawObjects.reduce((res, value) => res + value.width + _padding, _padding);
|
|
42
|
+ let h = this.#tubeDrawObjects[0].height + 2 * _padding + this.#scale;
|
54
|
43
|
|
55
|
|
- let color = tube.getColorAtLevel(i);
|
56
|
|
- color == null ? noFill() : fill(color);
|
57
|
|
-
|
58
|
|
- if (i > 0)
|
59
|
|
- {
|
60
|
|
- noStroke();
|
61
|
|
- rect(x, layerY, this.scale);
|
62
|
|
- stroke('black');
|
63
|
|
- line(x, layerY, x, layerY + this.scale);
|
64
|
|
- line(x + this.scale, layerY, x + this.scale, layerY + this.scale);
|
65
|
|
- }
|
66
|
|
- else
|
67
|
|
- {
|
68
|
|
- noStroke();
|
69
|
|
- rect(x, layerY, this.scale, this.scale/2);
|
70
|
|
- stroke('black');
|
71
|
|
- line(x, layerY, x, layerY + this.scale/2);
|
72
|
|
- line(x + this.scale, layerY, x + this.scale, layerY + this.scale/2);
|
73
|
|
-
|
74
|
|
- arc(x + this.scale/2, layerY + this.scale/2, this.scale, this.scale, 0, PI);
|
75
|
|
- }
|
76
|
|
- }
|
|
44
|
+ createCanvas(w, h);
|
77
|
45
|
}
|
78
|
46
|
|
79
|
|
- getTubeIdAt(x, y)
|
|
47
|
+ draw()
|
80
|
48
|
{
|
81
|
|
- if (!this.isInboundsCanvas(x,y)) return -1;
|
82
|
|
-
|
83
|
|
- let idWithPadding = (x - this.padding) / (this.scale + this.padding);
|
84
|
|
- return idWithPadding % 1 > 1 - (this.padding / (this.scale + this.padding)) ? -1 : int(idWithPadding);
|
|
49
|
+ clear();
|
|
50
|
+ background('orange');
|
|
51
|
+
|
|
52
|
+ this.#tubeDrawObjects.forEach(obj => obj.draw());
|
85
|
53
|
}
|
86
|
54
|
|
87
|
|
- isInboundsCanvas(x, y)
|
|
55
|
+ getTubeIdAt(x, y)
|
88
|
56
|
{
|
89
|
|
- return x >= this.padding
|
90
|
|
- && y >= this.padding
|
91
|
|
- && x < this.#canvas.width - this.padding
|
92
|
|
- && y < this.#canvas.height - this.padding;
|
93
|
|
- }
|
|
57
|
+ for (let i = 0; i < this.#tubeDrawObjects.length; i++)
|
|
58
|
+ {
|
|
59
|
+ if (this.#tubeDrawObjects[i].inbounds(x, y)) return i;
|
|
60
|
+ }
|
94
|
61
|
|
|
62
|
+ return -1;
|
|
63
|
+ }
|
95
|
64
|
}
|