Skip to content

Commit

Permalink
fix: increase space listening to slides
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreBellas committed May 5, 2024
1 parent b4cec93 commit 544534c
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 136 deletions.
26 changes: 2 additions & 24 deletions src/layouts/WebLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { BoardProvider } from '@contexts/BoardContext'
import { Box, Image, View } from '@gluestack-ui/themed'
import { IBoard } from '@interfaces/board'
import BoardDatabaseService from '@services/database/board.database'
import Board from '@views/Game/Board'
import MainPage from '@views/Game/MainPage'
import { useEffect, useState } from 'react'
import { BrowserView } from 'react-device-detect'

export default function WebLayout() {
const [board, setBoard] = useState<IBoard>()
Expand All @@ -21,27 +19,7 @@ export default function WebLayout() {

return (
<BoardProvider board={board}>
<View h="$full" w="$full" alignItems="center">
<BrowserView renderWithFragment>
<Image
w="$full"
h="$full"
position="absolute"
source={require('@assets/background.png')}
resizeMode="cover"
opacity="$20"
/>
</BrowserView>
<Box
backgroundColor="$white"
borderRadius="$2xl"
borderWidth="$4"
borderColor="$white"
p="$4"
>
<Board />
</Box>
</View>
<MainPage/>
</BoardProvider>
)
}
113 changes: 1 addition & 112 deletions src/views/Game/Board.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,132 +8,22 @@ import {
VStack,
View,
} from '@gluestack-ui/themed'
import { IDirection } from '@interfaces/direction'
import BoardDatabaseService from '@services/database/board.database'
import { RotateCcw } from 'lucide-react-native'
import { useEffect } from 'react'
import { Alert, Platform } from 'react-native'
import {
Directions,
Gesture,
GestureDetector,
} from 'react-native-gesture-handler'

import Tile from './Tile'

interface IGestureData {
[direction: string]: Directions
}

const gestureData: IGestureData = {
up: Directions.UP,
down: Directions.DOWN,
left: Directions.LEFT,
right: Directions.RIGHT,
} as const

export default function Board() {
// #region Contexts
const { board, isGameOver, hasWon, numOfMoves } = useBoard()
const { board } = useBoard()
const boardDispatch = useBoardDispatch()
// #endregion

// #region Constant values
const space = 'md'
// #endregion

// #region Animation
const gestures = Object.entries(gestureData).map(
([direction, gestureDirection]) =>
Gesture.Fling()
.direction(gestureDirection)
.numberOfPointers(1)
.shouldCancelWhenOutside(true)
.runOnJS(true)
.onEnd(() => {
boardDispatch({ type: 'move', direction: direction as IDirection })
}),
)
const moveGesture = Gesture.Race(...gestures)
// #endregion

// #region Effects

// onKeyboardMove
useEffect(() => {
if (Platform.OS !== 'web') return

function keyDownHandler(e: KeyboardEvent) {
const keyMap: Record<string, IDirection> = {
ArrowUp: 'up',
ArrowDown: 'down',
ArrowLeft: 'left',
ArrowRight: 'right',
}

const direction = keyMap[e.key]
if (direction === undefined) return

e.preventDefault()
boardDispatch({ type: 'move', direction })
}

window.addEventListener('keydown', keyDownHandler)
return () => window.removeEventListener('keydown', keyDownHandler)
}, [boardDispatch])

// onAfterMove
useEffect(() => {
if (isGameOver) return
boardDispatch({ type: 'insert' })
BoardDatabaseService.save(board)
}, [board, boardDispatch, numOfMoves, isGameOver])

// onEndGame
useEffect(() => {
if (!isGameOver) return

setTimeout(() => {
if (hasWon) {
if (Platform.OS === 'web') {
alert('Congratulations! ❤️ You deserve 2040 lickisses 🐶')
return
}

Alert.alert('Congratulations! ❤️', 'You deserve 2040 lickisses 🐶', [
{
text: 'Yay!',
},
{
text: 'Replay',
onPress: () => {
boardDispatch({ type: 'restart' })
},
},
])
return
}

if (Platform.OS === 'web' && confirm('You lost 🥺. Try again?')) {
boardDispatch({ type: 'restart' })
return
}

Alert.alert('You lost 🥺', 'Try again?', [
{
text: 'Yessss!',
onPress: () => {
boardDispatch({ type: 'restart' })
},
},
])
}, 100)
}, [boardDispatch, isGameOver, hasWon])

// #endregion

return (
<GestureDetector gesture={moveGesture}>
<Box alignItems="center" width="$full">
<View w="$full" alignItems="flex-end">
<Button
Expand Down Expand Up @@ -198,6 +88,5 @@ export default function Board() {
))}
</VStack>
</Box>
</GestureDetector>
)
}
146 changes: 146 additions & 0 deletions src/views/Game/MainPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import { Image } from "@gluestack-ui/themed";
import { Box } from "@gluestack-ui/themed";
import { View } from "@gluestack-ui/themed";
import { BrowserView } from "react-device-detect";
import Board from "./Board";
import { Directions, Gesture, GestureDetector } from "react-native-gesture-handler";
import { useBoard, useBoardDispatch } from "@contexts/BoardContext";
import { IDirection } from "@interfaces/direction";
import { useEffect } from "react";
import { Alert, Platform } from "react-native";
import BoardDatabaseService from "@services/database/board.database";

