Skip to content

Commit

Permalink
Add methods to initialize and access turtleList, and refactor turtle …
Browse files Browse the repository at this point in the history
…access methods for improved code consistency and readability. (#4370)

* Improve variable comments in TurtlesView for better code clarity

* Add methods to initialize and access turtleList in TurtlesModel

* Refactor turtle management to use getter methods for improved consistency and readability

* Refactor turtle access methods to improve code consistency and readability

* Remove redundant start script from package.json
  • Loading branch information
Ashrafmuhmed authored Feb 11, 2025
1 parent 93c40c8 commit a0d4ece
Show file tree
Hide file tree
Showing 25 changed files with 333 additions and 297 deletions.
4 changes: 2 additions & 2 deletions js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ used with the `Add-1` and `Add to` blocks.

```
setter(logo, value, turtle, blk) {
logo.turtles.turtleList[turtle].painter.doSetHeading(value);
logo.turtles.getTurtle(turtle).painter.doSetHeading(value);
}
```

Expand Down Expand Up @@ -370,7 +370,7 @@ class StartDrumBlock extends StartBlock {
if (logo.inStatusMatrix && logo.blocks.blockList[logo.blocks.blockList[blk].connections[0]].name === 'print') {
logo.statusFields.push([blk, 'x']);
} else {
return logo.turtles.screenX2turtleX(logo.turtles.turtleList[turtle].container.x);
return logo.turtles.screenX2turtleX(logo.turtles.getTurtle(turtle).container.x);
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions js/SaveInterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,10 +364,10 @@ class SaveInterface {
activity.logo.runningAbc = true;
activity.logo.notationOutput = ABCHEADER;
activity.logo.notationNotes = {};
for (let t = 0; t < activity.turtles.turtleList.length; t++) {
for (let t = 0; t < activity.turtles.getTurtleCount(); t++) {
activity.logo.notation.notationStaging[t] = [];
activity.logo.notation.notationDrumStaging[t] = [];
activity.turtles.turtleList[t].painter.doClear(true, true, true);
activity.turtles.getTurtle(t).painter.doClear(true, true, true);
}
activity.logo.runLogoCommands();
}
Expand Down Expand Up @@ -523,10 +523,10 @@ class SaveInterface {
}
this.activity.logo.notationOutput = lyheader;
this.activity.logo.notationNotes = {};
for (let t = 0; t < this.activity.turtles.turtleList.length; t++) {
for (let t = 0; t < this.activity.turtles.getTurtleCount(); t++) {
this.activity.logo.notation.notationStaging[t] = [];
this.activity.logo.notation.notationDrumStaging[t] = [];
this.activity.turtles.turtleList[t].painter.doClear(true, true, true);
this.activity.turtles.getTurtle(t).painter.doClear(true, true, true);
}
document.body.style.cursor = "wait";
this.activity.logo.runLogoCommands();
Expand Down Expand Up @@ -636,10 +636,10 @@ class SaveInterface {
// eslint-disable-next-line no-unused-vars
saveMxml(filename) {
this.activity.logo.runningMxml = true;
for (let t = 0; t < this.activity.turtles.turtleList.length; t++) {
for (let t = 0; t < this.activity.turtles.getTurtleCount(); t++) {
this.activity.logo.notation.notationStaging[t] = [];
this.activity.logo.notation.notationDrumStaging[t] = [];
this.activity.turtles.turtleList[t].painter.doClear(true, true, true);
this.activity.turtles.getTurtle(t).painter.doClear(true, true, true);
}

this.activity.logo.runLogoCommands();
Expand Down
41 changes: 21 additions & 20 deletions js/activity.js
Original file line number Diff line number Diff line change
Expand Up @@ -1035,12 +1035,13 @@ class Activity {

// Return mice to the center of the screen.
// Reset turtles' positions to center of the screen
for (let turtle = 0; turtle < this.turtles.turtleList.length; turtle++) {
const savedPenState = this.turtles.turtleList[turtle].painter.penState;
this.turtles.turtleList[turtle].painter.penState = false;
this.turtles.turtleList[turtle].painter.doSetXY(0, 0);
this.turtles.turtleList[turtle].painter.doSetHeading(0);
this.turtles.turtleList[turtle].painter.penState = savedPenState;
for (let turtle = 0; turtle < this.turtles.getTurtleCount(); turtle++) {
const requiredTurtle = this.turtles.getTurtle(turtle);
const savedPenState = requiredTurtle.painter.penState;
requiredTurtle.painter.penState = false;
requiredTurtle.painter.doSetXY(0, 0);
requiredTurtle.painter.doSetHeading(0);
requiredTurtle.painter.penState = savedPenState;
}
// Alternate mode switching on clicking Home button
this._isFirstHomeClick = !this._isFirstHomeClick;
Expand Down Expand Up @@ -1410,13 +1411,13 @@ class Activity {
this.turtles.setBackgroundColor(-1);
this.logo.svgOutput = "";
this.logo.notationOutput = "";
for (let turtle = 0; turtle < this.turtles.turtleList.length; turtle++) {
for (let turtle = 0; turtle < this.turtles.getTurtleCount(); turtle++) {
this.logo.turtleHeaps[turtle] = [];
this.logo.turtleDicts[turtle] = {};
this.logo.notation.notationStaging[turtle] = [];
this.logo.notation.notationDrumStaging[turtle] = [];
if (noErase === undefined || !noErase) {
this.turtles.turtleList[turtle].painter.doClear(true, true, true);
this.turtles.getTurtle(turtle).painter.doClear(true, true, true);
}
}

Expand Down Expand Up @@ -3566,8 +3567,8 @@ class Activity {
this.palettes.setMobile(false);
}

for (let turtle = 0; turtle < this.turtles.turtleList.length; turtle++) {
this.turtles.turtleList[turtle].painter.doClear(false, false, true);
for (let turtle = 0; turtle < this.turtles.getTurtleCount(); turtle++) {
this.turtles.getTurtle(turtle).painter.doClear(false, false, true);
}

const artcanvas = docById("overlayCanvas");
Expand Down Expand Up @@ -3715,8 +3716,8 @@ class Activity {

if (restoredBlock.name === 'start' || restoredBlock.name === 'drum') {
const turtle = restoredBlock.value;
this.turtles.turtleList[turtle].inTrash = false;
this.turtles.turtleList[turtle].container.visible = true;
this.turtles.getTurtle(turtle).inTrash = false;
this.turtles.getTurtle(turtle).container.visible = true;
} else if (restoredBlock.name === 'action') {
const actionArg = this.blocks.blockList[restoredBlock.connections[1]];
if (actionArg !== null) {
Expand Down Expand Up @@ -3974,8 +3975,8 @@ class Activity {
) {
const turtle = this.blocks.blockList[blk].value;
if (!this.blocks.blockList[blk].trash && turtle !== null) {
this.turtles.turtleList[turtle].inTrash = true;
this.turtles.turtleList[turtle].container.visible = false;
this.turtles.getTurtle(turtle).inTrash = true;
this.turtles.getTurtle(turtle).container.visible = false;
}
} else if (this.blocks.blockList[blk].name === "action") {
if (!this.blocks.blockList[blk].trash) {
Expand Down Expand Up @@ -4878,12 +4879,12 @@ class Activity {
const __afterLoad = async () => {
if (!that.turtles.running()) {
that.stage.update(event);
for (let turtle = 0; turtle < that.turtles.turtleList.length; turtle++) {
for (let turtle = 0; turtle < that.turtles.getTurtleCount(); turtle++) {
that.logo.turtleHeaps[turtle] = [];
that.logo.turtleDicts[turtle] = {};
that.logo.notation.notationStaging[turtle] = [];
that.logo.notation.notationDrumStaging[turtle] = [];
that.turtles.turtleList[turtle].painter.doClear(true, true, false);
that.turtles.getTurtle(turtle).painter.doClear(true, true, false);
}
if (_THIS_IS_MUSIC_BLOCKS_) {
const imgUrl =
Expand Down Expand Up @@ -5008,8 +5009,8 @@ class Activity {
}

if (run && that.firstRun) {
for (let turtle = 0; turtle < that.turtles.turtleList.length; turtle++) {
that.turtles.turtleList[turtle].painter.doClear(true, true, false);
for (let turtle = 0; turtle < that.turtles.getTurtleCount(); turtle++) {
that.turtles.getTurtle(turtle).painter.doClear(true, true, false);
}

that.textMsg(_("Click the run button to run the project."));
Expand Down Expand Up @@ -6013,10 +6014,10 @@ class Activity {
case "drum":
// Find the turtle associated with this block.
// eslint-disable-next-line no-case-declarations
const turtle = this.turtles.turtleList[myBlock.value];
const turtle = this.turtles.getTurtle(myBlock.value);
if (turtle === null || turtle === undefined) {
args = {
id: this.turtles.turtleList.length,
id: this.turtles.getTurtleCount(),
collapsed: false,
xcor: 0,
ycor: 0,
Expand Down
12 changes: 6 additions & 6 deletions js/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -738,9 +738,9 @@ class Block {

if (this.name === "start" || this.name === "drum") {
// Rescale the decoration on the start blocks.
for (let t = 0; t < this.activity.turtles.turtleList.length; t++) {
if (this.activity.turtles.turtleList[t].startBlock === this) {
this.activity.turtles.turtleList[t].resizeDecoration(
for (let t = 0; t < this.activity.turtles.getTurtleCount(); t++) {
if (this.activity.turtles.getTurtle(t).startBlock === this) {
this.activity.turtles.getTurtle(t).resizeDecoration(
scale,
this.bitmap.image.width
);
Expand Down Expand Up @@ -3323,9 +3323,9 @@ class Block {
dx = (25 * this.protoblock.scale) / 2;
}

for (let t = 0; t < this.activity.turtles.turtleList.length; t++) {
if (this.activity.turtles.turtleList[t].startBlock === this) {
this.activity.turtles.turtleList[t].decorationBitmap.x =
for (let t = 0; t < this.activity.turtles.getTurtleCount(); t++) {
if (this.activity.turtles.getTurtle(t).startBlock === this) {
this.activity.turtles.getTurtle(t).decorationBitmap.x =
this.width - dx - (30 * this.protoblock.scale) / 2;
break;
}
Expand Down
4 changes: 2 additions & 2 deletions js/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -3232,7 +3232,7 @@ class Blocks {
let thisBlock = this.blockList.length;
if (name === "start") {
postProcess = (thisBlock) => {
that.blockList[thisBlock].value = that.turtles.turtleList.length;
that.blockList[thisBlock].value = that.turtles.getTurtleCount();
that.turtles.addTurtle(that.blockList[thisBlock]);
};

Expand Down Expand Up @@ -5802,7 +5802,7 @@ class Blocks {
postProcess = (args) => {
const thisBlock = args[0];
const blkInfo = args[1];
that.blockList[thisBlock].value = that.turtles.turtleList.length;
that.blockList[thisBlock].value = that.turtles.getTurtleCount();
that.turtles.addTurtle(that.blockList[thisBlock], blkInfo);
};

Expand Down
12 changes: 6 additions & 6 deletions js/blocks/ActionBlocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,14 +326,14 @@ function setupActionBlocks(activity) {
actionArgs = receivedArg;

if (name in logo.actions) {
activity.turtles.turtleList[turtle].running = true;
activity.turtles.getTurtle(turtle).running = true;
logo.runFromBlockNow(
logo,
turtle,
logo.actions[name],
true,
actionArgs,
activity.turtles.turtleList[turtle].queue.length
activity.turtles.getTurtle(turtle).queue.length
);
return logo.returns[turtle].shift();
} else {
Expand Down Expand Up @@ -551,14 +551,14 @@ function setupActionBlocks(activity) {

if (name in logo.actions) {
// Just run the stack.
activity.turtles.turtleList[turtle].running = true;
activity.turtles.getTurtle(turtle).running = true;
logo.runFromBlockNow(
logo,
turtle,
logo.actions[name],
true,
actionArgs,
activity.turtles.turtleList[turtle].queue.length
activity.turtles.getTurtle(turtle).queue.length
);
return logo.returns[turtle].pop();
} else {
Expand Down Expand Up @@ -748,14 +748,14 @@ function setupActionBlocks(activity) {
} else {
const name = logo.parseArg(logo, turtle, cblk, blk, receivedArg);
if (name in logo.actions) {
activity.turtles.turtleList[turtle].running = true;
activity.turtles.getTurtle(turtle).running = true;
logo.runFromBlockNow(
logo,
turtle,
logo.actions[name],
true,
actionArgs,
activity.turtles.turtleList[turtle].queue.length
activity.turtles.getTurtle(turtle).queue.length
);
return logo.returns[turtle].pop();
} else {
Expand Down
Loading

0 comments on commit a0d4ece

Please sign in to comment.