2016-09-22 15:21:59 +08:00
|
|
|
const AutoLayout = {
|
2016-09-13 21:01:36 +08:00
|
|
|
nextX: 0,
|
|
|
|
nextY: 0,
|
|
|
|
sideLength: 1,
|
|
|
|
turnCount: 0,
|
|
|
|
nextXshift: 1,
|
|
|
|
nextYshift: 0,
|
|
|
|
timeToTurn: 0,
|
|
|
|
|
2016-11-07 15:25:08 -05:00
|
|
|
getNextCoord: function(opts = {}) {
|
2016-09-22 18:31:56 +08:00
|
|
|
var self = AutoLayout
|
2016-09-13 21:01:36 +08:00
|
|
|
var nextX = self.nextX
|
|
|
|
var nextY = self.nextY
|
|
|
|
|
|
|
|
var DISTANCE_BETWEEN = 120
|
|
|
|
|
|
|
|
self.nextX = self.nextX + DISTANCE_BETWEEN * self.nextXshift
|
|
|
|
self.nextY = self.nextY + DISTANCE_BETWEEN * self.nextYshift
|
|
|
|
|
|
|
|
self.timeToTurn += 1
|
|
|
|
// if true, it's time to turn
|
|
|
|
if (self.timeToTurn === self.sideLength) {
|
|
|
|
self.turnCount += 1
|
|
|
|
// if true, it's time to increase side length
|
|
|
|
if (self.turnCount % 2 === 0) {
|
|
|
|
self.sideLength += 1
|
|
|
|
}
|
|
|
|
self.timeToTurn = 0
|
|
|
|
|
|
|
|
// going right? turn down
|
2016-11-07 15:25:08 -05:00
|
|
|
if (self.nextXshift === 1 && self.nextYshift === 0) {
|
2016-09-13 21:01:36 +08:00
|
|
|
self.nextXshift = 0
|
|
|
|
self.nextYshift = 1
|
2016-11-07 15:25:08 -05:00
|
|
|
} else if (self.nextXshift === 0 && self.nextYshift === 1) {
|
|
|
|
// going down? turn left
|
2016-09-13 21:01:36 +08:00
|
|
|
self.nextXshift = -1
|
|
|
|
self.nextYshift = 0
|
2016-11-07 15:25:08 -05:00
|
|
|
} else if (self.nextXshift === -1 && self.nextYshift === 0) {
|
|
|
|
// going left? turn up
|
2016-09-13 21:01:36 +08:00
|
|
|
self.nextXshift = 0
|
|
|
|
self.nextYshift = -1
|
2016-11-07 15:25:08 -05:00
|
|
|
} else if (self.nextXshift === 0 && self.nextYshift === -1) {
|
|
|
|
// going up? turn right
|
2016-09-13 21:01:36 +08:00
|
|
|
self.nextXshift = 1
|
|
|
|
self.nextYshift = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-06 09:07:46 -04:00
|
|
|
if (opts.mappings && self.coordsTaken(nextX, nextY, opts.mappings)) {
|
2016-10-01 13:34:52 +08:00
|
|
|
// check if the coordinate is already taken on the current map
|
|
|
|
return self.getNextCoord(opts)
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
x: nextX,
|
|
|
|
y: nextY
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2016-11-07 15:25:08 -05:00
|
|
|
coordsTaken: function(x, y, mappings) {
|
2016-10-01 13:34:52 +08:00
|
|
|
if (mappings.findWhere({ xloc: x, yloc: y })) {
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
2016-09-13 21:01:36 +08:00
|
|
|
}
|
|
|
|
},
|
2016-11-07 15:25:08 -05:00
|
|
|
resetSpiral: function() {
|
2016-09-22 18:31:56 +08:00
|
|
|
var self = AutoLayout
|
2016-09-13 21:01:36 +08:00
|
|
|
self.nextX = 0
|
|
|
|
self.nextY = 0
|
|
|
|
self.nextXshift = 1
|
|
|
|
self.nextYshift = 0
|
|
|
|
self.sideLength = 1
|
|
|
|
self.timeToTurn = 0
|
|
|
|
self.turnCount = 0
|
|
|
|
}
|
|
|
|
}
|
2016-09-22 15:21:59 +08:00
|
|
|
|
|
|
|
export default AutoLayout
|