Skip to content

Commit

Permalink
update position and index in drive command
Browse files Browse the repository at this point in the history
  • Loading branch information
xXDMOGXx committed Nov 19, 2024
1 parent 0391926 commit 71e4d62
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/server/command/move-command.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { RobotCommand, Reversible } from "./command";
import { Position } from "../robot/position";
import { robotManager } from "../api/managers";
import { GridIndices } from "../robot/grid-indices";

/**
* Represents a rotation.
Expand Down Expand Up @@ -102,6 +103,17 @@ export class DriveCommand

public async execute(): Promise<void> {
const robot = robotManager.getRobot(this.robotId);
const currentPosition = robot.position;
const newPositionX = this.tileDistance * Math.cos(robot.headingRadians);
const newPositionY = this.tileDistance * Math.sin(robot.headingRadians);
robot.position = new Position(
newPositionX + currentPosition.x,
newPositionY + currentPosition.y,
);
robotManager.updateRobot(
this.robotId,
new GridIndices(Math.floor(newPositionX), Math.floor(newPositionY)),
);
return robot.sendDrivePacket(this.tileDistance);
}

Expand Down
4 changes: 2 additions & 2 deletions src/server/robot/robot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ export class Robot {
return this._position;
}

private set position(coords: Position) {
public set position(coords: Position) {
this._position = coords;
}

public get headingRadians(): number {
return this._headingRadians;
}

private set headingRadians(headingRadians: number) {
public set headingRadians(headingRadians: number) {
this._headingRadians = headingRadians;
}

Expand Down

0 comments on commit 71e4d62

Please sign in to comment.