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 1 #19

Open
wants to merge 1 commit into
base: main
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
43 changes: 24 additions & 19 deletions src/components/App/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useState } from "react";
import { GlobalContext } from "../../contexts";
import World from "../World";
import Player from "../Player";
import Npc from "../Npc";
Expand All @@ -9,6 +10,7 @@ import Lever from "../Lever";
import House from "../House";
import Fire from "../Fire";
import GameOver from "../GameOver";
import { GAME_STATES } from "../../constants";
import "./style.css";

/*
Expand All @@ -17,31 +19,34 @@ import "./style.css";
* - Use context to connect components
*/
export default function App() {
const [gameState, setGameState] = useState<GAME_STATES>(GAME_STATES.Game);
const [isCellarDoorOpen, setIsCellarDoorOpen] = useState(false);
const [isLeverUsed, setIsLeverUsed] = useState(false);
const [playerHealth, setPlayerHealth] = useState(4);

return (
<div className="App">
{playerHealth <= 0 && <GameOver />}
<World />
<Player
health={playerHealth}
onInteract={setIsLeverUsed}
onCollision={setPlayerHealth}
/>
<Npc left={1608} top={224} />
<CellarDoor isOpen={isCellarDoorOpen} />
<Lever
left={600}
top={264}
used={isLeverUsed}
onInteract={setIsCellarDoorOpen}
/>
<House />
<Fire left={480} top={524} />
<Heart left={320} top={828} />
<Coin left={1152} top={1172} />
<GlobalContext.Provider value={{ gameState: gameState, setGameState }}>
{gameState === GAME_STATES.GameOver && <GameOver />}
<World />
<Player
health={playerHealth}
onInteract={setIsLeverUsed}
onCollision={setPlayerHealth}
/>
<Npc left={1608} top={224} />
<CellarDoor isOpen={isCellarDoorOpen} />
<Lever
left={600}
top={264}
used={isLeverUsed}
onInteract={setIsCellarDoorOpen}
/>
<House />
<Fire left={480} top={524} />
<Heart left={320} top={828} />
<Coin left={1152} top={1172} />
</GlobalContext.Provider>
</div>
);
}
38 changes: 33 additions & 5 deletions src/components/CellarDoor/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { useEffect, FunctionComponent } from "react";
import { TILE_SETS } from "../../constants";
import "./style.css";

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

