Skip to content

Commit 04766d2

Browse files
committed
formatting code
1 parent 1d7372e commit 04766d2

File tree

5 files changed

+168
-178
lines changed

5 files changed

+168
-178
lines changed

modules.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,5 +116,10 @@
116116
"tabs": [
117117
"Nbody"
118118
]
119+
},
120+
"robot_minigame": {
121+
"tabs": [
122+
"RobotMaze"
123+
]
119124
}
120125
}
Lines changed: 116 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,183 +1,180 @@
1-
/*
2-
* Internally, the robot uses a grid based movement and collision system
1+
/*
2+
* Currently uses a grid based system, will upgrade to more fancy stuff later
3+
* The movement is simulated, then a series of movement points is passed to the module context, which the frontend then uses to render
34
*/
45

56
import context from 'js-slang/context';
67

7-
let robotPos: Point = {x: 0, y: 0};
8-
9-
let movePoints: Point[];
8+
const robotPos: Point = {x: 0, y: 0};
109

1110
const DIRECTIONS = {
12-
UP: 0,
13-
RIGHT: 1,
14-
DOWN: 2,
15-
LEFT: 3
11+
UP: 0,
12+
RIGHT: 1,
13+
DOWN: 2,
14+
LEFT: 3
1615
};
1716

1817
let robotRotation = 1;
1918

2019
// default grid width and height is 25
2120
context.moduleContexts.robot_minigame.state = {
22-
isInit: false,
23-
width: 25,
24-
height: 25,
25-
walls: [],
26-
movePoints: [],
27-
message: "moved successfully",
28-
success: true
29-
}
21+
isInit: false,
22+
width: 25,
23+
height: 25,
24+
walls: [],
25+
movePoints: [],
26+
message: 'moved successfully',
27+
success: true
28+
};
3029

31-
type Point = {x: number, y: number}
32-
type Wall = {p1: Point, p2: Point}
30+
type Point = {x: number, y: number};
31+
type Wall = {p1: Point, p2: Point};
3332

3433
export function set_pos(x: number, y: number): void {
3534
robotPos.x = x;
3635
robotPos.y = y;
3736
}
3837

3938
export function set_grid_width(width: number) {
40-
context.moduleContexts.robot_minigame.state.width = width;
39+
context.moduleContexts.robot_minigame.state.width = width;
4140
}
4241

4342
export function set_grid_height(height: number) {
44-
context.moduleContexts.robot_minigame.state.height = height;
43+
context.moduleContexts.robot_minigame.state.height = height;
4544
}
4645

4746
export function init(gridWidth: number, gridHeight: number, posX: number, posY: number) {
48-
set_grid_width(gridWidth);
49-
set_grid_height(gridHeight);
50-
set_pos(posX, posY);
51-
context.moduleContexts.robot_minigame.state.movePoints.push({x: posX, y: posY});
52-
context.moduleContexts.robot_minigame.state.isInit = true;
47+
set_grid_width(gridWidth);
48+
set_grid_height(gridHeight);
49+
set_pos(posX, posY);
50+
context.moduleContexts.robot_minigame.state.movePoints.push({x: posX, y: posY});
51+
context.moduleContexts.robot_minigame.state.isInit = true;
5352
}
5453

5554
export function turn_left() {
56-
if (alrCollided()) return;
55+
if (alrCollided()) return;
5756

58-
robotRotation -= 1;
59-
if (robotRotation < 0) {
60-
robotRotation = 3;
61-
}
57+
robotRotation -= 1;
58+
if (robotRotation < 0) {
59+
robotRotation = 3;
60+
}
6261
}
6362

6463
export function turn_right() {
65-
if (alrCollided()) return;
64+
if (alrCollided()) return;
6665

67-
robotRotation = (robotRotation + 1) % 4;
66+
robotRotation = (robotRotation + 1) % 4;
6867
}
6968

70-
// takes the top left and bottom right corners of walls
69+
// takes the top left and bottom right corners of walls
7170
// in terms of grid boxes
7271
// grid starts from (0, 0) at the top left corner btw
7372
export function set_wall(x1: number, y1: number, x2: number, y2: number) {
74-
let wall: Wall = {p1: {x: x1, y: y1}, p2: {x: x2, y: y2}};
75-
context.moduleContexts.robot_minigame.state.walls.push(wall);
73+
const wall: Wall = {p1: {x: x1, y: y1}, p2: {x: x2, y: y2}};
74+
context.moduleContexts.robot_minigame.state.walls.push(wall);
7675
}
7776

