Bläddra i källkod

petit rework

pull/2/head
demisel 11 månader sedan
förälder
incheckning
1c53e870c9
4 ändrade filer med 59 tillägg och 33 borttagningar
  1. 1
    0
      index.html
  2. 29
    6
      js/controller.js
  3. 9
    27
      js/drawer.js
  4. 20
    0
      js/ui.js

+ 1
- 0
index.html Visa fil

@@ -8,6 +8,7 @@
8 8
 	<link href="./css/style.css" rel="stylesheet">
9 9
 	<script src="./js/p5.min.js"></script>
10 10
 	<script src="./js/tube.js"></script>
11
+	<script src="./js/ui.js"></script>
11 12
 	<script src="./js/game.js"></script>
12 13
 	<script src="./js/drawer.js"></script>
13 14
 	<script src="./js/controller.js"></script>

+ 29
- 6
js/controller.js Visa fil

@@ -1,13 +1,17 @@
1 1
 function setup()
2 2
 {
3
+	this.width = 800
4
+	this.height = 600
5
+	this.ui = new UI(this.width, this.height);
6
+	this.lastTime = 0;
3 7
 	newGame();
4 8
 }
5 9
 
6 10
 function newGame() {
7
-	let params = gatherMenuValues();
11
+	const params = gatherMenuValues();
8 12
 	
9 13
 	this.game = new Game(params.TUBESNUMBERS, params.TUBESLEVELS);
10
-	this.drawer = new Drawer(params.PADDING);
14
+	this.drawer = new Drawer(params.PADDING, this.width, this.height);
11 15
 	
12 16
 	this.drawer.setTubesParams(params.TUBESNUMBERS, params.TUBESLEVELS, params.TUBESIZE);
13 17
 	
@@ -22,7 +26,6 @@ function mousePressed(e)
22 26
 	{
23 27
 		this.game.selectTube(tubeId);
24 28
 	}
25
-	
26 29
 	this.needUpdate = true;
27 30
 }
28 31
 
@@ -40,11 +43,31 @@ function gatherMenuValues()
40 43
 		PADDING: inputPadding
41 44
 	};
42 45
 }
46
+
43 47
 function draw()
44 48
 {
45
-	if (!this.needUpdate && !game.isCompleted) return;
49
+
50
+	const timeDelta = millis() - lastTime;
51
+	// peut-être pas nécessaire de limiter le framerate
52
+	if (timeDelta < 50)
53
+		return;
54
+	this.lastTime = millis();
46 55
 	
56
+	const steps = [
57
+		(this.needUpdate || this.game.isCompleted) && drawGame,
58
+		this.game.isCompleted && drawUI
59
+	]
60
+
61
+	steps.forEach(step => step && step());
62
+}
63
+
64
+function drawGame()
65
+{
47 66
 	this.drawer.draw(this.game);
48
-	
49 67
 	this.needUpdate = false;
50
-}
68
+}
69
+
70
+function drawUI()
71
+{
72
+	this.ui.drawWinView();
73
+}

+ 9
- 27
js/drawer.js Visa fil

@@ -1,25 +1,21 @@
1 1
 class Drawer
2 2
 {
3
-	#canvasWidth
4
-	#canvasHeight
5 3
 	
6
-	constructor(_padding)
4
+	constructor(_padding, width, height)
7 5
 	{
8 6
 		this.padding = _padding;
9
-		
10
-		createCanvas(0, 0);
7
+
8
+		this.width = width;
9
+		this.height = height;
10
+
11
+		createCanvas(width, height);
11 12
 	}
12 13
 	
13 14
 	setTubesParams(_tubesNumbers, _tubeLevels, _tubeSize)
14 15
 	{
15 16
 		this.tubesNumbers = _tubesNumbers;
16 17
 		this.tubeLevels = _tubeLevels;
17
-		this.tubeSize = _tubeSize;
18
-		
19
-		this.#canvasWidth = this.tubesNumbers * (this.tubeSize + this.padding) + this.padding;
20
-		this.#canvasHeight = 2 * this.padding + (this.tubeLevels+1) * this.tubeSize;
21
-		
22
-		resizeCanvas(this.#canvasWidth, this.#canvasHeight);
18
+		this.tubeSize = _tubeSize;		
23 19
 	}
24 20
 	
25 21
 	draw(game)
@@ -31,8 +27,6 @@ class Drawer
31 27
 		
32 28
 		this.drawTubes(this.padding, this.padding+this.tubeSize, tubes, selectedTube);
33 29
 
34
-		if(game.isCompleted)
35
-			this.drawWinView();
36 30
 	}
37 31
 
38 32
 	drawTubes(x, y, tubes, selectedTube)
@@ -78,20 +72,8 @@ class Drawer
78 72
 	{
79 73
 		return x >= this.padding
80 74
 			&& y >= this.padding
81
-			&& x < this.#canvasWidth - this.padding
82
-			&& y < this.#canvasHeight - this.padding;
75
+			&& x < this.width - this.padding
76
+			&& y < this.height - this.padding;
83 77
 	}
84 78
 
85
-	drawWinView(){
86
-		fill('red');
87
-		textSize(50);
88
-		let textX = this.#canvasWidth / 2 - 175 + 10 * sin(frameCount/10);
89
-		let textY = this.#canvasHeight / 2 		+ 10 * cos(frameCount/10);
90
-		let message = "👑 You Win! 👑"  
91
-		text(message, textX, textY);
92
-		textX ++;
93
-		textY ++;
94
-		fill('yellow');
95
-		text(message, textX, textY);
96
-	}
97 79
 }

+ 20
- 0
js/ui.js Visa fil

@@ -0,0 +1,20 @@
1
+class UI {
2
+
3
+    constructor(width, height){
4
+        this.width = width;
5
+        this.height = height;
6
+    }
7
+
8
+    drawWinView(){
9
+        fill('red');
10
+		textSize(50);
11
+		let textX = lerp(this.width / 2 - 175, 20 * sin(frameCount/10), 0.2);
12
+		let textY = lerp(this.height / 2, 20 * cos(frameCount/10), 0.2);
13
+		let message = "👑 You Win! 👑"  
14
+		text(message, textX, textY);
15
+		textX ++;
16
+		textY ++;
17
+		fill('yellow');
18
+		text(message, textX, textY);
19
+    }
20
+}

Laddar…
Avbryt
Spara