Skip to content

Commit 0d4b236

Browse files
committed
formatted code
1 parent 906716a commit 0d4b236

File tree

3 files changed

+58
-66
lines changed

3 files changed

+58
-66
lines changed

src/bundles/robot_minigame/functions.ts

Lines changed: 47 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import context from 'js-slang/context';
77

88
type Point = {x: number, y: number};
9-
type Wall = {p1: Point, p2: Point};
109

1110
type 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

2423
type 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
5453
context.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

116115
export 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
180179
function 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

src/tabs/RobotMaze/canvas.tsx

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,7 @@ type Point = {
2121
y: number;
2222
};
2323

24-
type Wall = {
25-
p1: Point;
26-
p2: Point
27-
};
28-
29-
type Polygon = Point[]
24+
type Polygon = Point[];
3025

3126
export default class Canvas extends React.Component<Props, State> {
3227
private canvasRef: React.RefObject<HTMLCanvasElement>;
@@ -78,7 +73,7 @@ export default class Canvas extends React.Component<Props, State> {
7873
ctx.clearRect(0, 0, this.CANVAS_WIDTH, this.CANVAS_HEIGHT);
7974
this.drawWalls(ctx);
8075
this.drawGrid(ctx);
81-
this.drawRobot(ctx, this.xPos, this.yPos)
76+
this.drawRobot(ctx, this.xPos, this.yPos);
8277
};
8378

8479
startAnimation = () => {
@@ -128,7 +123,7 @@ export default class Canvas extends React.Component<Props, State> {
128123
}
129124
}
130125

131-
this.drawRobot(ctx, this.xPos, this.yPos)
126+
this.drawRobot(ctx, this.xPos, this.yPos);
132127
// Request the next frame
133128
this.animationFrameId = requestAnimationFrame(this.animate);
134129
};
@@ -138,7 +133,7 @@ export default class Canvas extends React.Component<Props, State> {
138133

139134
ctx.arc(x, y, 20, 0, Math.PI * 2, false); // Full circle (0 to 2π radians)
140135

141-
ctx.fillStyle = "black"; // Set the fill color
136+
ctx.fillStyle = 'black'; // Set the fill color
142137
ctx.fill(); // Fill the circle
143138
}
144139

@@ -148,16 +143,16 @@ export default class Canvas extends React.Component<Props, State> {
148143
const wall: Polygon = this.walls[i];
149144

150145
ctx.beginPath();
151-
ctx.moveTo(wall[0].x, wall[0].y)
146+
ctx.moveTo(wall[0].x, wall[0].y);
152147
for (let j = 1; j < wall.length; j++) {
153148
ctx.lineTo(wall[j].x, wall[j].y);
154149
}
155150
ctx.closePath();
156151

157-
ctx.fillStyle = "rgba(169, 169, 169, 0.5)"; // Set the fill color
152+
ctx.fillStyle = 'rgba(169, 169, 169, 0.5)'; // Set the fill color
158153
ctx.fill(); // Fill the polygon
159154

160-
ctx.strokeStyle = "rgb(53, 53, 53)"; // Set the stroke color
155+
ctx.strokeStyle = 'rgb(53, 53, 53)'; // Set the stroke color
161156
ctx.lineWidth = 2; // Set the border width
162157
ctx.stroke(); // Stroke the polygon
163158
}
@@ -169,12 +164,12 @@ export default class Canvas extends React.Component<Props, State> {
169164
ctx.moveTo(0, 0);
170165
ctx.lineTo(0, this.CANVAS_HEIGHT);
171166
ctx.lineTo(this.CANVAS_WIDTH, this.CANVAS_HEIGHT);
172-
ctx.lineTo(this.CANVAS_WIDTH, 0)
173-
ctx.closePath()
167+
ctx.lineTo(this.CANVAS_WIDTH, 0);
168+
ctx.closePath();
174169

175170
ctx.strokeStyle = 'gray';
176171
ctx.lineWidth = 3;
177-
ctx.stroke()
172+
ctx.stroke();
178173
}
179174

180175
public render() {

src/tabs/RobotMaze/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { type DebuggerContext } from '../../typings/type_helpers';
2+
import type { DebuggerContext } from '../../typings/type_helpers';
33
import Canvas from './canvas';
44

55
/**

0 commit comments

Comments
 (0)