react-native-games
Version:
Free games for your react native projects.
1 lines • 3.04 kB
JavaScript
;import{create}from 'zustand';import{subscribeWithSelector}from 'zustand/middleware';import{GAME_CONFIG}from "./BalloonBlasterService.js";import{immerMiddleware}from "../../services/UtilsService.js";export const useBalloonBlasterStore = create()(subscribeWithSelector(immerMiddleware((set,get)=>({score:0,timeLeft:60,isPlaying:false,gameOver:false,combo:0,lives:GAME_CONFIG.MAX_LIVES,balloons:[],particles:[],slicePath:[],isSlicing:false,startGame:_gameDuration =>{const duration = 60;set(draft =>{draft.score = 0;draft.timeLeft = duration;draft.isPlaying = true;draft.gameOver = false;draft.combo = 0;draft.lives = GAME_CONFIG.MAX_LIVES;draft.balloons = [];draft.slicePath = [];draft.isSlicing = false;});},stopGame:()=>{set(draft =>{draft.score = 0;draft.timeLeft = 60;draft.isPlaying = false;draft.gameOver = false;draft.combo = 0;draft.lives = GAME_CONFIG.MAX_LIVES;draft.balloons = [];draft.slicePath = [];draft.isSlicing = false;});},resetGame:()=>{set(draft =>{draft.score = 0;draft.timeLeft = 60;draft.isPlaying = false;draft.gameOver = false;draft.combo = 0;draft.lives = GAME_CONFIG.MAX_LIVES;draft.balloons = [];draft.slicePath = [];draft.isSlicing = false;});},popBalloon:balloonId =>{const{balloons,isPlaying}= get();if(!isPlaying)return;let balloonIndex = -1;for(let i = 0;i < balloons.length;i++){if(balloons[i].id === balloonId){balloonIndex = i;break;}}if(balloonIndex === -1 || balloons[balloonIndex].isPopped)return;const points = 10;set(draft =>{draft.score = draft.score + points;if(draft.balloons[balloonIndex]){draft.balloons[balloonIndex].isPopped = true;draft.balloons[balloonIndex].popTime = Date.now();}});},updateScore:points =>{set(draft =>{draft.score = draft.score + points;});},decrementTime:()=>{set(draft =>{const newTimeLeft = draft.timeLeft - 1;if(newTimeLeft <= 0){draft.timeLeft = 0;draft.isPlaying = false;draft.gameOver = true;}else{draft.timeLeft = newTimeLeft;}});},addBalloon:balloon =>{set(draft =>{const{height}= require('react-native').Dimensions.get('window');const correctedBalloon ={...balloon,position:{x:balloon.position.x,y:height + 20}};draft.balloons.push(correctedBalloon);});},removeBalloon:balloonId =>{set(draft =>{draft.balloons = draft.balloons.filter(b => b.id !== balloonId);});},updateBalloon:(balloonId,updates)=>{set(draft =>{for(let i = 0;i < draft.balloons.length;i++){if(draft.balloons[i].id === balloonId){Object.assign(draft.balloons[i],updates);break;}}});},setParticles:particles =>{set(draft =>{draft.particles = particles;});},setSlicePath:path =>{set(draft =>{draft.slicePath = path;});},setIsSlicing:isSlicing =>{set(draft =>{draft.isSlicing = isSlicing;});},incrementCombo:()=>{set(draft =>{draft.combo = draft.combo + 1;});},resetCombo:()=>{set(draft =>{draft.combo = 0;});},loseLife:()=>{set(draft =>{const newLives = draft.lives - 1;if(newLives <= 0){draft.lives = 0;draft.isPlaying = false;draft.gameOver = true;}else{draft.lives = newLives;}});},addLife:()=>{set(draft =>{draft.lives = Math.min(draft.lives + 1,GAME_CONFIG.MAX_LIVES);});}}))));