generated from ModusCreateOrg/template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.tsx
65 lines (57 loc) · 1.45 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { useRef, FC, useContext, useState } from "react";
import { TILE_SIZE, TILE_SETS } from "../../constants";
import { GlobalContext } from "../../contexts";
import { useAnimatedSprite, useColliders } from "../../hooks";
import { Collider, ColliderType, Rect } from "../../utils";
import "./style.css";
const WIDTH = TILE_SIZE;
const HEIGHT = TILE_SIZE;
const TILE_X = 0;
const TILE_Y = 128;
const POINTS = 10;
const TIMEOUT = 2000;
type CoinProps = { left: number; top: number };
const Coin: FC<CoinProps> = ({ left, top }) => {
const { setScore } = useContext(GlobalContext);
const [isHidden, setIsHidden] = useState(false);
const canvasRef = useRef<HTMLCanvasElement>(null);
const onCollision = (c: Collider) => {
setScore(POINTS);
setIsHidden(true);
c.hide();
setTimeout(() => {
c.show();
setIsHidden(false);
}, TIMEOUT);
};
const colliderRef = useRef<Collider>(
new Collider(
new Rect(left, top, WIDTH, HEIGHT),
ColliderType.Bonus,
onCollision
)
);
useColliders(colliderRef);
useAnimatedSprite({
canvasRef,
left,
top,
tileSet: TILE_SETS.Objects,
width: WIDTH,
height: HEIGHT,
tileX: TILE_X,
tileY: TILE_Y,
animationLength: 3,
animationSpeed: 100,
});
return (
<canvas
className={isHidden ? "hidden" : ""}
ref={canvasRef}
id="coin-canvas"
width={WIDTH}
height={HEIGHT}
></canvas>
);
};
export default Coin;