From 643691033d95761135afb4064242fbe643df7bdf Mon Sep 17 00:00:00 2001 From: Martti Soininen Date: Tue, 2 Jul 2024 14:55:23 +0300 Subject: [PATCH] Fix next shape generation --- src/components/Tetris.tsx | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/components/Tetris.tsx b/src/components/Tetris.tsx index b57c079..fbcb111 100644 --- a/src/components/Tetris.tsx +++ b/src/components/Tetris.tsx @@ -124,6 +124,10 @@ class Shape { getKey() { return this.key; } + + clone() { + return Shape.newInstance(this.key, this.color); + } } const shapes: {[key: string]: number[][]} = { @@ -240,6 +244,7 @@ export interface TetrisProps { export default function(props: TetrisProps) { const [score, setScore] = createSignal(0); + let next = Shape.newInstance(randomItem(Object.keys(shapes), 'O'), Color.RED); let gameObject: Shape | null = null; let gameArea: Array2d; let canvas: HTMLCanvasElement; @@ -406,9 +411,12 @@ export default function(props: TetrisProps) { moveX = initialLoc.x; moveY = initialLoc.y; - const targetColor = randomItem(visibleColors, gameObject?.getColor()); - const targetShape = randomItem(Object.keys(shapes), gameObject?.getKey()); - gameObject = Shape.newInstance(targetShape, targetColor); + gameObject = next.clone(); + + const nextColor = randomItem(visibleColors, next.getColor()); + const nextShape = randomItem(Object.keys(shapes), next.getKey()); + next = Shape.newInstance(nextShape, nextColor); + moveGameObjectInsideBounds(gameObject.getPoints()); actionStack = []; }