react-native-games
Version:
Free games for your react native projects.
1 lines • 6.23 kB
JavaScript
"use strict";import{SPACE_FIGHTER_GAME_CONFIG as GAME_CONFIG,SPACE_FIGHTER_COLORS as COLORS}from "./SpaceFighterConstants.js";export{GAME_CONFIG,COLORS};class SpaceFighterGameService{gameTimer = null;asteroidSpawnTimer = null;physicsTimer = null;particles = [];lastCollisionTime = 0;currentAsteroidSpeed = GAME_CONFIG.ASTEROID_SPEED;currentSpawnInterval = GAME_CONFIG.ASTEROID_SPAWN_INTERVAL;constructor(screenWidth,screenHeight){this.width = screenWidth;this.height = screenHeight;}startGame(){this.cleanup();}cleanup(){if(this.gameTimer){clearInterval(this.gameTimer);this.gameTimer = null;}if(this.asteroidSpawnTimer){clearInterval(this.asteroidSpawnTimer);this.asteroidSpawnTimer = null;}if(this.physicsTimer){clearInterval(this.physicsTimer);this.physicsTimer = null;}this.particles = [];this.lastCollisionTime = 0;}startGameTimer(decrementTime){this.gameTimer = setInterval(decrementTime,1000);}updateDifficultySettings(asteroidSpeed,spawnInterval){this.currentAsteroidSpeed = asteroidSpeed;this.currentSpawnInterval = spawnInterval;}startAsteroidSpawning(spawnAsteroid){const initialAsteroids = this.generateAsteroidPair();spawnAsteroid(initialAsteroids);this.asteroidSpawnTimer = setInterval(()=>{const asteroids = this.generateAsteroidPair();spawnAsteroid(asteroids);},this.currentSpawnInterval);}startPhysicsLoop(updateSpacecraft,updateAsteroid,removeAsteroid,getSpacecraft,getAsteroids){this.physicsTimer = setInterval(()=>{this.updatePhysics(updateSpacecraft,updateAsteroid,removeAsteroid,getSpacecraft,getAsteroids);},16);}generateAsteroidPair(){const pairId = `asteroid_pair_${Date.now()}_${Math.random()}`;const minGapX = this.width * 0.1;const maxGapX = this.width * 0.9 - GAME_CONFIG.ASTEROID_GAP;const gapX = Math.random()*(maxGapX - minGapX)+ minGapX;const leftAsteroid ={id:`${pairId}_left`,x:0,y:-250,width:gapX,height:GAME_CONFIG.ASTEROID_WIDTH,type:'left',pairId,passed:false,spawnTime:Date.now()};const rightAsteroid ={id:`${pairId}_right`,x:gapX + GAME_CONFIG.ASTEROID_GAP,y:-250,width:this.width -(gapX + GAME_CONFIG.ASTEROID_GAP),height:GAME_CONFIG.ASTEROID_WIDTH,type:'right',pairId,passed:false,spawnTime:Date.now()};return [leftAsteroid,rightAsteroid];}updatePhysics(updateSpacecraft,updateAsteroid,removeAsteroid,getSpacecraft,getAsteroids){const spacecraft = getSpacecraft();const asteroids = getAsteroids();let newSpacecraft ={...spacecraft};newSpacecraft.y = spacecraft.y;newSpacecraft.velocityY = 0;if(!newSpacecraft.isControlled){newSpacecraft.velocityX = 0;}if(newSpacecraft.x <= newSpacecraft.size / 2){newSpacecraft.x = newSpacecraft.size / 2;newSpacecraft.velocityX = 0;}if(newSpacecraft.x >= this.width - newSpacecraft.size / 2){newSpacecraft.x = this.width - newSpacecraft.size / 2;newSpacecraft.velocityX = 0;}updateSpacecraft(newSpacecraft);for(let i = 0;i < asteroids.length;i++){const asteroid = asteroids[i];if(!asteroid)continue;const newY = asteroid.y + this.currentAsteroidSpeed;if(newY > this.height){removeAsteroid(asteroid.id);}else{updateAsteroid(asteroid.id,{y:newY});}}this.updateParticles();}controlSpacecraft(spacecraft,targetX,_targetY){const minX = spacecraft.size / 2;const maxX = this.width - spacecraft.size / 2;const constrainedX = Math.max(minX,Math.min(maxX,targetX));return{...spacecraft,x:constrainedX,y:spacecraft.y,velocityX:0,velocityY:0,isControlled:true};}releaseSpacecraft(spacecraft){return{...spacecraft,isControlled:false};}checkCollision(spacecraft,asteroids){const currentTime = Date.now();if(currentTime - this.lastCollisionTime < GAME_CONFIG.COLLISION_COOLDOWN){return{collision:false};}for(const asteroid of asteroids){if(this.spacecraftAsteroidCollision(spacecraft,asteroid)){this.lastCollisionTime = currentTime;return{collision:true,asteroidId:asteroid.id};}}return{collision:false};}spacecraftAsteroidCollision(spacecraft,asteroid){const spacecraftLeft = spacecraft.x - spacecraft.size / 2;const spacecraftRight = spacecraft.x + spacecraft.size / 2;const spacecraftTop = spacecraft.y - spacecraft.size / 2;const spacecraftBottom = spacecraft.y + spacecraft.size / 2;const asteroidLeft = asteroid.x;const asteroidRight = asteroid.x + asteroid.width;const asteroidTop = asteroid.y;const asteroidBottom = asteroid.y + asteroid.height;return spacecraftRight > asteroidLeft && spacecraftLeft < asteroidRight && spacecraftBottom > asteroidTop && spacecraftTop < asteroidBottom;}checkAsteroidPass(spacecraft,asteroids){const passedAsteroids = [];for(let i = 0;i < asteroids.length;i++){const asteroid = asteroids[i];if(!asteroid)continue;if(!asteroid.passed && spacecraft.y > asteroid.y + asteroid.height){passedAsteroids.push(asteroid.pairId);}}return passedAsteroids;}createCollisionParticles(x,y){for(let i = 0;i < 15;i++){const particle ={id:`particle_${Date.now()}_${i}`,x,y,velocityX:(Math.random()- 0.5)* 15,velocityY:(Math.random()- 0.5)* 15,color:i < 5 ? '#ff6600':i < 10 ? COLORS.SPACECRAFT:'#ffff00',life:1200,maxLife:1200,size:8 + Math.random()* 12};this.particles.push(particle);}for(let i = 0;i < 5;i++){const bigParticle ={id:`big_particle_${Date.now()}_${i}`,x:x +(Math.random()- 0.5)* 20,y:y +(Math.random()- 0.5)* 20,velocityX:(Math.random()- 0.5)* 8,velocityY:(Math.random()- 0.5)* 8,color:'#ff0000',life:1500,maxLife:1500,size:15 + Math.random()* 10};this.particles.push(bigParticle);}}updateParticles(){const activeParticles = [];for(let i = 0;i < this.particles.length;i++){const particle = this.particles[i];if(!particle)continue;particle.x += particle.velocityX;particle.y += particle.velocityY;particle.velocityY += 0.2;particle.life -= 16;if(particle.life > 0){activeParticles.push(particle);}}this.particles = activeParticles;}getParticles(){return this.particles;}isInCollisionImmunity(){const currentTime = Date.now();return currentTime - this.lastCollisionTime < GAME_CONFIG.COLLISION_COOLDOWN;}getRemainingImmunityTime(){const currentTime = Date.now();const elapsed = currentTime - this.lastCollisionTime;return Math.max(0,GAME_CONFIG.COLLISION_COOLDOWN - elapsed);}getInitialSpacecraft(){return{x:this.width / 2,y:this.height * 0.77,velocityX:0,velocityY:0,size:GAME_CONFIG.SPACECRAFT_SIZE,isControlled:false};}}export const createSpaceFighterService =(width,height)=>{return new SpaceFighterGameService(width,height);};