UNPKG

react-native-games

Version:

Free games for your react native projects.

1 lines 5.03 kB
"use strict";import{create}from 'zustand';import{subscribeWithSelector}from 'zustand/middleware';import{Game2048Service}from "./Game2048Service.js";import{GAME_CONFIG,GAME_2048_SOUNDS,GAME_2048_SETTINGS}from "./Game2048Constants.js";import{immerMiddleware}from "../../services/UtilsService.js";import{playSound}from "../../services/SoundsService.js";import{playHaptic,HapticType}from "../../services/HapticsService.js";export const useGame2048Store = create()(subscribeWithSelector(immerMiddleware((set,get)=>{const gameService = new Game2048Service();return{timeElapsed:0,isPlaying:false,isGameOver:false,isWon:false,isAnimating:false,grid:gameService.createGrid(),nextTileId:1,difficulty:'easy',targetValue:2048,gridSize:4,enableSounds:true,enableHaptics:true,startGame:()=>{set(state =>{state.isPlaying = true;state.timeElapsed = 0;});},stopGame:()=>{const state = get();state.initializeGame(state.difficulty);},resetGame:()=>{const state = get();state.initializeGame(state.difficulty);},initializeGame:(difficulty = 'easy')=>{set(state =>{const difficultySettings = GAME_2048_SETTINGS.DIFFICULTY_SETTINGS[difficulty];state.grid = gameService.createGrid();let tileId = 1;const tile1 = gameService.addRandomTile(state.grid,tileId++);const tile2 = gameService.addRandomTile(state.grid,tileId++);state.timeElapsed = 0;state.isPlaying = false;state.isGameOver = false;state.isWon = false;state.isAnimating = false;state.nextTileId = tileId;state.difficulty = difficulty;state.targetValue = difficultySettings.targetTile;state.gridSize = difficultySettings.gridSize;[tile1,tile2].forEach(tile =>{if(tile?.scaleValue){setTimeout(()=>{tile.scaleValue?.setValue(0);const{Animated}= require('react-native');Animated.spring(tile.scaleValue,{toValue:1,useNativeDriver:true,tension:GAME_CONFIG.ANIMATION_CONFIG.TENSION,friction:GAME_CONFIG.ANIMATION_CONFIG.FRICTION}).start();},100);}});});},updateGrid:newGrid =>{set(state =>{state.grid = newGrid;});},incrementTime:()=>{set(state =>{state.timeElapsed += 1;});},setIsAnimating:animating =>{set(state =>{state.isAnimating = animating;});},setGameOver:gameOver =>{set(state =>{state.isGameOver = gameOver;if(gameOver){state.isPlaying = false;}});},setWon:won =>{set(state =>{state.isWon = won;});},incrementTileId:()=>{const state = get();const newId = state.nextTileId;set(draft =>{draft.nextTileId = newId + 1;});return newId;},handleMove:direction =>{const state = get();if(state.isGameOver || state.isWon || state.isAnimating){return false;}set(draft =>{if(!draft.isPlaying){draft.isPlaying = true;}draft.isAnimating = true;});const gridCopy = state.grid.map(row => row.slice());const{moved,score:moveScore}= gameService.moveTiles(gridCopy,direction);if(moved){const newTileId = state.nextTileId;const newTile = gameService.addRandomTile(gridCopy,newTileId);const hasWon = gameService.hasWon(gridCopy,state.targetValue);const isGameOver = gameService.checkGameOver(gridCopy);set(draft =>{draft.grid = gridCopy;draft.nextTileId = newTileId + 1;if(hasWon)draft.isWon = true;if(isGameOver)draft.isGameOver = true;});Promise.resolve().then(()=>{playSound(GAME_2048_SOUNDS.TILE_MOVE,state.enableSounds);playHaptic(HapticType.LIGHT,state.enableHaptics);if(moveScore > 0){playSound(GAME_2048_SOUNDS.TILE_MERGE,state.enableSounds);playHaptic(HapticType.MEDIUM,state.enableHaptics);}if(newTile){playSound(GAME_2048_SOUNDS.NEW_TILE,state.enableSounds);}if(hasWon){playSound(GAME_2048_SOUNDS.GAME_WIN,state.enableSounds);playHaptic(HapticType.SUCCESS,state.enableHaptics);}else if(isGameOver){playSound(GAME_2048_SOUNDS.GAME_OVER,state.enableSounds);playHaptic(HapticType.ERROR,state.enableHaptics);}});if(newTile?.scaleValue){const{Animated}= require('react-native');newTile.scaleValue.setValue(0);Animated.spring(newTile.scaleValue,{toValue:1,useNativeDriver:true,tension:GAME_CONFIG.ANIMATION_CONFIG.TENSION,friction:GAME_CONFIG.ANIMATION_CONFIG.FRICTION}).start(()=>{set(draft =>{draft.isAnimating = false;});});}else{set(draft =>{draft.isAnimating = false;});}return true;}else{Promise.resolve().then(()=>{playSound(GAME_2048_SOUNDS.INVALID_MOVE,state.enableSounds);playHaptic(HapticType.WARNING,state.enableHaptics);});set(draft =>{draft.isAnimating = false;});return false;}},updateSettings:(enableSounds,enableHaptics)=>{set(state =>{state.enableSounds = enableSounds;state.enableHaptics = enableHaptics;});}};})));export const useTimeElapsed =()=> useGame2048Store(state => state.timeElapsed);export const useGrid =()=> useGame2048Store(state => state.grid);export const useIsPlaying =()=> useGame2048Store(state => state.isPlaying);export const useIsGameOver =()=> useGame2048Store(state => state.isGameOver);export const useIsWon =()=> useGame2048Store(state => state.isWon);export const useIsAnimating =()=> useGame2048Store(state => state.isAnimating);export const useDifficulty =()=> useGame2048Store(state => state.difficulty);export const useTargetValue =()=> useGame2048Store(state => state.targetValue);export const useGridSize =()=> useGame2048Store(state => state.gridSize);