react-native-games
Version:
Free games for your react native projects.
1 lines • 3.44 kB
JavaScript
;import{create}from 'zustand';import{subscribeWithSelector}from 'zustand/middleware';import{Dimensions}from 'react-native';import{GAME_CONFIG}from "./SpaceFighterService.js";import{immerMiddleware}from "../../services/UtilsService.js";const{width,height}= Dimensions.get('window');const getSpacecraftYPosition =(offset = 0)=>{const yPercent = offset > 0 ? 0.77:0.68;return height * yPercent;};export const useSpaceFighterStore = create()(subscribeWithSelector(immerMiddleware((set,_get)=>({score:0,timeLeft:60,isPlaying:false,gameOver:false,lives:3,asteroids:[],spacecraft:{x:width / 2,y:getSpacecraftYPosition(0),velocityX:0,velocityY:0,size:GAME_CONFIG.SPACECRAFT_SIZE,isControlled:false},spacecraftPath:[],isControllingSpacecraft:false,offset:0,setOffset:offset =>{set(draft =>{draft.offset = offset;draft.spacecraft.y = getSpacecraftYPosition(offset);});},startGame:_gameDuration =>{const duration = 60;set(draft =>{draft.score = 0;draft.timeLeft = duration;draft.isPlaying = true;draft.gameOver = false;draft.lives = 3;draft.asteroids = [];draft.spacecraftPath = [];draft.isControllingSpacecraft = false;draft.spacecraft ={x:width / 2,y:getSpacecraftYPosition(draft.offset),velocityX:0,velocityY:0,size:GAME_CONFIG.SPACECRAFT_SIZE,isControlled:false};});},stopGame:()=>{set(draft =>{draft.score = 0;draft.timeLeft = 60;draft.isPlaying = false;draft.gameOver = false;draft.lives = 3;draft.asteroids = [];draft.spacecraftPath = [];draft.isControllingSpacecraft = false;draft.spacecraft ={x:width / 2,y:getSpacecraftYPosition(draft.offset),velocityX:0,velocityY:0,size:GAME_CONFIG.SPACECRAFT_SIZE,isControlled:false};});},resetGame:()=>{set(draft =>{draft.score = 0;draft.timeLeft = 60;draft.isPlaying = false;draft.gameOver = false;draft.lives = 3;draft.asteroids = [];draft.spacecraftPath = [];draft.isControllingSpacecraft = false;draft.spacecraft ={x:width / 2,y:getSpacecraftYPosition(draft.offset),velocityX:0,velocityY:0,size:GAME_CONFIG.SPACECRAFT_SIZE,isControlled:false};});},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;}});},addAsteroids:asteroids =>{set(draft =>{draft.asteroids.push(...asteroids);});},removeAsteroid:asteroidId =>{set(draft =>{draft.asteroids = draft.asteroids.filter(a => a.id !== asteroidId);});},updateAsteroid:(asteroidId,updates)=>{set(draft =>{for(let i = 0;i < draft.asteroids.length;i++){if(draft.asteroids[i].id === asteroidId){Object.assign(draft.asteroids[i],updates);break;}}});},updateSpacecraft:spacecraft =>{set(draft =>{draft.spacecraft = spacecraft;});},setSpacecraftPath:path =>{set(draft =>{draft.spacecraftPath = path;});},setIsControllingSpacecraft:isControlling =>{set(draft =>{draft.isControllingSpacecraft = isControlling;});},markAsteroidPassed:pairId =>{set(draft =>{for(let i = 0;i < draft.asteroids.length;i++){if(draft.asteroids[i].pairId === pairId){draft.asteroids[i].passed = true;}}draft.score = draft.score + 10;});},loseLife:()=>{set(draft =>{const newLives = draft.lives - 1;draft.spacecraft ={x:width / 2,y:getSpacecraftYPosition(draft.offset),velocityX:0,velocityY:0,size:GAME_CONFIG.SPACECRAFT_SIZE,isControlled:false};if(newLives <= 0){draft.lives = 0;draft.isPlaying = false;draft.gameOver = true;}else{draft.lives = newLives;}});}}))));