@@ -29,6 +29,7 @@ interface Ray {
2929// Default state before initialisation
3030const state : RobotMinigame = {
3131 isInit : false ,
32+ hasCollided : false ,
3233 width : 500 ,
3334 height : 500 ,
3435 robot : { x : 250 , y : 250 , rotation : 0 , radius : 15 } ,
@@ -281,6 +282,9 @@ export function get_color() : string {
281282export function move_forward (
282283 distance : number
283284) {
285+ // Ignore if robot has collided with an obstacle
286+ if ( state . hasCollided ) return ;
287+
284288 // Get the robot
285289 const robot = getRobot ( ) ;
286290
@@ -295,17 +299,13 @@ export function move_forward(
295299 // Handle a collision with an obstacle
296300 if ( col . area . isObstacle ) {
297301 // Calculate find distance
298- const finalDistance = ( col . distance - robot . radius + 1 ) ;
299-
300- // Move the robot to its final position
301- robot . x = robot . x + finalDistance * Math . cos ( robot . rotation ) ;
302- robot . y = robot . y + finalDistance * Math . sin ( robot . rotation ) ;
302+ distance = col . distance - robot . radius + 1 ;
303303
304304 // Update the final message
305- state . message = `Collided with wall at (${ robot . x } ,${ robot . y } )` ;
305+ state . message = `Collided with wall at (${ robot . x + distance * Math . cos ( robot . rotation ) } ,${ robot . y + distance * Math . sin ( robot . rotation ) } )` ;
306306
307- // Throw an error to interrupt the simulation
308- throw new Error ( 'Collided with wall' ) ;
307+ // Update state to reflect that the robot has collided with an obstacle
308+ state . hasCollided = true ;
309309 }
310310 }
311311
@@ -324,6 +324,9 @@ const SAFE_DISTANCE_FROM_WALL : number = 10;
324324 * Move the robot forward to within a predefined distance of the wall
325325 */
326326export function move_forward_to_wall ( ) {
327+ // Ignore if robot has collided with an obstacle
328+ if ( state . hasCollided ) return ;
329+
327330 // Move forward the furthest possible safe distance + a lil extra offset
328331 move_forward ( Math . max ( get_distance ( ) - SAFE_DISTANCE_FROM_WALL , 0 ) ) ;
329332}
@@ -336,6 +339,9 @@ export function move_forward_to_wall() {
336339export function rotate (
337340 angle : number
338341) {
342+ // Ignore if robot has collided with an obstacle
343+ if ( state . hasCollided ) return ;
344+
339345 // Get the robot
340346 const robot = getRobot ( ) ;
341347
0 commit comments