🚀 refactor

This commit is contained in:
platane
2020-09-29 11:00:29 +02:00
committed by Platane
parent 8d8956229c
commit 9b92697ef9
5 changed files with 70 additions and 32 deletions

View File

@@ -0,0 +1,32 @@
import { pathRoundedRect } from "./pathRoundedRect";
import { Snake, snakeToCells } from "@snk/compute/snake";
type Options = {
colorSnake: string;
sizeCell: number;
};
export const drawSnake = (
ctx: CanvasRenderingContext2D,
snake: Snake,
o: Options
) => {
const cells = snakeToCells(snake);
for (let i = 0; i < cells.length; i++) {
const u = (i + 1) * 0.6;
ctx.save();
ctx.fillStyle = o.colorSnake;
ctx.translate(cells[i].x * o.sizeCell + u, cells[i].y * o.sizeCell + u);
ctx.beginPath();
pathRoundedRect(
ctx,
o.sizeCell - u * 2,
o.sizeCell - u * 2,
(o.sizeCell - u * 2) * 0.25
);
ctx.fill();
ctx.restore();
}
};

View File

@@ -1,7 +1,7 @@
import { Grid, Color } from "@snk/compute/grid";
import { pathRoundedRect } from "./pathRoundedRect";
import { drawGrid } from "./drawGrid";
import { Snake, snakeToCells } from "@snk/compute/snake";
import { Snake } from "@snk/compute/snake";
import { drawSnake } from "./drawSnake";
type Options = {
colorDots: Record<Color, string>;
@@ -13,31 +13,6 @@ type Options = {
sizeBorderRadius: number;
};
export const drawSnake = (
ctx: CanvasRenderingContext2D,
snake: Snake,
o: Options
) => {
const cells = snakeToCells(snake);
for (let i = 0; i < cells.length; i++) {
const u = (i + 1) * 0.6;
ctx.save();
ctx.fillStyle = o.colorSnake;
ctx.translate(cells[i].x * o.sizeCell + u, cells[i].y * o.sizeCell + u);
ctx.beginPath();
pathRoundedRect(
ctx,
o.sizeCell - u * 2,
o.sizeCell - u * 2,
(o.sizeCell - u * 2) * 0.25
);
ctx.fill();
ctx.restore();
}
};
export const drawWorld = (
ctx: CanvasRenderingContext2D,
grid: Grid,