diff --git a/examples/same-game/logic/block.ts b/examples/same-game/logic/block.ts index 953c5ee..4c18b29 100644 --- a/examples/same-game/logic/block.ts +++ b/examples/same-game/logic/block.ts @@ -13,6 +13,10 @@ export class Block { return this._color; } + public get multiplier(): number { + return this._multiplier; + } + public get isActive(): boolean { return this._isActive; } @@ -25,7 +29,11 @@ export class Block { return this._offsetY; } - constructor(private _color: BlockColors, private _isSelected: boolean = false) {} + constructor( + private _color: BlockColors, + private _multiplier: number, + private _isSelected: boolean = false, + ) {} public select(): void { this._isSelected = true; diff --git a/examples/same-game/logic/board.ts b/examples/same-game/logic/board.ts index 413b7a9..43561ca 100644 --- a/examples/same-game/logic/board.ts +++ b/examples/same-game/logic/board.ts @@ -17,6 +17,17 @@ export class Board { return this._blocks.filter((x) => x.isSelected).length; } + public get selectedBlockScore(): number { + const selectedBlocks = this._blocks.filter((x) => x.isSelected); + + let total = selectedBlocks.length; + for (const block of selectedBlocks) { + total += total * block.multiplier; + } + + return total; + } + public get score(): number { return this._score; } @@ -24,7 +35,13 @@ export class Board { constructor(private _random: Random) { for (let i = 0; i < Board.BlockCount; i++) { const blockColor = this._random.nextInt(0, 3) as BlockColors; - this._blocks[i] = new Block(blockColor); + + const multiplier2x = this._random.nextInt(0, 100) % 10 === 0; + const multiplier3x = this._random.nextInt(0, 100) % 25 === 0; + const multiplier5x = this._random.nextInt(0, 100) % 50 === 0; + const multiplier = multiplier5x ? 5 : (multiplier3x ? 3 : (multiplier2x ? 2 : 0)); + + this._blocks[i] = new Block(blockColor, multiplier); } } @@ -48,13 +65,11 @@ export class Board { return; } - let scoreForSelectedBlocks = 0; + this._score += this.selectedBlockScore; + for (const block of selected) { block.deactivate(); - scoreForSelectedBlocks += 1; } - - this._score += scoreForSelectedBlocks; } public update(elapsed: bigint): void { diff --git a/examples/same-game/main.ts b/examples/same-game/main.ts index 884c29b..cf06f2f 100644 --- a/examples/same-game/main.ts +++ b/examples/same-game/main.ts @@ -20,14 +20,14 @@ function main(): number { const [window, renderer] = SDL.CreateWindowAndRenderer( WINDOW_WIDTH, WINDOW_HEIGHT, - SDL.WindowFlags.SHOWN + SDL.WindowFlags.SHOWN, ); SDL.SetWindowTitle(window, "Same Game"); const blockTexture = IMG.LoadTexture( renderer, - join(ASSETS_PATH, "blocks.png") + join(ASSETS_PATH, "blocks.png"), ); if (blockTexture == null) { @@ -37,7 +37,7 @@ function main(): number { const font = createFontAtlas( renderer, join(ASSETS_PATH, "Hack.ttf"), - FONT_SIZE + FONT_SIZE, ); const board = new Board(new Random(12345)); @@ -94,7 +94,7 @@ function draw( renderer: Pointer, board: Board, blockTexture: Pointer, - font: FontAtlas + font: FontAtlas, ): void { SDL.SetRenderDrawColor(renderer, 0, 0, 0, 255); SDL.RenderClear(renderer); @@ -105,13 +105,13 @@ function draw( renderer, font, new SDL.Point(0, Board.HeightInPixels + 2), - `Score: ${board.score}` + `Score: ${board.score}`, ); drawString( renderer, font, new SDL.Point(0, Board.HeightInPixels + FONT_SIZE + 2), - `Selected: ${board.selectedBlockCount}` + `Selected: ${board.selectedBlockCount} => ${board.selectedBlockScore}`, ); SDL.RenderPresent(renderer); diff --git a/examples/same-game/rendering/board.ts b/examples/same-game/rendering/board.ts index c345f39..cc3b8a1 100644 --- a/examples/same-game/rendering/board.ts +++ b/examples/same-game/rendering/board.ts @@ -47,10 +47,20 @@ export function drawBoard( SDL.SetTextureColorMod(blockTexture, r, g, b); SDL.SetTextureAlphaMod(blockTexture, 255); + const blockSrcRect = new SDL.Rect(xSrc, 0, Block.WidthInPixels, Block.HeightInPixels); + + if (block.multiplier === 2) { + blockSrcRect.x += Block.WidthInPixels; + } else if (block.multiplier === 3) { + blockSrcRect.x += Block.WidthInPixels * 2; + } else if (block.multiplier === 5) { + blockSrcRect.x += Block.WidthInPixels * 3; + } + SDL.RenderCopy( renderer, blockTexture, - new SDL.Rect(xSrc, 0, Block.WidthInPixels, Block.HeightInPixels), + blockSrcRect, new SDL.Rect(xDest, yDest, Block.WidthInPixels, Block.HeightInPixels), );