66import context from 'js-slang/context' ;
77
88type Point = { x : number , y : number } ;
9- type Wall = { p1 : Point , p2 : Point } ;
109
1110type Polygon = Point [ ] ;
1211
@@ -16,20 +15,20 @@ type StateData = {
1615 height : number ,
1716 walls : Polygon [ ] ,
1817 movePoints : Point [ ] ,
19- message : String ,
18+ message : string ,
2019 success : boolean ,
21- messages : String [ ]
22- }
20+ messages : string [ ]
21+ } ;
2322
2423type Robot = {
2524 x : number ; // the top left corner
2625 y : number ;
2726 dx : number ;
2827 dy : number ;
2928 radius : number
30- }
29+ } ;
3130
32- let stateData : StateData = {
31+ const stateData : StateData = {
3332 isInit : false ,
3433 width : 500 ,
3534 height : 500 ,
@@ -38,17 +37,17 @@ let stateData: StateData = {
3837 message : 'moved successfully' ,
3938 success : true ,
4039 messages : [ ]
41- }
40+ } ;
4241
43- let robot : Robot = {
42+ const robot : Robot = {
4443 x : 25 , // default start pos, puts it at the top left corner of canvas without colliding with the walls
4544 y : 25 ,
4645 dx : 1 ,
4746 dy : 0 ,
48- radius : 20 //give the robot a circular hitbox
49- }
47+ radius : 20 // give the robot a circular hitbox
48+ } ;
5049
51- let bounds : Point [ ] = [ ]
50+ let bounds : Point [ ] = [ ] ;
5251
5352// default grid width and height is 25
5453context . moduleContexts . robot_minigame . state = stateData ;
@@ -78,7 +77,7 @@ export function init(width: number, height: number, posX: number, posY: number)
7877 { x : width , y : 0 } ,
7978 { x : width , y : height } ,
8079 { x : 0 , y : height }
81- ]
80+ ] ;
8281
8382}
8483
@@ -108,21 +107,21 @@ export function set_rect_wall(x: number, y: number, width: number, height: numbe
108107 { x : x + width , y : y } ,
109108 { x : x + width , y : y + height } ,
110109 { x : x , y :y + height }
111- ]
110+ ] ;
112111
113112 stateData . walls . push ( polygon ) ;
114113}
115114
116115export function move_forward ( ) : void {
117116 if ( alrCollided ( ) ) return ;
118117
119- const collisionPoint : Point | null = raycast ( stateData . walls )
118+ const collisionPoint : Point | null = raycast ( stateData . walls ) ;
120119 if ( collisionPoint !== null ) {
121- let nextPoint : Point = {
120+ const nextPoint : Point = {
122121 x : collisionPoint . x - robot . dx * ( robot . radius + 5 ) ,
123122 y : collisionPoint . y - robot . dy * ( robot . radius + 5 )
124- }
125-
123+ } ;
124+
126125 robot . x = nextPoint . x ;
127126 robot . y = nextPoint . y ;
128127 stateData . movePoints . push ( nextPoint ) ;
@@ -142,20 +141,20 @@ function raycast(polygons: Polygon[]): Point | null {
142141 let minDist = Infinity ;
143142
144143 for ( const polygon of polygons ) {
145- stateData . messages . push ( "checking polygon" ) ;
146- const numVertices = polygon . length ;
147-
148- for ( let i = 0 ; i < numVertices ; i ++ ) {
149- const x1 = polygon [ i ] . x , y1 = polygon [ i ] . y ;
150- const x2 = polygon [ ( i + 1 ) % numVertices ] . x , y2 = polygon [ ( i + 1 ) % numVertices ] . y ;
151-
152- const intersection = getIntersection ( robot . x , robot . y , robot . dx + robot . x , robot . dy + robot . y , x1 , y1 , x2 , y2 ) ;
153-
154- if ( intersection . collided && intersection . dist < minDist ) {
155- minDist = intersection . dist
156- nearest = { x : intersection . x , y : intersection . y } ;
157- }
144+ stateData . messages . push ( 'checking polygon' ) ;
145+ const numVertices = polygon . length ;
146+
147+ for ( let i = 0 ; i < numVertices ; i ++ ) {
148+ const x1 = polygon [ i ] . x , y1 = polygon [ i ] . y ;
149+ const x2 = polygon [ ( i + 1 ) % numVertices ] . x , y2 = polygon [ ( i + 1 ) % numVertices ] . y ;
150+
151+ const intersection = getIntersection ( robot . x , robot . y , robot . dx + robot . x , robot . dy + robot . y , x1 , y1 , x2 , y2 ) ;
152+
153+ if ( intersection . collided && intersection . dist < minDist ) {
154+ minDist = intersection . dist ;
155+ nearest = { x : intersection . x , y : intersection . y } ;
158156 }
157+ }
159158 }
160159
161160 // if no collisions with obstacles, check the outer bounds of map
@@ -167,7 +166,7 @@ function raycast(polygons: Polygon[]): Point | null {
167166 const intersection = getIntersection ( robot . x , robot . y , robot . dx + robot . x , robot . dy + robot . y , x1 , y1 , x2 , y2 ) ;
168167
169168 if ( intersection . collided && intersection . dist < minDist ) {
170- minDist = intersection . dist
169+ minDist = intersection . dist ;
171170 nearest = { x : intersection . x , y : intersection . y } ;
172171 }
173172 }
@@ -176,37 +175,35 @@ function raycast(polygons: Polygon[]): Point | null {
176175 return nearest ; // Closest intersection point
177176}
178177
179- //Determine if a ray and a line segment intersect, and if so, determine the collision point
178+ // Determine if a ray and a line segment intersect, and if so, determine the collision point
180179function getIntersection ( x1 , y1 , x2 , y2 , x3 , y3 , x4 , y4 ) {
181- var denom = ( ( x2 - x1 ) * ( y4 - y3 ) - ( y2 - y1 ) * ( x4 - x3 ) ) ;
182- var r ;
183- var s ;
184- var x ;
185- var y ;
186- var b = false ;
187-
188- //If lines not collinear or parallel
180+ const denom = ( ( x2 - x1 ) * ( y4 - y3 ) - ( y2 - y1 ) * ( x4 - x3 ) ) ;
181+ let r ;
182+ let s ;
183+ let x ;
184+ let y ;
185+ let b = false ;
186+
187+ // If lines not collinear or parallel
189188 if ( denom != 0 ) {
190- //Intersection in ray "local" coordinates
189+ // Intersection in ray "local" coordinates
191190 r = ( ( ( y1 - y3 ) * ( x4 - x3 ) ) - ( x1 - x3 ) * ( y4 - y3 ) ) / denom ;
192191
193- //Intersection in segment "local" coordinates
192+ // Intersection in segment "local" coordinates
194193 s = ( ( ( y1 - y3 ) * ( x2 - x1 ) ) - ( x1 - x3 ) * ( y2 - y1 ) ) / denom ;
195194
196- //The algorithm gives the intersection of two infinite lines, determine if it lies on the side that the ray is defined on
197- if ( r >= 0 )
198- {
199- //If point along the line segment
200- if ( s >= 0 && s <= 1 )
201- {
195+ // The algorithm gives the intersection of two infinite lines, determine if it lies on the side that the ray is defined on
196+ if ( r >= 0 ) {
197+ // If point along the line segment
198+ if ( s >= 0 && s <= 1 ) {
202199 b = true ;
203- //Get point coordinates (offset by r local units from start of ray)
200+ // Get point coordinates (offset by r local units from start of ray)
204201 x = x1 + r * ( x2 - x1 ) ;
205202 y = y1 + r * ( y2 - y1 ) ;
206203 }
207204 }
208205 }
209- var p = { collided : b , x : x , y : y , dist : r } ;
206+ const p = { collided : b , x : x , y : y , dist : r } ;
210207 return p ;
211208}
212209
0 commit comments