about summary refs log tree commit diff stats
path: root/app/assets/javascripts
diff options
context:
space:
mode:
Diffstat (limited to 'app/assets/javascripts')
-rw-r--r--app/assets/javascripts/trace2.js86
1 files changed, 78 insertions, 8 deletions
diff --git a/app/assets/javascripts/trace2.js b/app/assets/javascripts/trace2.js index 000e60b..20d050d 100644 --- a/app/assets/javascripts/trace2.js +++ b/app/assets/javascripts/trace2.js
@@ -521,6 +521,8 @@ window.onTraceStart = function(puzzle, pos, svg, start, symStart=null) {
521 data.sym = puzzle.getSymmetricalPos(pos.x, pos.y) 521 data.sym = puzzle.getSymmetricalPos(pos.x, pos.y)
522 data.puzzle = puzzle 522 data.puzzle = puzzle
523 data.path = [] 523 data.path = []
524 data.x_momentum = 0
525 data.y_momentum = 0
524 puzzle.startPoint = {'x': pos.x, 'y': pos.y} 526 puzzle.startPoint = {'x': pos.x, 'y': pos.y}
525 527
526 if (pos.x % 2 === 1) { // Start point is on a horizontal segment 528 if (pos.x % 2 === 1) { // Start point is on a horizontal segment
@@ -732,9 +734,9 @@ function push(dx, dy, dir, targetDir) {
732 // Fraction of movement to redirect in the other direction 734 // Fraction of movement to redirect in the other direction
733 var movementRatio = null 735 var movementRatio = null
734 if (targetDir === 'left' || targetDir === 'top') { 736 if (targetDir === 'left' || targetDir === 'top') {
735 movementRatio = -3 737 movementRatio = -1
736 } else if (targetDir === 'right' || targetDir === 'bottom') { 738 } else if (targetDir === 'right' || targetDir === 'bottom') {
737 movementRatio = 3 739 movementRatio = 1
738 } 740 }
739 if (window.settings.disablePushing === true) movementRatio *= 1000 741 if (window.settings.disablePushing === true) movementRatio *= 1000
740 742
@@ -779,6 +781,43 @@ function push(dx, dy, dir, targetDir) {
779// Redirect momentum from pushing against walls, so that all further moment steps 781// Redirect momentum from pushing against walls, so that all further moment steps
780// will be strictly linear. Returns a string for logging purposes only. 782// will be strictly linear. Returns a string for logging purposes only.
781function pushCursor(dx, dy) { 783function pushCursor(dx, dy) {
784 //if (Math.abs(dx) > Math.abs(dy)) {
785 // //data.y_momentum = 0
786 // if (dx > 0) {
787 // data.x_momentum = 1
788 // } else {
789 // data.x_momentum = -1
790 // }
791 //} else {
792 // //data.x_momentum = 0
793 // if (dy > 0) {
794 // data.y_momentum = 1
795 // } else {
796 // data.y_momentum = -1
797 // }
798 //}
799 var minimumMove = 0
800 if (Math.abs(dx) > minimumMove) {
801 //data.y_momentum = 0
802 if (dx > 0) {
803 data.x_momentum = 1
804 } else {
805 data.x_momentum = -1
806 }
807 } else if (Math.abs(dx) <= 1) {
808 data.x_momentum = 0
809 }
810 if (Math.abs(dy) > minimumMove) {
811 //data.x_momentum = 0
812 if (dy > 0) {
813 data.y_momentum = 1
814 } else {
815 data.y_momentum = -1
816 }
817 } else if (Math.abs(dy) <= 1) {
818 data.y_momentum = 0
819 }
820
782 // Outer wall collision 821 // Outer wall collision
783 var cell = data.puzzle.getCell(data.pos.x, data.pos.y) 822 var cell = data.puzzle.getCell(data.pos.x, data.pos.y)
784 if (cell == null) return 'nothing' 823 if (cell == null) return 'nothing'
@@ -787,29 +826,45 @@ function pushCursor(dx, dy) {
787 if ([undefined, 'top', 'bottom'].includes(cell.end)) { 826 if ([undefined, 'top', 'bottom'].includes(cell.end)) {
788 var leftCell = data.puzzle.getCell(data.pos.x - 1, data.pos.y) 827 var leftCell = data.puzzle.getCell(data.pos.x - 1, data.pos.y)
789 if (leftCell == null || leftCell.gap === window.GAP_FULL) { 828 if (leftCell == null || leftCell.gap === window.GAP_FULL) {
790 if (push(dx, dy, 'left', 'top')) return 'left outer wall' 829 if (data.y_momentum > 0) {
830 if (push(dx, dy, 'left', 'bottom')) return 'left outer wall, down'
831 } else {
832 if (push(dx, dy, 'left', 'top')) return 'left outer wall'
833 }
791 } 834 }
792 var rightCell = data.puzzle.getCell(data.pos.x + 1, data.pos.y) 835 var rightCell = data.puzzle.getCell(data.pos.x + 1, data.pos.y)
793 if (rightCell == null || rightCell.gap === window.GAP_FULL) { 836 if (rightCell == null || rightCell.gap === window.GAP_FULL) {
794 if (push(dx, dy, 'right', 'top')) return 'right outer wall' 837 if (data.y_momentum > 0) {
838 if (push(dx, dy, 'right', 'bottom')) return 'right outer wall, down'
839 } else {
840 if (push(dx, dy, 'right', 'top')) return 'right outer wall'
841 }
795 } 842 }
796 } 843 }
797 // Only consider non-endpoints or endpoints which are parallel 844 // Only consider non-endpoints or endpoints which are parallel
798 if ([undefined, 'left', 'right'].includes(cell.end)) { 845 if ([undefined, 'left', 'right'].includes(cell.end)) {
799 var topCell = data.puzzle.getCell(data.pos.x, data.pos.y - 1) 846 var topCell = data.puzzle.getCell(data.pos.x, data.pos.y - 1)
800 if (topCell == null || topCell.gap === window.GAP_FULL) { 847 if (topCell == null || topCell.gap === window.GAP_FULL) {
801 if (push(dx, dy, 'top', 'right')) return 'top outer wall' 848 if (data.x_momentum < 0) {
849 if (push(dx, dy, 'top', 'left')) return 'top outer wall, left'
850 } else {
851 if (push(dx, dy, 'top', 'right')) return 'top outer wall'
852 }
802 } 853 }
803 var bottomCell = data.puzzle.getCell(data.pos.x, data.pos.y + 1) 854 var bottomCell = data.puzzle.getCell(data.pos.x, data.pos.y + 1)
804 if (bottomCell == null || bottomCell.gap === window.GAP_FULL) { 855 if (bottomCell == null || bottomCell.gap === window.GAP_FULL) {
805 if (push(dx, dy, 'bottom', 'right')) return 'bottom outer wall' 856 if (data.x_momentum < 0) {
857 if (push(dx, dy, 'bottom', 'left')) return 'bottom outer wall, left'
858 } else {
859 if (push(dx, dy, 'bottom', 'right')) return 'bottom outer wall'
860 }
806 } 861 }
807 } 862 }
808 863
809 // Inner wall collision 864 // Inner wall collision
810 if (cell.end == null) { 865 if (cell.end == null) {
811 if (data.pos.x%2 === 1 && data.pos.y%2 === 0) { // Horizontal cell 866 if (data.pos.x%2 === 1 && data.pos.y%2 === 0) { // Horizontal cell
812 if (data.x < data.bbox.middle.x) { 867 if (data.x_momentum < 0 || (data.x_momentum == 0 && data.x < data.bbox.middle.x)) {
813 push(dx, dy, 'topbottom', 'left') 868 push(dx, dy, 'topbottom', 'left')
814 return 'topbottom inner wall, moved left' 869 return 'topbottom inner wall, moved left'
815 } else { 870 } else {
@@ -817,7 +872,7 @@ function pushCursor(dx, dy) {
817 return 'topbottom inner wall, moved right' 872 return 'topbottom inner wall, moved right'
818 } 873 }
819 } else if (data.pos.x%2 === 0 && data.pos.y%2 === 1) { // Vertical cell 874 } else if (data.pos.x%2 === 0 && data.pos.y%2 === 1) { // Vertical cell
820 if (data.y < data.bbox.middle.y) { 875 if (data.y_momentum < 0 || (data.y_momentum == 0 && data.y < data.bbox.middle.y)) {
821 push(dx, dy, 'leftright', 'top') 876 push(dx, dy, 'leftright', 'top')
822 return 'leftright inner wall, moved up' 877 return 'leftright inner wall, moved up'
823 } else { 878 } else {
@@ -832,6 +887,7 @@ function pushCursor(dx, dy) {
832 var turnMod = 2 887 var turnMod = 2
833 if ((data.pos.x%2 === 0 && data.pos.y%2 === 0) || cell.end != null) { 888 if ((data.pos.x%2 === 0 && data.pos.y%2 === 0) || cell.end != null) {
834 if (data.x < data.bbox.middle.x) { 889 if (data.x < data.bbox.middle.x) {
890 //if (data.x_momentum < 0 || (data.x_momentum == 0 && data.x < data.bbox.middle.x)) {
835 push(dx, dy, 'topbottom', 'right') 891 push(dx, dy, 'topbottom', 'right')
836 // Overshot the intersection and appears to be trying to turn 892 // Overshot the intersection and appears to be trying to turn
837 if (data.x > data.bbox.middle.x && Math.abs(dy) * turnMod > Math.abs(dx)) { 893 if (data.x > data.bbox.middle.x && Math.abs(dy) * turnMod > Math.abs(dx)) {
@@ -841,6 +897,7 @@ function pushCursor(dx, dy) {
841 } 897 }
842 return 'intersection moving right' 898 return 'intersection moving right'
843 } else if (data.x > data.bbox.middle.x) { 899 } else if (data.x > data.bbox.middle.x) {
900 //} else if (data.x_momentum > 0 || (data.x_momentum == 0 && data.x > data.bbox.middle.x)) {
844 push(dx, dy, 'topbottom', 'left') 901 push(dx, dy, 'topbottom', 'left')
845 // Overshot the intersection and appears to be trying to turn 902 // Overshot the intersection and appears to be trying to turn
846 if (data.x < data.bbox.middle.x && Math.abs(dy) * turnMod > Math.abs(dx)) { 903 if (data.x < data.bbox.middle.x && Math.abs(dy) * turnMod > Math.abs(dx)) {
@@ -851,6 +908,7 @@ function pushCursor(dx, dy) {
851 return 'intersection moving left' 908 return 'intersection moving left'
852 } 909 }
853 if (data.y < data.bbox.middle.y) { 910 if (data.y < data.bbox.middle.y) {
911 //if (data.y_momentum < 0 || (data.y_momentum == 0 && data.y < data.bbox.middle.y)) {
854 push(dx, dy, 'leftright', 'bottom') 912 push(dx, dy, 'leftright', 'bottom')
855 // Overshot the intersection and appears to be trying to turn 913 // Overshot the intersection and appears to be trying to turn
856 if (data.y > data.bbox.middle.y && Math.abs(dx) * turnMod > Math.abs(dy)) { 914 if (data.y > data.bbox.middle.y && Math.abs(dx) * turnMod > Math.abs(dy)) {
@@ -860,6 +918,7 @@ function pushCursor(dx, dy) {
860 } 918 }
861 return 'intersection moving down' 919 return 'intersection moving down'
862 } else if (data.y > data.bbox.middle.y) { 920 } else if (data.y > data.bbox.middle.y) {
921 //} else if (data.y_momentum > 0 || (data.y_momentum == 0 && data.y > data.bbox.middle.y)) {
863 push(dx, dy, 'leftright', 'top') 922 push(dx, dy, 'leftright', 'top')
864 // Overshot the intersection and appears to be trying to turn 923 // Overshot the intersection and appears to be trying to turn
865 if (data.y < data.bbox.middle.y && Math.abs(dx) * turnMod > Math.abs(dy)) { 924 if (data.y < data.bbox.middle.y && Math.abs(dx) * turnMod > Math.abs(dy)) {
@@ -923,6 +982,9 @@ function hardCollision() {
923 } else if (lastDir === MOVE_BOTTOM) { 982 } else if (lastDir === MOVE_BOTTOM) {
924 data.y = Math.min(data.y, data.bbox.middle.y - gapSize) 983 data.y = Math.min(data.y, data.bbox.middle.y - gapSize)
925 } 984 }
985
986 data.x_momentum = 0
987 data.y_momentum = 0
926} 988}
927 989
928// Check to see if we've gone beyond the edge of puzzle cell, and if the next cell is safe, 990// Check to see if we've gone beyond the edge of puzzle cell, and if the next cell is safe,
@@ -947,6 +1009,8 @@ function move() {
947 } 1009 }
948 } 1010 }
949 if (data.x < data.bbox.x1) { 1011 if (data.x < data.bbox.x1) {
1012 //data.x_momentum = -1
1013 //data.y_momentum = 0
950 return MOVE_LEFT 1014 return MOVE_LEFT
951 } 1015 }
952 } else if (data.x > data.bbox.x2 - 12) { // Moving right 1016 } else if (data.x > data.bbox.x2 - 12) { // Moving right
@@ -965,6 +1029,8 @@ function move() {
965 } 1029 }
966 } 1030 }
967 if (data.x > data.bbox.x2) { 1031 if (data.x > data.bbox.x2) {
1032 //data.x_momentum = 1
1033 //data.y_momentum = 0
968 return MOVE_RIGHT 1034 return MOVE_RIGHT
969 } 1035 }
970 } else if (data.y < data.bbox.y1 + 12) { // Moving up 1036 } else if (data.y < data.bbox.y1 + 12) { // Moving up
@@ -983,6 +1049,8 @@ function move() {
983 } 1049 }
984 } 1050 }
985 if (data.y < data.bbox.y1) { 1051 if (data.y < data.bbox.y1) {
1052 //data.x_momentum = 0
1053 //data.y_momentum = -1
986 return MOVE_TOP 1054 return MOVE_TOP
987 } 1055 }
988 } else if (data.y > data.bbox.y2 - 12) { // Moving down 1056 } else if (data.y > data.bbox.y2 - 12) { // Moving down
@@ -1001,6 +1069,8 @@ function move() {
1001 } 1069 }
1002 } 1070 }
1003 if (data.y > data.bbox.y2) { 1071 if (data.y > data.bbox.y2) {
1072 //data.x_momentum = 0
1073 //data.y_momentum = 1
1004 return MOVE_BOTTOM 1074 return MOVE_BOTTOM
1005 } 1075 }
1006 } 1076 }