void SpaceSim::Update() { while(!done) { //Delta time handling double current_timestamp = al_get_time(); double seconds_since_last_tick = current_timestamp - previous_tick_timestamp; /*if(seconds_since_last_tick > .25) { seconds_since_last_tick = 0.25; }*/ previous_tick_timestamp = current_timestamp; accumulator+=seconds_since_last_tick; //Do this before drawing while ( accumulator >= dt ) { TakeInput(); //Update all the entities for(std::vector<Entity*>::size_type i = 0; i != entities.size(); i++) { entities[i]->update(dt); /*if(entities[i]->hasComponent("Removal")) { RemovalComp* remove = (RemovalComp*)entities[i]->getComponent("Removal"); if(remove->getShouldDiscard()) { entities.erase(entities.begin() + i); i--; } }*/ } //CHECKING COLLISION and wrap for(int i = 0; i != physics_entities.size(); i++) { /*if(physics_entities[i]->hasComponent("Removal")) { RemovalComp* remove = (RemovalComp*)physics_entities[i]->getComponent("Removal"); if(remove->getShouldDiscard()) { physics_entities.erase(physics_entities.begin() + i); } }*/ PositionComp* pos = (PositionComp*) physics_entities[i]->getComponent("Position"); SpriteComp* spr = (SpriteComp*) physics_entities[i]->getComponent("Sprite"); PhysicsComp* phys = (PhysicsComp*) physics_entities[i]->getComponent("Physics"); //Collisions for(int q = 0; q != physics_entities.size(); q++) { if(i!=q) { PhysicsComp* collider = (PhysicsComp*) physics_entities[q]->getComponent("Physics"); PhysicsComp::checkCollision(phys,collider); } } //Boundaries //Right if( (pos->getPosition().x - al_get_bitmap_width (spr->getBitmap())/2 ) > screen_width) { pos->setPositionX(pos->getPosition().x - al_get_bitmap_width (spr->getBitmap())/2 - screen_width); //done = true; } //Left if( (pos->getPosition().x + al_get_bitmap_width (spr->getBitmap())/2 ) < 0) { pos->setPositionX(pos->getPosition().x + al_get_bitmap_width (spr->getBitmap())/2 + screen_width); //done = true; } //Top if( (pos->getPosition().y - al_get_bitmap_height (spr->getBitmap())/2 ) > 0) { pos->setPositionY(pos->getPosition().y + al_get_bitmap_height(spr->getBitmap())/2 - screen_height); //done = true; } //Bottom if( (pos->getPosition().y + al_get_bitmap_height (spr->getBitmap())/2 ) < -screen_height) { pos->setPositionY(pos->getPosition().y - al_get_bitmap_height (spr->getBitmap())/2 + screen_height); //done = true; } } /*UpdateUI();*/ accumulator -= dt; } /* Gets rid of displeasing effect const double alpha = accumulator / dt; State state = currentState*alpha + previousState * ( 1.0 - alpha );*/ Draw(); } };
int HandleInput (UnitList *SQ, UnitList *PDUL, FunctionQueue *FQ, Level *lvl, int *running) { char c = TakeInput(); if (c == ERR) { return 1; } else if (c == 'q') { InputQ(running); return 1; } else if (c == 'r') { CreateCLEARMSG(FQ); InputR(running); return 1; } else if (c == 's') { CreateCLEARMSG(FQ); if (SQ->size == 0) { CreatePRINTMSG(FQ, 3); } else { CreateCLEARPROMPTS(FQ); InputS(running); } return 1; } else if (c == 'x') { CreateCLEARMSG(FQ); InputX(SQ,lvl); CreateREDRAWQUEUE(FQ,SQ); CreateREDRAWRESOURCES(FQ,lvl); return 1; } else if (c-'a' >= 0 && c-'a' < UnitSize(PDUL)) { if (SQ->size < UNITQUEUESIZE) { int i, flag = false; CreateCLEARMSG(FQ); for (i = 0; i < AVAILUNITS; i++) { if (c == lvl->available[i]) flag = true; }; if (flag) { InputUnit(PDUL,c,SQ,lvl); CreateREDRAWQUEUE(FQ,SQ); CreateREDRAWRESOURCES(FQ,lvl); return 1; } else { CreatePRINTMSG(FQ, 1); return 1; } } return 0; } else { CreateCLEARMSG(FQ); CreatePRINTMSG(FQ, 1); return 0; } }