-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
138 lines (110 loc) · 3.48 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { useEffect, useState } from 'react';
import { StyleSheet, Text, View, Dimensions, TouchableWithoutFeedback, ImageBackground } from 'react-native';
import Player from './Player';
import Blocks from './Blocks';
import StartScreen from './StartScreen';
import Score from './Score';
export default function App() {
const [gameOn, setGameOn] = useState(false) //sätt till false
const [birdPosVertical, setBirdPosVertical] = useState((Dimensions.get("screen").height)/2)
const [boxPosHorizontal, setBoxPosHorizontal] = useState((Dimensions.get("screen").width) + 500)
const [boxHeight, setBoxHeight] = useState(200)
const [boxBottom, setBoxBottom] = useState(100)
const [boxGap, setBoxGap] = useState(80)
const [score, setScore] = useState(0)
let birdTimer;
let boxTimer;
let scoreTimer;
useEffect(() => {
start()
}, [])
function start(){
setGameOn(false) //sätt till false
setBirdPosVertical((Dimensions.get("screen").height)/2)
setBoxPosHorizontal(Dimensions.get("screen").width + 500)
setScore(0)
clearInterval(scoreTimer)
clearInterval(birdTimer)
clearInterval(boxTimer)
}
useEffect(() => {
birdTimer = setInterval(() => {
if(birdPosVertical < Dimensions.get("screen").height - 40){
setBirdPosVertical(birdPosVertical + 3)
if(birdPosVertical < 0)
setBirdPosVertical(0)
}
else
setBirdPosVertical(Dimensions.get("screen").height -40)
}, 20)
return () => clearInterval(birdTimer)
},[birdPosVertical])
useEffect(() => {
boxTimer = setInterval(() => {
if(boxPosHorizontal > 0){
setBoxPosHorizontal(boxPosHorizontal - 5)
//if(boxPosHorizontal-40 === 14 && birdPosVertical < (boxHeight + boxBottom + boxGap)) console.warn("TJdsfd")
}
else{
setBoxPosHorizontal(Dimensions.get("screen").width)
setBoxHeight(Math.floor(Math.random() * 299) + 350)
setBoxBottom(Math.floor(Math.random() * (80 - 0 + 1)) + 0)
setBoxGap(Math.floor(Math.random() * (100 - 80 + 1)) + 80)
}
}, 20)
return () => clearInterval(boxTimer)
}, [boxPosHorizontal])
useEffect(() => {
scoreTimer = setInterval(() => {
setScore(score + 10)
}, 500)
return () => clearInterval(scoreTimer)
}, [score])
//Collision checker
useEffect(() => {
const gameOverTimer = setInterval(() => {
if(birdPosVertical === Dimensions.get("screen").height - 40){
start()
}
}, 20)
return () => clearInterval(gameOverTimer)
}, [birdPosVertical, boxPosHorizontal])
function birdJump(){
clearInterval(birdTimer)
setBirdPosVertical(birdPosVertical - 40)
}
return (
<TouchableWithoutFeedback onPress={birdJump} style = {styles.container}>
<ImageBackground
source={require("./assets/bakgrund.png")}
style = {{width: "100%", height: "100%"}}
resizeMode='cover'
>
{gameOn ? (
<>
<Score
score = {score}
/>
<Player
birdPosVertical = {birdPosVertical}
/>
<Blocks
boxPosHorizontal = {boxPosHorizontal}
boxHeight = {boxHeight}
boxBottom = {boxBottom}
boxGap = {boxGap}
/>
</>
): (
<StartScreen gameOn = {gameOn} setGameOn = {setGameOn}/>
)}
</ImageBackground>
</TouchableWithoutFeedback>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
},
});