-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
96 lines (78 loc) · 2.45 KB
/
App.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
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
import { StyleSheet, ImageBackground, SafeAreaView } from 'react-native';
import { LinearGradient } from 'expo-linear-gradient';
import { useState, useEffect } from 'react';
import { useFonts } from 'expo-font';
import { StatusBar } from 'expo-status-bar';
import * as SplashScreen from 'expo-splash-screen';
// screens
import StartGameScreen from './screens/StartGameScreen';
import GameScreen from './screens/GameScreen';
import Colours from './constants/colours';
import GameOverScreen from './screens/GameOverScreen';
SplashScreen.preventAutoHideAsync();
export default function App() {
const [userNumber, setUserNumber] = useState<number | null>(null);
const [gameOver, setGameOver] = useState<boolean>(true);
const [rounds, setRounds] = useState<number>(0);
const [fontsLoaded] = useFonts({
'Open-Sans-Regular': require('./assets/fonts/OpenSans-Regular.ttf'),
'Open-Sans-Bold': require('./assets/fonts/OpenSans-Bold.ttf'),
});
useEffect(() => {
const hideSplashScreen = async () => {
if (fontsLoaded) {
await SplashScreen.hideAsync();
}
};
hideSplashScreen();
}, [fontsLoaded]);
if (!fontsLoaded) {
return null;
}
const startGameHandler = (selectedNumber: number) => {
setUserNumber(selectedNumber);
setGameOver(false);
}
const gameOverHandler = (numberOfRounds: number) => {
setGameOver(true);
setRounds(numberOfRounds)
};
const StartNewGameHandler = () => {
setUserNumber(null);
setRounds(0);
};
let screen = <StartGameScreen startGameHandler={startGameHandler} />;
if (userNumber) {
screen = <GameScreen userNumber={userNumber} gameOverHandler={gameOverHandler} />
}
if (gameOver && userNumber) {
screen = <GameOverScreen userNumber={userNumber} roundNumber={rounds} onStartNewGame={StartNewGameHandler} />
}
return (
<>
<StatusBar style='light' />
<LinearGradient
colors={[Colours.primary600, Colours.secondary500]}
style={styles.rootScreen}>
<ImageBackground
source={require('./assets/images/background.png')}
resizeMode='cover'
style={styles.rootScreen}
imageStyle={styles.backgroundImage}
>
<SafeAreaView style={styles.rootScreen}>
{screen}
</SafeAreaView>
</ImageBackground>
</LinearGradient>
</>
);
}
const styles = StyleSheet.create({
rootScreen: {
flex: 1,
},
backgroundImage: {
opacity: 0.45,
}
});