void nodachi2D::displayNextScene_new() { sf::Vector2f playerPos; bool isOnFloor; int currentSpacialObject = 0; objects::SpacialObject* tmpObject = globalGameObjectManager_->nextSpacialObject( currentSpacialObject ); while( tmpObject != NULL ) { sf::Vector2f positionOfCurrentObject; b2Body* tmpB2Body = tmpObject->getB2Body(); positionOfCurrentObject = toolBox::b2Vec_To_sfVec_f(tmpB2Body->GetPosition(), physicsVisualsRatio, true); if( tmpObject->getSpacialObjectId().compare( "player" ) == 0 ) { playerPos = positionOfCurrentObject; twoDCam_.SetCenter(playerPos); appWindow_->SetView(twoDCam_); if(tmpObject->standsOnSomething()) { isOnFloor = true; } handleInputEvents(tmpObject); } objects::Animation* tmpAnim = tmpObject->getVisualAppearance()->getCurrentAnimation(); sf::Sprite* tmpSprite = tmpAnim->getNextFrame(); tmpSprite->Scale(spritesScale); tmpSprite->SetPosition( positionOfCurrentObject ); tmpSprite->SetRotation( (tmpB2Body->GetAngle() + tmpObject->getAngleOffsetForAnimation()) *(180/3.14159265f) ); appWindow_->Draw( (*tmpSprite) ); currentSpacialObject++; tmpObject = globalGameObjectManager_->nextSpacialObject( currentSpacialObject ); } if(isOnFloor) { sf::String onFloor("On Floor"); sf::Shape BGRect = sf::Shape::Rectangle(0.0,0.0,250.0,80.0,sf::Color::Black); onFloor.SetScale(2.0,2.0); onFloor.SetColor(sf::Color::Green); BGRect.SetPosition( playerPos.x+280, playerPos.y+320); onFloor.SetPosition( playerPos.x+280, playerPos.y+320); appWindow_->Draw(BGRect); appWindow_->Draw(onFloor); } appWindow_->Display(); appWindow_->Clear(); }
void nodachi2D::displayNextScene() { bool isOnFloor; sf::String onFloor("On Floor"); sf::Shape BGRect = sf::Shape::Rectangle(0.0,0.0,250.0,80.0,sf::Color::Black); onFloor.SetScale(2.0,2.0); onFloor.SetColor(sf::Color::Green); int i = 0; objects::SpacialObject* tmpObject = globalGameObjectManager_->nextSpacialObject( i ); while( tmpObject != NULL ) { b2Body* tmpB2Body = tmpObject->getB2Body(); b2Vec2 position = tmpB2Body->GetPosition(); float32 angle = tmpB2Body->GetAngle(); std::string objectID = tmpObject->getSpacialObjectId(); objects::Animation* tmpAnim = tmpObject->getVisualAppearance()->getCurrentAnimation(); sf::Sprite* tmpSprite = tmpAnim->getNextFrame(); if( objectID.compare( "player" ) == 0 ) { sf::Vector2f tmpPos; tmpPos.x = (position.x*physicsVisualsRatio); tmpPos.y = -(position.y*physicsVisualsRatio); twoDCam_.SetCenter(tmpPos); appWindow_->SetView(twoDCam_); if(tmpObject->standsOnSomething()) { isOnFloor = true; BGRect.SetPosition( tmpPos.x+280, tmpPos.y+320); onFloor.SetPosition( tmpPos.x+280, tmpPos.y+320); } handleInputEvents(tmpObject); } tmpSprite->Scale(spritesScale); tmpSprite->SetPosition( (position.x*physicsVisualsRatio) , -(position.y*physicsVisualsRatio) ); tmpSprite->SetRotation( (angle + tmpObject->getAngleOffsetForAnimation()) *(180/3.14159265f) ); appWindow_->Draw( (*tmpSprite) ); i++; tmpObject = globalGameObjectManager_->nextSpacialObject( i ); } if(isOnFloor) { appWindow_->Draw(BGRect); appWindow_->Draw(onFloor); } appWindow_->Display(); appWindow_->Clear(); }
//Handle user input void HandleEvents() { SDL_Event event; while(SDL_PollEvent(&event)) { switch (event.type) { case SDL_KEYDOWN: switch (event.key.keysym.sym) { //Left and Right keys cause the user to accelerate respectively //Up key jumps. case SDLK_UP: if(onFloor(&player)) { player.speed.y = 0.7; } break; case SDLK_DOWN: if(onFloor(&player)) { player.speed.y = -0.7; } break; case SDLK_RIGHT: player.speed.x += 0.25; break; case SDLK_LEFT: player.speed.x -= 0.25; break; case SDLK_UNKNOWN: break; default: break; } break; case SDL_KEYUP: switch (event.key.keysym.sym) { //Releasing the left and right keys cause the player to accelerate //in the opposite direction (to counteract the initial acceleration) //unless the player has already been stopped by a wall. case SDLK_UP: break; case SDLK_DOWN: break; case SDLK_RIGHT: if(blocked_left) { blocked_left= FALSE; } else { player.speed.x -= 0.25; } break; case SDLK_LEFT: if(blocked_right) { blocked_right= FALSE; } else { player.speed.x += 0.25; } break; case SDLK_UNKNOWN: break; default: break; } break; case SDL_QUIT: exit(0); } } }
void UpdateState() { //If player is still, center it if(player.speed.x == 0) { player.center.x = TILE_CENTER_X; } //Stop player if he hits a wall if((player.speed.x >= 0 && atRightWall(&player)) || (player.speed.x <= 0 && atLeftWall(&player))) { if(player.speed.x < 0) { blocked_right = TRUE; } else if(player.speed.x > 0) { blocked_left = TRUE; } StopObject(&player,HORIZONTAL); } //Keep player from going through ceilings if(player.speed.y >= 0 && atCeiling(&player)) { StopObject(&player,VERTICAL); } //Create gravity for player if(player.speed.y <= 0 && onFloor(&player)) { StopObject(&player,VERTICAL); } else if(player.speed.y > -0.5) { player.speed.y -= 0.05; } else if(player.speed.y == 0) { player.center.y = TILE_CENTER_Y; } //Make sure player doesn't go diagonally through corners if(atCorner(&player,player.speed) && !(player.speed.x == 0 && player.speed.y == 0)) { if(abs(player.speed.x) > abs(player.speed.y)) { StopObject(&player,HORIZONTAL); } else { StopObject(&player,VERTICAL); } } //Detect collisions between player and monsters: //Kill the monster if the player lands on it, but kill the player //otherwise. if(grid[player.location.x][player.location.y-1] != NULL && grid[player.location.x][player.location.y-1]->type == MONSTER) { grid[player.location.x][player.location.y-1]->alive = FALSE; grid[player.location.x][player.location.y-1] = NULL; } else if((player.location.y != TOP && grid[player.location.x][player.location.y+1] != NULL && grid[player.location.x][player.location.y+1]->type == MONSTER) || (player.location.x != LEFT && grid[player.location.x-1][player.location.y] != NULL && grid[player.location.x-1][player.location.y]->type == MONSTER) || (player.location.x != RIGHT && grid[player.location.x+1][player.location.y] != NULL && grid[player.location.x+1][player.location.y]->type == MONSTER)) { player.alive = FALSE; } //Every second, spawn a new monster in one of the top two corners static time_t last_time = 0; time_t now = time(NULL); if (difftime(now, last_time) > 1) { struct point center = {TILE_CENTER_X, TILE_CENTER_Y}; struct vector speed = {0, 0}; struct point location = {LEFT,TOP}; if(rand()%10 >= 5) { location.x = RIGHT; } CreateObject(&g_monsters, location, center, speed, &monster_icon, MONSTER); last_time = time(NULL); } //update the monsters UpdateMonsters(); //change the player's location MoveObject(&player, &player.speed); //change the monsters' locations for(struct object_list *monsterp = g_monsters; monsterp != NULL; monsterp = monsterp->next_object) { if(monsterp->object.alive) { MoveObject(&(monsterp->object), &(monsterp->object.speed)); } } }
/* UpdateMonsters simulates the behavior of all of the game's "monster" objects. The "physics" and "AI" associated with them happen here. */ void UpdateMonsters() { for(struct object_list *monsterp = g_monsters; monsterp != NULL; monsterp = monsterp->next_object) { //If monster happens to be dead, merely cause it to fall some. if(!monsterp->object.alive) { if(monsterp->object.location.y > 0) { monsterp->object.location.y--; } break; } //Kill monsters that reach the end of their paths (bottom two corners), //More are constantly spawned anyway. if(monsterp->object.location.y == BOTTOM && (monsterp->object.location.x == RIGHT || monsterp->object.location.x == LEFT)) { monsterp->object.alive = FALSE; break; } //When a monsters hits an obstacle, have it reverse direction. (Also, start //moving if it's sitting next to a wall. if(monsterp->object.speed.x >= 0 && atRightWall(&(monsterp->object))) { monsterp->object.speed.x = -0.15; } else if(monsterp->object.speed.x <= 0 && atLeftWall(&(monsterp->object))) { monsterp->object.speed.x = 0.15; } //Make sure monsters don't go through ceilings if(monsterp->object.speed.y >= 0 && atCeiling(&monsterp->object)) { StopObject(&monsterp->object,VERTICAL); } //Give monsters gravity if(monsterp->object.speed.y <= 0 && onFloor(&monsterp->object)) { StopObject(&monsterp->object,VERTICAL); } else if(monsterp->object.speed.y > -0.5) { monsterp->object.speed.y -= 0.05; } else if(monsterp->object.speed.y == 0) { monsterp->object.center.y = TILE_CENTER_Y; } //Make sure monsters don't go diagonally through corners if(atCorner(&monsterp->object,monsterp->object.speed) && !(monsterp->object.speed.x == 0 && monsterp->object.speed.y == 0)) { if(abs(monsterp->object.speed.x) > abs(monsterp->object.speed.y)) { StopObject(&monsterp->object,HORIZONTAL); } else { StopObject(&monsterp->object,VERTICAL); } } } //Remove any monsters that happen to be dead and have fallen to the bottom of the //game area. struct object_list **next; for(struct object_list **monsterp = &g_monsters; *monsterp != NULL; monsterp = next) { next = &((*monsterp)->next_object); if(!(**monsterp).object.alive && (**monsterp).object.location.y <= BOTTOM) { DestroyObject(monsterp); } } }
int main(void) { int totCars, maxFloor, lenSeq; scanf("%d %d %d", &totCars, &maxFloor, &lenSeq); std::vector<int> seq = std::vector<int>(); for(int i=0; i<lenSeq; i++) { int a; scanf("%d", &a); //a--; seq.push_back(a); } //myfloor = std::vector<int>(); int floorSize = 0; int shelf = 0; for(int i=0; i<seq.size(); i++) { int car = seq[i]; if(onFloor(car, maxFloor)) continue; if(floorSize < maxFloor && !onFloor(car, floorSize)) { //printf("Floor has space left\n"); myfloor[floorSize] = car; cars[car-1] = i; //myfloor.push_back(car); //printf("size: %d\n", myfloor.size()); floorSize++; shelf++; continue; } //Car not on floor and full floor int max = 0; int ind = -1; //printf("Looking at car: %d\n", car); /*printf("On the floor now: "); for(int k=0; k<maxFloor; k++) {printf("%d ", myfloor[k]);} printf("\n"); */ for(int j=0; j<maxFloor; j++) { int car2 = myfloor[j]; if(cars[car2-1] < i && cars[car2-1] != -2) { cars[car2-1] = findCar(car2, seq, i); } if(cars[car2-1] == -2) { max = cars[car2-1]; ind = j; //printf("%d does not appear again\n", car2); break; } //printf("Next occurence of %d is %d\n", car2, cars[car2-1]); if(cars[car2-1] > max) { max = cars[car2-1]; ind = j; } } //printf("Swapped %d at pos: %d with %d\n\n", myfloor[ind], max, car); myfloor[ind] = car; //myfloor.erase(myfloor.begin()+ind, myfloor.begin()+ind+1); //myfloor.push_back(car); shelf++; } //printf("Mommy has to take minimum this # of cars: %d\n", shelf); printf("%d\n", shelf); return 0; }