-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShopScreen.js
More file actions
257 lines (249 loc) · 7.43 KB
/
ShopScreen.js
File metadata and controls
257 lines (249 loc) · 7.43 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import React, { useContext } from 'react';
import { StyleSheet, View, Text, TouchableOpacity, Image, ScrollView } from 'react-native';
import { CoinContext } from './CoinContext';
import { CollectionContext } from './CollectionContext';
import { characters, eggs } from './data';
const ShopScreen = () => {
const { coins, deductCoins } = useContext(CoinContext);
const { addCharacter } = useContext(CollectionContext);
const buyEgg = (egg) => {
if (coins >= egg.cost) {
deductCoins(egg.cost);
let roll = Math.random();
let character = null;
for (let i = 0; i < egg.probabilities.length; i++) {
if (roll <= egg.probabilities[i]) {
character = characters.find((c) => c.level === i + 1);
break;
}
roll -= egg.probabilities[i];
}
addCharacter(character);
alert(`You got a ${character.name}!`);
} else {
alert('Not enough coins!');
}
};
const buyItem = (price) => {
if (coins >= price) {
deductCoins(price);
alert('Item purchased!');
} else {
alert('Not enough coins!');
}
};
return (
<ScrollView style={styles.container}>
<View style={styles.section}>
<Text style={styles.sectionHeader}>Fit-Buddies</Text>
{characters.map((c) => (
<View key={c.id} style={[ styles.item, {flexDirection: "column", alignItems: "center"}]}>
<Image style={styles.charImage} source={c.image} />
<View style={[styles.itemSub, {alignItems: "center"}]}>
<Text style={styles.itemName}>{c.name}</Text>
<Text style={styles.itemPrice}>{c.buff} coins per run</Text>
</View>
<TouchableOpacity
style={styles.itemButton}
onPress={() => alert("Leveled Up!")}
>
<Text style={styles.itemButtonText}>Level Up</Text>
</TouchableOpacity>
</View>
))}
</View>
<View style={styles.section}>
<Text style={styles.sectionHeader}>Eggs</Text>
{eggs.map((e) => (
<View key={e.id} style={styles.item}>
<View key={e.id} style={styles.itemSub}>
<Text style={styles.itemName}>Tier {e.tier} Egg</Text>
<Text style={styles.itemPrice}>{e.cost} coins</Text>
</View>
<TouchableOpacity
style={styles.itemButton}
onPress={() => buyEgg(e)}
>
<Text style={styles.itemButtonText}>Buy</Text>
</TouchableOpacity>
</View>
))}
</View>
<View style={styles.section}>
<Text style={styles.sectionHeader}>Items</Text>
<View style={styles.item}>
<View style={styles.itemSub}>
<Text style={styles.itemName}>Headband</Text>
<Text style={styles.itemPrice}>100k coins</Text>
</View>
<TouchableOpacity
style={styles.itemButton}
onPress={() => buyItem(100000)}
>
<Text style={styles.itemButtonText}>Buy</Text>
</TouchableOpacity>
</View>
<View style={styles.item}>
<View style={styles.itemSub}>
<Text style={styles.itemName}>Fitbit</Text>
<Text style={styles.itemPrice}>2M coins</Text>
</View>
<TouchableOpacity
style={styles.itemButton}
onPress={() => buyItem(2000000)}
>
<Text style={styles.itemButtonText}>Buy</Text>
</TouchableOpacity>
</View>
<View style={styles.item}>
<View style={styles.itemSub}>
<Text style={styles.itemName}>Water Bottle</Text>
<Text style={styles.itemPrice}>100k coins</Text>
</View>
<TouchableOpacity
style={styles.itemButton}
onPress={() => buyItem(100000)}
>
<Text style={styles.itemButtonText}>Buy</Text>
</TouchableOpacity>
</View>
<View style={styles.item}>
<View style={styles.itemSub}>
<Text style={styles.itemName}>Deodorant</Text>
<Text style={styles.itemPrice}>200k coins</Text>
</View>
<TouchableOpacity
style={styles.itemButton}
onPress={() => buyItem(200000)}
>
<Text style={styles.itemButtonText}>Buy</Text>
</TouchableOpacity>
</View>
<View style={styles.item}>
<View style={styles.itemSub}>
<Text style={styles.itemName}>$5 Gift Card</Text>
<Text style={styles.itemPrice}>200k coins</Text>
</View>
<TouchableOpacity
style={styles.itemButton}
onPress={() => buyItem(200000)}
>
<Text style={styles.itemButtonText}>Buy</Text>
</TouchableOpacity>
</View>
<View style={styles.item}>
<View style={styles.itemSub}>
<Text style={styles.itemName}>$10 Gift Card</Text>
<Text style={styles.itemPrice}>400k coins</Text>
</View>
<TouchableOpacity
style={styles.itemButton}
onPress={() => buyItem(400000)}
>
<Text style={styles.itemButtonText}>Buy</Text>
</TouchableOpacity>
</View>
<View style={styles.item}>
<View style={styles.itemSub}>
<Text style={styles.itemName}>$25 Gift Card</Text>
<Text style={styles.itemPrice}>800k coins</Text>
</View>
<TouchableOpacity
style={styles.itemButton}
onPress={() => buyItem(800000)}
>
<Text style={styles.itemButtonText}>Buy</Text>
</TouchableOpacity>
</View>
<View style={styles.item}>
<View style={styles.itemSub}>
<Text style={styles.itemName}>$100 Gift Card</Text>
<Text style={styles.itemPrice}>2M coins</Text>
</View>
<TouchableOpacity
style={styles.itemButton}
onPress={() => buyItem(2000000)}
>
<Text style={styles.itemButtonText}>Buy</Text>
</TouchableOpacity>
</View>
</View>
</ScrollView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
width: "100%",
backgroundColor: "#fff",
},
eggSection: {
margin: 10,
},
eggTier: {
margin: 10,
},
eggTierTitle: {
fontSize: 20,
fontWeight: "bold",
marginBottom: 5,
},
egg: {
flexDirection: "row",
alignItems: "center",
marginBottom: 5,
},
eggText: {
fontSize: 18,
},
shopSection: {
margin: 10,
},
item: {
flexDirection: "row",
alignItems: "center",
justifyContent: "space-between",
marginVertical: 5,
padding: 5,
backgroundColor: "#EEE",
borderRadius: 5,
},
itemSub: {
flexDirection: "column",
alignItems: "flex-start",
margin: 5,
padding: 5,
},
itemName: {
fontSize: 18,
fontWeight: "bold",
marginBottom: 5,
},
itemPrice: {
fontSize: 16,
color: "#777777",
},
itemButton: {
backgroundColor: "#44AEEF",
paddingVertical: 10,
paddingHorizontal: 20,
marginRight: 10,
borderRadius: 5,
alignItems: "center",
},
itemButtonText: {
color: "#FFFFFF",
fontSize: 16,
},
sectionHeader: {
fontWeight: "bold",
fontSize: 16,
},
charImage: {
height: 200,
width: 200,
borderRadius: 5,
resizeMode: "contain"
}
});
export default ShopScreen;