Skip to content

Commit

Permalink
Merge pull request #14 from kaunta/i8
Browse files Browse the repository at this point in the history
Allow for multiple brush sizes
  • Loading branch information
kaunta committed Nov 23, 2019
2 parents ea3f7e3 + 4f8b044 commit 6a29a68
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
6 changes: 5 additions & 1 deletion game.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ <h1>Falling Sand Game</h1>
<h2>Brush Types</h2>
<button onclick="game.brush = Species.Wall">Wall</button>
<button onclick="game.brush = Species.Empty">Eraser</button>
<h2>Brush Sizes</h2>
<button onclick="game.brushSize = 1">1x</button>
<button onclick="game.brushSize = 4">4x</button>
<button onclick="game.brushSize = 16">16x</button>
<script>
game = new FallingSandGame(document.getElementById("game-canvas"));
tick = () => {
game.draw();
game.render();
requestAnimationFrame(tick);
};
tick();
Expand Down
26 changes: 17 additions & 9 deletions game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class FallingSandGame {
readonly world: Uint8Array;
drawing: boolean = false;
brush: Species = Species.Wall;

brushSize: number = 1;

constructor(canvas: HTMLCanvasElement) {
this.canvas = canvas;
Expand All @@ -14,12 +14,11 @@ class FallingSandGame {
this.canvas.addEventListener('mousemove', penMove(this));
}

draw() {
render() {
const ctx = this.canvas.getContext("2d");
const imageData = ctx.createImageData(this.canvas.width, this.canvas.height);
const unknownRGB = [236, 66, 245];
for (let i = 0; i < this.canvas.width * this.canvas.height; i++) {
const cell = this.world[i];
const [r, g, b] = Color[this.world[i]] || unknownRGB;
imageData.data[4 * i + 0] = r;
imageData.data[4 * i + 1] = g;
Expand All @@ -29,31 +28,40 @@ class FallingSandGame {
ctx.putImageData(imageData, 0, 0);
}

draw(x: number, y: number) {
for (let dx = 0; dx < this.brushSize; dx++) {
for (let dy = 0; dy < this.brushSize; dy++) {
const i = (x + dx) + (y + dy) * this.canvas.width;
this.world[i] = this.brush;
}
}
}

}

const penUp = (game: FallingSandGame) => (event: MouseEvent) => {
game.drawing = false;
};
const penDown = (game: FallingSandGame) => (event: MouseEvent) => {
game.drawing = true;
const i = event.offsetX + event.offsetY * game.canvas.width;
game.world[i] = game.brush;
game.draw(event.offsetX, event.offsetY);
};
const penMove = (game: FallingSandGame) => (event: MouseEvent) => {
if (game.drawing) {
const i = event.offsetX + event.offsetY * game.canvas.width;
game.world[i] = game.brush;
const x = event.offsetX;
const y = event.offsetY;
game.draw(x, y);
// dx line
const dx = Math.abs(event.movementX);
const sx = Math.sign(event.movementX);
for (let d = 0; d <= dx; d++) {
game.world[i - sx * d] = game.brush;
game.draw(x - d * sx, y);
}
// dy line
const dy = Math.abs(event.movementY);
const sy = Math.sign(event.movementY);
for (let d = 0; d <= dy; d++) {
game.world[i - sx * dx - game.canvas.width * sy * d] = game.brush;
game.draw(x - sx * dx, y - d * sy);
}
}
}
Expand Down

0 comments on commit 6a29a68

Please sign in to comment.