Skip to content

Commit 08b896c

Browse files
committed
Implemented pause/resume/reset for display
1 parent 7000840 commit 08b896c

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/tabs/RobotMaze/components/RobotSimulation.tsx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,13 @@ const RobotSimulation : React.FC<MapProps> = ({
161161
const ctx = canvas.getContext('2d');
162162
if (!ctx) return;
163163

164-
robot.current = actionLog[0].position;
164+
// Reset current action
165+
currentAction.current = 1;
165166

167+
// Reset robot position
168+
robot.current = Object.assign({}, actionLog[0].position);
169+
170+
// Update canvas dimensions
166171
canvas.width = width;
167172
canvas.height = height;
168173

@@ -250,13 +255,21 @@ const RobotSimulation : React.FC<MapProps> = ({
250255
return () => clearInterval(interval);
251256
}, [animationStatus, width, height, areas, robotSize]);
252257

258+
console.log(animationStatus);
259+
253260
// Store a reference to the HTML canvas
254261
const canvasRef = useRef<HTMLCanvasElement>(null);
255262

256263
return (
257264
<>
258-
<button onClick={() => {setAnimationStatus(1);}}>Start</button>
259-
<p>{animationStatus === 3 ? '' : message}</p>
265+
{animationStatus === 0
266+
? <button onClick={() => {setAnimationStatus(1);}}>Start</button>
267+
: animationStatus === 1
268+
? <button onClick={() => {setAnimationStatus(2);}}>Pause</button>
269+
: animationStatus === 2
270+
? <button onClick={() => {setAnimationStatus(1);}}>Resume</button>
271+
: <button onClick={() => {setAnimationStatus(0);}}>Restart</button>}
272+
{animationStatus === 3 && <p>{message}</p>}
260273
<div style={{display: 'flex', justifyContent: 'center'}}>
261274
<canvas ref={canvasRef}/>
262275
</div>

0 commit comments

Comments
 (0)