Pārlūkot izejas kodu

Add tracking of code boxes boundaries

In order to allow the fish to swim from one side of the box to another,
added tracking of the boxes upper bounds in both directions.
ApisNecros 1 gadu atpakaļ
vecāks
revīzija
b6fb226630
1 mainītis faili ar 31 papildinājumiem un 2 dzēšanām
  1. 31 2
      starfish.js

+ 31 - 2
starfish.js

@@ -25,6 +25,17 @@ class CodeBox {
          * @type {Array|Array[]}
          */
         this.box = [];
+        /**
+         * The farthest right the box goes
+         * @type {int}
+         */
+        this.maxBoxWidth = 0;
+        /**
+         * The bottom of the box
+         * 
+         * @type {int}
+         */
+        this.maxBoxHeight = 0;
         /**
          * The coordinates of the currently executing instruction inside the code box
          * @type {int[]}
@@ -103,6 +114,9 @@ class CodeBox {
         }
 
         this.EqualizeBoxWidth(maxRowLength);
+
+        this.maxBoxWidth = maxRowLength - 1;
+        this.maxBoxHeight = this.box.length - 1;
         
         let fin = null;
 
@@ -243,8 +257,23 @@ class CodeBox {
     }
 
     Move() {
-        const newX = this.pointer[0] + this.curr_direction[0];
-        const newY = this.pointer[1] + this.curr_direction[1];
+        let newX = this.pointer[0] + this.curr_direction[0];
+        let newY = this.pointer[1] + this.curr_direction[1];
+
+        // Keep the X coord in the boxes bounds
+        if(newX < 0) {
+            newX = this.maxBoxWidth;
+        }
+        else if(newX > this.maxBoxWidth) {
+            newX = 0;
+        }
+        // Keep the Y coord in the boxes bounds
+        if(newY < 0) {
+            newY = this.maxBoxHeight;
+        }
+        else if(newY > this.maxBoxHeight) {
+            newY = 0;
+        }
 
         this.SetPointer(newX, newY);
     }