starting refactoring

This commit is contained in:
DarthMooMancer
2025-11-18 23:53:53 -05:00
parent a955f2d067
commit 46a975f90e
2 changed files with 20 additions and 35 deletions
+11 -11
View File
@@ -1,6 +1,7 @@
#pragma once
#include "globals.hpp"
#include <array>
struct vec2;
struct Paddle;
@@ -17,19 +18,19 @@ bool is_block_buffer_empty(Block** block_buffer, int block_buffer_size);
enum Direction { LEFT, RIGHT, NONE };
struct vec2 {
vec2() : y(0), x(0) {};
vec2(int row, int col, char symbol) : y(row), x(col), m_symbol(symbol) {};
vec2() : x(0), y(0) {};
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;
char m_symbol;
void assign(int row, int col);
void set(int x, int y);
auto operator <=> (const vec2&) const = default;
};
struct Ball {
Ball(int default_vx, int default_vy) : vx(default_vx), vy(default_vy) {};
~Ball();
vec2* m_origin;
int vx;
int vy;
Ball();
vec2 m_origin { COL / 2, ROW - 5, '*' };
vec2 vel { 1, -1 };
void determine_new_position(int &paddle_lives, int reset_ball_x, int reset_ball_y);
void change_velocity(char d);
};
@@ -47,10 +48,9 @@ struct Paddle {
struct Block {
Block();
Block(int size, int row, int col, char symbol);
Block(vec2 pos, char symbol);
~Block();
int _size;
vec2** m_nodes;
std::array<vec2, 3> m_nodes = {};
bool check_collision(Ball &ball);
};
+9 -24
View File
@@ -12,13 +12,6 @@ Paddle create_new_paddle(vec2** buffer, int starting_array_index, int size, int
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** block_buffer = new Block*[rows * cols];
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) {
if(d == 'x') {
if(vx == -1) vx = 1;
else if (vx == 1) vx = -1;
vel.x *= -1;
} else if(d == 'y') {
if(vy == -1) vy = 1;
else if (vy == 1) vy = -1;
vel.y *= -1;
}
}
@@ -159,18 +145,17 @@ Block::~Block() {
}
}
Block::Block(int size, int row, int col, char symbol) {
_size = size;
m_nodes = new vec2*[size];
for(int i = 0; i < size; i++) {
m_nodes[i] = new vec2 { row, col + i, symbol };
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 < _size; i++) {
if(m_nodes[i]->x != ball.m_origin->x) continue;
if(m_nodes[i]->y != ball.m_origin->y) continue;
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 false;