7877
export function move_forward(dist: number): void {
79-
if (alrCollided()) return;
80-
81-
simulate(dist);
78+
if (alrCollided()) return;
79+
80+
simulate(dist);
8281
}
8382

8483
export function getX():number {
85-
return robotPos.x;
84+
return robotPos.x;
8685
}
8786

8887
export function getY():number {
89-
return robotPos.y;
88+
return robotPos.y;
9089
}
9190

9291
function simulate(moveDist: number) {
93-
let dx: number = 0;
94-
let dy: number = 0;
95-
switch (robotRotation) {
96-
case DIRECTIONS.UP:
97-
dy = -1;
98-
break;
99-
case DIRECTIONS.RIGHT:
100-
dx = 1;
101-
break;
102-
case DIRECTIONS.DOWN:
103-
dy = 1;
104-
break;
105-
case DIRECTIONS.LEFT:
106-
dx = -1;
107-
break;
108-
}
109-
110-
// moves robot by one grid box and checks collision
111-
for (var i = 0; i < moveDist; i++) {
112-
robotPos.x += dx;
113-
robotPos.y += dy;
114-
115-
var walls = context.moduleContexts.robot_minigame.state.walls;
116-
for (let j = 0; j < walls.length; j++) {
117-
if (checkWallCollision(walls[j], robotPos)) {
118-
context.moduleContexts.robot_minigame.state.success = false;
119-
context.moduleContexts.robot_minigame.state.message = "collided";
120-
context.moduleContexts.robot_minigame.state.movePoints.push({x: robotPos.x, y: robotPos.y});
121-
return;
122-
}
123-
}
92+
let dx: number = 0;
93+
let dy: number = 0;
94+
switch (robotRotation) {
95+
case DIRECTIONS.UP:
96+
dy = -1;
97+
break;
98+
case DIRECTIONS.RIGHT:
99+
dx = 1;
100+
break;
101+
case DIRECTIONS.DOWN:
102+
dy = 1;
103+
break;
104+
case DIRECTIONS.LEFT:
105+
dx = -1;
106+
break;
107+
}
108+
109+
// moves robot by one grid box and checks collision
110+
for (let i = 0; i < moveDist; i++) {
111+
robotPos.x += dx;
112+
robotPos.y += dy;
113+
114+
const walls = context.moduleContexts.robot_minigame.state.walls;
115+
for (let j = 0; j < walls.length; j++) {
116+
if (checkWallCollision(walls[j], robotPos)) {
117+
context.moduleContexts.robot_minigame.state.success = false;
118+
context.moduleContexts.robot_minigame.state.message = 'collided';
119+
context.moduleContexts.robot_minigame.state.movePoints.push({x: robotPos.x, y: robotPos.y});
120+
return;
121+
}
124122
}
125-
126-
context.moduleContexts.robot_minigame.state.movePoints.push({x: robotPos.x, y: robotPos.y});
127-
128-
// OLD CODE
129-
// let destX = robotPos.x + moveDist * Math.cos(robotAngle);
130-
// let destY = robotPos.y + moveDist * Math.sin(robotAngle);
131-
// let destPoint: Point = {x: destX, y: destY}
132-
133-
// for (let i = 0; i < steps; i++) {
134-
// let distX: number = moveSpeed * Math.cos(robotAngle);
135-
// let distY: number = moveSpeed * Math.sin(robotAngle);
136-
137-
// robotPos.x += distX;
138-
// robotPos.y += distY;
139-
140-
// for (let j = 0; j < walls.length; j++) {
141-
// if(checkWallCollision(walls[j], robotPos)) {
142-
// addMessage("Collided with wall!!");
143-
// addMessage(`Position: (${robotPos.x.toFixed(3)}, ${robotPos.y.toFixed(3)}), Rotation: ${robotAngle}\n`);
144-
// return;
145-
// }
146-
// }
147-
148-
// if (distanceBetween(destPoint, robotPos) < robotRadius) {
149-
// robotPos = destPoint;
150-
// addMessage(`Robot moved forward by ${moveDist} at angle ${robotAngle} radians\n`);
151-
// addMessage(`Position: (${robotPos.x.toFixed(3)}, ${robotPos.y.toFixed(3)}), Rotation: ${robotAngle}\n`);
152-
// return;
153-
// }
154-
// }
155-
123+
}
124+
125+
context.moduleContexts.robot_minigame.state.movePoints.push({x: robotPos.x, y: robotPos.y});
126+
127+
// OLD CODE
128+
// let destX = robotPos.x + moveDist * Math.cos(robotAngle);
129+
// let destY = robotPos.y + moveDist * Math.sin(robotAngle);
130+
// let destPoint: Point = {x: destX, y: destY}
131+
132+
// for (let i = 0; i < steps; i++) {
133+
// let distX: number = moveSpeed * Math.cos(robotAngle);
134+
// let distY: number = moveSpeed * Math.sin(robotAngle);
135+
136+
// robotPos.x += distX;
137+
// robotPos.y += distY;
138+
139+
// for (let j = 0; j < walls.length; j++) {
140+
// if(checkWallCollision(walls[j], robotPos)) {
141+
// addMessage("Collided with wall!!");
142+
// addMessage(`Position: (${robotPos.x.toFixed(3)}, ${robotPos.y.toFixed(3)}), Rotation: ${robotAngle}\n`);
143+
// return;
144+
// }
145+
// }
146+
147+
// if (distanceBetween(destPoint, robotPos) < robotRadius) {
148+
// robotPos = destPoint;
149+
// addMessage(`Robot moved forward by ${moveDist} at angle ${robotAngle} radians\n`);
150+
// addMessage(`Position: (${robotPos.x.toFixed(3)}, ${robotPos.y.toFixed(3)}), Rotation: ${robotAngle}\n`);
151+
// return;
152+
// }
153+
// }
156154

157155
}
158156

159-
160157
function checkWallCollision(wall: Wall, pos: Point): boolean {
161-
// // Apply the distance formula
162-
// const p1 = wall.p1;
163-
// const p2 = wall.p2;
158+
// // Apply the distance formula
159+
// const p1 = wall.p1;
160+
// const p2 = wall.p2;
161+
162+
// const numerator = Math.abs((p2.y - p1.y) * pos.x - (p2.x - p1.x) * pos.y + p2.x * p1.y - p2.y * p1.x);
163+
// const denominator = Math.sqrt((p2.y - p1.y) ** 2 + (p2.x - p1.x) ** 2);
164164

165-
// const numerator = Math.abs((p2.y - p1.y) * pos.x - (p2.x - p1.x) * pos.y + p2.x * p1.y - p2.y * p1.x);
166-
// const denominator = Math.sqrt((p2.y - p1.y) ** 2 + (p2.x - p1.x) ** 2);
167-
168-
// return numerator / denominator < robotRadius;
165+
// return numerator / denominator < robotRadius;
169166

170-
const p1 = wall.p1;
171-
const p2 = wall.p2;
167+
const p1 = wall.p1;
168+
const p2 = wall.p2;
172169

173-
const minX = Math.min(p1.x, p2.x);
174-
const maxX = Math.max(p1.x, p2.x);
175-
const minY = Math.min(p1.y, p2.y);
176-
const maxY = Math.max(p1.y, p2.y);
170+
const minX = Math.min(p1.x, p2.x);
171+
const maxX = Math.max(p1.x, p2.x);
172+
const minY = Math.min(p1.y, p2.y);
173+
const maxY = Math.max(p1.y, p2.y);
177174

178-
return pos.x >= minX && pos.x <= maxX && pos.y >= minY && pos.y <= maxY;
175+
return pos.x >= minX && pos.x <= maxX && pos.y >= minY && pos.y <= maxY;
179176
}
180177

181178
function alrCollided() {
182-
return !context.moduleContexts.robot_minigame.state.success;
179+
return !context.moduleContexts.robot_minigame.state.success;
183180
}

src/bundles/robot_minigame/index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,4 @@ export {
1717
turn_right,
1818
getX,
1919
getY
20-
} from "./functions";
21-
22-
23-
20+
} from './functions';

0 commit comments

Comments
 (0)