void Level::logic(sf::View& camera, unsigned int delta) { // Perform actions. ActionInterface::logic(*this); // Remove all dead entities. for(list<Entity*>::iterator it = mDeadEntities.begin(); it != mDeadEntities.end(); it++) mRemoveEntity(*it); mDeadEntities.clear(); // Perform all entity logic. for(list<Entity*>::iterator it = mEntities.begin(); it != mEntities.end(); it++) (*it)->logic(*this, delta); // If all items have been picked up, display a floating text and give a bonus. if(mPickups.empty() && !mPickupAward) { addFloatingText("Level Cleared!", mPlayer.getPosition(), sf::Color::Yellow, 30); distributeChangeScore(250); mPickupAward = true; } // Perform floating text logic if the interval is passed. if(mFloatingTextTimer.getTime() >= FLOATINGTEXT_TIMER_INTERVAL) { list<list<sf::Text>::iterator > removeList; for(list<sf::Text>::iterator it = mFloatingTexts.begin(); it != mFloatingTexts.end(); ++it) { it->move(0, -FLOATINGTEXT_MOVE_STEP); sf::Color c = it->getColor(); it->setColor(sf::Color(c.r, c.g, c.b, c.a - 3)); if(it->getColor().a == 0) removeList.push_back(it); } for(list<list<sf::Text>::iterator >::iterator it = removeList.begin(); it != removeList.end(); ++it) mFloatingTexts.erase(*it); mFloatingTextTimer.start(); } // Center the player on the screen. camera.setCenter(mPlayer.getX() + (mPlayer.getWidth() / 2), mPlayer.getY() + (mPlayer.getHeight() / 2)); // Make sure that the camera doesn't go out of bounds. sf::Vector2f center = camera.getCenter(), half(camera.getSize().x / 2, camera.getSize().y / 2); if(center.x - half.x < 0) center.x = half.x; if(center.x + half.x > mMap.getWidth()) center.x = mMap.getWidth() - half.x; if(center.y - half.y < 0) center.y = half.y; if(center.y + half.y > mMap.getHeight()) center.y = mMap.getHeight() - half.y; camera.setCenter(center); }
void EffectsHandler::Update(float elapsed, sf::View &view, sf::RenderWindow &rw) { if (effectsList.empty()) { return; } for (std::list<Effect>::iterator it = effectsList.begin(); it != effectsList.end(); it++) { switch (*it) { case ZoomIn: if (view.getSize().x > rw.getSize().x/2) { view.setSize(view.getSize().x-(ZOOM_SPEED_X*elapsed), view.getSize().y-(ZOOM_SPEED_Y*elapsed)); view.setCenter(view.getCenter().x, view.getCenter().y-(ZOOM_POS_ABOVE_MIDDLE*elapsed)); } else effectsList.erase(it++); break; case ZoomOut: if (view.getSize().x <= rw.getDefaultView().getSize().x && view.getSize().y <= rw.getDefaultView().getSize().y) //seems silly to take &window just for getdefaultview. { view.setSize(view.getSize().x+(ZOOM_SPEED_X*elapsed), view.getSize().y+(ZOOM_SPEED_Y*elapsed)); view.setCenter(view.getCenter().x, view.getCenter().y+(ZOOM_POS_ABOVE_MIDDLE*elapsed)); } else { effectsList.erase(it++); view = rw.getDefaultView(); } break; case Pause: totalelapsed += elapsed; if (totalelapsed > 1.0f) { effectsList.erase(it++); totalelapsed = 0; } break; default: break; } } }
void setView_Scroll(sf::View &view, Character &character, gameMap &map) { float height = view.getSize().y; float width = view.getSize().x; float maxW = map.getWidth()*(map.getTile(0).getTileDims().x); float maxH = map.getHeight()*(map.getTile(0).getTileDims().y); float newX = 0; float newY = 0; if(character.getPosition().x < width/2) { newX = width/2; }else if(character.getPosition().x > maxW - width/2) { newX = maxW - width/2; }else { newX = round(character.getPosition().x); } if(character.getPosition().y < height/2) { newY = height/2; }else if(character.getPosition().y > maxH - height/2) { newY = maxH - height/2; }else { newY = round(character.getPosition().y); } view.setCenter(newX, newY); }
void Level::update(double delta, sf::Event& evt, sf::View& v) { if(ltype == Menu) { //if spacebar is pressed in menu, start game if(evt.type == sf::Event::KeyPressed) if(evt.key.code == sf::Keyboard::Space) setLevel(Stage1); } else { //step an iteration of box2d world b2world->Step(delta/1000, 8, 3); //set view //remember to change from first dynamic entity to the player position v.setCenter(player->anim.xpos+20, player->anim.ypos+30); //iterate through dentity list and update auto itr = dentitylist.begin(); for(itr; itr != dentitylist.end(); ++itr) { (*itr)->update(delta); } //update player player->Player::update(evt, delta); //update enemies } }
int main(int argc, char** argv) { fcnt = 0; speed = 0.5; gen = 1; evolution_rate = 10; counter = 300; bestavg = 0; decline_counter = 0; srand(time(0)); camera.setSize(800,600); camera.setCenter(AREA_W/2,AREA_H/2); sf::ContextSettings settings; settings.antialiasingLevel = 8; window = new sf::RenderWindow(sf::VideoMode(SCREEN_W,SCREEN_H),"Evolution 0.3",sf::Style::Default,settings); window->setFramerateLimit(60); window->setView(camera); bg.setOutlineColor(sf::Color::Cyan); bg.setOutlineThickness(4.f); bg.setFillColor(sf::Color(51,153,153)); sf::CircleShape* tmp; tmp = new sf::CircleShape(15.f,5); tmp->setOutlineColor(sf::Color::Red); tmp->setFillColor(sf::Color(75,75,75)); tmp->setOutlineThickness(2.f); tmp->setOrigin(tmp->getLocalBounds().width/2,tmp->getLocalBounds().height/2); fToken = tmp; sf::Font font; font.loadFromFile("revalia.ttf"); text = new sf::Text(); text->setFont(font); text->setCharacterSize(15.f); text->setColor(sf::Color::White); vpop = new VectorPopulation(); vpop->setMutationScale(current_mutation_rate); for(int i = 0; i<CREATURE_CAP; i++) { creatures.push_back(std::shared_ptr<Creature>(new Creature(4,2,1,6))); creatures.back()->setPos(sf::Vector2f(10 + rand() % (AREA_W-20), 10 + rand() % (AREA_H-20))); creatures.back()->areaSize(AREA_W,AREA_H); creatures.back()->setSpeed(3); } for(int i = 0; i<FOOD_CAP; i++) { food.push_back(sf::Vector2f(20 + rand() % (AREA_W-40), 10 + rand() % (AREA_H-20))); } while(update()) { render(); } return 0; }
void aktualizuj_kamere() { kamera.setCenter( gracz->zwroc_wsp().x, gracz->zwroc_wsp().y); // if( gracz->dmg_krytyczny ){ // ::przyblizenie = 0.9; // gracz->dmg_krytyczny = false; // } // if( przyblizenie < 1 ) // przyblizenie += 0.00000000000001; // if( przyblizenie > 1 ) // przyblizenie = 1; kamera.zoom( przyblizenie ); }
void setViewRect(sf::View& view, sf::FloatRect const& newRect) { view.setCenter(center(newRect)); view.setSize(newRect.width, newRect.height); }
int main() { deltaTime = 0; sfg::SFGUI sfgui; // map sprite sf::Texture map_texture; sf::Sprite map_sprite; map_texture.loadFromFile("resources/map.png"); map_sprite.setTexture(map_texture); // window window.create(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 32), "Runestone"); view.setSize(sf::Vector2f(WINDOW_WIDTH, WINDOW_HEIGHT)); view.setCenter(sf::Vector2f(0, 0)); window.setView(view); // timer sf::Clock clock; // thor actions thor::Action quit(sf::Event::Closed); thor::Action left(sf::Keyboard::A, thor::Action::Hold); thor::Action right(sf::Keyboard::D, thor::Action::Hold); thor::Action up(sf::Keyboard::W, thor::Action::Hold); thor::Action down(sf::Keyboard::S, thor::Action::Hold); thor::Action mouse_down(sf::Mouse::Left, thor::Action::PressOnce); thor::Action mouse_up(sf::Mouse::Left, thor::Action::ReleaseOnce); // thor action map thor::ActionMap<std::string> action_map; action_map["quit"] = quit; action_map["left"] = left; action_map["right"] = right; action_map["up"] = up; action_map["down"] = down; action_map["mouse_down"] = mouse_down; action_map["mouse_up"] = mouse_up; thor::ActionMap<std::string>::CallbackSystem action_callbacks; action_callbacks.connect("quit", &OnQuit); action_callbacks.connect("left", &OnLeft); action_callbacks.connect("right", &OnRight); action_callbacks.connect("up", &OnUp); action_callbacks.connect("down", &OnDown); action_callbacks.connect("mouse_down", &OnMouseDown); action_callbacks.connect("mouse_up", &OnMouseUp); // thor particles sf::Texture particleTexture; particleTexture.loadFromFile("Media/particle.png"); emitter.setEmissionRate(100.0f); emitter.setParticleLifetime(sf::seconds(1.0f)); particle_system.setTexture(particleTexture); // sfgui auto sfgui_window = sfg::Window::Create(); sfgui_window->SetTitle("Runestones: "); auto rune_one = sfg::Button::Create("Rune One"); rune_one->GetSignal(sfg::Button::OnMouseLeftPress).Connect(std::bind(&OnRuneOne)); rune_one->GetSignal(sfg::Button::OnMouseLeftPress).Connect(std::bind(&OnButtonClicked)); auto rune_two = sfg::Button::Create("Rune Two"); rune_two->GetSignal(sfg::Button::OnMouseLeftPress).Connect(std::bind(&OnRuneTwo)); rune_two->GetSignal(sfg::Button::OnMouseLeftPress).Connect(std::bind(&OnButtonClicked)); auto rune_three = sfg::Button::Create("Rune Three"); rune_three->GetSignal(sfg::Button::OnMouseLeftPress).Connect(std::bind(&OnRuneThree)); rune_three->GetSignal(sfg::Button::OnMouseLeftPress).Connect(std::bind(&OnButtonClicked)); auto rune_four = sfg::Button::Create("Rune Four"); rune_four->GetSignal(sfg::Button::OnMouseLeftPress).Connect(std::bind(&OnRuneFour)); rune_four->GetSignal(sfg::Button::OnMouseLeftPress).Connect(std::bind(&OnButtonClicked)); auto hbox = sfg::Box::Create(sfg::Box::Orientation::HORIZONTAL, 4.0f); hbox->Pack(rune_one); hbox->Pack(rune_two); hbox->Pack(rune_three); hbox->Pack(rune_four); sfgui_window->Add(hbox); sfgui_window->SetStyle(!sfg::Window::Style::RESIZE); sfgui_window->SetPosition(sf::Vector2f((WINDOW_WIDTH / 2.0f) - (sfgui_window->GetAllocation().width / 2.0f), WINDOW_HEIGHT - sfgui_window->GetAllocation().height)); // tileset sf::Texture tileTexture; tileTexture.loadFromFile("resources/tiles.png"); rf::TileMap tileMap(4, 8); tileMap.setTexture(tileTexture); int layout[4][8] = { {-1, -1, -1, 1, 1, -1, -1, -1}, {1,1,1,1,1,1,1,1}, {-1,-1,1,1,1,1,-1,-1}, {1,1,1,1,1,1,1,1} }; for (size_t i = 0; i < 4; i++) { for (size_t j = 0; j < 8; j++) { tileMap.setTile(layout[i][j], i, j); } } while (window.isOpen()) { deltaTime = clock.restart().asSeconds(); action_map.clearEvents(); sf::Event event; while (window.pollEvent(event)) { sfgui_window->HandleEvent(event); action_map.pushEvent(event); } particle_system.update(sf::seconds(deltaTime)); sfgui_window->Update(deltaTime); action_map.invokeCallbacks(action_callbacks, &window); window.setView(view); window.clear(sf::Color::Black); window.draw(map_sprite); window.draw(tileMap); window.draw(particle_system); sfgui.Display(window); window.display(); } return 0; }
bool update() { sf::Event event; while(window->pollEvent(event)) { switch (event.type) { case sf::Event::Closed: return false; case sf::Event::KeyPressed: switch(event.key.code) { case sf::Keyboard::Up: camera.move(0,-1); break; case sf::Keyboard::Down: camera.move(0,1); std::cout << camera.getCenter().x; break; case sf::Keyboard::Left: camera.move(-1,0); break; case sf::Keyboard::Right: camera.move(1,0); break; case sf::Keyboard::F1: for(int i = 0; i<10; i++) { updateCreatures(); } break; case sf::Keyboard::F2: for(int i = 0; i<100; i++) { updateCreatures(); } break; case sf::Keyboard::F3: fastForward(10); break; case sf::Keyboard::F4: fastForward(50); break; } break; case sf::Event::MouseMoved: if(sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) { camera.move(mousepos-window->mapPixelToCoords(sf::Mouse::getPosition(),camera)); } mousepos = window->mapPixelToCoords(sf::Mouse::getPosition(),camera); break; case sf::Event::MouseWheelMoved: if(event.mouseWheel.delta>0) { camera.zoom(1.15); } else if(event.mouseWheel.delta<0) { camera.zoom(0.85); } break; case sf::Event::MouseButtonPressed: if(mouseclock.getElapsedTime().asMilliseconds()<333) { auto center = window->mapPixelToCoords(sf::Vector2i(event.mouseButton.x,event.mouseButton.y),camera); camera.setCenter(center); camera.setSize(sf::Vector2f(SCREEN_W,SCREEN_H)); } prev_click = mouseclock.restart(); break; } } double ftest = 0; avg = 0; for(auto& creature: creatures) { creature->foodPool(food); creature->update(); for(auto fd = food.begin(); fd<food.end(); fd++) { if(distance(creature->getPos(),*fd)<15) { fd = food.erase(fd); creature->increaseFitness(1.f); } } avg += creature->getFitness(); if(creature->getFitness()>ftest) { ftest = creature->getFitness(); } } pavg = avg; avg = avg/creatures.size(); if(avg>bestavg) { best_so_far = vpop->getPopulationRef(); } else if(avg < bestavg*0.85) { vpop->revertPopulation(best_so_far); } while(food.size()<FOOD_CAP) { food.push_back(sf::Vector2f(20 + rand() % (AREA_W-40), 10 + rand() % (AREA_H-20))); } if(counter>1) { counter--; } else { updateCreatures(); counter = 60*evolution_rate; std::cout << "Generation: " << gen++; std::cout << " fittest: " << ftest; std::cout << " average: " << avg << std::endl; } return true; }
sf::View updateView(sf::View view) { // Calculate view movement int x = view.getCenter().x; int y = view.getCenter().y; float percX = gameEvents.lastMouseWindow.x / window_width; float percY = gameEvents.lastMouseWindow.y / window_height; if(gameEvents.isAHeld) { x -= SCROLL_SPEED; } else if(percX >= 0.0f && percX <= DEFAULT_SCROLL_BORDER && global_scrollBordersEnabled) { x -= SCROLL_SPEED_MODIFIER * SCROLL_SPEED; } if(gameEvents.isDHeld) { x += SCROLL_SPEED; } else if(percX >= 1.0f - DEFAULT_SCROLL_BORDER && percX <= 1.0f && global_scrollBordersEnabled) { x += SCROLL_SPEED_MODIFIER * SCROLL_SPEED; } if(gameEvents.isWHeld) { y -= SCROLL_SPEED; } else if(percY >= 0.0f && percY <= DEFAULT_SCROLL_BORDER && global_scrollBordersEnabled) { y -= SCROLL_SPEED_MODIFIER * SCROLL_SPEED; } if(gameEvents.isSHeld) { y += SCROLL_SPEED; } else if(percY >= 1.0f - DEFAULT_SCROLL_BORDER && percY <= 1.0f && global_scrollBordersEnabled) { y += SCROLL_SPEED_MODIFIER * SCROLL_SPEED; } // Check for horizontal boundaries // if(x < window_width/2 - BOX_SIZE/2) // { // x = window_width/2 - BOX_SIZE/2; // } // // if(x > MAX_WORLD_WIDTH * BOX_SIZE - window_width/2 - BOX_SIZE/2) // { // x = MAX_WORLD_WIDTH * BOX_SIZE - window_width/2 - BOX_SIZE/2; // } // Check for vertical boundaries if(y > MAX_WORLD_HEIGHT * BOX_SIZE + GROUND_STARTING_HEIGHT - window_height/2 - BOX_SIZE/2) { y = MAX_WORLD_HEIGHT * BOX_SIZE + GROUND_STARTING_HEIGHT - window_height/2 - BOX_SIZE/2; } // 53 Blocks to the top! view.setCenter(x, y); return view; }
//Player rendering void Player::draw(sf::RenderWindow& window, sf::View& view, Level level, string move) { //Player control scheme if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) || move == "up") { //Grounded check if (grounded) { //Jump velocity.y = -20; position.y += 0.5; grounded = false; } } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) || move == "left") { //Move player left velocity.x = -7; } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) || move == "right") { //Move player right velocity.x = 7; } //Add force of gravity to player velocity velocity.y += 0.8; //Position at player center int centerX = position.x - (size / 2); int centerY = position.y - (size / 2); //Dimensions int halfSize = size / 2; float skin = 2; //Nearest tiles for preforming collision checks Tile currentTile = level.getTileFromWorld(sf::Vector2f(centerX, centerY)); Tile below = level.getTileFromWorld(sf::Vector2f(centerX, centerY + (currentTile.size / 2) + velocity.y)); Tile right = level.getTileFromWorld(sf::Vector2f(centerX + skin + (currentTile.size / 2), centerY)); Tile top = level.getTileFromWorld(sf::Vector2f(centerX, centerY - skin - (currentTile.size / 2))); Tile left = level.getTileFromWorld(sf::Vector2f(centerX - skin - (currentTile.size / 2), centerY)); //Check bottom collision if (below.type != 0 && velocity.y > 0) { grounded = true; velocity.y = 0; } else { grounded = false; } //Check top collision if (top.type != 0 && velocity.y < 0) velocity.y = 0; //Check left collision if (left.type != 0 && velocity.x < 0) velocity.x = 0; //Check right collision if (right.type != 0 && velocity.x > 0) velocity.x = 0; //Update player position position.y += velocity.y; position.x += velocity.x; //Update position tracking lastX = position.x; //Centered camera view.setCenter(position); //Reset x velocity to prevent momentum build up velocity.x = 0; //Render player shape.setPosition(position); window.draw(shape); };
void Camera::parralax(sf::View& v, float factor) { v.setRotation(getRotation()); v.setCenter(getCenter() / factor); }
void Level::update(double deltatime, sf::Event evt, sf::View& view) { switch(currentleveltype) { case Begin: if(!imenu->done) { imenu->update(deltatime, evt); break; } else { values = imenu->getValues(); currentleveltype = Dungeon; } break; case Dungeon: srand(time(0)); //generate a map based on current depth if(depthcount<5 && map == NULL) { map = new Map(40, 40); map->createMap(30 + rand()%10, 2+rand()%2, 3 + rand()%4, 3); map->addMonsters(5 + rand()%6, 8, 10, 100); map->drawMap(); seen = new int*[map->height]; for(int i = 0; i < map->height; ++i) { seen[i] = new int[map->width](); } //debug only /* for(int x = 0; x < map->height; ++x) { for(int y = 0; y< map->width; ++y) { seen[x][y] = 1; } } */ elist = map->retrieveEntityList(); getentityPos(); player = new Player(playerpos.x, playerpos.y); player->setValues(values); } //initialize seen array recalculate(8); auto itr = enemylist.begin(); for(itr; itr != enemylist.end(); ++itr) { itr->update(deltatime, player->x, player->y, map->grid, player->HP); } player->update(deltatime, evt, map->grid); view.setCenter(player->anim.xpos, player->anim.ypos); //recalculate seen array break; } }
void viewSetRect(sf::View &view, sf::IntRect &rect) { sf::Vector2i size (rect.width, rect.height); sf::Vector2i center (rect.left + rect.width / 2, rect.top + rect.height / 2); view.setCenter((sf::Vector2f) center); view.setSize((sf::Vector2f) size); }
void PLAYER::CalcView(sf::View& view) const { view.setCenter(Position()); }