void Game::HandleView(sf::View &view, sf::View &static_view, sf::RenderWindow &window){ if(sf::Keyboard::isKeyPressed(sf::Keyboard::W)) view.move(0, -16); if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)) view.move(-16, 0); if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)) view.move(0, 16); if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)) view.move(16, 0); window.setView(static_view); money_text.setString("Money: " + SSTR(values.Money())); window.draw(money_text); static bool help_toggled = true; static bool key_released = true; if(sf::Keyboard::isKeyPressed(sf::Keyboard::H) and key_released == true){ help_toggled = !help_toggled; key_released = false; }else if(!sf::Keyboard::isKeyPressed(sf::Keyboard::H)) key_released = true; if(help_toggled){ help_text.setString("building price: " + SSTR(values.BuildingPrice()) + "\nclaim & upgrade price = 10\nmoney cap = " + SSTR(values.MoneyCap()) + "\n\nc = claim\nr = build resource\nb = build bank\nu = improve\np = sell for 10\n\ntoggle help = h"); window.draw(help_text); } }
void updateView(sf::View& view, sf::Time& deltaTime) { float dx = 0; float dy = 0; if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) || sf::Keyboard::isKeyPressed(sf::Keyboard::A)) { dx -= 1; } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) || sf::Keyboard::isKeyPressed(sf::Keyboard::D)) { dx += 1; } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) || sf::Keyboard::isKeyPressed(sf::Keyboard::W)) { dy -= 1; } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) || sf::Keyboard::isKeyPressed(sf::Keyboard::S)) { dy += 1; } float moveSpeed = 1000; float timeSeconds = deltaTime.asSeconds(); float dd = dx * dx + dy * dy; if (dd != 0) { dd = sqrt(dd); dx /= dd; dy /= dd; } dx *= moveSpeed; dy *= moveSpeed; view.move(dx * timeSeconds, dy * timeSeconds); }
void Player::progress(float frame, sf::View &view1){ if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { view1.move(-SPEED*frame, 0); player.move(-SPEED*frame, 0); } if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { view1.move(SPEED*frame, 0); player.move(SPEED*frame, 0); } if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) { view1.move(0, -SPEED*frame); player.move(0, -SPEED*frame); } if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) { view1.move(0, SPEED*frame); player.move(0, SPEED*frame); } }
void gestionTouches (sf::View &View, sf::Clock &timer) { float Offset = timer.getElapsedTime().asMilliseconds(); // le deplacement de la map if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) ) View.move( 0, -Offset); if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) ) View.move( 0, Offset); if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) ) View.move(-Offset, 0); if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) ) View.move(Offset, 0); // le zoom if (sf::Keyboard::isKeyPressed(sf::Keyboard::Add) ) View.zoom(1.005f); if (sf::Keyboard::isKeyPressed(sf::Keyboard::Subtract) ) View.zoom(0.995f); // la gestion du "temps" if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) ) MonTimer::getSpeed() * 1.07; if (sf::Keyboard::isKeyPressed(sf::Keyboard::Z) ) MonTimer::getSpeed() / 1.07; // clean if (sf::Keyboard::isKeyPressed(sf::Keyboard::E) ) std::cout <<std::endl <<std::endl<<std::endl<<std::endl; }
void GamePlay::screenMovement(Player &p, sf::View &v, sf::Sprite &s) //!< screen movement function to keep in track with player { //!screen move right if(p.getPos().getX() > v.getCenter().x + 40)v.move(2.75f,0.f); if(p.getPos().getX() > v.getCenter().x + 40 && p.getBuff() == 1)v.move(4.f,0.f); //!screen move left if(p.getPos().getX() < v.getCenter().x - 40)v.move(-2.75f,0.f); if(p.getPos().getX() < v.getCenter().x - 40 && p.getBuff() == 1)v.move(-4.f,0.f); //!screen move up if(p.getPos().getY() < v.getCenter().y + 110)v.move(0.f,-5.f); //!screen move down if(p.getPos().getY() > v.getCenter().y + 120) v.move(0.f,5.f); //!keeps background static s.setPosition(v.getCenter().x-400.f ,v.getCenter().y -300.f); //!keeps messages in place message[1].setPosition(v.getCenter().x-295.f ,v.getCenter().y -225.f); message[2].setPosition(v.getCenter().x+175.f ,v.getCenter().y -225.f); message[3].setPosition(v.getCenter().x+120.f ,v.getCenter().y -200.f); }
void OnDown(thor::ActionContext<std::string> context) { //printf("DOWN, %f\n", deltaTime); view.move(sf::Vector2f(0.0f, MOVEMENT_SPEED*1.0f*deltaTime)); }
void OnRight(thor::ActionContext<std::string> context) { //printf("RIGHT, %f\n", deltaTime); view.move(sf::Vector2f(MOVEMENT_SPEED*1.0f*deltaTime, 0.0f)); }
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; }
int render(Phys* phys) { window->clear(sf::Color::Black); sf::Vector2f viewmove(0, 0); if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) viewmove.x -= base_camera_move; if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) viewmove.x += base_camera_move; if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) viewmove.y += base_camera_move; if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) viewmove.y -= base_camera_move; viewmove *= (phys->dt * view->getSize().x / scrw); viewmove = sv2rotate(viewmove, -view->getRotation()); view->move(viewmove); view->zoom(pow(1.2, -mouse_wheel)); mouse_wheel = 0; float viewrotate = 0; if (sf::Keyboard::isKeyPressed(sf::Keyboard::LBracket)) viewrotate += base_camera_rotate; if (sf::Keyboard::isKeyPressed(sf::Keyboard::RBracket)) viewrotate -= base_camera_rotate; viewrotate *= phys->dt; view->rotate(viewrotate); window->setView(*view); for (std::vector<Ship*>::iterator its = phys->ship_list.begin(); its != phys->ship_list.end(); ++its) { sf::ConvexShape& shape = (*its)->shape; cpVect pos = cpBodyGetPosition((*its)->body); cpFloat angle = cpBodyGetAngle((*its)->body); shape.setPosition(pos.x, pos.y); shape.setRotation(180 / PI * angle); window->draw(shape); } for (std::vector<Shell*>::iterator its = phys->shell_list.begin(); its != phys->shell_list.end(); ++its) { sf::ConvexShape& shape = (*its)->shape; cpVect pos = cpBodyGetPosition((*its)->body); cpFloat angle = cpBodyGetAngle((*its)->body); shape.setPosition(pos.x, pos.y); shape.setRotation(180 / PI * angle); window->draw(shape); } char* str = new char[30]; float acc = 0; for (int i = 0; i < frame_time_count; i++) acc += phys->last_frame_deltas[i]; acc /= frame_time_count; sprintf(str, "%.1f", 1./acc); drawtext(str, 0.02, 0.98, 10); int sc = (*phys->ship_list.begin())->score; sprintf(str, "%d", sc); drawtext(str, 0.98, 0.98, 10); delete str; window->display(); return 0; }