-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
100 lines (94 loc) · 2.27 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
import React, {PureComponent} from 'react';
import {StatusBar, View, Text, StyleSheet} from 'react-native';
import Systems from './src/systems';
import Entities from './src/entities';
import {GameEngine} from 'react-native-game-engine';
import FastImage from 'react-native-fast-image';
import GameOver from './src/components/GameOver';
import {height, width} from './src/utils/styleSheet';
const backgroundImage = require('./src/assets/nature.jpg');
export default class App extends PureComponent {
constructor(props) {
super(props);
this.state = {
running: true,
score: 0,
};
this.gameEngine = null;
console.disableYellowBox = true;
}
onEvent = e => {
if (e.type === 'gameOver') {
this.setState({
running: false,
});
} else if (e.type === 'score') {
this.setState(prevState => {
return {score: prevState.score + 1};
});
}
};
restart = () => {
this.setState({running: true, score: 0});
this.gameEngine.swap(Entities());
};
render() {
const {state} = this;
return (
<View style={styles.container}>
<FastImage style={styles.imageBackground} source={backgroundImage} />
<GameEngine
ref={ref => {
this.gameEngine = ref;
}}
style={styles.gameContainer}
systems={Systems}
onEvent={this.onEvent}
entities={Entities()}
running={state.running}>
<StatusBar hidden={true} />
</GameEngine>
{state.running ? (
<Text style={styles.score}>{state.score}</Text>
) : (
<GameOver score={state.score} restart={this.restart} />
)}
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#000',
},
gameContainer: {
position: 'absolute',
top: 0,
bottom: 0,
left: 0,
right: 0,
},
score: {
color: '#ffffff',
fontSize: 50,
fontWeight: 'bold',
textAlign: 'center',
top: 100,
fontFamily: 'crackman-regular',
},
imageBackground: {
width: width,
height: height,
position: 'absolute',
top: 0,
bottom: 0,
left: 0,
right: 0,
},
gameOverContainer: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
});