finally added pipes + collision

This commit is contained in:
brxxh 2023-11-24 17:43:17 +01:00
parent ba56c0d911
commit 5a9eb10507

View file

@ -46,6 +46,8 @@ struct Background {
paradiso::Sprite* scrolling[2] = {&backgroundLeft, &backgroundRight}; paradiso::Sprite* scrolling[2] = {&backgroundLeft, &backgroundRight};
paradiso::Renderer renderer{};
Background() { Background() {
auto backgroundImage = auto backgroundImage =
paradiso::BitmapIO::get().load("background-day.png"); paradiso::BitmapIO::get().load("background-day.png");
@ -71,18 +73,18 @@ struct Background {
void draw(const paradiso::Shader& shader) { void draw(const paradiso::Shader& shader) {
for (auto sprite : scrolling) { for (auto sprite : scrolling) {
if (game_over == false) {
if (sprite->pivot.x() <= -2.0f) { if (sprite->pivot.x() <= -2.0f) {
sprite->pivot.x() += 4.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("pivot", sprite->pivot);
shader.set_uniform("scale", sprite->scale); shader.set_uniform("scale", sprite->scale);
shader.set_uniform("rotation", sprite->rotation); shader.set_uniform("rotation", sprite->rotation);
renderer.draw(*sprite, shader); renderer.draw(*sprite, shader);
} }
} }
paradiso::Renderer renderer{};
}; };
struct Pipe { struct Pipe {
@ -92,7 +94,9 @@ struct Pipe {
paradiso::Renderer renderer1{}; paradiso::Renderer renderer1{};
paradiso::Renderer renderer2{}; 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; float pipe_spawn_rand = float(pipe_spawn_rand_int) / 100;
bool pos_reset = false; bool pos_reset = false;
@ -102,31 +106,42 @@ struct Pipe {
pipe_top = paradiso::Sprite{ pipe_top = paradiso::Sprite{
.bitmap = pipe_image, .bitmap = pipe_image,
.pivot = {paradiso::Vector2<float>::make(0.0f, pipe_spawn_rand + 1.0f)}, .pivot = {paradiso::Vector2<float>::make(1.4f, pipe_spawn_rand + 1.0f)},
.scale = {paradiso::Vector2<float>::make(((500.0f - (500.0f - 52.0f)) / 500.0f) * 2.25f, ((700.0f - (700.0f - 320.0f)) / 700.0f) * 2.25f)}, .scale = {paradiso::Vector2<float>::make(((500.0f - (500.0f - 52.0f)) / 500.0f) * 2.25f, ((700.0f - (700.0f - 320.0f)) / 700.0f) * 2.25f)},
.rotation = 3.1415926f}; .rotation = 3.1415926f};
pipe_bottom = paradiso::Sprite{ pipe_bottom = paradiso::Sprite{
.bitmap = pipe_image, .bitmap = pipe_image,
.pivot = {paradiso::Vector2<float>::make(0.0f, pipe_spawn_rand - 1.5f)}, .pivot = {paradiso::Vector2<float>::make(1.4f, pipe_spawn_rand - 1.5f)},
.scale = {paradiso::Vector2<float>::make(((500.0f - (500.0f - 52.0f)) / 500.0f) * 2.25f, ((700.0f - (700.0f - 320.0f)) / 700.0f) * 2.25f)}}; .scale = {paradiso::Vector2<float>::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() { void update() {
pipe_spawn_rand_int = rand() % 90 + 20;
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_spawn_rand = float(pipe_spawn_rand_int) / 100;
pipe_top.pivot.x() -= 0.02f; pipe_top.pivot.x() -= 0.02f;
pipe_bottom.pivot.x() -= 0.02f; pipe_bottom.pivot.x() -= 0.02f;
risky_pos_x = pipe_top.pivot.x(); risky_pos_x = pipe_top.pivot.x();
risky_pos_max_x = risky_pos_max_x = pipe_top.pivot.x() + ((500.0f - (500.0f - 52.0f)) / 500.0f) * 2.25f;
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_y = pipe_top.pivot.y();
risky_pos_top_max_y = pipe_top.pivot.y() + 10.0f; risky_pos_top_max_y = pipe_top.pivot.y() + 10.0f;
// std::cout << risky_pos_top_x << std::endl; risky_pos_bottom_y = pipe_bottom.pivot.y();
// std::cout << risky_pos_top_max_x << std::endl; risky_pos_bottom_max_y = pipe_bottom.pivot.y() - 10.0f;
if (pipe_top.pivot.x() <= -1.4f || pipe_bottom.pivot.x() <= -1.4f) { if (pipe_top.pivot.x() <= -1.4f || pipe_bottom.pivot.x() <= -1.4f) {
pos_reset = true; pos_reset = true;
@ -141,7 +156,7 @@ struct Pipe {
pipe_top.pivot.x() = 1.4f; pipe_top.pivot.x() = 1.4f;
pipe_bottom.pivot.x() = 1.4f; pipe_bottom.pivot.x() = 1.4f;
} }
}
} }
void draw(const paradiso::Shader& shader) { void draw(const paradiso::Shader& shader) {
@ -177,10 +192,12 @@ struct Grass {
void draw(const paradiso::Shader& shader) { void draw(const paradiso::Shader& shader) {
for (auto sprite : scrolling) { for (auto sprite : scrolling) {
if (game_over == false) {
if (sprite->pivot.x() <= -1.0f) { if (sprite->pivot.x() <= -1.0f) {
sprite->pivot.x() += 2.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("pivot", sprite->pivot);
shader.set_uniform("scale", sprite->scale); shader.set_uniform("scale", sprite->scale);
shader.set_uniform("rotation", sprite->rotation); shader.set_uniform("rotation", sprite->rotation);
@ -216,6 +233,8 @@ struct QuickWings {
float rotation = 0.0f; float rotation = 0.0f;
int collision_counter = 0;
QuickWings() { QuickWings() {
float scaleh = 0.08f; float scaleh = 0.08f;
float scalew = 0.158f; float scalew = 0.158f;
@ -307,16 +326,26 @@ struct QuickWings {
rotation = velocity * 10.0f; rotation = velocity * 10.0f;
} }
std::cout << risky_pos_top_y << std::endl; float final_risky_pos_top_y = risky_pos_top_y - 1.06f;
std::cout << risky_pos_top_max_y << std::endl; float final_risky_pos_bottom_y = risky_pos_bottom_y + 1.06f;
std::cout << pos << std::endl; if (risky_pos_x - 0.3f <= 0.0f && risky_pos_max_x >= 0.0f) {
std::cout << "\n\n" << std::endl; if (pos >= final_risky_pos_top_y && pos <= risky_pos_top_max_y) {
if (risky_pos_x <= 0.0f && risky_pos_max_x >= 0.0f) {
if (pos >= risky_pos_top_y && pos <= risky_pos_top_max_y) {
game_over = true; 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 message = Message{};
auto gameover = GameOverMessage{}; auto gameover = GameOverMessage{};
// timer // timer
// das update führt den hier mitgegebnen Ausdruck innerhalb der internen // 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(); auto t1 = std::chrono::high_resolution_clock::now();
// Keyboard and state change + update // Keyboard and state change + update
if (quickwingsapp.paused == false) {
pipe.paused = false;
}
pipe.update(); pipe.update();
quickwingsapp.on_keyboard(w.keyboard_input()); quickwingsapp.on_keyboard(w.keyboard_input());