Skip to content

Commit aef74d5

Browse files
committed
updated to command system, includes move, rotate and sensor
1 parent d149702 commit aef74d5

File tree

3 files changed

+168
-48
lines changed

3 files changed

+168
-48
lines changed

src/bundles/robot_minigame/functions.ts

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import {
66
} from 'js-slang/dist/stdlib/list';
77

88
type Point = {x: number, y: number};
9-
type PointWithRotation = {x: number, y: number, angle: number};
9+
type PointWithRotation = {x: number, y: number, rotation: number};
1010

11-
type CommandData = {
11+
type Command = {
1212
type: string,
13-
location: PointWithRotation
13+
position: PointWithRotation
1414
};
1515

1616
type Polygon = Point[];
@@ -20,10 +20,11 @@ type StateData = {
2020
width: number,
2121
height: number,
2222
walls: Polygon[],
23-
movePoints: Point[],
23+
moveCommands: Command[],
2424
message: string,
2525
success: boolean,
26-
messages: string[]
26+
messages: string[],
27+
robotSize: number
2728
};
2829

2930
type Robot = {
@@ -39,18 +40,19 @@ const stateData: StateData = {
3940
width: 500,
4041
height: 500,
4142
walls: [],
42-
movePoints: [],
43+
moveCommands: [],
4344
message: 'moved successfully',
4445
success: true,
45-
messages: []
46+
messages: [],
47+
robotSize: 15
4648
};
4749

4850
const robot: Robot = {
4951
x: 25, // default start pos, puts it at the top left corner of canvas without colliding with the walls
5052
y: 25,
5153
dx: 1,
5254
dy: 0,
53-
radius: 20 // give the robot a circular hitbox
55+
radius: 15 // give the robot a circular hitbox
5456
};
5557

5658
let bounds: Point[] = [];
@@ -63,6 +65,12 @@ export function set_pos(x: number, y: number): void {
6365
robot.y = y;
6466
}
6567

68+
export function set_rotation(rotation: number) {
69+
robot.dx = Math.cos(rotation);
70+
robot.dy = -Math.sin(rotation);
71+
}
72+
73+
6674
export function set_width(width: number) {
6775
stateData.width = width;
6876
}
@@ -72,14 +80,15 @@ export function set_height(height: number) {
7280
}
7381

7482
// condenses setting the width and height of map, and the initial position of robot in one call
75-
export function init(width: number, height: number, posX: number, posY: number) {
83+
export function init(width: number, height: number, posX: number, posY: number, rotation: number) {
7684
if (stateData.isInit) return; // dont allow init more than once
7785

7886
set_width(width);
7987
set_height(height);
8088
set_pos(posX, posY);
89+
set_rotation(rotation);
8190

82-
stateData.movePoints.push({x: posX, y: posY}); // push starting point to movepoints data
91+
stateData.moveCommands.push({type: "begin", position: getPositionWithRotation()}); // push starting point to movepoints data
8392
stateData.isInit = true;
8493

8594
bounds = [
@@ -102,10 +111,13 @@ export function turn_left() {
102111
if (robot.dx < 0.00001 && robot.dx > -0.00001) robot.dx = 0;
103112
if (robot.dy < 0.00001 && robot.dy > -0.00001) robot.dy = 0;
104113

114+
stateData.moveCommands.push({type: "rotateLeft", position: getPositionWithRotation()});
115+
105116
// debug log
106117
logCoordinates();
107118
}
108119

120+
109121
export function turn_right() {
110122
let currentAngle = Math.atan2(-robot.dy, robot.dx);
111123

@@ -117,6 +129,8 @@ export function turn_right() {
117129
if (robot.dx < 0.00001 && robot.dx > -0.00001) robot.dx = 0;
118130
if (robot.dy < 0.00001 && robot.dy > -0.00001) robot.dy = 0;
119131

132+
stateData.moveCommands.push({type: "rotateRight", position: getPositionWithRotation()});
133+
120134
// debug log
121135
logCoordinates();
122136
}
@@ -132,6 +146,8 @@ export function rotate_right(angle: number) {
132146
if (robot.dx < 0.00001 && robot.dx > -0.00001) robot.dx = 0;
133147
if (robot.dy < 0.00001 && robot.dy > -0.00001) robot.dy = 0;
134148

149+
stateData.moveCommands.push({type: "rotateRight", position: getPositionWithRotation()});
150+
135151
// debug log
136152
logCoordinates();
137153
}
@@ -147,6 +163,8 @@ export function rotate_left(angle: number) {
147163
if (robot.dx < 0.00001 && robot.dx > -0.00001) robot.dx = 0;
148164
if (robot.dy < 0.00001 && robot.dy > -0.00001) robot.dy = 0;
149165

166+
stateData.moveCommands.push({type: "rotateLeft", position: getPositionWithRotation()});
167+
150168
logCoordinates();
151169
}
152170

@@ -188,7 +206,7 @@ export function getY():number {
188206
export function move_forward_to_wall(): void {
189207
if (alrCollided()) return;
190208

191-
let distance = findMoveDistance(); // do the raycast, figure out how far the robot is from the nearest wall
209+
let distance = findDistanceToWall(); // do the raycast, figure out how far the robot is from the nearest wall
192210

193211
// a lil extra offset from wall
194212
distance = Math.max(distance - robot.radius - 5, 0);
@@ -200,34 +218,49 @@ export function move_forward_to_wall(): void {
200218

201219
robot.x = nextPoint.x;
202220
robot.y = nextPoint.y;
203-
stateData.movePoints.push(nextPoint);
221+
stateData.moveCommands.push({type: "move", position: getPositionWithRotation()});
204222

205223
// for debug
206224
stateData.messages.push(`Distance is ${distance} Collision point at x: ${nextPoint.x}, y: ${nextPoint.y}`);
207225
}
208226

209-
// Moves forward by a small amount
227+
// Moves forward by a specified amount
210228
export function move_forward(moveDist: number): void {
229+
// need to check for collision with wall
230+
const dist = findDistanceToWall();
231+
stateData.messages.push(`${dist}`);
232+
233+
if (dist < moveDist + robot.radius) {
234+
stateData.message = "collided";
235+
stateData.success = false;
236+
moveDist = dist - robot.radius + 1; // move only until the wall
237+
}
238+
211239
const nextPoint: Point = {
212240
x: robot.x + moveDist * robot.dx,
213-
y: robot.y + moveDist + robot.dy
241+
y: robot.y + moveDist * robot.dy
214242
};
215243

216-
// need to check for collision with wall
217-
218244
robot.x = nextPoint.x;
219245
robot.y = nextPoint.y;
220-
stateData.movePoints.push(nextPoint);
246+
stateData.moveCommands.push({type: "move", position: getPositionWithRotation()});
221247

222248
logCoordinates();
223249
}
224250

251+
// detects if there are walls 10 units ahead of the robot
252+
// add as a command later
225253
export function sensor(): boolean {
254+
const dist = findDistanceToWall();
255+
stateData.moveCommands.push({type: "sensor", position: getPositionWithRotation()})
256+
if (dist <= 10 + robot.radius) {
257+
return true;
258+
}
226259
return false;
227260
}
228261

229262
// returns the distance from the nearest wall
230-
function findMoveDistance(): number {
263+
function findDistanceToWall(): number {
231264
let minDist: number = Infinity;
232265

233266
// loop through all the walls
@@ -328,6 +361,11 @@ function alrCollided() {
328361
return !stateData.success;
329362
}
330363

364+
function getPositionWithRotation(): PointWithRotation {
365+
const angle = Math.atan2(-robot.dy, robot.dx);
366+
return {x: robot.x, y: robot.y, rotation: angle}
367+
}
368+
331369
// debug
332370
function logCoordinates() {
333371
stateData.messages.push(`x: ${robot.x}, y: ${robot.y}, dx: ${robot.dx}, dy: ${robot.dy}`);

src/bundles/robot_minigame/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
export {
1212
init,
1313
set_pos,
14+
set_rotation,
1415
set_rect_wall,
1516
set_polygon_wall,
1617
move_forward,

0 commit comments

Comments
 (0)