typescript(loopover): feat: added the moveY method to the GameState class
Signed-off-by: prescientmoon <git@moonythm.dev>
This commit is contained in:
parent
73f94f3018
commit
90b404f68b
|
@ -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()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue