1- import KeyHandler , { KEYDOWN } from 'react-key-handler'
21import React from 'react'
3- import Swipeable from 'react-swipeable'
42import Head from 'next/head'
53import game from '../lib/game'
64import GameBoard from '../components/GameBoard'
5+ import GameControls from '../components/GameControls'
76
87export default class Page extends React . Component {
9- constructor ( props ) {
8+ constructor ( ) {
109 super ( )
1110 this . game = game ( )
1211 this . game . onUpdate ( s => this . setState ( s ) )
1312 }
1413
1514 componentDidMount ( ) {
16- this . loadHighScore ( )
17- }
18-
19- loadHighScore ( ) {
2015 if ( typeof window === 'undefined' ) return
2116 this . game . setHighScore ( window . localStorage [ 'score' ] || 0 )
2217 }
2318
24- isCloseToAngle ( angle , target ) {
25- const tolerance = 0.6
26- return target - tolerance < angle && angle < target + tolerance
27- }
28-
29- swiped ( e , deltaX , deltaY , isFlick , velocity ) {
30- if ( ! isFlick ) return
31- e . preventDefault ( )
32-
33- const angle = Math . atan2 ( deltaY , deltaX )
34- console . log ( angle )
35- if ( this . isCloseToAngle ( angle , 0 ) ) {
36- this . game . moveDotsLeft ( )
37- } else if ( this . isCloseToAngle ( angle , Math . PI ) ) {
38- this . game . moveDotsRight ( )
39- } else if ( this . isCloseToAngle ( angle , Math . PI / 2 ) ) {
40- this . game . rotateDots ( )
41- } else if ( this . isCloseToAngle ( angle , - Math . PI / 2 ) ) {
42- this . game . pushDots ( )
43- }
44- }
45-
4619 render ( ) {
4720 return (
48- < Swipeable onSwiped = { this . swiped . bind ( this ) } >
49- < KeyHandler
50- keyEventName = { KEYDOWN }
51- keyValue = 'ArrowDown'
52- onKeyHandle = { ( ) => this . game . pushDots ( ) }
53- />
54- < KeyHandler
55- keyEventName = { KEYDOWN }
56- keyValue = 'ArrowLeft'
57- onKeyHandle = { ( ) => this . game . moveDotsLeft ( ) }
58- />
59- < KeyHandler
60- keyEventName = { KEYDOWN }
61- keyValue = 'ArrowRight'
62- onKeyHandle = { ( ) => this . game . moveDotsRight ( ) }
63- />
64- < KeyHandler
65- keyEventName = { KEYDOWN }
66- keyValue = 'ArrowUp'
67- onKeyHandle = { ( ) => this . game . rotateDots ( ) }
68- />
21+ < GameControls
22+ up = { this . game . rotateDots }
23+ down = { this . game . pushDots }
24+ left = { this . game . moveDotsLeft }
25+ right = { this . game . moveDotsRight }
26+ >
6927 < Head >
7028 < title > Combine!</ title >
7129 < meta charSet = 'utf-8' />
@@ -75,16 +33,31 @@ export default class Page extends React.Component {
7533 />
7634 </ Head >
7735
78- < GameBoard
79- width = { 500 }
80- colors = { this . game . colors ( ) }
81- score = { this . game . score ( ) }
82- highScore = { this . game . highScore ( ) }
83- gameover = { this . game . gameover ( ) }
84- level = { this . game . level ( ) }
85- balls = { this . game . balls ( ) }
86- />
87- </ Swipeable >
36+ < div
37+ style = { {
38+ display : 'flex' ,
39+ alignItems : 'center' ,
40+ justifyContent : 'center' ,
41+ position : 'fixed' ,
42+ background : '#f5f5f5' ,
43+ margin : 0 ,
44+ top : 0 ,
45+ bottom : 0 ,
46+ left : 0 ,
47+ right : 0
48+ } }
49+ >
50+ < GameBoard
51+ width = { 500 }
52+ colors = { this . game . colors ( ) }
53+ score = { this . game . score ( ) }
54+ highScore = { this . game . highScore ( ) }
55+ gameover = { this . game . gameover ( ) }
56+ level = { this . game . level ( ) }
57+ balls = { this . game . balls ( ) }
58+ />
59+ </ div >
60+ </ GameControls >
8861 )
8962 }
9063}
0 commit comments