|
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 |
3 | 4 | */ |
4 | 5 |
|
5 | 6 | import context from 'js-slang/context'; |
6 | 7 |
|
7 | | -let robotPos: Point = {x: 0, y: 0}; |
8 | | - |
9 | | -let movePoints: Point[]; |
| 8 | +const robotPos: Point = {x: 0, y: 0}; |
10 | 9 |
|
11 | 10 | 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 |
16 | 15 | }; |
17 | 16 |
|
18 | 17 | let robotRotation = 1; |
19 | 18 |
|
20 | 19 | // default grid width and height is 25 |
21 | 20 | 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 | +}; |
30 | 29 |
|
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}; |
33 | 32 |
|
34 | 33 | export function set_pos(x: number, y: number): void { |
35 | 34 | robotPos.x = x; |
36 | 35 | robotPos.y = y; |
37 | 36 | } |
38 | 37 |
|
39 | 38 | export function set_grid_width(width: number) { |
40 | | - context.moduleContexts.robot_minigame.state.width = width; |
| 39 | + context.moduleContexts.robot_minigame.state.width = width; |
41 | 40 | } |
42 | 41 |
|
43 | 42 | export function set_grid_height(height: number) { |
44 | | - context.moduleContexts.robot_minigame.state.height = height; |
| 43 | + context.moduleContexts.robot_minigame.state.height = height; |
45 | 44 | } |
46 | 45 |
|
47 | 46 | 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; |
53 | 52 | } |
54 | 53 |
|
55 | 54 | export function turn_left() { |
56 | | - if (alrCollided()) return; |
| 55 | + if (alrCollided()) return; |
57 | 56 |
|
58 | | - robotRotation -= 1; |
59 | | - if (robotRotation < 0) { |
60 | | - robotRotation = 3; |
61 | | - } |
| 57 | + robotRotation -= 1; |
| 58 | + if (robotRotation < 0) { |
| 59 | + robotRotation = 3; |
| 60 | + } |
62 | 61 | } |
63 | 62 |
|
64 | 63 | export function turn_right() { |
65 | | - if (alrCollided()) return; |
| 64 | + if (alrCollided()) return; |
66 | 65 |
|
67 | | - robotRotation = (robotRotation + 1) % 4; |
| 66 | + robotRotation = (robotRotation + 1) % 4; |
68 | 67 | } |
69 | 68 |
|
70 | | -// takes the top left and bottom right corners of walls |
| 69 | +// takes the top left and bottom right corners of walls |
71 | 70 | // in terms of grid boxes |
72 | 71 | // grid starts from (0, 0) at the top left corner btw |
73 | 72 | 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); |
76 | 75 | } |
77 | 76 |
|
78 | 77 | export function move_forward(dist: number): void { |
79 | | - if (alrCollided()) return; |
80 | | - |
81 | | - simulate(dist); |
| 78 | + if (alrCollided()) return; |
| 79 | + |
| 80 | + simulate(dist); |
82 | 81 | } |
83 | 82 |
|
84 | 83 | export function getX():number { |
85 | | - return robotPos.x; |
| 84 | + return robotPos.x; |
86 | 85 | } |
87 | 86 |
|
88 | 87 | export function getY():number { |
89 | | - return robotPos.y; |
| 88 | + return robotPos.y; |
90 | 89 | } |
91 | 90 |
|
92 | 91 | 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 | + } |
124 | 122 | } |
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 | + // } |
156 | 154 |
|
157 | 155 | } |
158 | 156 |
|
159 | | - |
160 | 157 | 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); |
164 | 164 |
|
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; |
169 | 166 |
|
170 | | - const p1 = wall.p1; |
171 | | - const p2 = wall.p2; |
| 167 | + const p1 = wall.p1; |
| 168 | + const p2 = wall.p2; |
172 | 169 |
|
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); |
177 | 174 |
|
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; |
179 | 176 | } |
180 | 177 |
|
181 | 178 | function alrCollided() { |
182 | | - return !context.moduleContexts.robot_minigame.state.success; |
| 179 | + return !context.moduleContexts.robot_minigame.state.success; |
183 | 180 | } |
0 commit comments