소스 검색

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 년 전
부모
커밋
b6fb226630
1개의 변경된 파일31개의 추가작업 그리고 2개의 파일을 삭제
  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);
     }