void Game::menu() { okno.setView(okno.getDefaultView()); okno.setMouseCursorVisible(true); Text title("Nazwa Gry", font,80); Texture tekstura; tekstura.loadFromFile("data/ground.jpg"); tekstura.setRepeated(true); Sprite background; background.setTexture(tekstura); background.setPosition(Vector2f(0, 0)); background.setOrigin(Vector2f(0, 0)); background.setTextureRect(IntRect(0, 0, 1280, 720)); title.setColor(Color(138, 7, 7)); title.setPosition(1280 / 2 - title.getGlobalBounds().width / 2, 10); const int ile = 2; Text tekst[ile]; string str[] = { "PLAY","EXIT" }; for (int i = 0; i<ile; i++) { tekst[i].setFont(font); tekst[i].setCharacterSize(50); tekst[i].setString(str[i]); tekst[i].setPosition((float)(1280 / 2.0 - tekst[i].getGlobalBounds().width / 2), (float)(250 + i * 120.0)); } while (state == MENU) { Vector2f mouse(Mouse::getPosition(okno)); Event event; while (okno.pollEvent(event)) { if (event.type == Event::Closed || event.type == Event::KeyPressed && event.key.code == Keyboard::Escape) state = END; else if (tekst[1].getGlobalBounds().contains(mouse) && event.type == Event::MouseButtonReleased && event.key.code == Mouse::Left) state = END; else if (tekst[0].getGlobalBounds().contains(mouse) && event.type == Event::MouseButtonReleased && event.key.code == Mouse::Left) state = GAME; } for (int i = 0; i<ile; i++) if (tekst[i].getGlobalBounds().contains(mouse)) { tekst[i].setColor(Color(138, 7, 7)); } else tekst[i].setColor(Color::White); okno.clear(); okno.draw(background); okno.draw(title); for (int i = 0; i<ile; i++) okno.draw(tekst[i]); okno.display(); } }
//map<color, Color> colors = { // { Black, Color::Black }, // { White, Color::White }, // { Red, Color::Red }, // { Green, Color::Green }, // { Blue, Color::Blue }, // { Yellow, Color::Yellow }, // { Magenta, Color::Magenta }, // { Cyan, Color::Cyan }, // { Transparent, Color::Transparent }, // { Orange, Color(255, 128, 0) } //}; ////////////////////////// this returns a lambda ////////////////////////// function<void()> ////////////////////////// TYPE APP NAME ON LINE BELOW ////////////////////////// mine_solver (RenderWindow&window, ui &UI) { return [&window, &UI]() { #ifndef COMMON_INITS //void smoothmouse(RenderWindow&window, ui &UI){ // tidy code organization, here we predeclare methods just like a header would, it's redundant but necessary function<void()> draw, loop, init, update; function<void(Vec2 pos)> mousemoved; function<void(sf::Mouse::Button button)> mouseclick, mouserelease; function<void(Event&e)> treatotherevent; function<void(Keyboard::Key k)> treatkeyevent; // we don't have a class/interface/struct with data, everything is local to this function, like a classic stack that all programs are anyway. Texture cursor_tx; Sprite cursor; configfile cfg; cfg.init("bedlab.cfg"); Color background = cfg.getvar<Color>("background"); if (!cursor_tx.loadFromFile(cfg.getstr("cursor"))) cout << "did not load cursor" << endl; cursor.setTexture(cursor_tx); cursor.setOrigin(3, 3); window.setMouseCursorVisible(false); Vec2 mpos; bool leftclicked = false, rightclicked = false; // view and zoom View view, ui_view; ui_view = view = window.getDefaultView(); float zoomlevel = 1; Vec2 mpos_abs; #endif // COMMON_INITS // ************************ CODE BEGIN ************************ barstack bst(FONT, FONTSIZE); slider_finder sl; randgenfloat cell_rnd(0, 16); //grd. vector<string> mines_str = { " ", " 21114X411 ", " 3X22XX212 ", " X3--4433X ", " 23--4XX3X ", " 1X4---433 ", " 12X4--4X2 ", " 123X3XX3X ", " 2X2122221 ", " " }; int w = mines_str[0].length(); int h = mines_str.size(); vector<int> mine_states(h*w); grid grd; grd.screen_init(Vec2u(window.getSize()), Vec2u( w,h ), true); /* 1-8: surrounding mines 0 no mine, "clear" -1: unknown -2 mine */ map<char, int> lookup = { { '-',-1 }, { 'X',-2 }, { ' ',0 } }; int i = 0; logfile lg("minelog.txt"); lg.logw(to_str("i", "j", "index", "value")); for (auto&a : mines_str) { int j = 0; for (auto&b : a) { int value = -10; //value = // lookup.count(b) ? // lookup[b] // : (b > '0' || b <= '9') ? // int(b - '0') // : value; //msg(value); if (lookup.count(b)) value = lookup[b]; else if (b > '0' || b <= '9')value = int(b - '0'); int index = getat(j, i, w); mine_states[index] = value; //msgm(i,j,index, value); lg.logw(to_str(i,j,index, value)); ++j; } ++i; } map<int, Color> mine_colors = { { -2,Color::Red }, { -1,Color::Cyan }, { 0,Color::Black } }; msg(mine_states); i = 0; for (auto&a : mine_states) { if(a > 0) grd.texts[i].setString(to_str(a)); else grd.cells[i].setFillColor(mine_colors[a]); //grd.texts2[i].setString(to_str(i)); //grd.texts3[i].setString(to_str(getat(i,w))); ++i; } auto analyse = [&mine_states, w, h, &grd]() { vector<Vec2i> offsets = { { -1,-1 }, { 1,-1 }, { -1,1 }, { 1,1 }, { 0,-1 }, { -1,0 }, { 0,1 }, { 1,0 } }; int i = 0; for (auto&a : mine_states) { auto pos = getat(i, w); int unknown = 0, is_mine = 0; for (auto&off : offsets) { auto cell = pos + off; int cell_index = getat(cell,w); if (cell_index < 0 || cell_index >= mine_states.size() || cell.x < 0 || cell.x > w) continue; switch (mine_states[cell_index]) { case -1: ++is_mine; break; //unknown case -2: ++unknown; break; //mine } } int mine_left = a - is_mine; float prob = mine_left / unknown; ++i; } }; auto nearest_pt = mkcircle({ 0, 0 }, Color::Transparent, 10); nearest_pt.setOutlineThickness(2); // ************************ INITS END ********************** //then we actually define the functions, note how all function captures the local context by reference #ifndef LOOP_LAMBDAS draw = [&]() { window.setView(view); // object draw, AFTER normal view //////////////// obj draw START //////////////// for (auto&a : glob_pts)window.draw(a); for (auto&a : glob_rects)window.draw(a); for (auto&a : glob_vert)window.draw(a); for (auto&a : glob_texts)window.draw(a); window.draw(nearest_pt); grd.draw(window); //////////////// obj draw END //////////////// // UI draw, AFTER ui view and BEFORE other draw window.setView(ui_view); bst.draw(window); // nothing after here please UI.draw(window); window.draw(cursor); }; update = [&]() { }; treatkeyevent = [&](Keyboard::Key k) { switch (k) { case Keyboard::BackSpace: glob_pts.clear(); glob_texts.clear(); glob_rects.clear(); glob_vert.clear(); break; case Keyboard::Space: break; case Keyboard::Q: break; } }; mousemoved = [&](Vec2 pos) { cursor.setPosition(pos); if (leftclicked) sl.mouse_callback(pos); }; mouseclick = [&](sf::Mouse::Button button) { if (button == Mouse::Button::Left) leftclicked = true; if (button == Mouse::Button::Right) rightclicked = true; }; mouserelease = [&](sf::Mouse::Button button) { if (button == Mouse::Button::Left) leftclicked = false; if (button == Mouse::Button::Right) rightclicked = false; sl.release(); }; loop = [&]() { while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { switch (event.type) { case sf::Event::KeyPressed: if (event.key.code == sf::Keyboard::Escape) window.close(); treatkeyevent(event.key.code); break; case sf::Event::Closed: window.close(); break; case sf::Event::MouseButtonPressed: mouseclick(event.mouseButton.button); break; case sf::Event::MouseButtonReleased: mouserelease(event.mouseButton.button); break; case sf::Event::MouseMoved: mpos = Vec2(event.mouseMove.x, event.mouseMove.y); mousemoved(mpos); break; default: treatotherevent(event); break; } } window.clear(background); update(); draw(); window.display(); } }; treatotherevent = [&](Event&e) { if (e.type == Event::MouseWheelMoved && e.mouseWheel.delta) { mpos_abs = window.mapPixelToCoords(Vec2i(mpos), view); //view = window.getView(); if (e.mouseWheel.delta < 0) { zoomlevel *= 2.f; view.setSize(view.getSize()*2.f); view.setCenter(interp(mpos_abs, view.getCenter(), 2.f)); //view.setCenter(interp(mpos_abs, view.getCenter(), 2.f)); } if (e.mouseWheel.delta > 0) { zoomlevel *= 0.5; view.setSize(view.getSize()*.5f); view.setCenter(.5f*(view.getCenter() + mpos_abs)); //view.setCenter(.5f*(view.getCenter() + mpos_abs)); } window.setView(view); } }; loop(); #endif // LOOP_LAMBDAS }; }
void Game::gra() { srand(time(0)); /////////////////////////////// Texture tekstura_cel; //wczytanie tekstur i dzwiêków i tekstów tekstura_cel.loadFromFile("data/cel.png"); Texture tekstura_z; tekstura_z.loadFromFile("data/zombie_after_attack_rect.png"); Texture tekstura; tekstura.loadFromFile("data/ground.png"); Texture tekstura_bl; tekstura_bl.loadFromFile("data/blood.png"); Texture tekstura_zombie_bl; tekstura_zombie_bl.loadFromFile("data/zombie_after_attack_rect.png"); Texture tekstura_zombie_aa; tekstura_zombie_aa.loadFromFile("data/zombie_after_attack_rect.png"); Texture tekstura_zombie_full; tekstura_zombie_full.loadFromFile("data/zombie_after_attack_rect.png"); Texture tekstura_gun; tekstura_gun.loadFromFile("data/gun.png"); Texture tekstura_bullet; Texture tekstura_floor; tekstura_floor.loadFromFile("data/floor.png"); Texture tekstura_ak_47; tekstura_ak_47.loadFromFile("data/ak_47.png"); tekstura_bullet.loadFromFile("data/bullet.png"); Texture tekstura_corps; tekstura_corps.loadFromFile("data/corps.png"); Texture tekstura_ammo; tekstura_ammo.loadFromFile("data/ammo_pickup.png"); Texture tekstura_hp; tekstura_hp.loadFromFile("data/hp_pickup.png"); Texture tekstura_wall; tekstura_wall.loadFromFile("data/wall.jpg"); tekstura_wall.setRepeated(true); SoundBuffer buffer, buffer_z; buffer_z.loadFromFile("data/hit.wav"); buffer.loadFromFile("data/shot.wav"); Sound sound, sound_hit; sound.setBuffer(buffer); sound_hit.setBuffer(buffer_z); SoundBuffer zombie_bite; zombie_bite.loadFromFile("data/zombie_bite.wav"); Sound sound_bite; sound_bite.setBuffer(zombie_bite); SoundBuffer reload_wav, ammo_out; reload_wav.loadFromFile("data/long_reload.wav"); ammo_out.loadFromFile("data/out_of_ammo.wav"); SoundBuffer health_wav, ammo_wav; health_wav.loadFromFile("data/health.wav"); ammo_wav.loadFromFile("data/ammo.wav"); Sound health_collect, ammo_collect; health_collect.setBuffer(health_wav); ammo_collect.setBuffer(ammo_wav); SoundBuffer ak_shot; ak_shot.loadFromFile("data/AK_47_shot.wav"); Sound ak; ak.setBuffer(ak_shot); Sound sound_reload, sound_ammo_out; sound_reload.setBuffer(reload_wav); sound_ammo_out.setBuffer(ammo_out); Music muzyka; muzyka.openFromFile("data/COD4_theme.wav"); muzyka.setLoop(true); muzyka.play(); Font font; font.loadFromFile("data/font_UI.ttf"); Text hp; hp.setFont(font); hp.setCharacterSize(25); hp.setColor(Color(138, 7, 7)); Text round; round.setCharacterSize(25); round.setFont(font); round.setColor(Color(138, 7, 7)); Text gun_name; gun_name.setCharacterSize(25); gun_name.setFont(font); gun_name.setColor(Color(138, 7, 7)); Text bullets_Left; bullets_Left.setFont(font); bullets_Left.setCharacterSize(25); bullets_Left.setColor(Color(138, 7, 7)); Text licznik_zombie; licznik_zombie.setFont(font); licznik_zombie.setCharacterSize(25); licznik_zombie.setColor(Color(138, 7, 7)); //////////////////////////////// Sprite celownik; celownik.setScale(0.045, 0.045); celownik.setTexture(tekstura_cel); celownik.setOrigin(celownik.getLocalBounds().width/2.0f, celownik.getLocalBounds().height/ 2.0f); RectangleShape sprint_ui; sprint_ui.setSize(Vector2f(15 * 5, 15)); sprint_ui.setFillColor(Color(138, 7, 7)); /////////////////////////// tablice obiektów vector<Blood> tab_bl; vector < Bullet > tab; //tablica przechowuj¹ca pociski vector <Zombie> tab_z; //zombie deque <Vector2f> tab_p;//pozycje gracza z ostatnich 100 klatek vector <Corps> tab_corps; vector <Ammo_Pickup> tab_am; vector <Hp_Pickup> tab_hp; Sprite map(tekstura); tekstura.setRepeated(true); map.setTextureRect(IntRect(0, 0, 10000, 10000)); map.setOrigin(5000,5000); map.setPosition(0, 0); Sprite floor; floor.setPosition(-500, -480); floor.setTexture(tekstura_floor); tekstura_floor.setRepeated(true); floor.setTextureRect(IntRect(0, 0, 1000, 980)); vector<RectangleShape> tab_wall; for (int i = 0; i < 12; i++) { RectangleShape wall; wall.setFillColor(Color(7, 138, 7, 100)); if (i == 0) { wall.setPosition(-1000, -1000); //wall.setFillColor(Color::White); } else if (i == 1) { wall.setPosition(-1000, 1000); //wall.setFillColor(Color::Red); } else if (i == 2) { wall.setPosition(-1100, -1000); //wall.setFillColor(Color::Cyan); } else if (i == 3) { //wall.setOrigin(100, 0); wall.setPosition(1000, -1000); //wall.setFillColor(Color::Blue); } if (i == 0 || i == 1)wall.setSize(Vector2f(2000, 100)); else if (i == 2 || i == 3)wall.setSize(Vector2f(100, 2100)); if (i == 4) { wall.setPosition(-500,-500); } else if (i == 5) { wall.setPosition(-520,500); } else if (i == 6) { wall.setPosition(-520, -500); } else if (i == 7) { wall.setPosition(500, -500); } if (i == 4 || i == 5)wall.setSize(Vector2f(1020, 20)); else if (i == 6 || i == 7)wall.setSize(Vector2f(20, 1020)); if (i == 8) { wall.setPosition(-100, -489); wall.setFillColor(Color(138, 7, 7, 150)); } else if (i == 9) { wall.setPosition(-100, 510); wall.setFillColor(Color(138, 7, 7, 150)); } else if (i == 10) { wall.setPosition(-509, -100); wall.setFillColor(Color(138, 7, 7, 150)); } else if (i == 11) { wall.setPosition(510, -100); wall.setFillColor(Color(138, 7, 7,150)); } if (i == 8 || i == 9)wall.setSize(Vector2f(100, 6)); else if (i == 10 || i == 11)wall.setSize(Vector2f(6, 100)); if (i == 8 || i == 9 || i == 10 || i == 11)wall.setOrigin(wall.getLocalBounds().width / 2.0f, wall.getLocalBounds().height / 2.0f); tab_wall.push_back(wall); } Player gracz; gracz.body.setPosition(0,0); int runda = 1; float vx = 0, vy = 0; //prêdkoæ gracza float angle; float sprint = 1; Clock zombie_player_collisionTimer; Clock reload_time; Clock zombie_generator_timer; Clock zegar; Time czas; Event event; float x, y; //x,y myszy okno.setMouseCursorVisible(false); int max_zombie_per_round; int generated_zombie=0; bool generate = true; bool menu = false; bool canReload = false; bool canShoot = true; bool Rpressed = false; bool isSprint = false; bool canSprint = true; int zombies_killed_in_round = 0; zombie_generator_timer.restart(); Gun *wsk_gun; Gun pistol("colt .45", 80.0f,16.0f,2000.0f,2.0f,tekstura_bullet, tekstura_gun,Vector2f(2,2), Vector2f (1,1),gracz.body.getPosition(),0.1); Gun ak_47("ak 47", 120.0f, 30.0f, 2800.0f, 2.0f, tekstura_bullet, tekstura_ak_47, Vector2f(2, 2), Vector2f(1, 1), gracz.body.getPosition(),0.08f); //view.zoom(1.0f/1.2f); //view.zoom(1.0f / 1.2f); wsk_gun = &pistol; while (!menu) { //cout << tab_z.size() << " | "<< wsk_gun->bullets_fired.size() <<endl; gracz.canMoveDown = true; gracz.canMoveUP= true; gracz.canMoveRight = true; gracz.canMoveLeft = true; max_zombie_per_round = runda * 4 * 7 ; if (generated_zombie >= max_zombie_per_round) { generate = false; } if (!generate&&tab_z.empty()) { zombies_killed_in_round = 0; generated_zombie = 0; generate = true; runda++; } if (generate && (zombie_generator_timer.getElapsedTime().asSeconds() >= 1.0f)) { zombie_generator_timer.restart(); if (tab_z.size() <=20) { generated_zombie += 4; zombie_generator(tab_z, Vector2f((tab_wall[0].getPosition().x + rand() % ((int)tab_wall[0].getGlobalBounds().width + 1)), (tab_wall[0].getPosition().y + tab_wall[0].getGlobalBounds().height)), tekstura_z, runda, tab_wall[8].getPosition()); zombie_generator(tab_z, Vector2f((tab_wall[1].getPosition().x + rand() % ((int)tab_wall[1].getGlobalBounds().width + 1)), (tab_wall[1].getPosition().y + tab_wall[1].getGlobalBounds().height)), tekstura_z, runda, tab_wall[9].getPosition()); zombie_generator(tab_z, Vector2f((tab_wall[2].getPosition().x + tab_wall[2].getGlobalBounds().width), (tab_wall[2].getPosition().y + rand() % ((int)tab_wall[2].getGlobalBounds().height + 1))), tekstura_z, runda, tab_wall[10].getPosition()); zombie_generator(tab_z, Vector2f((tab_wall[3].getPosition().x + tab_wall[3].getGlobalBounds().width), (tab_wall[3].getPosition().y + rand() % ((int)tab_wall[3].getGlobalBounds().height + 1))), tekstura_z, runda, tab_wall[11].getPosition()); /*zombie_generator(tab_z, Vector2f((tab_wall[0].getPosition().x + rand() % ((int)tab_wall[0].getGlobalBounds().width + 1)), (tab_wall[0].getPosition().y + tab_wall[0].getGlobalBounds().height)), tekstura_z, runda, Vector2f(0,0)); zombie_generator(tab_z, Vector2f((tab_wall[1].getPosition().x + rand() % ((int)tab_wall[1].getGlobalBounds().width + 1)), (tab_wall[1].getPosition().y + tab_wall[1].getGlobalBounds().height)), tekstura_z, runda, Vector2f(0,0)); zombie_generator(tab_z, Vector2f((tab_wall[2].getPosition().x + tab_wall[2].getGlobalBounds().width), (tab_wall[2].getPosition().y + rand() % ((int)tab_wall[2].getGlobalBounds().height + 1))), tekstura_z, runda, Vector2f(0,0)); zombie_generator(tab_z, Vector2f((tab_wall[3].getPosition().x + tab_wall[3].getGlobalBounds().width), (tab_wall[3].getPosition().y + rand() % ((int)tab_wall[3].getGlobalBounds().height + 1))), tekstura_z, runda, Vector2f(0,0)); */ } /*else { generated_zombie += 4; zombie_generator(tab_z, Vector2f((tab_wall[4].getPosition().x + rand() % ((int)tab_wall[4].getGlobalBounds().width + 1)), (tab_wall[4].getPosition().y + tab_wall[4].getGlobalBounds().height)), tekstura_z, runda); zombie_generator(tab_z, Vector2f((tab_wall[5].getPosition().x + rand() % ((int)tab_wall[5].getGlobalBounds().width + 1)), (tab_wall[5].getPosition().y + tab_wall[5].getGlobalBounds().height)), tekstura_z, runda); zombie_generator(tab_z, Vector2f((tab_wall[6].getPosition().x + tab_wall[6].getGlobalBounds().width), (tab_wall[6].getPosition().y + rand() % ((int)tab_wall[6].getGlobalBounds().height + 1))), tekstura_z, runda); zombie_generator(tab_z, Vector2f((tab_wall[7].getPosition().x + tab_wall[7].getGlobalBounds().width), (tab_wall[7].getPosition().y + rand() % ((int)tab_wall[7].getGlobalBounds().height + 1))), tekstura_z, runda); }*/ } view.setCenter(gracz.body.getPosition()); //gracz ca³y czas w centrum widoku x = (float)Mouse::getPosition(okno).x+view.getCenter().x-1280/2, y = (float)Mouse::getPosition(okno).y+view.getCenter().y-720/2; //zapisanie pozycji myszy wzglêdem lewgo górnego rogu widoku celownik.setPosition(x, y); //przypisanie pozycji celownika angle = (float)atan2((float)(y - gracz.body.getPosition().y), (float)(x - gracz.body.getPosition().x)) * 180.0f / (float)M_PI - 90.0f; //zapisanie k¹t miêdzy rodkiem ekranu a pozycj¹ myszy gracz.body.setRotation(angle); //obrócenie gracza w kierunku myszy cout <<x << " | "<< y << endl; ///////////////////////////// if (tab_p.size() != 30) //wype³nienie tablicy pozycjami gracza z ostatnich stu klatek (czyli 1 sekundy) tab_p.push_back(gracz.body.getPosition()); if (tab_p.size() == 30) { tab_p.pop_front(); tab_p.push_back(gracz.body.getPosition()); } /////////////////////////////// while (okno.pollEvent(event)) { if (event.type == Event::Closed || event.type == Event::KeyPressed && event.key.code == Keyboard::Escape) menu = true; //if (event.type == Event::MouseButtonPressed && Mouse::isButtonPressed(Mouse::Right))//stworzenie zombie i dodanie go do tablicy //{ // Zombie zombie(x, y, tekstura_z,rand()%51+50, 1+runda); // tab_z.push_back(zombie); //} if (wsk_gun==&pistol) { if (event.type == Event::MouseButtonPressed && Mouse::isButtonPressed(Mouse::Left))//stworzenie pocisku i dodanie go do tablicy { if (wsk_gun->magazynek == 0) { sound_ammo_out.play(); if (wsk_gun->bulletsLeft != 0 && !wsk_gun->reload_demand&& !wsk_gun->reloading) { sound_reload.play(); wsk_gun->reload_demand = true; } } else if (!wsk_gun->reloading) { sound.play(); wsk_gun->shoot(gracz.body.getPosition()); wsk_gun->magazynek--; } else { sound_ammo_out.play(); } } } if (event.type == Event::MouseButtonPressed && Mouse::isButtonPressed(Mouse::Left) && (wsk_gun->magazynek == 0 || wsk_gun->reloading || wsk_gun->reload_demand)) sound_ammo_out.play(); if (event.type == Event::KeyPressed && event.key.code == Keyboard::R && wsk_gun->bulletsLeft != 0 && wsk_gun->magazynek != wsk_gun->magazynekSize &&!wsk_gun->reloading&& !wsk_gun->reload_demand) { wsk_gun->reload_demand = true; sound_reload.play(); } if (event.type == Event::KeyPressed && event.key.code == Keyboard::Num1) { wsk_gun = &pistol; //buffer = wsk_gun->shot; } if (event.type == Event::KeyPressed && event.key.code == Keyboard::Num2) { wsk_gun = &ak_47; //buffer = wsk_gun->shot; } if (event.type == Event::KeyPressed && event.key.code == Keyboard::Up)view.zoom(1.2f); if (event.type == Event::KeyPressed && event.key.code == Keyboard::Down)view.zoom(1.0f/1.2f); } /////// strza³ pistoletem if (wsk_gun != &pistol) { if (wsk_gun->fire_timer.getElapsedTime().asSeconds() >= wsk_gun->fireRate) { if (Mouse::isButtonPressed(Mouse::Left))//stworzenie pocisku i dodanie go do tablicy { if (wsk_gun->magazynek == 0) { if (wsk_gun->bulletsLeft != 0 && !wsk_gun->reload_demand&& !wsk_gun->reloading) { sound_reload.play(); wsk_gun->reload_demand = true; } } else if (!wsk_gun->reloading) { sound.play(); wsk_gun->shoot(gracz.body.getPosition()); wsk_gun->magazynek--; } /*else { sound_ammo_out.play(); }*/ } wsk_gun->fire_timer.restart(); } } wsk_gun->update(angle, czas, gracz.body.getPosition()); if(!tab_z.empty())//zombie update { for (int i = 0; i < tab_z.size(); i++) tab_z[i].update(gracz.body.getPosition(), czas,tab_p[0]); } //if (!tab.empty())//obrócenie zombie w kierunku pozycji gracz z przed sekundy //{ // for (int i = 0; i < tab.size(); i++) // tab[i].rotate(); //} //////////////////////////////////////kolizja // kolizja zombie-gracz for (int i = 0; i < tab_z.size(); i++) { //if(gracz.body.getGlobalBounds().intersects(tab_z[i].body.getGlobalBounds())) if (Collision::PixelPerfectTest(gracz.body, tab_z[i].body)) { tab_z[i].collidesWithPlayer = true; if ((tab_z[i].body.getPosition().y > gracz.body.getPosition().y >0) || (gracz.body.getPosition().y <tab_z[i].body.getPosition().y <0)) { gracz.canMoveDown = false; //gracz.body.move(0, -1); } if ((gracz.body.getPosition().y >tab_z[i].body.getPosition().y >0) || (tab_z[i].body.getPosition().y < gracz.body.getPosition().y <0)) { gracz.canMoveUP = false; //gracz.body.move(0, 1); } if ((tab_z[i].body.getPosition().x > gracz.body.getPosition().x >0)||(gracz.body.getPosition().x <tab_z[i].body.getPosition().x <0)) { gracz.canMoveRight = false; //gracz.body.move(-1, 0); } if ((gracz.body.getPosition().x >tab_z[i].body.getPosition().x >0)||(tab_z[i].body.getPosition().x < gracz.body.getPosition().x <0)) { gracz.canMoveLeft = false; //gracz.body.move(1, 0); } if (zombie_player_collisionTimer.getElapsedTime().asSeconds() >= 0.5) { zombie_player_collisionTimer.restart(); sound_bite.play(); //cout << gracz.healthPoints << endl; if (tab_z[i].healthPoints < 3)tab_z[i].body.setTexture(tekstura_zombie_full); else tab_z[i].body.setTexture(tekstura_zombie_aa); tab_z[i].haveAttacked = true; gracz.healthPoints--; if (gracz.healthPoints == 0) { menu = true; } break; } } else tab_z[i].collidesWithPlayer = false; } // kolizja zombie-zombie if (!tab_z.empty()) { for (int i = 0; i < tab_z.size(); i++) { for (int j = 0; j < tab_z.size(); j++) { if (i != j) { //if (tab_z[j].body.getGlobalBounds().intersects( tab_z[i].body.getGlobalBounds())) if (Collision::PixelPerfectTest(tab_z[j].body, tab_z[i].body)) { if (fabs(tab_z[i].body.getPosition().x - gracz.body.getPosition().x) + fabs(tab_z[i].body.getPosition().y - gracz.body.getPosition().y) >fabs(tab_z[j].body.getPosition().x - gracz.body.getPosition().x) + fabs(tab_z[j].body.getPosition().y - gracz.body.getPosition().y)) { tab_z[i].collide_parameter = 0; tab_z[j].collide_parameter = 1.2; } else { tab_z[j].collide_parameter = 0; tab_z[i].collide_parameter = 1.2; } tab_z[i].col_in = true; tab_z[j].col_in = true; break; } } } } } // kolizja gracz-ciana if (tab_wall[4].getGlobalBounds().intersects(gracz.body.getGlobalBounds())) { gracz.canMoveUP = false; sprint = 1; //vx = 0; //gracz.body.move(0, 1); //gracz.canMoveUP = true; } if (tab_wall[5].getGlobalBounds().intersects(gracz.body.getGlobalBounds())) { sprint = 1; gracz.canMoveDown = false; //vx = 0; //gracz.body.move(0, -1); //gracz.canMoveDown = true; } if (tab_wall[6].getGlobalBounds().intersects(gracz.body.getGlobalBounds())) { sprint = 1; gracz.canMoveLeft = false; //vy = 0; //gracz.body.move(1, 0); //gracz.canMoveRight = true; } if (tab_wall[7].getGlobalBounds().intersects(gracz.body.getGlobalBounds())) { sprint = 1; gracz.canMoveRight= false; //vy = 0; //gracz.body.move(-1, 0); //gracz.canMoveLeft = true; } ///////////kolizja zombie pocisk if (!tab_z.empty() && !wsk_gun->bullets_fired.empty()) //zniszczenie pocisku i zombie w przypadku kolizji { for (int i = 0; i < wsk_gun->bullets_fired.size(); i++) { for (int j = 0; j < tab_z.size(); j++) { if (wsk_gun->bullets_fired[i].body.getGlobalBounds().intersects(tab_z[j].body.getGlobalBounds())) { sound_hit.play(); bullet_destroy(wsk_gun->bullets_fired, i); Blood krew(tekstura_bl, tab_z[j].body.getPosition()); krew.body.setRotation((rand() % 360) + 1); tab_bl.push_back(krew); tab_z[j].healthPoints--; if (tab_z[j].healthPoints == 1) { tab_z[j].setSpeed(80.0f); if (tab_z[j].haveAttacked == true)tab_z[j].body.setTexture(tekstura_zombie_full); else tab_z[j].body.setTexture(tekstura_zombie_bl); } if (tab_z[j].healthPoints == 0) { //mieræ zombie Corps corpses(tekstura_corps, tab_z[j].body.getPosition(), tab_z[j].rotate_angle); tab_corps.push_back(corpses); if (tab_z[j].isInside) { Ammo_Pickup am(tekstura_ammo, tab_z[j].body.getPosition()); Hp_Pickup hp(tekstura_hp, tab_z[j].body.getPosition()); switch (rand() % 10) { case 0: tab_am.push_back(am); break; case 1: tab_am.push_back(am); break; case 5: if (gracz.healthPoints < 5)tab_hp.push_back(hp); break; } } tab_z[j] = tab_z[tab_z.size() - 1]; tab_z.pop_back(); zombies_killed_in_round++; } break; } } } } //kolizja gracz-ammo_pickup for (int i = 0; i < tab_am.size(); i++) { if (gracz.body.getGlobalBounds().intersects(tab_am[i].body.getGlobalBounds())) { ammo_collect.play(); wsk_gun->bulletsLeft += tab_am[i].add_ammo; ammo_destroy(tab_am, i); } } //kolizja gracz-hp_pickup for (int i = 0; i < tab_hp.size(); i++) { if (gracz.body.getGlobalBounds().intersects(tab_hp[i].body.getGlobalBounds())) { if (gracz.healthPoints + tab_hp[i].add_hp <= 5) { health_collect.play(); gracz.healthPoints += tab_hp[i].add_hp; hp_destroy(tab_hp, i); } } } //////////////////////////////////////// for (int j = 4; j < tab_wall.size()-4;j++) for (int i = 0; i < wsk_gun->bullets_fired.size(); i++) { if (wsk_gun->bullets_fired[i].body.getGlobalBounds().intersects(tab_wall[j].getGlobalBounds())) bullet_destroy(wsk_gun->bullets_fired, i); } for (int j = 8; j < tab_wall.size(); j++) for (int i = 0; i < tab_z.size(); i++) if (tab_z[i].body.getGlobalBounds().intersects(tab_wall[j].getGlobalBounds()))tab_z[i].isInside = true; for (int i = 0; i < wsk_gun->bullets_fired.size(); i++) { if (wsk_gun->bullets_fired[i].onCreate_clock.getElapsedTime().asSeconds() >= wsk_gun->bullets_fired[i].lifeTime) { bullet_destroy(wsk_gun->bullets_fired, i); } } for (int i = 0; i < tab_bl.size(); i++) { if (tab_bl[i].onCreate_clock.getElapsedTime().asSeconds() >= tab_bl[i].lifeTime) { blood_destroy(tab_bl, i); } } for (int i = 0; i < tab_corps.size(); i++) { if (tab_corps[i].onCreate_clock.getElapsedTime().asSeconds() >= tab_corps[i].lifeTime) { corps_destroy(tab_corps, i); } } for(int i = 0; i < tab_am.size(); i++) { if (tab_am[i].onCreate_clock.getElapsedTime().asSeconds() >= tab_am[i].lifeTime) { ammo_destroy(tab_am, i); } } for (int i = 0; i < tab_hp.size(); i++) { if (tab_hp[i].onCreate_clock.getElapsedTime().asSeconds() >= tab_hp[i].lifeTime) { hp_destroy(tab_hp, i); } } /////////////////////////////////////////// interfejs(gracz.healthPoints, wsk_gun->bulletsLeft, hp, bullets_Left, view.getCenter(), wsk_gun->magazynek, runda, round, max_zombie_per_round - zombies_killed_in_round, licznik_zombie, gun_name, wsk_gun->name, sprint_ui, isSprint&&(tab_p[0]!=gracz.body.getPosition()), czas); okno.setView(view); okno.clear(); //cout << "Up: " << gracz.canMoveUP << " | Down: " << gracz.canMoveDown << " | Left: " << gracz.canMoveLeft << " | Right: " << gracz.canMoveRight << endl; ///////////////////////////////////////////////////// gracz.input(vx, vy); if (Keyboard::isKeyPressed(Keyboard::LShift) && canSprint) { if (sprint_ui.getSize().x < 1) { canSprint = false; } sprint = 1.5; isSprint = true; } else { if (sprint_ui.getSize().x >15) { canSprint = true; } sprint = 1; isSprint = false; } ////////////////////////////////////// // ruch // gracz.ruch(czas, vx, vy, sprint); /*if (!tab_z.empty()) { for (int i = 0; i < tab_z.size(); i++) { tab_z[i].ruch(tab_p[0], czas); } }*/ //////////////////////////////// okno.draw(map); //rysowanie okno.draw(floor); if (!wsk_gun->bullets_fired.empty()) { for (int i = 0; i <wsk_gun->bullets_fired.size(); i++) if (!gracz.body.getGlobalBounds().intersects(wsk_gun->bullets_fired[i].body.getGlobalBounds())) okno.draw(wsk_gun->bullets_fired[i].body); } if (!tab_bl.empty()) { for (int i = 0; i < tab_bl.size(); i++) { okno.draw(tab_bl[i].body); //okno.draw(tab_bl[i].rect); } } if (!tab_corps.empty()) { for (int i = 0; i < tab_corps.size(); i++) okno.draw(tab_corps[i].body); } if (!tab_am.empty()) { for (int i = 0; i < tab_am.size(); i++) { if(tab_am[i].onCreate_clock.getElapsedTime().asSeconds() < tab_am[i].lifeTime-8.0f) okno.draw(tab_am[i].body); if (tab_am[i].onCreate_clock.getElapsedTime().asSeconds() >= tab_am[i].lifeTime - 8.0f) { if (tab_am[i].onCreate_clock.getElapsedTime().asMilliseconds() % 500 >= 0 && tab_am[i].onCreate_clock.getElapsedTime().asMilliseconds() % 500 <= 125) { //nie rysij } else okno.draw(tab_am[i].body); } } } if (!tab_hp.empty()) { for (int i = 0; i < tab_hp.size(); i++) { if (tab_hp[i].onCreate_clock.getElapsedTime().asSeconds() < tab_hp[i].lifeTime - 8.0f) okno.draw(tab_hp[i].body); if (tab_hp[i].onCreate_clock.getElapsedTime().asSeconds() >= tab_hp[i].lifeTime - 8.0f) { if (tab_hp[i].onCreate_clock.getElapsedTime().asMilliseconds() % 500 >= 0 && tab_hp[i].onCreate_clock.getElapsedTime().asMilliseconds() % 500 <= 125) { //nie rysij } else okno.draw(tab_hp[i].body); } } } if (!tab_z.empty()) { for (int i = 0; i < tab_z.size(); i++) okno.draw(tab_z[i].body); } for (int i = 4; i < tab_wall.size(); i++) { okno.draw(tab_wall[i]); } okno.draw(celownik); okno.draw(gracz.body); //okno.draw(gracz.point); okno.draw(wsk_gun->body); okno.draw(hp); okno.draw(bullets_Left); okno.draw(round); okno.draw(licznik_zombie); okno.draw(gun_name); okno.draw(sprint_ui); ////////////////////////////////// View view = okno.getDefaultView(); okno.display(); czas = zegar.getElapsedTime(); zegar.restart(); } }
function<void()> ////////////////////////// TYPE APP NAME ON LINE BELOW ////////////////////////// delaun_distr (RenderWindow&window, ui &UI) { return [&window, &UI]() { #ifndef COMMON_INITS //void smoothmouse(RenderWindow&window, ui &UI){ // tidy code organization, here we predeclare methods just like a header would, it's redundant but necessary function<void()> draw, loop, init, update; function<void(Vec2 pos)> mousemoved; function<void(sf::Mouse::Button button)> mouseclick, mouserelease; function<void(Event&e)> treatotherevent; function<void(Keyboard::Key k)> treatkeyevent; // we don't have a class/interface/struct with data, everything is local to this function, like a classic stack that all programs are anyway. Texture cursor_tx; Sprite cursor; configfile cfg; cfg.init("bedlab.cfg"); Color background = cfg.getvar<Color>("background"); if (!cursor_tx.loadFromFile(cfg.getstr("cursor"))) cout << "did not load cursor" << endl; cursor.setTexture(cursor_tx); cursor.setOrigin(3, 3); window.setMouseCursorVisible(false); Vec2 mpos; bool leftclicked = false, rightclicked = false; // view and zoom View view, ui_view; ui_view = view = window.getDefaultView(); float zoomlevel = 1; Vec2 mpos_abs; #endif // COMMON_INITS // ************************ CODE BEGIN ************************ auto random_graph = [](int pick_count, float distrib_radius) { vector<pair<int,Vec2>> points; vector<pair<int, int>> graph; //float distrib_radius;// = cfg.getvar<float>("distrib_radius"); paused_distr paus_distr(distrib_radius, 5, 321); //int winheight = cfg.getvar<Vec2i>("windowsize").y; //scaler scl(winheight*0.9f, { 0,0 }); DelaunayTriangulation dela(1, 1); //int pick_count = cfg.getvar<int>("pick_count"); for (int i = 0; i < pick_count; ++i) { Vec2 cand; int retcode = -1; do { retcode = paus_distr.pick5(cand); } while (retcode == 2); if (retcode != -1 && retcode != 0) { dela.AddPoint(Point(cand.x, cand.y)); //glob_pts.push_back(mkcircle(scl(cand), Color::White, .5f)); //glob_pts.push_back(mkcircle(scl(cand), Color::White, 3.f)); points.push_back({ points.size(),cand }); } } //msg(glob_pts.size()); for (auto&triangle : dela.triangles) { int a = triangle.get()->v[0], b = triangle.get()->v[1], c = triangle.get()->v[2]; Point A = dela.points[a], B = dela.points[b], C = dela.points[c]; if ( A.x == 0 || A.x == 1 || A.y == 0 || A.y == 1 || B.x == 0 || B.x == 1 || B.y == 0 || B.y == 1 || C.x == 0 || C.x == 1 || C.y == 0 || C.y == 1 ) { continue; } graph.push_back({a,b}); graph.push_back({b,c}); graph.push_back({a,c}); // those are the ones! //segment(scl(Vec2(A.x, A.y)), scl(Vec2(B.x, B.y))); //segment(scl(Vec2(C.x, C.y)), scl(Vec2(B.x, B.y))); //segment(scl(Vec2(A.x, A.y)), scl(Vec2(C.x, C.y))); //segment(Vec2(A.x, A.y), Vec2(B.x, B.y)); //segment(Vec2(C.x, C.y), Vec2(B.x, B.y)); //segment(Vec2(A.x, A.y), Vec2(C.x, C.y)); //segment(pts[a], pts[b]); //segment(pts[a], pts[c]); //segment(pts[c], pts[b]); } return make_pair(points, graph); }; float distrib_radius = cfg.getvar<float>("distrib_radius"); paused_distr paus_distr(distrib_radius, 5, 321); int winheight = cfg.getvar<Vec2i>("windowsize").y; scaler scl(winheight*0.9f, { 0,0 }); DelaunayTriangulation dela(1, 1); int pick_count = cfg.getvar<int>("pick_count"); for(int i = 0; i < pick_count; ++i) { Vec2 cand; int retcode = -1; do { retcode = paus_distr.pick5(cand); } while (retcode == 2); if (retcode != -1 && retcode != 0) { dela.AddPoint(Point(cand.x, cand.y)); //glob_pts.push_back(mkcircle(scl(cand), Color::White, .5f)); glob_pts.push_back(mkcircle(scl(cand), Color::White, 3.f)); } } msg(glob_pts.size()); for (auto&triangle : dela.triangles) { int a = triangle.get()->v[0], b = triangle.get()->v[1], c = triangle.get()->v[2]; Point A = dela.points[a], B = dela.points[b], C = dela.points[c]; if ( A.x == 0 || A.x == 1 || A.y == 0 || A.y == 1 || B.x == 0 || B.x == 1 || B.y == 0 || B.y == 1 || C.x == 0 || C.x == 1 || C.y == 0 || C.y == 1 ) { continue; } segment(scl(Vec2(A.x, A.y)), scl(Vec2(B.x, B.y))); segment(scl(Vec2(C.x, C.y)), scl(Vec2(B.x, B.y))); segment(scl(Vec2(A.x, A.y)), scl(Vec2(C.x, C.y))); //segment(Vec2(A.x, A.y), Vec2(B.x, B.y)); //segment(Vec2(C.x, C.y), Vec2(B.x, B.y)); //segment(Vec2(A.x, A.y), Vec2(C.x, C.y)); //segment(pts[a], pts[b]); //segment(pts[a], pts[c]); //segment(pts[c], pts[b]); } barstack bst(FONT, FONTSIZE); slider_finder sl; size_t edit_mode = 1; // ************************ INITS END ************************ //then we actually define the functions, note how all function captures the local context by reference #ifndef LOOP_LAMBDAS draw = [&]() { window.setView(view); // object draw, AFTER normal view //Vec2 cand; //int retcode = -1; //do //{ // retcode = paus_distr.pick4(cand); //} while (retcode == 2); //if (retcode != -1 && retcode != 0) //{ // //glob_pts.push_back(mkcircle(scl(cand), Color::White, .5f)); // glob_pts.push_back(mkcircle(scl(cand), Color::White, .5f)); //} //////////////// obj draw START //////////////// for (auto&a : glob_pts)window.draw(a); for (auto&a : glob_rects)window.draw(a); for (auto&a : glob_vert)window.draw(a); for (auto&a : glob_texts)window.draw(a); //window.draw(nearest_pt); //////////////// obj draw END //////////////// // UI draw, AFTER ui view and BEFORE other draw window.setView(ui_view); bst.draw(window); // nothing after here please UI.draw(window); window.draw(cursor); }; update = [&]() { }; treatkeyevent = [&](Keyboard::Key k) { switch (k) { case Keyboard::BackSpace: glob_pts.clear(); glob_texts.clear(); glob_rects.clear(); glob_vert.clear(); break; case Keyboard::Space: break; case Keyboard::Q: break; } }; mousemoved = [&](Vec2 pos) { cursor.setPosition(pos); if (leftclicked) sl.mouse_callback(pos); }; mouseclick = [&](sf::Mouse::Button button) { if (button == Mouse::Button::Left) leftclicked = true; if (button == Mouse::Button::Right) rightclicked = true; }; mouserelease = [&](sf::Mouse::Button button) { if (button == Mouse::Button::Left) leftclicked = false; if (button == Mouse::Button::Right) rightclicked = false; sl.release(); }; loop = [&]() { while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { switch (event.type) { case sf::Event::KeyPressed: if (event.key.code == sf::Keyboard::Escape) window.close(); treatkeyevent(event.key.code); break; case sf::Event::Closed: window.close(); break; case sf::Event::MouseButtonPressed: mouseclick(event.mouseButton.button); break; case sf::Event::MouseButtonReleased: mouserelease(event.mouseButton.button); break; case sf::Event::MouseMoved: mpos = Vec2(event.mouseMove.x, event.mouseMove.y); mousemoved(mpos); break; default: treatotherevent(event); break; } } window.clear(background); update(); draw(); window.display(); } }; treatotherevent = [&](Event&e) { if (e.type == Event::MouseWheelMoved && e.mouseWheel.delta) { mpos_abs = window.mapPixelToCoords(Vec2i(mpos), view); //view = window.getView(); if (e.mouseWheel.delta < 0) { zoomlevel *= 2.f; view.setSize(view.getSize()*2.f); view.setCenter(interp(mpos_abs, view.getCenter(), 2.f)); //view.setCenter(interp(mpos_abs, view.getCenter(), 2.f)); } if (e.mouseWheel.delta > 0) { zoomlevel *= 0.5; view.setSize(view.getSize()*.5f); view.setCenter(.5f*(view.getCenter() + mpos_abs)); //view.setCenter(.5f*(view.getCenter() + mpos_abs)); } window.setView(view); } }; loop(); #endif // LOOP_LAMBDAS }; }