Skip to content
Permalink
Browse files

Persisting data - storing best time and high score

  • Loading branch information...
dandesz198 committed Feb 12, 2019
1 parent 539efb8 commit bc34e9c2997a1bd8506820c48866d43ac7d8c9c4
Showing with 53 additions and 10 deletions.
  1. +23 −6 screens/Game/index.js
  2. +5 −2 screens/Home/index.js
  3. +3 −2 utilities/index.js
  4. +22 −0 utilities/storage.js
@@ -9,13 +9,20 @@ import {
} from "react-native";
import { Audio } from "expo";
import { Header } from "../../components";
import { generateRGB, mutateRGB } from "../../utilities";
import {
generateRGB,
mutateRGB,
storeData,
retrieveData
} from "../../utilities";
import styles from "./styles";

export default class Home extends Component {
export default class Game extends Component {
state = {
points: 0,
bestPoints: 0, // < new
timeLeft: 15,
bestTime: 0, // < new
rgb: generateRGB(),
size: 2,
gameState: "INGAME", // three possible states: 'INGAME', 'PAUSED' and 'LOST'
@@ -24,14 +31,24 @@ export default class Home extends Component {

async componentWillMount() {
this.generateNewRound();
this.interval = setInterval(() => {
retrieveData('highScore').then(val => this.setState({ bestPoints: val }));
retrieveData('bestTime').then(val => this.setState({ bestTime: val }));
this.interval = setInterval(async () => {
if (this.state.gameState === "INGAME") {
if (this.state.timeLeft > this.state.bestTime) {
this.setState(state => ({ bestTime: state.timeLeft }));
storeData('bestTime', this.state.timeLeft);
}
if (this.state.timeLeft <= 0) {
this.loseFX.replayAsync();
this.backgroundMusic.stopAsync();
if (this.state.points > this.state.bestPoints) {
this.setState(state => ({ bestPoints: state.points }));
storeData('highScore', this.state.points);
}
this.setState({ gameState: "LOST" });
} else {
this.setState({ timeLeft: this.state.timeLeft - 1 });
this.setState(state => ({ timeLeft: state.timeLeft - 1 }));
}
}
}, 1000);
@@ -239,7 +256,7 @@ export default class Home extends Component {
source={require("../../assets/icons/trophy.png")}
style={styles.bestIcon}
/>
<Text style={styles.bestLabel}>0</Text>
<Text style={styles.bestLabel}>{this.state.bestPoints}</Text>
</View>
</View>
<View style={{ flex: 1 }}>
@@ -258,7 +275,7 @@ export default class Home extends Component {
source={require("../../assets/icons/clock.png")}
style={styles.bestIcon}
/>
<Text style={styles.bestLabel}>0</Text>
<Text style={styles.bestLabel}>{this.state.bestTime}</Text>
</View>
</View>
</View>
@@ -2,16 +2,19 @@ import React, { Component } from "react";
import { View, Text, Image, TouchableOpacity } from "react-native";
import { Audio } from "expo";
import { Header } from "../../components";
import { retrieveData } from '../../utilities';
import styles from "./styles";

export default class Home extends Component {
state = {
isSoundOn: true
isSoundOn: true,
highScore: 0
};

async componentWillMount() {
this.backgroundMusic = new Audio.Sound();
this.buttonFX = new Audio.Sound();
retrieveData('highScore').then(val => this.setState({ highScore: val }));
try {
await this.backgroundMusic.loadAsync(
require("../../assets/music/Komiku_Mushrooms.mp3")
@@ -64,7 +67,7 @@ export default class Home extends Component {
source={require("../../assets/icons/trophy.png")}
style={styles.trophyIcon}
/>
<Text style={styles.hiscore}>Hi-score: 0</Text>
<Text style={styles.hiscore}>Hi-score: {this.state.highScore}</Text>
</View>
<TouchableOpacity
onPress={this.onLeaderboardPress}
@@ -1,3 +1,4 @@
export * from './color'
export * from './color';
export * from './storage';

export default {}
export default {};
@@ -0,0 +1,22 @@
import { AsyncStorage } from "react-native";

export const storeData = async (key, value) => {
try {
await AsyncStorage.setItem(`@ColorBlinder:${key}`, String(value));
} catch (error) {
console.log(error);
}
};

export const retrieveData = async key => {
try {
const value = await AsyncStorage.getItem(`@ColorBlinder:${key}`);
if (value !== null) {
return value;
}
} catch (error) {
console.log(error);
}
};

export default {};

0 comments on commit bc34e9c

Please sign in to comment.
You can’t perform that action at this time.