1- import context from 'js-slang/context' ;
1+ // import context from 'js-slang/context'; NOT ALLOWED
22import {
33 head ,
44 tail ,
55 type List
66} from 'js-slang/dist/stdlib/list' ;
77
8+ // A point (x, y)
89interface Point {
910 x : number
1011 y : number
1112}
1213
13- interface PointWithRotation extends Point {
14+ // A point (x, y) with rotation
15+ export interface PointWithRotation extends Point {
1416 rotation : number
1517}
1618
17- interface Command {
19+ // A prior movement
20+ export interface Action {
1821 type : string
1922 position : PointWithRotation
2023}
@@ -23,19 +26,19 @@ interface AreaFlags {
2326 [ name : string ] : boolean
2427}
2528
26- interface Area {
29+ export interface Area {
2730 vertices : Point [ ]
2831 isCollidable : boolean
2932 flags : AreaFlags
3033}
3134
32- interface StateData {
35+ export interface StateData {
3336 isInit : boolean
3437 width : number
3538 height : number
3639 areas : Area [ ]
37- areasEntered : number [ ]
38- moveCommands : Command [ ]
40+ areaLog : number [ ]
41+ actionLog : Action [ ]
3942 message : string
4043 success : boolean
4144 messages : string [ ]
@@ -55,8 +58,8 @@ const stateData: StateData = {
5558 width : 500 ,
5659 height : 500 ,
5760 areas : [ ] ,
58- areasEntered : [ ] ,
59- moveCommands : [ ] ,
61+ areaLog : [ ] ,
62+ actionLog : [ ] ,
6063 message : 'moved successfully' ,
6164 success : true ,
6265 messages : [ ] ,
@@ -74,7 +77,7 @@ const robot: Robot = {
7477let bounds : Point [ ] = [ ] ;
7578
7679// sets the context to the statedata obj, mostly for convenience so i dont have to type context.... everytime
77- context . moduleContexts . robot_minigame . state = stateData ;
80+ // context.moduleContexts.robot_minigame.state = stateData;
7881
7982export function set_pos ( x : number , y : number ) : void {
8083 robot . x = x ;
@@ -122,7 +125,7 @@ export function init(
122125 set_pos ( posX , posY ) ;
123126 set_rotation ( rotation ) ;
124127
125- stateData . moveCommands . push ( { type : 'begin' , position : getPositionWithRotation ( ) } ) ; // push starting point to movepoints data
128+ stateData . actionLog . push ( { type : 'begin' , position : getPositionWithRotation ( ) } ) ; // push starting point to movepoints data
126129 stateData . isInit = true ;
127130
128131 bounds = [
@@ -143,15 +146,43 @@ export function init(
143146export function create_area (
144147 vertices : List ,
145148 isCollidable : boolean ,
146- flags : AreaFlags
149+ flags : AreaFlags = { }
147150) {
148- // TO BE IMPLEMENTED
149- }
151+ // Parse vertices list into a points array
152+ const points : Point [ ] = [ ] ;
150153
154+ while ( vertices != null ) {
155+ const p = head ( vertices ) ;
156+ points . push ( { x : head ( p ) , y : tail ( p ) } ) ;
157+ vertices = tail ( vertices ) ;
158+ }
151159
160+ // Store the new area
161+ stateData . areas . push ( {
162+ vertices : points ,
163+ isCollidable,
164+ flags
165+ } ) ;
166+ }
152167
153- /* REFACTOR / REIMPLEMENT >>>
168+ /**
169+ * Creates a new obstacle
170+ *
171+ * @param vertices: List
172+ */
173+ export function create_obstacle (
174+ vertices : List
175+ ) {
176+ create_area ( vertices , true ) ;
177+ }
178+
179+ /**
180+ * Creates a new rectangular, axis-aligned obstacle
181+ *
182+ * @param x top left corner of the
183+ */
154184
185+ /*
155186// easily set up a rectangular wall using the x and y of the top left corner, and width/height
156187export function set_rect_wall(x: number, y: number, width: number, height: number) {
157188 const polygon: Polygon = [
@@ -250,7 +281,7 @@ export function move_forward(distance: number) {
250281
251282 robot . x = nextPoint . x ;
252283 robot . y = nextPoint . y ;
253- stateData . moveCommands . push ( { type : 'move' , position : getPositionWithRotation ( ) } ) ;
284+ stateData . actionLog . push ( { type : 'move' , position : getPositionWithRotation ( ) } ) ;
254285
255286 logCoordinates ( ) ;
256287}
@@ -276,7 +307,7 @@ export function move_forward_to_wall() {
276307
277308 robot . x = nextPoint . x ;
278309 robot . y = nextPoint . y ;
279- stateData . moveCommands . push ( { type : 'move' , position : getPositionWithRotation ( ) } ) ;
310+ stateData . actionLog . push ( { type : 'move' , position : getPositionWithRotation ( ) } ) ;
280311
281312 // for debug
282313 stateData . messages . push ( `Distance is ${ distance } Collision point at x: ${ nextPoint . x } , y: ${ nextPoint . y } ` ) ;
@@ -297,7 +328,7 @@ export function rotate(angle: number) {
297328 if ( robot . dx < 0.00001 && robot . dx > - 0.00001 ) robot . dx = 0 ;
298329 if ( robot . dy < 0.00001 && robot . dy > - 0.00001 ) robot . dy = 0 ;
299330
300- stateData . moveCommands . push ( { type : 'rotateRight ' , position : getPositionWithRotation ( ) } ) ;
331+ stateData . actionLog . push ( { type : 'rotate ' , position : getPositionWithRotation ( ) } ) ;
301332
302333 // debug log
303334 logCoordinates ( ) ;
@@ -318,7 +349,7 @@ export function turn_left() {
318349 if ( robot . dx < 0.00001 && robot . dx > - 0.00001 ) robot . dx = 0 ;
319350 if ( robot . dy < 0.00001 && robot . dy > - 0.00001 ) robot . dy = 0 ;
320351
321- stateData . moveCommands . push ( { type : 'rotateLeft' , position : getPositionWithRotation ( ) } ) ;
352+ stateData . actionLog . push ( { type : 'rotateLeft' , position : getPositionWithRotation ( ) } ) ;
322353
323354 // debug log
324355 logCoordinates ( ) ;
@@ -338,7 +369,7 @@ export function turn_right() {
338369 if ( robot . dx < 0.00001 && robot . dx > - 0.00001 ) robot . dx = 0 ;
339370 if ( robot . dy < 0.00001 && robot . dy > - 0.00001 ) robot . dy = 0 ;
340371
341- stateData . moveCommands . push ( { type : 'rotateRight' , position : getPositionWithRotation ( ) } ) ;
372+ stateData . actionLog . push ( { type : 'rotateRight' , position : getPositionWithRotation ( ) } ) ;
342373
343374 // debug log
344375 logCoordinates ( ) ;
@@ -375,7 +406,7 @@ export function rotate_left(angle: number) {
375406 if ( robot . dx < 0.00001 && robot . dx > - 0.00001 ) robot . dx = 0 ;
376407 if ( robot . dy < 0.00001 && robot . dy > - 0.00001 ) robot . dy = 0 ;
377408
378- stateData . moveCommands . push ( { type : 'rotateLeft' , position : getPositionWithRotation ( ) } ) ;
409+ stateData . actionLog . push ( { type : 'rotateLeft' , position : getPositionWithRotation ( ) } ) ;
379410
380411 logCoordinates ( ) ;
381412}
@@ -392,7 +423,7 @@ export function getY():number {
392423// add as a command later
393424export function sensor ( ) : boolean {
394425 const dist = findDistanceToWall ( ) ;
395- stateData . moveCommands . push ( { type : 'sensor' , position : getPositionWithRotation ( ) } ) ;
426+ stateData . actionLog . push ( { type : 'sensor' , position : getPositionWithRotation ( ) } ) ;
396427 if ( dist <= 10 + robot . radius ) {
397428 return true ;
398429 }
0 commit comments