feat: the start of a strong collision system

This commit is contained in:
DarthMooMancer
2025-09-18 12:09:43 -04:00
parent d6a769900f
commit b446c4c158
4 changed files with 62 additions and 36 deletions
+3 -5
View File
@@ -6,13 +6,12 @@
#include "globals.hpp" #include "globals.hpp"
struct Point { struct Point {
Point() : m_row(0), m_col(0), m_symbol(2), m_direction('n'), m_collide(false) {}; Point() : m_row(0), m_col(0), m_symbol(2), m_direction('n') {};
Point(int row, int col) : m_row(row), m_col(col), m_symbol(2), m_direction('n'), m_collide(false) {}; Point(int row, int col) : m_row(row), m_col(col), m_symbol(2), m_direction('n') {};
char m_direction; char m_direction;
int m_row; int m_row;
int m_col; int m_col;
int m_symbol; int m_symbol;
bool m_collide;
void assign(); void assign();
void assign(int row, int col); void assign(int row, int col);
std::string to_string(); std::string to_string();
@@ -20,10 +19,9 @@ struct Point {
struct PointCollection { struct PointCollection {
std::vector<Point> m_point_list; std::vector<Point> m_point_list;
void move();
void move(Point* m_board[ROW][COL]); void move(Point* m_board[ROW][COL]);
void collide_with_adjacent(Point* board[ROW][COL]);
void set_direction(char direction); void set_direction(char direction);
void check_collision();
}; };
#endif #endif
+46 -1
View File
@@ -3,7 +3,6 @@
#include <ctime> #include <ctime>
void Point::assign() { void Point::assign() {
std::srand(std::time(0));
m_row = (rand() % (ROW)); m_row = (rand() % (ROW));
m_col = (rand() % (ROW)); m_col = (rand() % (ROW));
} }
@@ -52,3 +51,49 @@ void PointCollection::move(Point* board[ROW][COL]) {
} }
} }
} }
void PointCollection::collide_with_adjacent(Point* board[ROW][COL]) {
for(int r = 0; r < ROW; r++) {
for(int c = 0; c < COL; c++) {
Point* p = board[r][c];
Point* npu = board[r - 1][c];
Point* npd = board[r + 1][c];
Point* npl = board[r][c - 1];
Point* npr = board[r][c + 1];
if(p != nullptr) {
if(p->m_direction == 'u' && npu != nullptr && npu->m_row >= 0) {
if(p->m_symbol != npu->m_symbol) continue;
npu->m_symbol *= 2;
board[r][c] = nullptr;
// Board is not cleared as the point still exists in m_points_list so it needs to be removed
// I recommend adding an id to the points to track which to remove and make a function in
// the PointCollection to remove the point
}
// else if(p->m_direction == 'd') {
// np = board[p->m_row + 1][p->m_col];
// if(np->m_row >= ROW - 1) continue;
// if(np == nullptr) continue;
// if(p->m_symbol != np->m_symbol) continue;
// board[r][c] = nullptr;
// np->m_symbol *= 2;
// }
// else if(p->m_direction == 'l') {
// np = board[p->m_row][p->m_col - 1];
// if(np->m_col <= 0) continue;
// if(np == nullptr) continue;
// if(p->m_symbol != np->m_symbol) continue;
// board[r][c] = nullptr;
// np->m_symbol *= 2;
// }
// else if(p->m_direction == 'r') {
// np = board[p->m_row][p->m_col + 1];
// if(np->m_col >= COL - 1) continue;
// if(np == nullptr) continue;
// if(p->m_symbol != np->m_symbol) continue;
// board[r][c] = nullptr;
// np->m_symbol *= 2;
// }
}
}
}
}
+8 -28
View File
@@ -12,47 +12,27 @@ void Input::get_input(bool &running, PointCollection &collection, Point* board[R
if(_getch == 65) { if(_getch == 65) {
_getch = 0; _getch = 0;
for(int i = 0; i < collection.m_point_list.size() - 1; i++) {
if(!collection.m_point_list[i].m_collide) {
collection.m_point_list[i].m_collide = true;
}
}
collection.set_direction('u'); collection.set_direction('u');
collection.m_point_list.push_back(Point()); // collection.m_point_list.push_back(Point());
collection.m_point_list.back().assign(); // collection.m_point_list.back().assign();
} }
else if(_getch == 66) { else if(_getch == 66) {
_getch = 0; _getch = 0;
for(int i = 0; i < collection.m_point_list.size() - 1; i++) {
if(!collection.m_point_list[i].m_collide) {
collection.m_point_list[i].m_collide = true;
}
}
collection.set_direction('d'); collection.set_direction('d');
collection.m_point_list.push_back(Point()); // collection.m_point_list.push_back(Point());
collection.m_point_list.back().assign(); // collection.m_point_list.back().assign();
} }
else if(_getch == 67) { else if(_getch == 67) {
_getch = 0; _getch = 0;
for(int i = 0; i < collection.m_point_list.size() - 1; i++) {
if(!collection.m_point_list[i].m_collide) {
collection.m_point_list[i].m_collide = true;
}
}
collection.set_direction('r'); collection.set_direction('r');
collection.m_point_list.push_back(Point()); // collection.m_point_list.push_back(Point());
collection.m_point_list.back().assign(); // collection.m_point_list.back().assign();
} }
else if(_getch == 68) { else if(_getch == 68) {
_getch = 0; _getch = 0;
for(int i = 0; i < collection.m_point_list.size() - 1; i++) {
if(!collection.m_point_list[i].m_collide) {
collection.m_point_list[i].m_collide = true;
}
}
collection.set_direction('l'); collection.set_direction('l');
collection.m_point_list.push_back(Point()); // collection.m_point_list.push_back(Point());
collection.m_point_list.back().assign(); // collection.m_point_list.back().assign();
} }
else if(_getch == 113) running = false; else if(_getch == 113) running = false;
} }
+5 -2
View File
@@ -1,10 +1,10 @@
#include <iostream>
#include <thread> #include <thread>
#include "input.hpp" #include "input.hpp"
#include "window.hpp" #include "window.hpp"
#include "elements.hpp" #include "elements.hpp"
int main() { int main() {
std::srand(std::time(0));
Input input; Input input;
Window window; Window window;
PointCollection collection; PointCollection collection;
@@ -12,10 +12,13 @@ int main() {
std::thread input_thread(&Input::get_input, &input, std::ref(running), std::ref(collection), std::ref(window.m_board)); std::thread input_thread(&Input::get_input, &input, std::ref(running), std::ref(collection), std::ref(window.m_board));
collection.m_point_list.push_back(Point()); collection.m_point_list.push_back(Point());
collection.m_point_list.back().assign(); collection.m_point_list[0].assign();
collection.m_point_list.push_back(Point());
collection.m_point_list[1].assign();
while (running) { while (running) {
collection.move(window.m_board); collection.move(window.m_board);
collection.collide_with_adjacent(window.m_board);
window.draw_window(collection); window.draw_window(collection);
} }