forked from Hartmut/paradiso
Started working on pipes collision (WIP)
This commit is contained in:
parent
68915cb24b
commit
ba56c0d911
2 changed files with 67 additions and 8 deletions
|
@ -5,6 +5,8 @@
|
|||
*
|
||||
*/
|
||||
|
||||
#include <bits/iterator_concepts.h>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <ostream>
|
||||
#include <paradiso/bitmap.hpp>
|
||||
|
@ -29,6 +31,12 @@
|
|||
|
||||
const int frame_rate = 60;
|
||||
bool game_over = false;
|
||||
float risky_pos_x;
|
||||
float risky_pos_max_x;
|
||||
float risky_pos_bottom_y;
|
||||
float risky_pos_top_y;
|
||||
float risky_pos_bottom_max_y;
|
||||
float risky_pos_top_max_y;
|
||||
|
||||
struct Background {
|
||||
paradiso::Sprite backgroundLeft;
|
||||
|
@ -84,9 +92,11 @@ struct Pipe {
|
|||
paradiso::Renderer renderer1{};
|
||||
paradiso::Renderer renderer2{};
|
||||
|
||||
int pipe_spawn_rand_int = rand() % 80 + 25;
|
||||
int pipe_spawn_rand_int = rand() % 90 + 25;
|
||||
float pipe_spawn_rand = float(pipe_spawn_rand_int) / 100;
|
||||
|
||||
bool pos_reset = false;
|
||||
|
||||
Pipe() {
|
||||
auto pipe_image = paradiso::BitmapIO::get().load("pipe-green.png");
|
||||
|
||||
|
@ -97,10 +107,43 @@ struct Pipe {
|
|||
.rotation = 3.1415926f};
|
||||
pipe_bottom = paradiso::Sprite{
|
||||
.bitmap = pipe_image,
|
||||
.pivot = {paradiso::Vector2<float>::make(0.0f, pipe_spawn_rand - 1.6f)},
|
||||
.pivot = {paradiso::Vector2<float>::make(0.0f, 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)}};
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void draw(const paradiso::Shader& shader) {
|
||||
shader.set_uniform("pivot", pipe_bottom.pivot);
|
||||
shader.set_uniform("scale", pipe_bottom.scale);
|
||||
|
@ -137,7 +180,7 @@ struct Grass {
|
|||
if (sprite->pivot.x() <= -1.0f) {
|
||||
sprite->pivot.x() += 2.0f;
|
||||
}
|
||||
sprite->pivot.x() -= 0.03f;
|
||||
sprite->pivot.x() -= 0.02f;
|
||||
shader.set_uniform("pivot", sprite->pivot);
|
||||
shader.set_uniform("scale", sprite->scale);
|
||||
shader.set_uniform("rotation", sprite->rotation);
|
||||
|
@ -158,10 +201,10 @@ struct QuickWings {
|
|||
unsigned int flapCounter = 0; // How many ticks since last flap
|
||||
|
||||
float velocity = 0.0f;
|
||||
const float max_velocity = 0.04f;
|
||||
const float max_velocity = 0.02f;
|
||||
|
||||
const float gravity = -0.004f;
|
||||
const float move_up_velocity = 0.04f;
|
||||
const float gravity = -0.002f;
|
||||
const float move_up_velocity = 0.02f;
|
||||
|
||||
bool move_up = false;
|
||||
bool paused = true;
|
||||
|
@ -231,8 +274,9 @@ struct QuickWings {
|
|||
}
|
||||
|
||||
// Stop game
|
||||
if (paused)
|
||||
if (paused) {
|
||||
return;
|
||||
}
|
||||
else {
|
||||
// Apply gravity
|
||||
velocity += gravity;
|
||||
|
@ -262,6 +306,19 @@ struct QuickWings {
|
|||
// Update rotation
|
||||
rotation = velocity * 10.0f;
|
||||
}
|
||||
|
||||
std::cout << risky_pos_top_y << std::endl;
|
||||
std::cout << risky_pos_top_max_y << std::endl;
|
||||
|
||||
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) {
|
||||
game_over = true;
|
||||
std::cout << "collision!" << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// keyboard handler
|
||||
|
@ -402,7 +459,9 @@ auto main() -> int {
|
|||
ctx.clear();
|
||||
auto t1 = std::chrono::high_resolution_clock::now();
|
||||
|
||||
// Keyboard and state change
|
||||
// Keyboard and state change + update
|
||||
pipe.update();
|
||||
|
||||
quickwingsapp.on_keyboard(w.keyboard_input());
|
||||
quickwingsapp.update();
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue