// Create a particleGraph ParticleGraph createString(glm::vec2 A, glm::vec2 B, glm::vec3 color, glm::vec3 headColor, uint32_t discFactor, ParticleManager& particleManager) { glm::vec2 AB = B - A; glm::vec2 step = glm::vec2(AB[0]/discFactor, AB[1]/discFactor); glm::vec2 position = A; float mass = 1.f; ParticleGraph graph; unsigned int id = 0; for(int i = 0; i < discFactor + 1; ++i) { id = particleManager.addParticle(mass, position, glm::vec2(0.f, 0.f), glm::vec2(0.f, 0.f), color, ParticleManager::Type::P_SNAKE); if(id != 0) { std::pair<unsigned int, unsigned int> pair (id, id - 1); graph.push_back(pair); } else if(id == discFactor) { std::pair<unsigned int, unsigned int> pair (id, 0); graph.push_back(pair); } position += step; } particleManager.getParticleColor(0) = headColor; return graph; }
void StartMeny::update(){ mLoadButton->update(); for (std::vector<Button*>::iterator i = mButtons.begin(); i != mButtons.end(); i++){ (*i)->update(); } //if (sf::Keyboard::isKeyPressed(sf::Keyboard::P)){ // StateManager::getInst().addState(new OptionsMeny); //} //if (sf::Keyboard::isKeyPressed(sf::Keyboard::Num1)){ // EventManager::getInst().addEvent("cutscene_intro"); //} //if (sf::Keyboard::isKeyPressed(sf::Keyboard::Num2)){ // EventManager::getInst().addEvent("cutscene_2"); //} //if (sf::Keyboard::isKeyPressed(sf::Keyboard::Num3)){ // EventManager::getInst().addEvent("cutscene_3"); //} //if (sf::Keyboard::isKeyPressed(sf::Keyboard::Num4)){ // EventManager::getInst().addEvent("cutscene_ending"); //} ParticleManager* particleManager = &ParticleManager::getInst(); particleManager->update(); }
// Check if the snake is outside bool isOutside(ParticleManager& snakeManager) { for(int i = 0; i < snakeManager.getCount(); ++i) { //Right if(snakeManager.getParticleX(i) >= 1.f) { return true; } // Left if(snakeManager.getParticleX(i) <= -1.f) { return true; } // Up if(snakeManager.getParticleY(i) >= 1.f) { return true; } // Down if(snakeManager.getParticleY(i) <= -1.f) { return true; } } return false; }
void updateParticle(ParticleManager& sourceManager, int sourceId, ParticleManager& destinationManager, int destinationId) { destinationManager.getParticleMass(destinationId) = sourceManager.getParticleMass(sourceId); destinationManager.getParticlePosition(destinationId) = sourceManager.getParticlePosition(sourceId); destinationManager.getParticleVelocity(destinationId) = sourceManager.getParticleVelocity(sourceId); destinationManager.getParticleForce(destinationId) = sourceManager.getParticleForce(sourceId); destinationManager.getParticleColor(destinationId) = sourceManager.getParticleColor(sourceId); }
/*----------------------------------------------------------------------------------------------- Description: This is the rendering function. It tells OpenGL to clear out some color and depth buffers, to set up the data to draw, to draw than stuff, and to report any errors that it came across. This is not a user-called function. This function is registered with glutDisplayFunc(...) during glut's initialization. Parameters: None Returns: None Exception: Safe Creator: John Cox (2-13-2016) -----------------------------------------------------------------------------------------------*/ void Display() { glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepth(1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // in the absence of an actual timer, use a hard-coded delta time gParticleManager.Update(0.01f); // this handles its own bindings and cleans up when it is done gParticleManager.Render(); // tell the GPU to swap out the displayed buffer with the one that was just rendered glutSwapBuffers(); // tell glut to call this display() function again on the next iteration of the main loop // Note: https://www.opengl.org/discussion_boards/showthread.php/168717-I-dont-understand-what-glutPostRedisplay()-does // Also Note: This display() function will also be registered to run if the window is moved // or if the viewport is resized. If glutPostRedisplay() is not called, then as long as the // window stays put and doesn't resize, display() won't be called again (tested with // debugging). // Also Also Note: It doesn't matter where this is called in this function. It sets a flag // for glut's main loop and doesn't actually call the registered display function, but I // got into the habbit of calling it at the end. glutPostRedisplay(); }
void Graphics::CallSpawnFunction(SpawnMessage* spawn) { //relevant ifs to determine spawn type if (spawn->type == EntityID::Icecream) { GraphicsComponent *gcp = new GraphicsComponent; gcp->SetComponentId(CompID::Graphics); //gcp->type = EntityID::Icecream; gcp->trans = spawn->trans; gcp->SetOwner(spawn->owner); //pushes GCP, into the entity components //that was passed in through the message. spawn->components->push_back(gcp); TexInstances[spawn->entid] = gcp; } else if (spawn->type == EntityID::ParticleGroup) { /*ParticleGroup *gcp; if (spawn->info == NULL) gcp = new ParticleGroup(1.0f,0.f,0.f, 30); else { std::cout << (((float *)spawn->info)[4]); gcp = new ParticleGroup((((float *)spawn->info)[1]), (((float *)spawn->info)[2]), (((float *)spawn->info)[3]), (((float *)spawn->info)[4]), (((float *)spawn->info)[5])); //gcp = new ParticleGroup; } gcp->SetComponentId(CompID::ParticleGroup); //gcp->type = EntityID::ParticleGroup; gcp->trans = spawn->trans; gcp->SetOwner(spawn->owner); spawn->components->push_back(gcp);*/ //NonTexInstances.push_back(gcp); } else if (spawn->type == EntityID::ParticleManager) { ParticleManager<ParticleGroup> *pcp = new ParticleManager<ParticleGroup>; pcp->trans = spawn->trans; pcp->SetOwner(spawn->owner); spawn->components->push_back(pcp); for (int i = 0; i < 50; ++i) pcp->AddParticle(new ParticleGroup(100)); NonTexInstances[spawn->entid] = pcp; } else if (spawn->type == EntityID::ParticleSystem) { ParticleSystem * psys = new ParticleSystem; psys->trans = spawn->trans; psys->SetOwner(spawn->owner); spawn->components->push_back(psys); NonTexInstances[spawn->entid] = psys; } };
ParticleManager* ParticleManager::create() { ParticleManager* ret = new ParticleManager(); if (ret && ret->init()) { ret->autorelease(); } else { CC_SAFE_DELETE(ret); } return ret; }
// Add attractive force void addAttractiveForce(ParticleManager& foodManager, ParticleManager& snakeManager) { int attractiveCoeff = 50; glm::vec2 attractiveForce = foodManager.getParticlePosition(0) - snakeManager.getParticlePosition(0); float d = glm::length(attractiveForce); attractiveForce = glm::normalize(attractiveForce); // Plus on divise et plus c'est petit : pluattractivecoeff augmente et mois l'attraction est forte au loin snakeManager.addForceToParticle(0, glm::vec2(attractiveForce[0]/(attractiveCoeff*d), attractiveForce[1]/(attractiveCoeff*d))); }
// Add a particle to the Snake void addParticletoSnake(ParticleGraph& graph, int id, ParticleManager& foodManager, ParticleManager& snakeManager) { id = snakeManager.addParticleToHead(foodManager.getParticleMass(id), foodManager.getParticlePosition(id), foodManager.getParticleVelocity(id), foodManager.getParticleForce(id), snakeManager.getHeadColor()); foodManager.clear(); std::pair<unsigned int, unsigned int> pair (id, snakeManager.getCount() - 2); graph.push_back(pair); snakeManager.getParticleColor(1) = snakeManager.getParticleColor(2); }
/*----------------------------------------------------------------------------------------------- Description: Governs window creation, the initial OpenGL configuration (face culling, depth mask, even though this is a 2D demo and that stuff won't be of concern), the creation of geometry, and the creation of a texture. Parameters: argc (From main(...)) The number of char * items in argv. For glut's initialization. argv (From main(...)) A collection of argument strings. For glut's initialization. Returns: False if something went wrong during initialization, otherwise true; Exception: Safe Creator: John Cox (3-7-2016) -----------------------------------------------------------------------------------------------*/ void Init() { glEnable(GL_CULL_FACE); glCullFace(GL_BACK); glFrontFace(GL_CCW); glEnable(GL_DEPTH_TEST); glDepthMask(GL_TRUE); glDepthFunc(GL_LEQUAL); glDepthRange(0.0f, 1.0f); GLuint particleProgramId = GenerateVertexShaderProgram(); GLuint computeProgramId = GenerateComputeShaderProgram(); // all values are in windows space (X and Y limited to [-1,+1]) // Note: Toy with the values as you will. //unsigned int totalParticles = 20000; unsigned int totalParticles = 600000; unsigned int maxParticlesEmittedPerFrame = 200; glm::vec2 center = glm::vec2(+0.3f, +0.3f); float radius = 1.1f; float minVelocity = 0.05f; float maxVelocity = 0.6f; gParticleManager.Init(particleProgramId, computeProgramId, totalParticles, maxParticlesEmittedPerFrame, center, radius, minVelocity, maxVelocity); }
void StartMeny::render(){ sf::RenderWindow* window = WindowManager::getInst().getWindow(); window->draw(mBackground); window->draw(mLoadButton->getSprite()); for (std::vector<Button*>::iterator i = mButtons.begin(); i != mButtons.end(); i++){ window->draw((*i)->getSprite()); } ParticleManager* particleManager = &ParticleManager::getInst(); particleManager->render(*window); }
// Copy a particle int copyParticle(ParticleManager& sourceManager, ParticleManager& destinationManager, int id) { int newId = destinationManager.addParticle(sourceManager.getParticleMass(id), sourceManager.getParticlePosition(id), sourceManager.getParticleVelocity(id), sourceManager.getParticleForce(id), sourceManager.getParticleColor(id)); return newId; }
void BasicApp::setup() { this->setFullScreen(false); // start the sound engine with default parameters engine = createIrrKlangDevice(); if (!engine) { return; } mFont = Font( "Times New Roman", 32 ); mParams = params::InterfaceGl( "App parameters", Vec2i( 200, 400 ) ); game_state = Game_Demo; firingTime = 0; score = 0; numberAsteriods = 0; delay = 20; currentShip.Init(Vec2f((float)getWindowWidth()/2,(float)getWindowHeight()/2)); particleManager.Init_Particles(); enemy.Init(); for (int i=0; i<5;i++) { Ship ship; ship.Init(Vec2f(10+20*i,70)); ships.push_back(ship); } for (int i=0; i<8; i++) { Asteroid asteroid; int shape = rand()%2+1; asteroid.Init(1, 0.5); asteroids.push_back(asteroid); } }
// Add repulsive force void addRepulsiveForce(ParticleManager& repulsiveManager, ParticleManager& snakeManager) { if(repulsiveManager.getCount() > 0) { for(int i = 0; i < snakeManager.getCount(); ++i) { for(int j = 0; j < repulsiveManager.getCount(); ++j) { glm::vec2 repulse = snakeManager.getParticlePosition(i) - repulsiveManager.getParticlePosition(j); float d = glm::length(repulse); repulse = glm::normalize(repulse); int repulsiveCoeff = 40; if(d < 0.2) { // Plus on divise et plus c'est petit : pluattractivecoeff augmente et mois l'attraction est forte au loin snakeManager.addForceToParticle(i, glm::vec2(repulse[0]/(repulsiveCoeff*d), repulse[1]/(repulsiveCoeff*d))); } } } } }
void ConstantForce::apply(ParticleManager &pm) { unsigned int count = pm.getSize(); for(int i = 0; i < count; ++i){ pm.addForce(i, m_force); } }
// Create bonus void addBonus(ParticleManager& bonusManager) { bonusManager.removeParticle(0); bonusManager.addRandomParticle(1, ParticleManager::Type::P_BONUS); }
/*----------------------------------------------------------------------------------------------- Description: Cleans up GPU memory. This might happen when the processes die, but be a good memory steward and clean up properly. Note: A big program would have the textures, program IDs, buffers, and other things encapsulated somewhere, and each other those would do the cleanup, but in this barebones demo, I can just clean up everything here. Parameters: None Returns: None Exception: Safe Creator: John Cox (2-13-2016) -----------------------------------------------------------------------------------------------*/ void CleanupAll() { gParticleManager.Cleanup(); }
void ConstantForce::apply(ParticleManager& pm) { for(int i = 0; i < pm.getCount(); ++i) { pm.addForceToParticle(i, this->getForce()); } }
// Check collision between snake and food int checkFoodCollision(ParticleGraph& snakeGraph, ParticleManager& snakeManager, ParticleManager& foodManager, ParticleManager& fireworkManager, float step, int init) { for(int i = init; i < foodManager.getCount(); ++i) { if(foodManager.getParticleX(i) - step * foodManager.getParticleMass(i) <= snakeManager.getParticleX(0) && snakeManager.getParticleX(0) <= foodManager.getParticleX(i) + step * foodManager.getParticleMass(i) && foodManager.getParticleY(i) - step * foodManager.getParticleMass(i) <= snakeManager.getParticleY(0) && snakeManager.getParticleY(0) <= foodManager.getParticleY(i) + step* foodManager.getParticleMass(i)) { fireworkManager.addCircleParticlesAtPosition(3, snakeManager.getParticlePosition(0), snakeManager.getParticleColor(1), 25); addParticletoSnake(snakeGraph, i, foodManager, snakeManager); foodManager.addRandomParticle(snakeManager.getCount()); return i; } } return -1; }
int main() { WindowManager wm(WINDOW_WIDTH, WINDOW_HEIGHT, "ZIPIX"); wm.setFramerate(30); // FMOD FMOD_SYSTEM *system; /* Création et initialisation d'un objet système */ FMOD_System_Create(&system); FMOD_System_Init(system, 2, FMOD_INIT_NORMAL, NULL); FMOD_SOUND* background = NULL; FMOD_SOUND* firework = NULL; FMOD_RESULT check; check = FMOD_System_CreateSound(system, "../music/firework_explode_and_crackle.mp3", FMOD_CREATESAMPLE, 0, &firework); if (check != FMOD_OK) { fprintf(stderr, "Impossible de lire le fichier firework_explode_and_crackle.mp3\n"); exit(EXIT_FAILURE); } check = FMOD_System_CreateSound(system, "../music/Timer.mp3", FMOD_SOFTWARE | FMOD_2D | FMOD_CREATESTREAM, 0, &background); if (check != FMOD_OK) { fprintf(stderr, "Impossible de lire le fichier Timer.mp3\n"); exit(EXIT_FAILURE); } FMOD_CHANNEL *backgroundChannel = NULL; FMOD_CHANNEL *fireworkChannel = NULL; FMOD_System_GetChannel(system, 0, &backgroundChannel); FMOD_System_GetChannel(system, 1, &fireworkChannel); FMOD_Sound_SetLoopCount(background, -1); FMOD_System_PlaySound(system, FMOD_CHANNEL_FREE, background, 0, &backgroundChannel); // Time time_t beginTime; time_t currentTime; time(&beginTime); // Managers and Renderer ParticleRenderer2D renderer; ParticleManager snakeManager; snakeManager.setHeadColor(glm::vec3(0.4f, 0.8f, 0.3f)); ParticleManager redManager; redManager.setHeadColor(glm::vec3(0.9f, 0.4f, 0.3f)); ParticleManager blueManager; blueManager.setHeadColor(glm::vec3(0.2f, 0.4f, 0.9f)); ParticleManager foodManager; ParticleManager bonusManager; ParticleManager fireworkManager; ParticleManager autoManager; // Graph Forces GraphHookForce graphHook = GraphHookForce(1.f, 0.15f/4.f); GraphBrakeForce graphBrake = GraphBrakeForce(0.3f, 10.f); // 0.5 = viscosité max GraphHookForce redGraphHook = GraphHookForce(1.f, 0.15f/4.f); GraphBrakeForce redGraphBrake = GraphBrakeForce(0.3f, 0.0001f); GraphHookForce blueGraphHook = GraphHookForce(1.f, 0.15f/4.f); GraphBrakeForce blueGraphBrake = GraphBrakeForce(0.3f, 0.0001f); // Box Polygon box = buildBox(glm::vec3(1.f, 1.f, 1.f), glm::vec2(-1.f, -1.f), 2, 2, true); // // Ajout des particules int id = foodManager.addRandomParticle(1); bonusManager.addParticle(1.5f, glm::vec2(0.5, 0.5), glm::vec2(0, 0), glm::vec2(0, 0), glm::vec3(0.619f, 0.05f, 0.25f)); bonusManager.addParticle(1.5f, glm::vec2(-0.2, 0.6), glm::vec2(0, 0), glm::vec2(0, 0), glm::vec3(0.619f, 0.05f, 0.25f)); bonusManager.addParticle(1.5f, glm::vec2(0.8, -0.7), glm::vec2(0, 0), glm::vec2(0, 0), glm::vec3(0.619f, 0.05f, 0.25f)); bonusManager.addParticle(1.5f, glm::vec2(-0.7, -0.5), glm::vec2(0, 0), glm::vec2(0, 0), glm::vec3(0.619f, 0.05f, 0.25f)); bonusManager.addParticle(1.5f, glm::vec2(0.3, -0.1), glm::vec2(0, 0), glm::vec2(0, 0), glm::vec3(0.619f, 0.05f, 0.25f)); // Copy the food to the autoManager copyParticle(foodManager, autoManager, id); // Forces ConstantForce mg(glm::vec2(0.f, -0.005)); // LeapfrogSolver LeapfrogSolver leapfrog; PolygonForce boxForce(box, 1.5f, leapfrog); // Snake's creation ParticleGraph snakeGraph = createString(glm::vec2(0.f, 0.0f), glm::vec2(0.f, -0.15f), glm::vec3(0.2f, 0.6f, 0.2f), glm::vec3(0.4f, 0.8f, 0.3f), 4.f, snakeManager); ParticleGraph redGraph = createString(glm::vec2(0.f, 0.2f), glm::vec2(0.15f, 0.2f), glm::vec3(0.9f, 0.2f, 0.2f), glm::vec3(0.9f, 0.4f, 0.3f), 4.f, redManager); ParticleGraph blueGraph = createString(glm::vec2(0.f, -0.2f), glm::vec2(-0.15f, -0.2f), glm::vec3(0.2f, 0.2f, 0.6f), glm::vec3(0.2f, 0.4f, 0.9f), 4.f, blueManager); copyParticle(snakeManager, autoManager, 0); copyParticle(redManager, autoManager, 0); copyParticle(blueManager, autoManager, 0); // Variables int score = 0; int bonus = 0; // Temps s'écoulant entre chaque frame float dt = 0.f; bool done = false; while(!done) { time(¤tTime); wm.startMainLoop(); // Renderer renderer.clear(); // Draw particles foodManager.drawParticles(renderer); snakeManager.drawParticles(renderer); redManager.drawParticles(renderer); blueManager.drawParticles(renderer); fireworkManager.drawParticles(renderer); bonusManager.drawParticles(renderer); mg.apply(fireworkManager); // Mise à jour du graph autoGraph updateParticle(snakeManager, 0, autoManager, 1); updateParticle(redManager, 0, autoManager, 2); updateParticle(blueManager, 0, autoManager, 3); // Force attractive addAttractiveForce(foodManager, snakeManager); addAttractiveForce(foodManager, redManager); addAttractiveForce(foodManager, blueManager); // Forces graphHook.setGraph(&snakeGraph); graphBrake.setGraph(&snakeGraph); redGraphHook.setGraph(&redGraph); redGraphBrake.setGraph(&redGraph); blueGraphHook.setGraph(&blueGraph); blueGraphBrake.setGraph(&blueGraph); // Simulation if(dt != 0) { //Bonus if(difftime(currentTime, beginTime) == 10) { beginTime = currentTime; time(¤tTime); addBonus(bonusManager); bonus++; } // Snake - Food if( checkFoodCollision(snakeGraph, snakeManager, foodManager, fireworkManager, 0.05f, 0) != -1 || checkFoodCollision(redGraph, redManager, foodManager, fireworkManager, 0.05f, 0) != -1 || checkFoodCollision(blueGraph, blueManager, foodManager, fireworkManager, 0.05f, 0) != -1) { FMOD_System_PlaySound(system, FMOD_CHANNEL_FREE, firework, 0, &fireworkChannel); updateParticle(foodManager, 0, autoManager, 0); bonus++; } // Apply forces graphBrake.setDt(dt); redGraphBrake.setDt(dt); blueGraphBrake.setDt(dt); boxForce.setDt(dt); boxForce.apply(snakeManager); boxForce.apply(redManager); boxForce.apply(blueManager); graphHook.apply(snakeManager); graphBrake.apply(snakeManager); redGraphHook.apply(redManager); redGraphBrake.apply(redManager); blueGraphHook.apply(blueManager); blueGraphBrake.apply(blueManager); addRepulsiveForce(bonusManager, snakeManager); addRepulsiveForce(bonusManager, redManager); addRepulsiveForce(bonusManager, blueManager); // Leapfrog solver leapfrog.solve(snakeManager, dt); leapfrog.solve(autoManager, dt); leapfrog.solve(redManager, dt); leapfrog.solve(blueManager, dt); leapfrog.solve(fireworkManager, dt); leapfrog.solve(bonusManager, dt); } // Gestion des evenements SDL_Event e; while(wm.pollEvent(e)) { switch(e.type) { default: break; case SDL_QUIT: //FMOD /* On libère le son et on ferme et libère l'objet système */ FMOD_Sound_Release(background); FMOD_Sound_Release(firework); FMOD_System_Close(system); FMOD_System_Release(system); done = true; break; } } // Mise à jour de la fenêtre dt = wm.update(); } return EXIT_SUCCESS; }
// Check collision between snake and snake int checkSnakeCollision(ParticleManager& snakeManager, ParticleManager& foodManager, float step, int init) { for(int i = init; i < foodManager.getCount() - 1; ++i) { if(foodManager.getParticleX(i) - step * foodManager.getParticleMass(i) <= snakeManager.getParticleX(0) && snakeManager.getParticleX(0) <= foodManager.getParticleX(i) + step * foodManager.getParticleMass(i) && foodManager.getParticleY(i) - step * foodManager.getParticleMass(i) <= snakeManager.getParticleY(0) && snakeManager.getParticleY(0) <= foodManager.getParticleY(i) + step* foodManager.getParticleMass(i)) { return i; } } return -1; }
void UniversalForce::apply(ParticleManager &pm) { unsigned int count = pm.getSize(); std::vector<unsigned int> toErase; bool mustErase = false; for(int i = 0; i < count; ++i){ // ------------------------------------- // ----- calcul de la durée de vie ----- // ------------------------------------- if(pm.decreaseParticleLifetime(i) == true){ toErase.push_back(i); mustErase = true; } // --------------------- // ----- Répulsion ----- // --------------------- glm::vec2 p1=pm.getParticlePosition(i); for(int j = 0; j < count; ++j){ if(i != j){ glm::vec2 p2=pm.getParticlePosition(j); glm::vec2 vect = p2 - p1; float norm = glm::length(vect); glm::vec2 force = glm::vec2(0.0); // Répulsion if(norm < m_fLInf - 0.04f){ force = m_fKRep*(1-(m_fLInf-0.04f)/glm::max(norm,0.0001f))*(vect); } // Sticky if(norm > m_fLInf && norm < m_fLSup){ force = m_fKSticky*(1-m_fLInf/glm::max(norm,0.0001f))*(vect); } pm.addForce(i, force); } } // Fin répulsion // ---------------------------------------- // - Attraction vers le centre de l'écran - // ---------------------------------------- glm::vec2 pos = pm.getParticlePosition(i); glm::vec2 p2 = glm::vec2(0.,0.); glm::vec2 vect = p2 - pos; float norm = glm::length(vect); glm::vec2 force = glm::vec2(0.0); force = m_fConstK*(1-m_fConstL/glm::max(norm,0.0001f))*(vect); pm.addForce(i, force); // Fin attraction centre // ----------------- // ----- Brake ----- // ----------------- glm::vec2 v1=pm.getParticleVelocity(i); for(int j = 0; j < count; ++j){ if(i != j){ glm::vec2 v2 = pm.getParticleVelocity(j); glm::vec2 p1 = pm.getParticlePosition(i); glm::vec2 p2 = pm.getParticlePosition(j); if(m_fDt > 0){ glm::vec2 force = glm::vec2(0.0, 0.0); float norm = glm::length(p2-p1); if(norm < m_fBrakeL){ force = m_fBrakeV*((v2-v1)/m_fDt); } // amortissement if(m_fBrakeAmort > 0.0f){ glm::vec2 amortissment = glm::vec2(0.0); Leapfrog::ParticleState actualState; actualState.position = pm.getParticlePosition(i); actualState.velocity = pm.getParticleVelocity(i); Leapfrog::ParticleState nextState = Leapfrog::getNextState(i, pm, m_fDt); if(m_fDt > 0.0){ glm::vec2 dV = (nextState.velocity - actualState.velocity) / m_fDt; dV *= m_fBrakeAmort; pm.addForce(i, -dV); } } pm.addForce(i, force); } } } // fin Brake } // effacer les particules en fin de vie if(mustErase){ for(unsigned int i = 0; i < toErase.size(); ++i){ pm.killParticle(i); } } }
void BasicApp::update() { if (game_state == Game_Demo) { for (list<Asteroid>::iterator itr = asteroids.begin(); itr!=asteroids.end(); itr++) { itr->Update(); } } else if(game_state == GAME_Play) { for (list<Asteroid>::iterator itr = asteroids.begin(); itr!=asteroids.end(); itr++) { itr->Update(); } for (list<Bullet>::iterator itr_bullet = currentShip.m_Bullets.begin(); itr_bullet!=currentShip.m_Bullets.end();) { for (list<Asteroid>::iterator itr_astroid = asteroids.begin(); itr_astroid!=asteroids.end();) { if(checkCollision(itr_bullet->getCenter(),itr_astroid->getCenter(), itr_bullet->getRadius(), itr_astroid->getRadius())) { particleManager.Start_Explosion(itr_astroid->getCenter(),10,1.0f,1.0f); engine->play2D("explode_high.wav"); if(itr_astroid->m_size >= 0.5) { Asteroid asteroid_1, asteroid_2; asteroid_1.m_state = Asteroid_Medium; asteroid_1.Init(rand()%2+1, 0.3); asteroid_1.m_Vel.y = -itr_astroid->m_Vel.y*1.5; asteroid_1.m_Vel.x = -itr_astroid->m_Vel.x*1.5; asteroid_1.m_Pos = itr_astroid->m_Pos; asteroid_2.m_state = Asteroid_Medium; asteroid_2.Init(rand()%2+1, 0.3); asteroid_2.m_Vel.y = itr_astroid->m_Vel.y*1.5; asteroid_2.m_Vel.x = itr_astroid->m_Vel.x*1.5; asteroid_2.m_Pos = itr_astroid->m_Pos; asteroids.push_back(asteroid_1); asteroids.push_back(asteroid_2); itr_astroid = asteroids.erase(itr_astroid); itr_astroid->m_bHit = true; score +=100; } else if(itr_astroid->m_state == Asteroid_Medium) { Asteroid asteroid_1, asteroid_2; asteroid_1.m_state = Asteroid_Small; asteroid_1.Init(rand()%2+1, 0.1); asteroid_1.m_Vel.y = -itr_astroid->m_Vel.y*2; asteroid_1.m_Vel.x = -itr_astroid->m_Vel.x*2; asteroid_1.m_Pos = itr_astroid->m_Pos; asteroid_2.m_state = Asteroid_Small; asteroid_2.Init(rand()%2+1, 0.1); asteroid_2.m_Vel.y = itr_astroid->m_Vel.y*2; asteroid_2.m_Vel.x = itr_astroid->m_Vel.x*2; asteroid_2.m_Pos = itr_astroid->m_Pos; asteroids.push_back(asteroid_1); asteroids.push_back(asteroid_2); itr_astroid = asteroids.erase(itr_astroid); itr_astroid->m_bHit = true; score +=100; } else { score +=50; itr_astroid->m_bHit = true; itr_astroid = asteroids.erase(itr_astroid); } itr_bullet->m_Hit = true; break; } else { itr_astroid++; } } if(itr_bullet->m_Hit) { itr_bullet = currentShip.m_Bullets.erase(itr_bullet); } else { itr_bullet++; } } if(shipCollision) { for (list<Asteroid>::iterator itr_astroid = asteroids.begin(); itr_astroid!=asteroids.end(); ) { if(currentShip.m_state == Ship::Ship_Alive) { if(checkCollision(itr_astroid->getCenter(), currentShip.getCenter(), itr_astroid->getRadius(), currentShip.getRadius() )) { particleManager.Start_Explosion(currentShip.getCenter(),10,0.3f,1.0f); particleLineManager.Init(currentShip.getCenter()); engine->play2D("explode_high.wav"); currentShip.m_state = Ship::Ship_Dead; currentShip.m_Pos.x = -getWindowWidth()/2; currentShip.m_Pos.y = -getWindowHeight()/2; if(ships.size()>0) { ships.pop_back(); } if(itr_astroid->m_size >= 0.5) { Asteroid asteroid_1, asteroid_2; asteroid_1.m_state = Asteroid_Medium; asteroid_1.Init(rand()%2+1, 0.3); asteroid_1.m_Vel.y = -itr_astroid->m_Vel.y*1.5; asteroid_1.m_Vel.x = -itr_astroid->m_Vel.x*1.5; asteroid_1.m_Pos = itr_astroid->m_Pos; asteroid_2.m_state = Asteroid_Medium; asteroid_2.Init(rand()%2+1, 0.3); asteroid_2.m_Vel.y = itr_astroid->m_Vel.y*1.5; asteroid_2.m_Vel.x = itr_astroid->m_Vel.x*1.5; asteroid_2.m_Pos = itr_astroid->m_Pos; asteroids.push_back(asteroid_1); asteroids.push_back(asteroid_2); itr_astroid = asteroids.erase(itr_astroid); itr_astroid->m_bHit = true; score +=100; } else if(itr_astroid->m_state == Asteroid_Medium) { Asteroid asteroid_1, asteroid_2; asteroid_1.m_state = Asteroid_Small; asteroid_1.Init(rand()%2+1, 0.1); asteroid_1.m_Vel.y = -itr_astroid->m_Vel.y*2; asteroid_1.m_Vel.x = -itr_astroid->m_Vel.x*2; asteroid_1.m_Pos = itr_astroid->m_Pos; asteroid_2.m_state = Asteroid_Small; asteroid_2.Init(rand()%2+1, 0.1); asteroid_2.m_Vel.y = itr_astroid->m_Vel.y*2; asteroid_2.m_Vel.x = itr_astroid->m_Vel.x*2; asteroid_2.m_Pos = itr_astroid->m_Pos; asteroids.push_back(asteroid_1); asteroids.push_back(asteroid_2); itr_astroid = asteroids.erase(itr_astroid); itr_astroid->m_bHit = true; score +=100; } else { score +=50; itr_astroid->m_bHit = true; itr_astroid = asteroids.erase(itr_astroid); } break; } else { itr_astroid++; } } else { itr_astroid++; } } } currentShip.m_saveField = currentShip.m_state == Ship::Ship_Dead; if (currentShip.m_saveField) { for (list<Asteroid>::iterator itr_astroid = asteroids.begin(); itr_astroid!=asteroids.end(); itr_astroid++) { float distance = itr_astroid->getCenter().distance(Vec2f(getWindowWidth() / 2, getWindowHeight() / 2)); if (distance < 100) { currentShip.m_saveField = false; break; } } } if(ships.size()<1) { game_state = GAME_End; } if(asteroids.size()<1) { game_state = GAME_End; } currentShip.Update(); enemy.Update(); } else if ( game_state == GAME_End) { } Sleep(delay); }
void BasicApp::draw() { // this pair of lines is the standard way to clear the screen in OpenGL gl::clear( Color( 0.0f, 0.0f, 0.0f ) ); gl::setMatricesWindow( getWindowSize() ); if(game_state == Game_Demo) { gl::enableAlphaBlending( true ); for (list<Asteroid>::iterator itr = asteroids.begin(); itr!=asteroids.end(); itr++) { if(itr->m_bHit) { } itr->Draw(); } Font font = Font( "Times New Roman", 50 ); gl::drawString("Atari Asteroids Clone" , Vec2f(app::getWindowWidth()/4,app::getWindowHeight()/4),cinder::ColorA(1,1,1,1), font); gl::drawString("Presse SpaceBar to Play" , Vec2f(app::getWindowWidth()/4,app::getWindowHeight()/2 + 100),cinder::ColorA(1,1,1,1), font); } else if(game_state == GAME_Play) { for (list<Asteroid>::iterator itr = asteroids.begin(); itr!=asteroids.end(); itr++) { if(itr->m_bHit) { } itr->Draw(); } enemy.Draw(); particleManager.Move(); particleManager.Draw(); particleLineManager.Update(); particleLineManager.Draw(); currentShip.Draw(); for (std::vector<Ship>::iterator itrShip = ships.begin(); itrShip!=ships.end();itrShip++) { if(itrShip->m_state== Ship::Ship_Alive) { itrShip->Draw(); } } stringstream scoreString; scoreString<< score; gl::drawString(" " + scoreString.str(), Vec2f(10,10),cinder::ColorA(1,1,1,1), mFont); } else if (game_state == GAME_End) { gl::drawString("Game Over", Vec2f(getWindowWidth()/2,getWindowHeight()/2),cinder::ColorA(1,1,1,1), mFont); } }