Skip to content

Commit c63f209

Browse files
committed
Remove multiple map functionality
1 parent 5a5c541 commit c63f209

File tree

4 files changed

+40
-84
lines changed

4 files changed

+40
-84
lines changed

src/bundles/robot_minigame/functions.ts

Lines changed: 33 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ export interface Robot extends PointWithRotation {
4848
radius: number
4949
}
5050

51-
export interface RobotMap {
51+
export interface RobotMinigame {
52+
isInit: boolean
5253
width: number
5354
height: number
5455
robot: Robot
@@ -58,18 +59,16 @@ export interface RobotMap {
5859
message: string
5960
}
6061

61-
export interface RobotMinigame {
62-
isInit: boolean
63-
isComplete: boolean
64-
activeMap: number
65-
maps: RobotMap[]
66-
}
67-
62+
// Default state before initialisation
6863
const state: RobotMinigame = {
6964
isInit: false,
70-
isComplete: false,
71-
activeMap: -1,
72-
maps: []
65+
width: 500,
66+
height: 500,
67+
robot: {x: 250, y: 250, rotation: 0, radius: 15},
68+
areas: [],
69+
areaLog: [],
70+
actionLog: [],
71+
message: ""
7372
};
7473

7574
// sets the context to the state obj, mostly for convenience so i dont have to type context.... everytime
@@ -106,16 +105,18 @@ export function init(
106105
radius: 15
107106
};
108107

109-
// Push the new map to state and make it the active map
110-
state.activeMap = state.maps.push({
111-
width,
112-
height,
113-
robot,
114-
areas: [],
115-
areaLog: [],
116-
actionLog: [{type: 'begin', position: Object.assign({}, robot)}],
117-
message: 'Moved successfully!'
118-
}) - 1;
108+
// Update the map's dimensions
109+
state.width = width;
110+
state.height = height;
111+
112+
// Update the robot
113+
state.robot = robot;
114+
115+
// Update the action log with the robot's starting position
116+
state.actionLog = [{type: 'begin', position: Object.assign({}, robot)}];
117+
118+
// Update the success message
119+
state.message = "Please run this in the assessments tab!";
119120
}
120121

121122
/**
@@ -164,7 +165,7 @@ export function create_area(
164165
}
165166

166167
// Store the new area
167-
getMap().areas.push({
168+
state.areas.push({
168169
vertices: parsedVertices,
169170
isObstacle,
170171
flags: parsedFlags
@@ -238,6 +239,8 @@ export function create_rect_obstacle(
238239
* Inform the simulator that the initialisation phase is complete
239240
*/
240241
export function complete_init() {
242+
if (state.actionLog.length === 0) throw new Error("May not complete initialization without first running init()");
243+
241244
state.isInit = true;
242245
}
243246

@@ -300,7 +303,7 @@ export function move_forward(
300303
robot.y = robot.y + finalDistance * Math.sin(robot.rotation);
301304

302305
// Update the final message
303-
getMap().message = `Collided with wall at (${robot.x},${robot.y})`;
306+
state.message = `Collided with wall at (${robot.x},${robot.y})`;
304307

305308
// Throw an error to interrupt the simulation
306309
throw new Error('Collided with wall');
@@ -364,32 +367,6 @@ export function turn_right() {
364367
// TESTING //
365368
// ======= //
366369

367-
/**
368-
* Inform the simulator that the testing phase is starting
369-
*/
370-
export function start_testing() {
371-
if (state.isComplete) throw new Error('May not start testing twice!');
372-
373-
state.isComplete = true;
374-
}
375-
376-
/**
377-
* Set the given map as the active map
378-
*
379-
* @param id index of the map in the array
380-
*/
381-
export function set_active_map(
382-
id: number
383-
) {
384-
// Testing functions should only run after the simulation is complete
385-
if (!state.isComplete) throw new Error('May not use testing functions before starting testing! Use start_testing() first!');
386-
387-
// Confirm that map with the given id exists
388-
if (id >= state.maps.length) throw new Error('Given map does not exist!');
389-
390-
state.activeMap = id;
391-
}
392-
393370
/**
394371
* Checks if the robot's entered areas satisfy the callback
395372
*
@@ -398,10 +375,7 @@ export function set_active_map(
398375
export function entered_areas(
399376
callback : (areas : Area[]) => boolean
400377
) : boolean {
401-
// Testing functions should only run after the simulation is complete
402-
if (!state.isComplete) throw new Error('May not use testing functions before starting testing! Use start_testing() first!');
403-
404-
return callback(getMap().areaLog);
378+
return callback(state.areaLog);
405379
}
406380

407381
/**
@@ -413,9 +387,6 @@ export function entered_areas(
413387
export function entered_colors(
414388
colors: string[]
415389
) : boolean {
416-
// Testing functions should only run after the simulation is complete
417-
if (!state.isComplete) throw new Error('May not use testing functions before starting testing! Use start_testing() first!');
418-
419390
return entered_areas(areas => {
420391
const coloredAreas = areas
421392
.filter(area => colors.includes(area.flags.color)) // Filter relevant colors
@@ -435,22 +406,13 @@ export function entered_colors(
435406
// MAP HELPERS //
436407
// =========== //
437408

438-
/**
439-
* Get the active map
440-
*
441-
* @returns the active map
442-
*/
443-
function getMap() : RobotMap {
444-
return state.maps[state.activeMap];
445-
}
446-
447409
/**
448410
* Get the active robot
449411
*
450412
* @returns the active robot
451413
*/
452414
function getRobot() : Robot {
453-
return getMap().robot;
415+
return state.robot;
454416
}
455417

456418
/**
@@ -460,7 +422,7 @@ function getRobot() : Robot {
460422
*/
461423
function getBounds() : Point[] {
462424
// Get active map
463-
const { width, height } = getMap();
425+
const { width, height } = state;
464426

465427
return [
466428
{x: 0, y: 0},
@@ -503,7 +465,7 @@ interface Collision {
503465
function robot_raycast(
504466
filter: (area: Area) => boolean = () => true
505467
) : Collision[] {
506-
return getMap().areas
468+
return state.areas
507469
.filter(filter) // Apply filter
508470
.map(area => robot_raycast_area(area)) // Raycast each area on the map
509471
.concat([
@@ -609,7 +571,7 @@ function area_of_point(
609571
point: Point
610572
) : Area | null {
611573
// Return the first area the point is within
612-
for (const area of getMap().areas) {
574+
for (const area of state.areas) {
613575
if (is_within_area(point, area)) return area;
614576
}
615577

@@ -697,7 +659,7 @@ function logAction(
697659
type: 'begin' | 'move' | 'rotate' | 'sensor',
698660
position: PointWithRotation
699661
) {
700-
getMap().actionLog.push({type, position});
662+
state.actionLog.push({type, position});
701663
}
702664

703665
/**
@@ -709,7 +671,7 @@ function logArea(
709671
area: Area
710672
) {
711673
// Get the area log
712-
const areaLog = getMap().areaLog;
674+
const areaLog = state.areaLog;
713675

714676
if (
715677
areaLog.length > 0 // Check for empty area log

src/bundles/robot_minigame/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ export {
1010
init, create_area, create_rect_area, create_obstacle, create_rect_obstacle, complete_init,
1111
get_distance, get_color,
1212
move_forward, move_forward_to_wall, rotate, turn_left, turn_right,
13-
start_testing, set_active_map, entered_areas, entered_colors
13+
entered_areas, entered_colors
1414
} from './functions';

src/tabs/RobotMaze/components/RobotSimulation.tsx

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,21 +115,15 @@ interface MapProps {
115115
}
116116

117117
const RobotSimulation : React.FC<MapProps> = ({
118-
state: { maps }
119-
}) => {
120-
// Store the active map
121-
const [active, setActive] = useState(0);
122-
123-
// Retrieve the relevant map
124-
const {
118+
state: {
125119
width,
126120
height,
127121
robot: {radius: robotSize},
128122
areas,
129123
actionLog,
130124
message
131-
} = maps[active];
132-
125+
}
126+
}) => {
133127
// Store animation status
134128
// 0 => Loaded / Loading
135129
// 1 => Running
@@ -258,15 +252,15 @@ const RobotSimulation : React.FC<MapProps> = ({
258252
return (
259253
<>
260254
<div>
261-
<span style={{marginRight: '10px'}}>{maps.map((_, i) => <button onClick={() => {setActive(i); setAnimationStatus(0);}} key={i}>{i}</button>)}</span>
255+
262256
{animationStatus === 0
263257
? <button onClick={() => {setAnimationStatus(1);}}>Start</button>
264258
: animationStatus === 1
265259
? <button onClick={() => {setAnimationStatus(2);}}>Pause</button>
266260
: animationStatus === 2
267261
? <button onClick={() => {setAnimationStatus(1);}}>Resume</button>
268262
: <button onClick={() => {setAnimationStatus(0);}}>Reset</button>}
269-
{animationStatus === 3 && message}
263+
{animationStatus === 3 && <span style={{marginLeft: '5px'}}>{message}</span>}
270264
</div>
271265
<div style={{display: 'flex', justifyContent: 'center'}}>
272266
<canvas ref={canvasRef}/>

src/tabs/RobotMaze/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export default {
3838
// !!! TEMPORARY DEBUGGING FUNCTION, REMOVE ONCE MODULE IS COMPLETE !!!
3939
console.log(context.context?.moduleContexts?.robot_minigame.state);
4040

41-
return context.context?.moduleContexts?.robot_minigame.state.isComplete;
41+
return context.context?.moduleContexts?.robot_minigame.state.isInit;
4242
},
4343

4444
/**

0 commit comments

Comments
 (0)