-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFoxComponent.tsx
54 lines (50 loc) · 1.37 KB
/
FoxComponent.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
import React, {useMemo} from 'react';
import {
useImage,
ImageShader,
Shader,
Vertices,
Rect,
SharedValueType,
} from '@shopify/react-native-skia';
import {vertices, side} from './Fox';
import {shader} from './Shader';
import {useDerivedValue} from 'react-native-reanimated';
import {GameState} from './GameState';
import {indicies} from './utils';
export type FoxProps = {
game_state: SharedValueType<GameState>;
pd: number;
};
export function FoxComponent(props: FoxProps) {
const {pd} = props;
const shader_scale = useMemo(() => [{scale: pd}], [pd]);
const fox = useImage(require('./images/fox_sprite_sheet.png'));
const transform = useDerivedValue(() => {
const {x, y} = props.game_state.value.fox_state;
return [{translateY: y * pd}, {translateX: x * pd}] as [
{translateY: number},
{translateX: number},
];
});
const uniforms = useDerivedValue(() => {
const {x_offset, y_offset} = props.game_state.value.fox_state;
return {x_offset, y_offset};
});
if (!fox) {
return null;
}
return (
<Rect
transform={transform}
x={0}
y={0}
width={side * pd}
height={side * pd}>
<Shader transform={shader_scale} source={shader!} uniforms={uniforms}>
<ImageShader image={fox} />
</Shader>
<Vertices textures={vertices} vertices={vertices} indices={indicies} />
</Rect>
);
}