1
Fork 0

typescript(loopover): feat: added the moveY method to the GameState class

Signed-off-by: prescientmoon <git@moonythm.dev>
This commit is contained in:
Matei Adriel 2019-11-25 17:31:07 +02:00 committed by prescientmoon
parent 73f94f3018
commit 90b404f68b
Signed by: prescientmoon
SSH key fingerprint: SHA256:UUF9JT2s8Xfyv76b8ZuVL7XrmimH4o49p4b+iexbVH4

View file

@ -19,7 +19,7 @@ export class GameState {
public moveX(direction: Direction, layer: number) {
const minimumLayerIndex = layer * this.width
const newState = this.cellList.reduce((accumulated, current, index) => {
const newState = this.cellList.reduce((accumulated, _, index) => {
if (
index < minimumLayerIndex ||
index >= (layer + 1) * this.width
@ -38,6 +38,28 @@ export class GameState {
return new GameState(newState, this.width)
}
public moveY(direction: Direction, layer: number) {
const minimumLayerIndex = layer * this.width
const newState = this.cellList.reduce((accumulated, current, index) => {
const currentX = index % this.width
const currentY = Math.floor(index / this.width)
if (currentX !== layer) {
return accumulated
}
const copyFrom =
currentX +
this.width *
((currentY + this.height - direction) % this.height)
return accumulated.set(index, this.cellList.get(copyFrom)!)
}, this.cellList)
return new GameState(newState, this.width)
}
public get cells() {
return this.cellList.toArray()
}