Skip to content

Commit

Permalink
Blocks have multipliers.
Browse files Browse the repository at this point in the history
  • Loading branch information
smack0007 committed Oct 29, 2024
1 parent fe02379 commit 906f86a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 13 deletions.
10 changes: 9 additions & 1 deletion examples/same-game/logic/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export class Block {
return this._color;
}

public get multiplier(): number {
return this._multiplier;
}

public get isActive(): boolean {
return this._isActive;
}
Expand All @@ -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;
Expand Down
25 changes: 20 additions & 5 deletions examples/same-game/logic/board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,31 @@ 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;
}

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);
}
}

Expand All @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions examples/same-game/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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));
Expand Down Expand Up @@ -94,7 +94,7 @@ function draw(
renderer: Pointer<SDL.Renderer>,
board: Board,
blockTexture: Pointer<SDL.Texture>,
font: FontAtlas
font: FontAtlas,
): void {
SDL.SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL.RenderClear(renderer);
Expand All @@ -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);
Expand Down
12 changes: 11 additions & 1 deletion examples/same-game/rendering/board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);

Expand Down

0 comments on commit 906f86a

Please sign in to comment.