Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor 3 #21

Open
wants to merge 1 commit into
base: refactor-2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 12 additions & 29 deletions src/components/CellarDoor/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { useEffect, useRef, FC } from "react";
import { useRef, FC } from "react";
import { TILE_SETS } from "../../constants";
import { useSprite } from "../../hooks";
import "./style.css";

const WIDTH = 64;
const HEIGHT = 64;
const TILE_X = 992;
const TILE_Y = 160;

type CellarDoorProps = { top: number; left: number; isOpen?: boolean };

Expand All @@ -16,34 +16,17 @@ type CellarDoorProps = { top: number; left: number; isOpen?: boolean };
*/
const CellarDoor: FC<CellarDoorProps> = ({ isOpen = false, top, left }) => {
const canvasRef = useRef<HTMLCanvasElement>(null);
useEffect(() => {
const ctx = canvasRef.current?.getContext("2d");

if (!canvasRef.current || !ctx) {
return;
}

canvasRef.current.style.left = `${left}px`;
canvasRef.current.style.top = `${top}px`;

const tileSet = new Image();
tileSet.src = TILE_SETS.World;
tileSet.onload = () => {
ctx.clearRect(0, 0, WIDTH, HEIGHT);

ctx.drawImage(
tileSet,
isOpen ? TILE_X + WIDTH : TILE_X,
TILE_Y,
WIDTH,
HEIGHT,
0,
0,
WIDTH,
HEIGHT
);
};
}, [isOpen, left, top]);
useSprite({
canvasRef,
left,
top,
tileSet: TILE_SETS.World,
width: WIDTH,
height: HEIGHT,
tileX: isOpen ? TILE_X + WIDTH : TILE_X,
tileY: 160,
});

return (
<canvas
Expand Down
61 changes: 14 additions & 47 deletions src/components/Coin/index.tsx
Original file line number Diff line number Diff line change
@@ -1,61 +1,28 @@
import { useEffect, useRef, FC } from "react";
import { useRef, FC } from "react";
import { TILE_SIZE, TILE_SETS } from "../../constants";
import { useAnimatedSprite } from "../../hooks";
import "./style.css";

const WIDTH = TILE_SIZE;
const HEIGHT = TILE_SIZE;
const TILE_X = 0;
const TILE_Y = 128;
const ANIMATION_LENGTH = 3;

type CoinProps = { left: number; top: number };

/*
* TODO:
* - util function for tile set, tiles and animation
*/
const Coin: FC<CoinProps> = ({ left, top }) => {
const canvasRef = useRef<HTMLCanvasElement>(null);

useEffect(() => {
const ctx = canvasRef.current?.getContext("2d");
let intervalId: number;

if (!canvasRef.current || !ctx) {
return;
}

canvasRef.current.style.left = `${left}px`;
canvasRef.current.style.top = `${top}px`;

const tileSet = new Image();
tileSet.src = TILE_SETS.Objects;
tileSet.onload = () => {
let currentFrame = 0;

intervalId = window.setInterval(() => {
ctx.clearRect(0, 0, WIDTH, HEIGHT);

ctx.drawImage(
tileSet,
TILE_X + WIDTH * currentFrame,
TILE_Y,
WIDTH,
HEIGHT,
0,
0,
WIDTH,
HEIGHT
);

currentFrame = currentFrame === ANIMATION_LENGTH ? 0 : currentFrame + 1;
}, 100);
};

return () => {
clearInterval(intervalId);
};
}, [left, top]);
useAnimatedSprite({
canvasRef,
left,
top,
tileSet: TILE_SETS.Objects,
width: WIDTH,
height: HEIGHT,
tileX: 0,
tileY: 128,
animationLength: 3,
animationSpeed: 100,
});

return (
<canvas
Expand Down
61 changes: 14 additions & 47 deletions src/components/Fire/index.tsx
Original file line number Diff line number Diff line change
@@ -1,61 +1,28 @@
import { useEffect, useRef, FC } from "react";
import { useRef, FC } from "react";
import { TILE_SIZE, TILE_SETS } from "../../constants";
import { useAnimatedSprite } from "../../hooks";
import "./style.css";

const WIDTH = TILE_SIZE;
const HEIGHT = TILE_SIZE;
const TILE_X = 130;
const TILE_Y = 98;
const ANIMATION_LENGTH = 6;

type FireProps = { left: number; top: number };

/*
* TODO:
* - util function for tile set, tiles and animation
*/
const Fire: FC<FireProps> = ({ left, top }) => {
const canvasRef = useRef<HTMLCanvasElement>(null);

useEffect(() => {
const ctx = canvasRef.current?.getContext("2d");
let intervalId: number;

if (!canvasRef.current || !ctx) {
return;
}

canvasRef.current.style.left = `${left}px`;
canvasRef.current.style.top = `${top}px`;

const tileSet = new Image();
tileSet.src = TILE_SETS.Objects;
tileSet.onload = () => {
let currentFrame = 0;

intervalId = window.setInterval(() => {
ctx.clearRect(0, 0, WIDTH, HEIGHT);

ctx.drawImage(
tileSet,
TILE_X + WIDTH * currentFrame,
TILE_Y,
WIDTH,
HEIGHT,
0,
0,
WIDTH,
HEIGHT
);

currentFrame = currentFrame === ANIMATION_LENGTH ? 0 : currentFrame + 1;
}, 125);
};

return () => {
clearInterval(intervalId);
};
}, [left, top]);
useAnimatedSprite({
canvasRef,
left,
top,
tileSet: TILE_SETS.Objects,
width: WIDTH,
height: HEIGHT,
tileX: 130,
tileY: 98,
animationLength: 6,
animationSpeed: 125,
});

return (
<canvas
Expand Down
60 changes: 14 additions & 46 deletions src/components/Heart/index.tsx
Original file line number Diff line number Diff line change
@@ -1,60 +1,28 @@
import { useEffect, useRef, FC } from "react";
import { useRef, FC } from "react";
import { TILE_SIZE, TILE_SETS } from "../../constants";
import { useAnimatedSprite } from "../../hooks";
import "./style.css";

const WIDTH = TILE_SIZE;
const HEIGHT = TILE_SIZE;
const TILE_X = 0;
const TILE_Y = 96;
const ANIMATION_LENGTH = 3;

type HeartProps = { left: number; top: number };

/*
* TODO:
* - util function for tile set, tiles and animation
*/
const Heart: FC<HeartProps> = ({ left, top }) => {
const canvasRef = useRef<HTMLCanvasElement>(null);

useEffect(() => {
const ctx = canvasRef.current?.getContext("2d");
let intervalId: number;

if (!canvasRef.current || !ctx) {
return;
}
canvasRef.current.style.left = `${left}px`;
canvasRef.current.style.top = `${top}px`;

const tileSet = new Image();
tileSet.src = TILE_SETS.Objects;
tileSet.onload = () => {
let currentFrame = 0;

intervalId = window.setInterval(() => {
ctx.clearRect(0, 0, WIDTH, HEIGHT);

ctx.drawImage(
tileSet,
TILE_X + WIDTH * currentFrame,
TILE_Y,
WIDTH,
HEIGHT,
0,
0,
WIDTH,
HEIGHT
);

currentFrame = currentFrame === ANIMATION_LENGTH ? 0 : currentFrame + 1;
}, 75);
};

return () => {
clearInterval(intervalId);
};
}, [left, top]);
useAnimatedSprite({
canvasRef,
left,
top,
tileSet: TILE_SETS.Objects,
width: WIDTH,
height: HEIGHT,
tileX: 0,
tileY: 96,
animationLength: 3,
animationSpeed: 75,
});

return (
<canvas
Expand Down
45 changes: 12 additions & 33 deletions src/components/House/index.tsx
Original file line number Diff line number Diff line change
@@ -1,47 +1,26 @@
import { useEffect, useRef, FC } from "react";
import { useRef, FC } from "react";
import { TILE_SETS } from "../../constants";
import { useSprite } from "../../hooks";
import "./style.css";

const WIDTH = 148;
const HEIGHT = 160;
const TILE_X = 198;
const TILE_Y = 0;

type HouseProps = { left: number; top: number };

/*
* TODO:
* - util function for tile set, tiles and animation
*/
const House: FC<HouseProps> = ({ left, top }) => {
const canvasRef = useRef<HTMLCanvasElement>(null);

useEffect(() => {
const ctx = canvasRef.current?.getContext("2d");

if (!canvasRef.current || !ctx) {
return;
}

canvasRef.current.style.left = `${left}px`;
canvasRef.current.style.top = `${top}px`;

const tileSet = new Image();
tileSet.src = TILE_SETS.World;
tileSet.onload = () => {
ctx.drawImage(
tileSet,
TILE_X,
TILE_Y,
WIDTH,
HEIGHT,
0,
0,
WIDTH,
HEIGHT
);
};
}, [left, top]);
useSprite({
canvasRef,
left,
top,
tileSet: TILE_SETS.World,
width: WIDTH,
height: HEIGHT,
tileX: 198,
tileY: 0,
});

return (
<canvas
Expand Down
Loading