Skip to content

Commit 4c8f338

Browse files
committed
Add highscore
1 parent 3658a98 commit 4c8f338

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

pages/index.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class Page extends React.Component {
7474
this.state = {
7575
dots: [],
7676
score: 0,
77+
highScore: this.getHighScore(),
7778
floatingDots: [this.newDot(4, 1), this.newDot(5, 0)],
7879
level: 2,
7980
blocksKilled: 0
@@ -222,6 +223,21 @@ class Page extends React.Component {
222223
floatingDots: []
223224
})
224225
}
226+
227+
setHighScore () {
228+
if (typeof window === 'undefined') return
229+
const localStorage = window.localStorage
230+
if (!localStorage) return
231+
localStorage['score'] = Math.max(this.state.score, this.state.highScore)
232+
}
233+
234+
getHighScore () {
235+
if (typeof window === 'undefined') return
236+
const localStorage = window.localStorage
237+
if (!localStorage) return
238+
return localStorage['score'] || 0
239+
}
240+
225241
pushDots () {
226242
const floatingDots = this.state.floatingDots
227243
if (!floatingDots.length) return
@@ -463,11 +479,12 @@ class Page extends React.Component {
463479
fontFamily: 'Open sans, sans-serif',
464480
color: '#aaa',
465481
fontWeight: 'bold',
466-
left: '5%',
482+
right: '105%',
467483
top: '5%'
468484
}}
469485
>
470-
SCORE: { this.state.score }
486+
<div>SCORE: { this.state.score }</div>
487+
<div>HIGHSCORE: { this.state.highScore }</div>
471488
</div>
472489
<div
473490
style={{

0 commit comments

Comments
 (0)