feat: _check_collision for stage one of checks to collide

This commit is contained in:
DarthMooMancer
2025-09-16 09:10:41 -04:00
parent 3d184ee7a7
commit c25beb21fb
4 changed files with 74 additions and 20 deletions
+4 -3
View File
@@ -2,17 +2,18 @@
#define ELEMENTS_HPP #define ELEMENTS_HPP
#include <vector> #include <vector>
#include "globals.hpp"
struct Point { struct Point {
Point() : m_row(0), m_col(0), m_symbol(2), m_direction('n') {}; Point() : m_row(0), m_col(0), 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') {}; Point(int row, int col) : m_row(row), m_col(col), m_symbol(2), m_direction('n'), m_collide(false) {};
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();
}; };
struct PointCollection { struct PointCollection {
+41 -16
View File
@@ -1,6 +1,9 @@
#include "elements.hpp" #include "elements.hpp"
#include "globals.hpp"
#include <cstdlib> #include <cstdlib>
#include <ctime> #include <ctime>
#include <iostream>
#include <string>
void Point::assign() { void Point::assign() {
std::srand(std::time(0)); std::srand(std::time(0));
@@ -13,6 +16,10 @@ void Point::assign(int row, int col) {
m_col = col; m_col = col;
} }
std::string Point::to_string() {
return "(" + std::to_string(m_row) + ", " + std::to_string(m_col) + " )\n";
}
void PointCollection::set_direction(char direction) { void PointCollection::set_direction(char direction) {
for(int i = m_point_list.size() - 1; i >= 0; i--) { for(int i = m_point_list.size() - 1; i >= 0; i--) {
m_point_list[i].m_direction = direction; m_point_list[i].m_direction = direction;
@@ -37,22 +44,40 @@ void PointCollection::move() {
} }
} }
void PointCollection::check_collision() { // bool _check_collision(Point &a, Point &b) {
for(int i = m_point_list.size() - 1; i >= 0; i--) { // if(a.m_row == b.m_row && a.m_col == b.m_col && a.m_symbol == b.m_symbol) {
if(m_point_list[i].m_direction == 'u') { // return true;
// if(m_point_list[i + 1].m_row == m_point_list[i].m_row || m_point_list[i - 1].m_row == m_point_list[i].m_row) { // }
// if(m_point_list[i + 1].m_col == m_point_list[i].m_col || m_point_list[i - 1].m_col < m_point_list[i].m_col) { // return false;
// if(m_point_list[i].m_row > m_point_list[i + 1].m_row || m_point_list[i].m_row > m_point_list[i - 1].m_row) { // }
// m_point_list[i].m_row += 1;
// void PointCollection::check_collision() {
// for(int i = m_point_list.size() - 1; i >= 0; i--) {
// for(int a = m_point_list.size() - 1; a >= 0; a--) {
// if (i == a) continue;
// _check_collision(m_point_list[i], m_point_list[a]);
// if(m_point_list[i].m_row == m_point_list[a].m_row) {
// if(m_point_list[i].m_col == m_point_list[a].m_col) {
// if (m_point_list[i].m_symbol == m_point_list[a].m_symbol) {
// if(m_point_list[i].m_direction == 'u') {
// if(!m_point_list[a].m_collide) {
// m_point_list[a].m_collide = true;
// m_point_list[a].m_symbol *= 2;
// m_point_list.erase(m_point_list.begin() + i);
// }
// else {
// m_point_list[i].m_row++;
// }
// }
// if(m_point_list[i].m_direction == 'd') {
// }
// if(m_point_list[i].m_direction == 'l') {
// }
// if(m_point_list[i].m_direction == 'r') {
// }
// } // }
// } // }
// } // }
} // }
if(m_point_list[i].m_direction == 'd') { // }
} // }
if(m_point_list[i].m_direction == 'l') {
}
if(m_point_list[i].m_direction == 'r') {
}
}
}
+12
View File
@@ -15,24 +15,36 @@ void Input::get_input(bool &running, PointCollection &collection) {
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();
for(int i = collection.m_point_list.size() - 1; i >= 0; i--) {
collection.m_point_list[i].m_collide = false;
}
} }
else if(_getch == 66) { else if(_getch == 66) {
_getch = 0; _getch = 0;
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();
for(int i = collection.m_point_list.size() - 1; i >= 0; i--) {
collection.m_point_list[i].m_collide = false;
}
} }
else if(_getch == 67) { else if(_getch == 67) {
_getch = 0; _getch = 0;
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();
for(int i = collection.m_point_list.size() - 1; i >= 0; i--) {
collection.m_point_list[i].m_collide = false;
}
} }
else if(_getch == 68) { else if(_getch == 68) {
_getch = 0; _getch = 0;
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();
for(int i = collection.m_point_list.size() - 1; i >= 0; i--) {
collection.m_point_list[i].m_collide = false;
}
} }
else if(_getch == 113) running = false; else if(_getch == 113) running = false;
} }
+17 -1
View File
@@ -4,6 +4,13 @@
#include "window.hpp" #include "window.hpp"
#include "elements.hpp" #include "elements.hpp"
bool _check_collision(Point &a, Point &b) {
if(a.m_row == b.m_row && a.m_col == b.m_col && a.m_symbol == b.m_symbol) {
return true;
}
return false;
}
int main() { int main() {
Input input; Input input;
Window window; Window window;
@@ -15,9 +22,18 @@ int main() {
collection.m_point_list.back().assign(); collection.m_point_list.back().assign();
while (running) { while (running) {
collection.check_collision(); // collection.check_collision();
collection.move(); collection.move();
window.draw_window(collection); window.draw_window(collection);
for(int i = collection.m_point_list.size() - 1; i >= 0; i--) {
for(int a = collection.m_point_list.size() - 1; a >= 0; a--) {
if (i == a) continue;
if(_check_collision(collection.m_point_list[i], collection.m_point_list[a])) {
std::cout << "Collision with " << collection.m_point_list[i].to_string() << " & " << collection.m_point_list[a].to_string() << "\n";
}
}
}
std::cout << collection.m_point_list.size() << "\n";
} }
input_thread.join(); input_thread.join();