Skip to content

Commit

Permalink
Fix next shape generation
Browse files Browse the repository at this point in the history
  • Loading branch information
hakuzumon committed Jul 2, 2024
1 parent 3f142ff commit 6436910
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/components/Tetris.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ class Shape {
getKey() {
return this.key;
}

clone() {
return Shape.newInstance(this.key, this.color);
}
}

const shapes: {[key: string]: number[][]} = {
Expand Down Expand Up @@ -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<Color>;
let canvas: HTMLCanvasElement;
Expand Down Expand Up @@ -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 = [];
}
Expand Down

0 comments on commit 6436910

Please sign in to comment.