starting refactoring
This commit is contained in:
+11
-11
@@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "globals.hpp"
|
#include "globals.hpp"
|
||||||
|
#include <array>
|
||||||
|
|
||||||
struct vec2;
|
struct vec2;
|
||||||
struct Paddle;
|
struct Paddle;
|
||||||
@@ -17,19 +18,19 @@ bool is_block_buffer_empty(Block** block_buffer, int block_buffer_size);
|
|||||||
enum Direction { LEFT, RIGHT, NONE };
|
enum Direction { LEFT, RIGHT, NONE };
|
||||||
|
|
||||||
struct vec2 {
|
struct vec2 {
|
||||||
vec2() : y(0), x(0) {};
|
vec2() : x(0), y(0) {};
|
||||||
vec2(int row, int col, char symbol) : y(row), x(col), m_symbol(symbol) {};
|
vec2(int x, int y) : x(x), y(y) {};
|
||||||
|
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 assign(int row, int col);
|
void set(int x, int y);
|
||||||
|
auto operator <=> (const vec2&) const = default;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Ball {
|
struct Ball {
|
||||||
Ball(int default_vx, int default_vy) : vx(default_vx), vy(default_vy) {};
|
Ball();
|
||||||
~Ball();
|
vec2 m_origin { COL / 2, ROW - 5, '*' };
|
||||||
vec2* m_origin;
|
vec2 vel { 1, -1 };
|
||||||
int vx;
|
|
||||||
int vy;
|
|
||||||
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);
|
||||||
void change_velocity(char d);
|
void change_velocity(char d);
|
||||||
};
|
};
|
||||||
@@ -47,10 +48,9 @@ struct Paddle {
|
|||||||
|
|
||||||
struct Block {
|
struct Block {
|
||||||
Block();
|
Block();
|
||||||
Block(int size, int row, int col, char symbol);
|
Block(vec2 pos, char symbol);
|
||||||
~Block();
|
~Block();
|
||||||
int _size;
|
std::array<vec2, 3> m_nodes = {};
|
||||||
vec2** m_nodes;
|
|
||||||
bool check_collision(Ball &ball);
|
bool check_collision(Ball &ball);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+9
-24
@@ -12,13 +12,6 @@ Paddle create_new_paddle(vec2** buffer, int starting_array_index, int size, int
|
|||||||
return paddle;
|
return paddle;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ball create_new_ball(vec2** buffer, int buffer_index, int default_vx, int default_vy, int row, int col, char symbol) {
|
|
||||||
Ball ball { default_vx, default_vy };
|
|
||||||
ball.m_origin = new vec2 { row, col, symbol };
|
|
||||||
buffer[buffer_index] = ball.m_origin;
|
|
||||||
return ball;
|
|
||||||
}
|
|
||||||
|
|
||||||
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];
|
||||||
int row, int_symbol, col;
|
int row, int_symbol, col;
|
||||||
@@ -115,18 +108,11 @@ void Paddle::check_collision(Ball &ball) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ball::~Ball() {
|
|
||||||
delete m_origin;
|
|
||||||
m_origin = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Ball::change_velocity(char d) {
|
void Ball::change_velocity(char d) {
|
||||||
if(d == 'x') {
|
if(d == 'x') {
|
||||||
if(vx == -1) vx = 1;
|
vel.x *= -1;
|
||||||
else if (vx == 1) vx = -1;
|
|
||||||
} else if(d == 'y') {
|
} else if(d == 'y') {
|
||||||
if(vy == -1) vy = 1;
|
vel.y *= -1;
|
||||||
else if (vy == 1) vy = -1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -159,18 +145,17 @@ Block::~Block() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Block::Block(int size, int row, int col, char symbol) {
|
Block::Block(vec2 pos, char symbol) {
|
||||||
_size = size;
|
for(int i = 0; i < m_nodes.size(); i++) {
|
||||||
m_nodes = new vec2*[size];
|
m_nodes[i].set(pos.x + i, pos.y);
|
||||||
for(int i = 0; i < size; i++) {
|
m_nodes[i].m_symbol = symbol;
|
||||||
m_nodes[i] = new vec2 { row, col + i, symbol };
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Block::check_collision(Ball &ball) {
|
bool Block::check_collision(Ball &ball) {
|
||||||
for(int i = 0; i < _size; i++) {
|
for(int i = 0; i < m_nodes.size(); i++) {
|
||||||
if(m_nodes[i]->x != ball.m_origin->x) continue;
|
if(m_nodes[i] != ball.m_origin) continue;
|
||||||
if(m_nodes[i]->y != ball.m_origin->y) continue;
|
if(m_nodes[i] != ball.m_origin) continue;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
Reference in New Issue
Block a user