Last active
August 29, 2015 14:16
-
-
Save JogoShugh/bd614fdfef4e62de56ee to your computer and use it in GitHub Desktop.
boxStep with Promises
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For boxStep: | |
/* | |
* Returns a Promise or a function that returns a Promise to do a single rectangular | |
* job or a straight line when the height or width is only 1 | |
* When height and width are both 0, then just resolves immediately. | |
*/ | |
function get(startX, startY, width, height, startNow) { | |
width--; | |
height--; | |
var travel = function(resolve, reject) { | |
if (width < 1 && height < 1) resolve(); | |
else if (width >= 1 && height >= 1) { | |
game.player.move(startX + ' ' + startY, width+' r', height+' d', width+' l', height+' u', resolve); | |
} | |
else if (width === 0) { | |
game.player.move(startX + ' ' + startY, height+' d', resolve); | |
} | |
else if (height === 0) { | |
game.player.move(startX + ' ' + startY, width+' r', resolve); | |
} | |
}; | |
if (startNow === true) return new Promise(travel); | |
else return function() { return new Promise(travel); } | |
} | |
// Use training?id=rectangles | |
// Test multiple gets then'd together: | |
get(0, 0, 2, 12, true) | |
.then(get(3, 0, 2, 12)) | |
.then(get(6, 0, 2, 12)) | |
.then(get(9, 0, 2, 12)) | |
.then(get(12, 0, 2, 12)) | |
.then(get(15, 0, 2, 12)); | |
// Processes an array of Promises in succession | |
function run(steps, done) { | |
steps.reduce(function(current, next){ | |
return current.then(next); | |
}, Promise.resolve()) | |
.then(function() { | |
if (done) done(); | |
}); | |
} | |
// Test similar sequence as before, but with more | |
run([ | |
get(0, 0, 2, 12), | |
get(3, 0, 2, 12), | |
get(6, 0, 2, 12), | |
get(9, 0, 2, 12), | |
get(12, 0, 2, 12), | |
get(15, 0, 2, 12) | |
]); | |
// Now do for rows | |
run([ | |
get(0, 0, 5, 1), | |
get(0, 2, 5, 1), | |
get(0, 4, 5, 1), | |
get(0, 6, 5, 1), | |
get(0, 8, 5, 1), | |
get(0, 10, 5, 1) | |
]); | |
// Try getting rows without manually creating each one! | |
function getRow(distanceY, width, height) { | |
return function(rowNumber) { | |
return get(0, rowNumber * distanceY, width, height); | |
}; | |
} | |
run([0, 1, 2, 3, 4, 5].map(getRow(2, 5, 1))); | |
/* | |
// Now for columns | |
function getCol(distanceX, width, height) { | |
return function(colNumber) { | |
return get(colNumber * distanceX, width, height); | |
} | |
} | |
run([0, 1, 2, 3, 4, 5].map(getCol(2, 1, 5))); | |
*/ | |
// While based approach to populate an array of promises | |
// to feed off to run(). This can handle 6 columns of 2 wide and 12 high | |
var count = 0; | |
var groups = []; | |
while(count < 6) { | |
groups[count] = get(count * 3, 0, 2, 12); | |
count = count + 1; | |
} | |
// Test it... | |
run(groups); | |
// This is like above, but it lets us specify any starting positions, | |
// any width and height, and the distance between X groups | |
function pickUpColumns(startX, startY, width, height, distanceX, times) { | |
var count = 0, | |
groups = []; | |
while(count < times) { | |
groups[count] = get(count * distanceX + startX, startY, width, height); | |
count = count + 1; | |
} | |
run(groups); | |
} | |
// Test it... | |
pickUpColumns(0, 0, 1, 12, 2, 9) | |
// This is like above, but it picks up horizontally in rows | |
function pickUpRows(startX, startY, width, height, distanceY, times) { | |
var count = 0, | |
groups = []; | |
while(count < times) { | |
groups[count] = get(startX, count * distanceY + startY, width, height); | |
count = count + 1; | |
} | |
run(groups); | |
} | |
// Test it | |
pickUpRows(0, 0, 19, 1, 2, 6); | |
// Test multiple (Is this for jumps?) | |
run( | |
[ | |
pickUpRows(0, 0, 5, 1, 6, 3), | |
pickUpRows(0, 2, 5, 1, 6, 3), | |
pickUpRows(0, 4, 5, 1, 6, 3), | |
pickUpRows(0, 6, 5, 1, 6, 3), | |
pickUpRows(0, 8, 5, 1, 6, 3), | |
pickUpRows(0, 10, 5, 1, 6, 3), | |
] | |
); | |
// REAL box Step, but only where x and y start at 0, 0 | |
var count = 0, | |
groups = []; | |
while(count < 9) { | |
var nextX = (count % 3 * 7); | |
var nextY = (Math.floor(count / 3) * 4); | |
groups[count] = get(nextX, nextY, 4, 4); | |
count = count + 1; | |
} | |
run(groups); | |
// Box step that accounts for X and Y starts other than 0: | |
var count = 0, | |
groups = [], | |
startX = 1, | |
startY = 1; | |
while(count < 9) { | |
var nextX = (count % 3 * 6) + startX; | |
var nextY = (Math.floor(count / 3) * 4) + startY; | |
groups[count] = get(nextX, nextY, 4, 4); | |
count = count + 1; | |
} | |
run(groups); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment