diff --git a/examples/quickwings/quickwings.cpp b/examples/quickwings/quickwings.cpp index 875f838..f8a4995 100644 --- a/examples/quickwings/quickwings.cpp +++ b/examples/quickwings/quickwings.cpp @@ -46,6 +46,8 @@ struct Background { paradiso::Sprite* scrolling[2] = {&backgroundLeft, &backgroundRight}; + paradiso::Renderer renderer{}; + Background() { auto backgroundImage = paradiso::BitmapIO::get().load("background-day.png"); @@ -71,18 +73,18 @@ struct Background { void draw(const paradiso::Shader& shader) { for (auto sprite : scrolling) { - if (sprite->pivot.x() <= -2.0f) { - sprite->pivot.x() += 4.0f; + if (game_over == false) { + if (sprite->pivot.x() <= -2.0f) { + sprite->pivot.x() += 4.0f; + } + sprite->pivot.x() -= 0.002f; } - sprite->pivot.x() -= 0.002f; shader.set_uniform("pivot", sprite->pivot); shader.set_uniform("scale", sprite->scale); shader.set_uniform("rotation", sprite->rotation); renderer.draw(*sprite, shader); } } - - paradiso::Renderer renderer{}; }; struct Pipe { @@ -92,7 +94,9 @@ struct Pipe { paradiso::Renderer renderer1{}; paradiso::Renderer renderer2{}; - int pipe_spawn_rand_int = rand() % 90 + 25; + bool paused = false; + + int pipe_spawn_rand_int = rand() % 80 + 15; float pipe_spawn_rand = float(pipe_spawn_rand_int) / 100; bool pos_reset = false; @@ -102,46 +106,57 @@ struct Pipe { pipe_top = paradiso::Sprite{ .bitmap = pipe_image, - .pivot = {paradiso::Vector2::make(0.0f, pipe_spawn_rand + 1.0f)}, + .pivot = {paradiso::Vector2::make(1.4f, pipe_spawn_rand + 1.0f)}, .scale = {paradiso::Vector2::make(((500.0f - (500.0f - 52.0f)) / 500.0f) * 2.25f, ((700.0f - (700.0f - 320.0f)) / 700.0f) * 2.25f)}, .rotation = 3.1415926f}; pipe_bottom = paradiso::Sprite{ .bitmap = pipe_image, - .pivot = {paradiso::Vector2::make(0.0f, pipe_spawn_rand - 1.5f)}, + .pivot = {paradiso::Vector2::make(1.4f, pipe_spawn_rand - 1.5f)}, .scale = {paradiso::Vector2::make(((500.0f - (500.0f - 52.0f)) / 500.0f) * 2.25f, ((700.0f - (700.0f - 320.0f)) / 700.0f) * 2.25f)}}; + + paused = true; + } void update() { - pipe_spawn_rand_int = rand() % 90 + 20; - pipe_spawn_rand = float(pipe_spawn_rand_int) / 100; - pipe_top.pivot.x() -= 0.02f; - pipe_bottom.pivot.x() -= 0.02f; - - risky_pos_x = pipe_top.pivot.x(); - risky_pos_max_x = - pipe_top.pivot.x() + ((500.0f - (500.0f - 52.0f)) / 500.0f) * 2.25f; - - risky_pos_top_y = pipe_top.pivot.y(); - risky_pos_top_max_y = pipe_top.pivot.y() + 10.0f; - - // std::cout << risky_pos_top_x << std::endl; - // std::cout << risky_pos_top_max_x << std::endl; - - if (pipe_top.pivot.x() <= -1.4f || pipe_bottom.pivot.x() <= -1.4f) { - pos_reset = true; - - if (pos_reset == true) { - pipe_top.pivot.y() = pipe_spawn_rand + 1.0f; - pipe_bottom.pivot.y() = pipe_spawn_rand - 1.5; - - pos_reset = false; - } - - pipe_top.pivot.x() = 1.4f; - pipe_bottom.pivot.x() = 1.4f; + if (game_over == true) { + paused = true; } + if (paused == true) { + return; + } + else { + pipe_spawn_rand_int = rand() % 80 + 15; + pipe_spawn_rand = float(pipe_spawn_rand_int) / 100; + + pipe_top.pivot.x() -= 0.02f; + pipe_bottom.pivot.x() -= 0.02f; + + risky_pos_x = pipe_top.pivot.x(); + risky_pos_max_x = pipe_top.pivot.x() + ((500.0f - (500.0f - 52.0f)) / 500.0f) * 2.25f; + + risky_pos_top_y = pipe_top.pivot.y(); + risky_pos_top_max_y = pipe_top.pivot.y() + 10.0f; + + risky_pos_bottom_y = pipe_bottom.pivot.y(); + risky_pos_bottom_max_y = pipe_bottom.pivot.y() - 10.0f; + + if (pipe_top.pivot.x() <= -1.4f || pipe_bottom.pivot.x() <= -1.4f) { + pos_reset = true; + + if (pos_reset == true) { + pipe_top.pivot.y() = pipe_spawn_rand + 1.0f; + pipe_bottom.pivot.y() = pipe_spawn_rand - 1.5; + + pos_reset = false; + } + + pipe_top.pivot.x() = 1.4f; + pipe_bottom.pivot.x() = 1.4f; + } + } } void draw(const paradiso::Shader& shader) { @@ -177,10 +192,12 @@ struct Grass { void draw(const paradiso::Shader& shader) { for (auto sprite : scrolling) { - if (sprite->pivot.x() <= -1.0f) { - sprite->pivot.x() += 2.0f; + if (game_over == false) { + if (sprite->pivot.x() <= -1.0f) { + sprite->pivot.x() += 2.0f; + } + sprite->pivot.x() -= 0.02f; } - sprite->pivot.x() -= 0.02f; shader.set_uniform("pivot", sprite->pivot); shader.set_uniform("scale", sprite->scale); shader.set_uniform("rotation", sprite->rotation); @@ -216,6 +233,8 @@ struct QuickWings { float rotation = 0.0f; + int collision_counter = 0; + QuickWings() { float scaleh = 0.08f; float scalew = 0.158f; @@ -307,16 +326,26 @@ struct QuickWings { rotation = velocity * 10.0f; } - std::cout << risky_pos_top_y << std::endl; - std::cout << risky_pos_top_max_y << std::endl; + float final_risky_pos_top_y = risky_pos_top_y - 1.06f; + float final_risky_pos_bottom_y = risky_pos_bottom_y + 1.06f; - std::cout << pos << std::endl; - std::cout << "\n\n" << std::endl; - - if (risky_pos_x <= 0.0f && risky_pos_max_x >= 0.0f) { - if (pos >= risky_pos_top_y && pos <= risky_pos_top_max_y) { + if (risky_pos_x - 0.3f <= 0.0f && risky_pos_max_x >= 0.0f) { + if (pos >= final_risky_pos_top_y && pos <= risky_pos_top_max_y) { game_over = true; - std::cout << "collision!" << std::endl; + } + if (pos <= final_risky_pos_bottom_y && pos >= risky_pos_bottom_max_y) { + if (collision_counter == 0) { + collision_counter++; + std::cout << "incremented!" << std::endl; + } + else { + collision_counter = 2; + } + + + if (collision_counter == 2) { + game_over = true; + } } } } @@ -447,6 +476,7 @@ auto main() -> int { auto message = Message{}; auto gameover = GameOverMessage{}; + // timer // das update führt den hier mitgegebnen Ausdruck innerhalb der internen @@ -460,6 +490,10 @@ auto main() -> int { auto t1 = std::chrono::high_resolution_clock::now(); // Keyboard and state change + update + if (quickwingsapp.paused == false) { + pipe.paused = false; + } + pipe.update(); quickwingsapp.on_keyboard(w.keyboard_input()); @@ -490,7 +524,7 @@ auto main() -> int { // Quit return !(w.keyboard_input().size() && w.keyboard_input().top().key == 'Q'); - })) { + })) { }; return 0;