refactor: synchronize turtle facing state with global variable on turn commands
This commit is contained in:
@@ -406,15 +406,15 @@ export class Turtle extends EventEmitter {
|
||||
|
||||
const turn = (direction - this._facing + 4) % 4;
|
||||
if (turn === 1) {
|
||||
await this.exec('return turtle.turnRight()');
|
||||
await this.exec(`turtle.turnRight(); _G._turtleFacing = ${direction}`);
|
||||
this._facing = direction;
|
||||
this._emitUpdate();
|
||||
} else if (turn === 2) {
|
||||
await this.exec('turtle.turnLeft(); return turtle.turnLeft()');
|
||||
await this.exec(`turtle.turnLeft(); turtle.turnLeft(); _G._turtleFacing = ${direction}`);
|
||||
this._facing = direction;
|
||||
this._emitUpdate();
|
||||
} else if (turn === 3) {
|
||||
await this.exec('return turtle.turnLeft()');
|
||||
await this.exec(`turtle.turnLeft(); _G._turtleFacing = ${direction}`);
|
||||
this._facing = direction;
|
||||
this._emitUpdate();
|
||||
}
|
||||
@@ -427,6 +427,8 @@ export class Turtle extends EventEmitter {
|
||||
const result = await this.exec('return turtle.turnLeft()');
|
||||
if (result === true || (Array.isArray(result) && result[0] === true)) {
|
||||
this._facing = (this._facing + 3) % 4;
|
||||
// Sync facing on turtle side
|
||||
this.exec(`_G._turtleFacing = ${this._facing}`).catch(() => {});
|
||||
this._emitUpdate();
|
||||
}
|
||||
return result;
|
||||
@@ -439,6 +441,8 @@ export class Turtle extends EventEmitter {
|
||||
const result = await this.exec('return turtle.turnRight()');
|
||||
if (result === true || (Array.isArray(result) && result[0] === true)) {
|
||||
this._facing = (this._facing + 1) % 4;
|
||||
// Sync facing on turtle side
|
||||
this.exec(`_G._turtleFacing = ${this._facing}`).catch(() => {});
|
||||
this._emitUpdate();
|
||||
}
|
||||
return result;
|
||||
|
||||
Reference in New Issue
Block a user