/*
* TODO:
* - useRef instead of getElementById
Expand All @@ -23,20 +29,42 @@ const CellarDoor: FunctionComponent<{ isOpen?: boolean }> = ({
canvas.style.top = "272px";

const tileSet = new Image();
tileSet.src = "assets/overworld.png";
tileSet.src = TILE_SETS.World;
tileSet.onload = () => {
ctx.clearRect(0, 0, 64, 64);
ctx.clearRect(0, 0, WIDTH, HEIGHT);

if (isOpen) {
ctx.drawImage(tileSet, 1056, 160, 64, 64, 0, 0, 64, 64);
ctx.drawImage(
tileSet,
TILE_X + WIDTH,
TILE_Y,
WIDTH,
HEIGHT,
0,
0,
WIDTH,
HEIGHT
);
} else {
ctx.drawImage(tileSet, 992, 160, 64, 64, 0, 0, 64, 64);
ctx.drawImage(
tileSet,
TILE_X,
TILE_Y,
WIDTH,
HEIGHT,
0,
0,
WIDTH,
HEIGHT
);
}
};
}
}, [isOpen]);

return <canvas id="cellar-door-canvas" width="64" height="64"></canvas>;
return (
<canvas id="cellar-door-canvas" width={WIDTH} height={HEIGHT}></canvas>
);
};

export default CellarDoor;
64 changes: 56 additions & 8 deletions src/components/Coin/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { useEffect, FunctionComponent } from "react";
import { TILE_SIZE, TILE_SETS } from "../../constants";
import "./style.css";

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

/*
* TODO:
* - useRef instead of getElementById
Expand All @@ -24,30 +31,71 @@ const Coin: FunctionComponent<{ left: number; top: number }> = ({
canvas.style.top = `${top}px`;

const tileSet = new Image();
tileSet.src = "assets/objects.png";
tileSet.src = TILE_SETS.Objects;
tileSet.onload = () => {
let currentFrame = 0;

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

if (currentFrame === 0) {
ctx.drawImage(tileSet, 0, 128, 32, 32, 0, 0, 32, 32);
ctx.drawImage(
tileSet,
TILE_X,
TILE_Y,
WIDTH,
HEIGHT,
0,
0,
WIDTH,
HEIGHT
);
} else if (currentFrame === 1) {
ctx.drawImage(tileSet, 32, 128, 32, 32, 0, 0, 32, 32);
ctx.drawImage(
tileSet,
TILE_X + WIDTH,
TILE_Y,
WIDTH,
HEIGHT,
0,
0,
WIDTH,
HEIGHT
);
} else if (currentFrame === 2) {
ctx.drawImage(tileSet, 64, 128, 32, 32, 0, 0, 32, 32);
ctx.drawImage(
tileSet,
TILE_X + WIDTH * 2,
TILE_Y,
WIDTH,
HEIGHT,
0,
0,
WIDTH,
HEIGHT
);
} else if (currentFrame === 3) {
ctx.drawImage(tileSet, 96, 128, 32, 32, 0, 0, 32, 32);
ctx.drawImage(
tileSet,
TILE_X + WIDTH * 3,
TILE_Y,
WIDTH,
HEIGHT,
0,
0,
WIDTH,
HEIGHT
);
}

currentFrame = currentFrame === 3 ? 0 : currentFrame + 1;
currentFrame =
currentFrame === ANIMATION_LENGTH ? 0 : currentFrame + 1;
}, 100);
};
}
}, [left, top]);

return <canvas id="coin-canvas" width="32" height="32"></canvas>;
return <canvas id="coin-canvas" width={WIDTH} height={HEIGHT}></canvas>;
};

export default Coin;
100 changes: 89 additions & 11 deletions src/components/Fire/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { useEffect, FunctionComponent } from "react";
import { TILE_SIZE, TILE_SETS } from "../../constants";
import "./style.css";

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

/*
* TODO:
* - useRef instead of getElementById
Expand All @@ -24,36 +31,107 @@ const Fire: FunctionComponent<{ left: number; top: number }> = ({
canvas.style.top = `${top}px`;

const tileSet = new Image();
tileSet.src = "assets/objects.png";
tileSet.src = TILE_SETS.Objects;
tileSet.onload = () => {
let currentFrame = 0;

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

if (currentFrame === 0) {
ctx.drawImage(tileSet, 130, 98, 32, 32, 0, 0, 32, 32);
ctx.drawImage(
tileSet,
TILE_X,
TILE_Y,
WIDTH,
HEIGHT,
0,
0,
WIDTH,
HEIGHT
);
} else if (currentFrame === 1) {
ctx.drawImage(tileSet, 162, 98, 32, 32, 0, 0, 32, 32);
ctx.drawImage(
tileSet,
TILE_X + WIDTH,
TILE_Y,
WIDTH,
HEIGHT,
0,
0,
WIDTH,
HEIGHT
);
} else if (currentFrame === 2) {
ctx.drawImage(tileSet, 194, 98, 32, 32, 0, 0, 32, 32);
ctx.drawImage(
tileSet,
TILE_X + WIDTH * 2,
TILE_Y,
WIDTH,
HEIGHT,
0,
0,
WIDTH,
HEIGHT
);
} else if (currentFrame === 3) {
ctx.drawImage(tileSet, 226, 98, 32, 32, 0, 0, 32, 32);
ctx.drawImage(
tileSet,
TILE_X + WIDTH * 3,
TILE_Y,
WIDTH,
HEIGHT,
0,
0,
WIDTH,
HEIGHT
);
} else if (currentFrame === 4) {
ctx.drawImage(tileSet, 258, 98, 32, 32, 0, 0, 32, 32);
ctx.drawImage(
tileSet,
TILE_X + WIDTH * 4,
TILE_Y,
WIDTH,
HEIGHT,
0,
0,
WIDTH,
HEIGHT
);
} else if (currentFrame === 5) {
ctx.drawImage(tileSet, 290, 98, 32, 32, 0, 0, 32, 32);
ctx.drawImage(
tileSet,
TILE_X + WIDTH * 5,
TILE_Y,
WIDTH,
HEIGHT,
0,
0,
WIDTH,
HEIGHT
);
} else if (currentFrame === 6) {
ctx.drawImage(tileSet, 322, 98, 32, 32, 0, 0, 32, 32);
ctx.drawImage(
tileSet,
TILE_X + WIDTH * 6,
TILE_Y,
WIDTH,
HEIGHT,
0,
0,
WIDTH,
HEIGHT
);
}

currentFrame = currentFrame === 6 ? 0 : currentFrame + 1;
currentFrame =
currentFrame === ANIMATION_LENGTH ? 0 : currentFrame + 1;
}, 125);
};
}
}, [left, top]);

return <canvas id="fire-canvas" width="32" height="32"></canvas>;
return <canvas id="fire-canvas" width={WIDTH} height={HEIGHT}></canvas>;
};

export default Fire;
Loading