211 lines
5.7 KiB
C++
211 lines
5.7 KiB
C++
#include <engine.hpp>
|
|
#include <ncurses.h>
|
|
#include "globals.hpp"
|
|
#include <iostream>
|
|
#include <chrono>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
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, std::vector<Block>) {
|
|
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;
|
|
}
|
|
view[ball.m_origin.y][ball.m_origin.x] = ball.m_origin.m_symbol;
|
|
}
|
|
|
|
void Window::draw_display(int fps) const {
|
|
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);
|
|
vec2 block_size = { 5, 3 };
|
|
int row, int_symbol, col;
|
|
char char_symbol;
|
|
for(int index = 0; index < block_size.y * block_size.x; index++) {
|
|
row = index / 5;
|
|
int_symbol = ('3' - '0') - row;
|
|
char_symbol = '0' + int_symbol;
|
|
col = (index % 5) * 3;
|
|
Block new_block = { { col, row }, char_symbol };
|
|
block_buffer.push_back(new_block);
|
|
}
|
|
}
|
|
|
|
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);
|
|
|
|
// while (process_input() && quit_if()) {
|
|
while (process_input()) {
|
|
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, block_buffer);
|
|
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";
|
|
}
|
|
}
|
|
|
|
bool Engine::process_input() {
|
|
int ch;
|
|
while((ch = getch()) != ERR) {
|
|
if(ch == 'd') paddle.dir = Direction::RIGHT;
|
|
if(ch == 'a') paddle.dir = Direction::LEFT;
|
|
if(ch == 'q') return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
//
|
|
// bool is_block_buffer_empty(Block** block_buffer, int block_buffer_size) {
|
|
// int total_clear_blocks = 0;
|
|
// for(int block_index = 0; block_index < block_buffer_size; block_index++) {
|
|
// if(block_buffer[block_index] != nullptr) continue;
|
|
// total_clear_blocks++;
|
|
// }
|
|
//
|
|
// if(total_clear_blocks == block_buffer_size) return true;
|
|
// return false;
|
|
// }
|
|
|
|
// void cleanup(Block** block_buffer, int block_buffer_size) {
|
|
// for(int i = 0; i < block_buffer_size; i++) {
|
|
// delete block_buffer[i];
|
|
// }
|
|
// delete[] block_buffer;
|
|
// }
|
|
//
|
|
// void rasterize_buffer(vec2** buffer, Block** block_buffer, int offset) {
|
|
// for(int block_index = 0; block_index < 15; block_index++) {
|
|
// if(block_buffer[block_index] == nullptr) continue;
|
|
// for(int node_index = 0; node_index < block_buffer[block_index]->_size; node_index++) {
|
|
// buffer[offset + node_index] = block_buffer[block_index]->m_nodes[node_index];
|
|
// }
|
|
// offset += 3;
|
|
// }
|
|
// }
|
|
|
|
// void trim_block_buffer_on_collision(vec2** buffer, const int buffer_size, Block** block_buffer, const int block_buffer_size, Ball &ball) {
|
|
// for(int i = 0; i < block_buffer_size; i++) {
|
|
// if(block_buffer[i] == nullptr) continue;
|
|
// if(block_buffer[i]->check_collision(ball)) {
|
|
// ball.change_velocity('y');
|
|
// for(int a = 0; a < block_buffer[i]->_size; a++) {
|
|
// vec2* doomed = block_buffer[i]->m_nodes[a];
|
|
// for(int j = 0; j < buffer_size; j++) {
|
|
// if(buffer[j] == doomed) {
|
|
// buffer[j] = nullptr;
|
|
// }
|
|
// }
|
|
// }
|
|
// delete block_buffer[i];
|
|
// block_buffer[i] = nullptr;
|
|
// }
|
|
// }
|
|
// }
|
|
//
|
|
Paddle::Paddle() {
|
|
for(int i = 0; i < m_nodes.size(); i++) {
|
|
m_nodes[i].set((COL - 2) - (m_nodes.size() - i), ROW - 2);
|
|
m_nodes[i].m_symbol = '-';
|
|
}
|
|
}
|
|
|
|
void Paddle::determine_new_position(char (*board)[COL]) {
|
|
if(dir == Direction::LEFT || dir == Direction::RIGHT) return;
|
|
if(dir == Direction::LEFT && m_nodes[0].x - 1 >= 0) {
|
|
m_nodes.front().x--;
|
|
}
|
|
if(dir == Direction::RIGHT && m_nodes.back().x + 1 < ROW) {
|
|
m_nodes.front().x++;
|
|
}
|
|
for(int i = 1; i < m_nodes.size(); i++) {
|
|
m_nodes[i].set(m_nodes.front().x - (m_nodes.size() - i), ROW - 2);
|
|
m_nodes[i].m_symbol = '-';
|
|
}
|
|
dir = Direction::NONE;
|
|
}
|
|
|
|
void Paddle::check_collision(Ball &ball) {
|
|
for(int i = 0; i < _size; i++) {
|
|
if(ball.m_origin != m_nodes[i]) continue;
|
|
ball.vel.y *= -1;
|
|
}
|
|
}
|
|
|
|
void Ball::change_velocity(char d) {
|
|
if(d == 'x') {
|
|
vel.x *= -1;
|
|
} else if(d == 'y') {
|
|
vel.y *= -1;
|
|
}
|
|
}
|
|
|
|
void Ball::determine_new_position(int &paddle_lives, int reset_ball_x, int reset_ball_y) {
|
|
if(m_origin.y <= 0) {
|
|
vel.y = 1;
|
|
} else if(m_origin.y >= ROW - 1) {
|
|
paddle_lives--;
|
|
m_origin.set(reset_ball_x, reset_ball_y);
|
|
}
|
|
if(m_origin.x <= 0 || m_origin.x > COL - 1) {
|
|
vel.x *= -1;
|
|
}
|
|
m_origin.x += vel.x;
|
|
m_origin.y += vel.y;
|
|
}
|
|
|
|
Block::Block(vec2 pos, char symbol) {
|
|
for(int i = 0; i < m_nodes.size(); i++) {
|
|
m_nodes[i].set(pos.x + i, pos.y);
|
|
m_nodes[i].m_symbol = symbol;
|
|
}
|
|
}
|
|
|
|
bool Block::check_collision(Ball &ball) {
|
|
for(int i = 0; i < m_nodes.size(); i++) {
|
|
if(m_nodes[i] != ball.m_origin) continue;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|