moving a bunch of things around
This commit is contained in:
+30
-13
@@ -17,18 +17,24 @@ bool is_block_buffer_empty(Block** block_buffer, int block_buffer_size);
|
|||||||
|
|
||||||
enum Direction { LEFT, RIGHT, NONE };
|
enum Direction { LEFT, RIGHT, NONE };
|
||||||
|
|
||||||
|
struct Window {
|
||||||
|
char view[ROW][COL] = {};
|
||||||
|
void clear_display();
|
||||||
|
void draw_display(int fps);
|
||||||
|
void update_board(Paddle paddle, Ball ball);
|
||||||
|
};
|
||||||
|
|
||||||
struct vec2 {
|
struct vec2 {
|
||||||
vec2() : x(0), y(0) {};
|
vec2() : x(0), y(0) {};
|
||||||
vec2(int x, int y) : x(x), y(y) {};
|
vec2(int x, int y) : x(x), y(y) {};
|
||||||
vec2(int x, int y, char symbol) : x(x), y(y), m_symbol(symbol) {};
|
vec2(int x, int y, char symbol) : x(x), y(y), m_symbol(symbol) {};
|
||||||
int x, y;
|
int x, y;
|
||||||
char m_symbol;
|
char m_symbol;
|
||||||
void set(int x, int y);
|
void set(int _x, int _y) { x = _x; y = _y; };
|
||||||
auto operator <=> (const vec2&) const = default;
|
auto operator <=> (const vec2&) const = default;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Ball {
|
struct Ball {
|
||||||
Ball();
|
|
||||||
vec2 m_origin { COL / 2, ROW - 5, '*' };
|
vec2 m_origin { COL / 2, ROW - 5, '*' };
|
||||||
vec2 vel { 1, -1 };
|
vec2 vel { 1, -1 };
|
||||||
void determine_new_position(int &paddle_lives, int reset_ball_x, int reset_ball_y);
|
void determine_new_position(int &paddle_lives, int reset_ball_x, int reset_ball_y);
|
||||||
@@ -40,19 +46,30 @@ struct Paddle {
|
|||||||
int _size;
|
int _size;
|
||||||
int m_hearts { 3 };
|
int m_hearts { 3 };
|
||||||
std::array<vec2, 3> m_nodes;
|
std::array<vec2, 3> m_nodes;
|
||||||
enum Direction m_direction;
|
enum Direction dir;
|
||||||
void determine_new_position(char (*board)[COL]);
|
void determine_new_position(char (*board)[COL]);
|
||||||
void check_collision(Ball &ball);
|
void check_collision(Ball &ball);
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Block {
|
// struct Block {
|
||||||
Block();
|
// Block();
|
||||||
Block(vec2 pos, char symbol);
|
// Block(vec2 pos, char symbol);
|
||||||
~Block();
|
// ~Block();
|
||||||
std::array<vec2, 3> m_nodes = {};
|
// std::array<vec2, 3> m_nodes = {};
|
||||||
bool check_collision(Ball &ball);
|
// bool check_collision(Ball &ball);
|
||||||
};
|
|
||||||
|
|
||||||
// class Engine {
|
|
||||||
//
|
|
||||||
// };
|
// };
|
||||||
|
|
||||||
|
class Engine {
|
||||||
|
public:
|
||||||
|
Engine();
|
||||||
|
~Engine();
|
||||||
|
void run();
|
||||||
|
|
||||||
|
private:
|
||||||
|
Window win;
|
||||||
|
Paddle paddle;
|
||||||
|
Ball ball;
|
||||||
|
bool process_input();
|
||||||
|
bool quit_if();
|
||||||
|
|
||||||
|
};
|
||||||
|
|||||||
+103
-26
@@ -1,5 +1,83 @@
|
|||||||
#include <engine.hpp>
|
#include <engine.hpp>
|
||||||
|
#include <ncurses.h>
|
||||||
#include "globals.hpp"
|
#include "globals.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
#include <chrono>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
void Window::clear_display() {
|
||||||
|
for(int i = 0; i < ROW; i++) {
|
||||||
|
for(int j = 0; j < COL; j++) {
|
||||||
|
view[i][j] = '.';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Window::update_board(Paddle paddle, Ball ball) {
|
||||||
|
for(int i = 0; i < paddle.m_nodes.size(); i++) {
|
||||||
|
view[paddle.m_nodes[i].y][paddle.m_nodes[i].x] = paddle.m_nodes[i].m_symbol;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Window::draw_display(int fps) {
|
||||||
|
std::cout << "\033[H" << std::flush;
|
||||||
|
for(int i = 0; i < ROW; i++) {
|
||||||
|
for(int j = 0; j < COL; j++) {
|
||||||
|
if(view[i][j] != '.') std::cout << view[i][j] << " ";
|
||||||
|
else std::cout << ". ";
|
||||||
|
}
|
||||||
|
std::cout << "\r\n";
|
||||||
|
}
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(fps));
|
||||||
|
}
|
||||||
|
|
||||||
|
Engine::Engine() {
|
||||||
|
initscr();
|
||||||
|
cbreak();
|
||||||
|
noecho();
|
||||||
|
nodelay(stdscr, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
Engine::~Engine() {
|
||||||
|
endwin();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Engine::run() {
|
||||||
|
// const int buffer_size = 50, block_buffer_size = 15;
|
||||||
|
// vec2* buffer[buffer_size] = {};
|
||||||
|
|
||||||
|
// Ball ball = create_new_ball(buffer, 0, 1, -1, (ROW - 5), (COL / 2), '*' );
|
||||||
|
// Paddle paddle = create_new_paddle(buffer, 1, 3, ROW - 2, '-');
|
||||||
|
// Block** block_buffer = create_block_buffer('3', 3, 5, 3);
|
||||||
|
// rasterize_buffer(buffer, block_buffer, 4);
|
||||||
|
|
||||||
|
std::thread input_thread([&] { input.get_input(running, paddle.m_direction); } );
|
||||||
|
|
||||||
|
while (process_input() && quit_if()) {
|
||||||
|
paddle.determine_new_position(win.view);
|
||||||
|
paddle.check_collision(ball);
|
||||||
|
// trim_block_buffer_on_collision(buffer, buffer_size, block_buffer, block_buffer_size, ball);
|
||||||
|
ball.determine_new_position(paddle.m_hearts, (ROW - 5), (COL / 2));
|
||||||
|
win.clear_display();
|
||||||
|
win.update_board(paddle, ball);
|
||||||
|
win.draw_display(FPMS);
|
||||||
|
if(paddle.m_hearts <= 0) {
|
||||||
|
running = false;
|
||||||
|
std::cout << "No Hearts left! You lose!\n";
|
||||||
|
}
|
||||||
|
// if(is_block_buffer_empty(block_buffer, block_buffer_size)) {
|
||||||
|
// running = false;
|
||||||
|
// std::cout << "No Blocks left! You win!\n";
|
||||||
|
// }
|
||||||
|
std::cout << "Hearts left: " << paddle.m_hearts << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
input_thread.join();
|
||||||
|
// cleanup(block_buffer, block_buffer_size);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// Block** create_block_buffer(int starting_symbol, int rows, int cols, int length) {
|
// Block** create_block_buffer(int starting_symbol, int rows, int cols, int length) {
|
||||||
// Block** block_buffer = new Block*[rows * cols];
|
// Block** block_buffer = new Block*[rows * cols];
|
||||||
@@ -61,28 +139,27 @@
|
|||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
//
|
||||||
Paddle::Paddle() {
|
Paddle::Paddle() {
|
||||||
for(int i = 0; i < m_nodes.size(); i++) {
|
for(int i = 0; i < m_nodes.size(); i++) {
|
||||||
m_nodes[i].set(COL -2 , (ROW / 2) - (m_nodes.size() - i));
|
m_nodes[i].set((COL - 2) - (m_nodes.size() - i), ROW - 2);
|
||||||
m_nodes[i].m_symbol = '-';
|
m_nodes[i].m_symbol = '-';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Paddle::determine_new_position(char (*board)[COL]) {
|
void Paddle::determine_new_position(char (*board)[COL]) {
|
||||||
if(m_direction == LEFT && m_nodes[0].x - 1 >= 0) {
|
if(dir == Direction::LEFT || dir == Direction::RIGHT) return;
|
||||||
m_nodes[0].x--;
|
if(dir == Direction::LEFT && m_nodes[0].x - 1 >= 0) {
|
||||||
for(int i = 1; i < _size; i++) {
|
m_nodes.front().x--;
|
||||||
m_nodes[i].x = m_nodes[0].x + i;
|
|
||||||
}
|
}
|
||||||
|
if(dir == Direction::RIGHT && m_nodes.back().x + 1 < ROW) {
|
||||||
|
m_nodes.front().x++;
|
||||||
}
|
}
|
||||||
else if(m_direction == RIGHT && m_nodes[_size - 1].x + 1 < ROW) {
|
for(int i = 1; i < m_nodes.size(); i++) {
|
||||||
m_nodes[_size - 1].x++;
|
m_nodes[i].set(m_nodes.front().x - (m_nodes.size() - i), ROW - 2);
|
||||||
for(int i = 1; i < _size; i++) {
|
m_nodes[i].m_symbol = '-';
|
||||||
m_nodes[_size - (i + 1)].x = m_nodes[_size - 1].x - i;
|
|
||||||
}
|
}
|
||||||
}
|
dir = Direction::NONE;
|
||||||
m_direction = NONE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Paddle::check_collision(Ball &ball) {
|
void Paddle::check_collision(Ball &ball) {
|
||||||
@@ -114,17 +191,17 @@ void Ball::determine_new_position(int &paddle_lives, int reset_ball_x, int reset
|
|||||||
m_origin.y += vel.y;
|
m_origin.y += vel.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
Block::Block(vec2 pos, char symbol) {
|
// Block::Block(vec2 pos, char symbol) {
|
||||||
for(int i = 0; i < m_nodes.size(); i++) {
|
// for(int i = 0; i < m_nodes.size(); i++) {
|
||||||
m_nodes[i].set(pos.x + i, pos.y);
|
// m_nodes[i].set(pos.x + i, pos.y);
|
||||||
m_nodes[i].m_symbol = symbol;
|
// m_nodes[i].m_symbol = symbol;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
bool Block::check_collision(Ball &ball) {
|
// bool Block::check_collision(Ball &ball) {
|
||||||
for(int i = 0; i < m_nodes.size(); i++) {
|
// for(int i = 0; i < m_nodes.size(); i++) {
|
||||||
if(m_nodes[i] != ball.m_origin) continue;
|
// if(m_nodes[i] != ball.m_origin) continue;
|
||||||
return true;
|
// return true;
|
||||||
}
|
// }
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
|
|||||||
+3
-3
@@ -10,13 +10,13 @@ void Input::get_input(bool &running, enum Direction &dir) {
|
|||||||
endwin();
|
endwin();
|
||||||
|
|
||||||
switch (m_getch) {
|
switch (m_getch) {
|
||||||
case 67:
|
case 'd':
|
||||||
dir = RIGHT;
|
dir = RIGHT;
|
||||||
break;
|
break;
|
||||||
case 68:
|
case 'a':
|
||||||
dir = LEFT;
|
dir = LEFT;
|
||||||
break;
|
break;
|
||||||
case 113:
|
case 'q':
|
||||||
running = false;
|
running = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user