interface IGestureData {
[direction: string]: Directions
}

const gestureData: IGestureData = {
up: Directions.UP,
down: Directions.DOWN,
left: Directions.LEFT,
right: Directions.RIGHT,
} as const

export default function MainPage() {
// #region Contexts
const { board, isGameOver, numOfMoves, hasWon } = useBoard()
const boardDispatch = useBoardDispatch()
// #endregion

// #region Gestures
const gestures = Object.entries(gestureData).map(
([direction, gestureDirection]) =>
Gesture.Fling()
.direction(gestureDirection)
.numberOfPointers(1)
.shouldCancelWhenOutside(true)
.runOnJS(true)
.onEnd(() => {
boardDispatch({ type: 'move', direction: direction as IDirection })
}),
)
const moveGesture = Gesture.Race(...gestures)
// #endregion

// #region Effects

// onKeyboardMove
useEffect(() => {
if (Platform.OS !== 'web') return

function keyDownHandler(e: KeyboardEvent) {
const keyMap: Record<string, IDirection> = {
ArrowUp: 'up',
ArrowDown: 'down',
ArrowLeft: 'left',
ArrowRight: 'right',
}

const direction = keyMap[e.key]
if (direction === undefined) return

e.preventDefault()
boardDispatch({ type: 'move', direction })
}

window.addEventListener('keydown', keyDownHandler)
return () => window.removeEventListener('keydown', keyDownHandler)
}, [boardDispatch])

// onAfterMove
useEffect(() => {
if (isGameOver) return
boardDispatch({ type: 'insert' })
BoardDatabaseService.save(board)
}, [board, boardDispatch, numOfMoves, isGameOver])

// onEndGame
useEffect(() => {
if (!isGameOver) return

setTimeout(() => {
if (hasWon) {
if (Platform.OS === 'web') {
alert('Congratulations! ❤️ You deserve 2040 lickisses 🐶')
return
}

Alert.alert('Congratulations! ❤️', 'You deserve 2040 lickisses 🐶', [
{
text: 'Yay!',
},
{
text: 'Replay',
onPress: () => {
boardDispatch({ type: 'restart' })
},
},
])
return
}

if (Platform.OS === 'web' && confirm('You lost 🥺. Try again?')) {
boardDispatch({ type: 'restart' })
return
}

Alert.alert('You lost 🥺', 'Try again?', [
{
text: 'Yessss!',
onPress: () => {
boardDispatch({ type: 'restart' })
},
},
])
}, 100)
}, [boardDispatch, isGameOver, hasWon])

// #endregion


return (
<GestureDetector gesture={moveGesture}>
<View h="$full" w="$full" alignItems="center">
<BrowserView renderWithFragment>
<Image
w="$full"
h="$full"
position="absolute"
source={require('@assets/background.png')}
resizeMode="cover"
opacity="$20"
/>
</BrowserView>
<Box
backgroundColor="$white"
borderRadius="$2xl"
borderWidth="$4"
borderColor="$white"
p="$4"
>
<Board />
</Box>
</View>
</GestureDetector>
)
}

0 comments on commit 544534c

Please sign in to comment.