@@ -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
6863const 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 */
240241export 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(
398375export 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(
413387export 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 */
452414function getRobot ( ) : Robot {
453- return getMap ( ) . robot ;
415+ return state . robot ;
454416}
455417
456418/**
@@ -460,7 +422,7 @@ function getRobot() : Robot {
460422 */
461423function 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 {
503465function 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
0 commit comments