forked from EvanBacon/snake
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.js
104 lines (93 loc) · 2.75 KB
/
App.js
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import {AppLoading, SplashScreen, Updates} from 'expo';
import {Asset} from 'expo-asset';
import Constants from 'expo-constants';
import React from 'react';
import {Animated, Button, StyleSheet, Text, View, Platform} from 'react-native';
import GameScreen from './GameScreen';
import * as Font from 'expo-font';
SplashScreen.preventAutoHide(); // Instruct SplashScreen not to hide yet
export default function App() {
return <AnimatedAppLoader image={require('./assets/loading.png')} />;
}
function AnimatedAppLoader({image}) {
const [isSplashReady, setSplashReady] = React.useState(false);
const startAsync = React.useMemo(
() => () => {
return Asset.fromModule(image).downloadAsync();
},
[image],
);
const onFinish = React.useMemo(() => setSplashReady(true), []);
if (!isSplashReady) {
return (
<AppLoading
startAsync={startAsync}
onError={console.error}
onFinish={onFinish}
/>
);
}
return <AnimatedSplashScreen image={image} />;
}
function AnimatedSplashScreen({image}) {
const animation = React.useMemo(() => new Animated.Value(1), []);
const [isAppReady, setAppReady] = React.useState(false);
const [isGameReady, setGameReady] = React.useState(false);
const [isSplashAnimationComplete, setAnimationComplete] = React.useState(
false,
);
React.useEffect(() => {
if (isGameReady) {
Animated.timing(animation, {
toValue: 0,
duration: 200,
useNativeDriver: Platform.select({web: false, default: true}),
}).start(() => setAnimationComplete(true));
}
}, [isGameReady]);
const onImageLoaded = React.useMemo(() => async () => {
SplashScreen.hide();
try {
// Load stuff
await Promise.all([
Font.loadAsync('kombat', require('./assets/kombat.ttf')),
]);
} catch (e) {
// handle errors
} finally {
setAppReady(true);
}
});
return (
<View style={{flex: 1}}>
{isAppReady && <GameScreen onReady={() => setGameReady(true)} />}
{!isSplashAnimationComplete && (
<Animated.View
pointerEvents="none"
style={[
StyleSheet.absoluteFill,
{
backgroundColor: Constants.manifest.splash.backgroundColor,
opacity: animation,
},
]}>
<Animated.Image
style={{
width: '100%',
height: '100%',
resizeMode: Constants.manifest.splash.resizeMode || 'contain',
transform: [
{
scale: animation,
},
],
}}
source={image}
onLoadEnd={onImageLoaded}
fadeDuration={0}
/>
</Animated.View>
)}
</View>
);
}