Skip to content

Commit a5acb35

Browse files
committed
No longer throw error on collision with obstacle
1 parent 827df68 commit a5acb35

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

src/bundles/robot_minigame/functions.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ interface Ray {
2929
// Default state before initialisation
3030
const 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 {
281282
export 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
*/
326326
export 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() {
336339
export 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

src/bundles/robot_minigame/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export interface AreaTest extends Test {
4242

4343
export interface RobotMinigame {
4444
isInit: boolean
45+
hasCollided: boolean
4546
width: number
4647
height: number
4748
robot: Robot

0 commit comments

Comments
 (0)