Browse Source

fixes #3

Ajout d'une fonctionnalité d'historique des
actions
master
Figg 10 months ago
parent
commit
bb55ada145
4 changed files with 63 additions and 1 deletions
  1. 3
    0
      index.html
  2. 12
    1
      js/controller.js
  3. 39
    0
      js/historyHandler.js
  4. 9
    0
      js/model/historyItem.js

+ 3
- 0
index.html View File

9
 	<script src="./js/lib/p5.min.js"></script>
9
 	<script src="./js/lib/p5.min.js"></script>
10
 	<script src="./js/model/tube.js"></script>
10
 	<script src="./js/model/tube.js"></script>
11
 	<script src="./js/model/game.js"></script>
11
 	<script src="./js/model/game.js"></script>
12
+	<script src="./js/model/historyItem.js"></script>
12
 	<script src="./js/ui.js"></script>
13
 	<script src="./js/ui.js"></script>
13
 	<script src="./js/drawer.js"></script>
14
 	<script src="./js/drawer.js"></script>
15
+	<script src="./js/historyHandler.js"></script>
14
 	<script src="./js/controller.js"></script>
16
 	<script src="./js/controller.js"></script>
15
 	<script src="./js/main.js"></script>
17
 	<script src="./js/main.js"></script>
16
 </head>
18
 </head>
34
 			<input type="number" size="5" id="scale" value="50">
36
 			<input type="number" size="5" id="scale" value="50">
35
 		</span>
37
 		</span>
36
 		<button type="button" onclick="controller.initializeGame()">New game</button>
38
 		<button type="button" onclick="controller.initializeGame()">New game</button>
39
+		<button type="button" onclick="controller.undoPreviousAction()">Undo</button>
37
 	</div>
40
 	</div>
38
 	<div class="row justify-center">
41
 	<div class="row justify-center">
39
 		<main oncontextmenu="return false;">
42
 		<main oncontextmenu="return false;">

+ 12
- 1
js/controller.js View File

3
 	#game;
3
 	#game;
4
 	#drawer;
4
 	#drawer;
5
 	#colorsBank = ['black','silver','gray','maroon','red','purple','fuchsia','green','lime','olive','yellow','navy','blue','teal','aqua'];
5
 	#colorsBank = ['black','silver','gray','maroon','red','purple','fuchsia','green','lime','olive','yellow','navy','blue','teal','aqua'];
6
+	#historyHandler;
6
 	
7
 	
7
 	constructor()
8
 	constructor()
8
 	{
9
 	{
14
 		
15
 		
15
 		this.#game = new Game(params.TUBESNUMBERS, params.TUBESLEVELS);
16
 		this.#game = new Game(params.TUBESNUMBERS, params.TUBESLEVELS);
16
 		this.#drawer = new Drawer(params.PADDING, params.SCALE);
17
 		this.#drawer = new Drawer(params.PADDING, params.SCALE);
17
-		
18
+		this.#historyHandler = new HistoryHandler();		
18
 		this.#drawer.computeGameParams(params.TUBESNUMBERS, params.TUBESLEVELS);
19
 		this.#drawer.computeGameParams(params.TUBESNUMBERS, params.TUBESLEVELS);
19
 
20
 
20
 		this.ui = new UI(800, 600);
21
 		this.ui = new UI(800, 600);
90
 	
91
 	
91
 	pourColorInto(sourceTube, targetTube)
92
 	pourColorInto(sourceTube, targetTube)
92
 	{
93
 	{
94
+		let times = 0;
95
+
93
 		do
96
 		do
94
 		{
97
 		{
95
 			targetTube.addColorLayer(sourceTube.removeColorLayer());
98
 			targetTube.addColorLayer(sourceTube.removeColorLayer());
99
+			times++;
96
 		} while (this.canPourInto(sourceTube, targetTube) && !targetTube.isComplete());
100
 		} while (this.canPourInto(sourceTube, targetTube) && !targetTube.isComplete());
101
+
102
+		this.#historyHandler.log(sourceTube, targetTube, times);
97
 		
103
 		
98
 		//this.checkTubeCompletion(sourceTube);
104
 		//this.checkTubeCompletion(sourceTube);
99
 		//this.checkTubeCompletion(targetTube);
105
 		//this.checkTubeCompletion(targetTube);
116
 		}
122
 		}
117
 	}
123
 	}
118
 
124
 
125
+	undoPreviousAction()
126
+	{
127
+		this.#historyHandler.restore();
128
+	}
129
+	
119
 	mousePressed(e) 
130
 	mousePressed(e) 
120
 	{
131
 	{
121
 		let clickedTubeId = this.#drawer.getTubeIdAt(mouseX, mouseY);
132
 		let clickedTubeId = this.#drawer.getTubeIdAt(mouseX, mouseY);

+ 39
- 0
js/historyHandler.js View File

1
+class HistoryHandler
2
+{
3
+    #history;
4
+
5
+    constructor()
6
+    {
7
+        this.#history = [];
8
+    }
9
+
10
+    restore()
11
+    {
12
+        if (this.empty()) return;
13
+
14
+        let lastAction = this.#history.pop();
15
+
16
+        for (let i = 0; i < lastAction.times; i++)
17
+        {
18
+            let color = lastAction.targetTube.removeColorLayer();
19
+            lastAction.sourceTube.addColorLayer(color);
20
+        }
21
+    }
22
+
23
+    empty()
24
+    {
25
+        return this.#history.length == 0;
26
+    }
27
+
28
+    size()
29
+    {
30
+        return this.#history.length;
31
+    }
32
+
33
+    log(_source, _target, _times)
34
+    {
35
+        let historyItem = new HistoryItem(_source, _target, _times);
36
+
37
+        this.#history.push(historyItem);
38
+    }
39
+}

+ 9
- 0
js/model/historyItem.js View File

1
+class HistoryItem
2
+{
3
+    constructor(_source, _target, _times)
4
+    {
5
+        this.sourceTube = _tbAIndex;
6
+        this.targetTube = _tbBIndex;
7
+        this.times = _times;
8
+    }
9
+}

Loading…
Cancel
Save