Bläddra i källkod

Ajout d'un TubeDrawObject pour représenter un tube

affiché sur le canvas.
Modification du code en conséquence
pull/4/head
Figg 7 månader sedan
förälder
incheckning
3aa6c29318
5 ändrade filer med 127 tillägg och 79 borttagningar
  1. 2
    1
      index.html
  2. 21
    7
      js/controller.js
  3. 64
    0
      js/drawer/TubeDrawObject.js
  4. 39
    70
      js/drawer/drawer.js
  5. 1
    1
      js/model/tube.js

+ 2
- 1
index.html Visa fil

@@ -11,7 +11,8 @@
11 11
 	<script src="./js/model/game.js"></script>
12 12
 	<script src="./js/model/historyItem.js"></script>
13 13
 	<script src="./js/ui.js"></script>
14
-	<script src="./js/drawer.js"></script>
14
+	<script src="./js/drawer/TubeDrawObject.js"></script>
15
+	<script src="./js/drawer/drawer.js"></script>
15 16
 	<script src="./js/historyHandler.js"></script>
16 17
 	<script src="./js/controller.js"></script>
17 18
 	<script src="./js/main.js"></script>

+ 21
- 7
js/controller.js Visa fil

@@ -9,14 +9,18 @@ class Controller
9 9
 	{
10 10
 		this.initializeGame();
11 11
 	}
12
+
13
+	drawer()
14
+	{
15
+		return this.#drawer
16
+	}
12 17
 	
13 18
 	initializeGame() {
14 19
 		let params = this.gatherMenuValues();
15 20
 		
16 21
 		this.#game = new Game(params.TUBESNUMBERS, params.TUBESLEVELS);
17
-		this.#drawer = new Drawer(params.PADDING, params.SCALE);
18
-		this.#historyHandler = new HistoryHandler();		
19
-		this.#drawer.computeGameParams(params.TUBESNUMBERS, params.TUBESLEVELS);
22
+		this.#drawer = new Drawer(params.PADDING, params.SCALE, this.#game);
23
+		this.#historyHandler = new HistoryHandler();
20 24
 
21 25
 		this.ui = new UI(800, 600);
22 26
 		this.lastTime = millis();
@@ -68,17 +72,27 @@ class Controller
68 72
 	clickTube(tubeIndex)
69 73
 	{
70 74
 		let _clickedTube = this.#game.tubeAt(tubeIndex);
75
+		let _selectedTube = this.#game.selectedTube();
76
+
71 77
 		if (_clickedTube == null) return;
72 78
 		
73
-		if (_clickedTube == this.#game.selectedTube())
79
+		if (_clickedTube == _selectedTube)
80
+		{
81
+			this.#drawer.removeSelectedEffect(this.#game.selectedTubeIndex());
74 82
 			this.#game.unselectTube();
75
-		else if (this.canPourInto(this.#game.selectedTube(), _clickedTube))
83
+		}
84
+		else if (this.canPourInto(_selectedTube, _clickedTube))
76 85
 		{
77
-			this.pourColorInto(this.#game.selectedTube(), _clickedTube);
86
+			this.pourColorInto(_selectedTube, _clickedTube);
87
+			this.#drawer.removeSelectedEffect(this.#game.selectedTubeIndex());
78 88
 			this.#game.unselectTube();
79 89
 		}
80 90
 		else if (!_clickedTube.isEmpty())
91
+		{
92
+			if (_selectedTube != null) this.#drawer.removeSelectedEffect(this.#game.selectedTubeIndex());
93
+			this.#drawer.applySelectedEffect(tubeIndex);
81 94
 			this.#game.selectTubeAt(tubeIndex);
95
+		}
82 96
 	}
83 97
 	
84 98
 	canPourInto(sourceTube, targetTube)
@@ -153,7 +167,7 @@ class Controller
153 167
 
154 168
 	drawGame()
155 169
 	{
156
-		this.#drawer.draw(this.#game);
170
+		this.#drawer.draw();
157 171
 	}
158 172
 
159 173
 	drawUI()

+ 64
- 0
js/drawer/TubeDrawObject.js Visa fil

@@ -0,0 +1,64 @@
1
+class TubeDrawObject
2
+{
3
+    #tube;
4
+
5
+    constructor(_x, _y, _tube, _scale)
6
+    {
7
+        this.x = _x;
8
+        this.y = _y;
9
+        this.scale = _scale;
10
+        this.#tube = _tube;
11
+    }
12
+
13
+    get height()
14
+    {
15
+        return this.scale * this.#tube.height();
16
+    }
17
+
18
+    get width()
19
+    {
20
+        return this.scale;
21
+    }
22
+
23
+    draw()
24
+	{
25
+		for (let layerIndex = 0; layerIndex < this.#tube.height(); layerIndex++)
26
+        {
27
+            let color = this.#tube.getColorAtLevel(layerIndex);
28
+            color == null ? noFill() : fill(color);
29
+
30
+            let layerY = this.y + (this.#tube.height()-layerIndex-1) * this.scale;
31
+
32
+            if (layerIndex == 0) this.#drawTubeBottom(this.x, layerY, this.scale);
33
+            else this.#drawTubeLayer(this.x, layerY, this.scale)
34
+        }
35
+	}
36
+
37
+    #drawTubeLayer(_x, _y, _scale)
38
+    {
39
+        noStroke();
40
+        rect(_x, _y, _scale);
41
+
42
+        stroke('black');
43
+		strokeWeight(2);
44
+        line(_x, _y, _x, _y + _scale);
45
+        line(_x + _scale, _y, _x + _scale, _y + _scale);
46
+    }
47
+
48
+    #drawTubeBottom(_x, _y, _scale)
49
+    {
50
+        noStroke();
51
+        rect(_x, _y, _scale, _scale/2);
52
+
53
+        stroke('black');
54
+        line(_x, _y, _x, _y + _scale/2);
55
+        line(_x + _scale, _y, _x + _scale, _y + _scale/2);
56
+        arc(_x + _scale/2, _y + _scale/2, _scale, _scale, 0, PI);
57
+    }
58
+
59
+    inbounds(_x, _y)
60
+    {
61
+        return _x > this.x && _x < this.x + this.width
62
+            && _y > this.y && _y < this.y + this.height;
63
+    }
64
+}

+ 39
- 70
js/drawer/drawer.js Visa fil

@@ -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
 }

+ 1
- 1
js/model/tube.js Visa fil

@@ -50,7 +50,7 @@ class Tube {
50 50
 	
51 51
 	getColorAtLevel(index)
52 52
 	{
53
-		if (index < 0 || index >= this.#colors.length) return '';
53
+		if (index < 0 || index >= this.#colors.length) return null;
54 54
 		
55 55
 		return this.#colors[index].toString();
56 56
 	}

Laddar…
Avbryt
Spara