feat: points and pointcollection
This commit is contained in:
@@ -7,6 +7,7 @@ include_directories(include)
|
||||
set(SOURCES
|
||||
src/main.cpp
|
||||
src/input.cpp
|
||||
src/elements.cpp
|
||||
src/window.cpp
|
||||
)
|
||||
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
#ifndef ELEMENTS_HPP
|
||||
#define ELEMENTS_HPP
|
||||
|
||||
#include <vector>
|
||||
#include "globals.hpp"
|
||||
|
||||
struct Point {
|
||||
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') {};
|
||||
char m_direction;
|
||||
int m_row;
|
||||
int m_col;
|
||||
int m_symbol;
|
||||
void assign(int row, int col);
|
||||
};
|
||||
|
||||
struct PointCollection {
|
||||
std::vector<Point> m_point_list;
|
||||
void move();
|
||||
void set_direction(char direction);
|
||||
};
|
||||
|
||||
#endif
|
||||
+3
-1
@@ -1,9 +1,11 @@
|
||||
#ifndef INPUT_HPP
|
||||
#define INPUT_HPP
|
||||
|
||||
#include "elements.hpp"
|
||||
|
||||
struct Input {
|
||||
int _getch;
|
||||
void get_input(bool &running);
|
||||
void get_input(bool &running, PointCollection &collection);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
+2
-1
@@ -2,11 +2,12 @@
|
||||
#define WINDOW_HPP
|
||||
|
||||
#include "globals.hpp"
|
||||
#include "elements.hpp"
|
||||
#include <array>
|
||||
|
||||
struct Window {
|
||||
std::array<std::array<int, COL>, ROW> m_board = {};
|
||||
void draw_window();
|
||||
void draw_window(PointCollection &collection);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
#include "elements.hpp"
|
||||
|
||||
void Point::assign(int row, int col) {
|
||||
m_row = row;
|
||||
m_col = col;
|
||||
}
|
||||
|
||||
void PointCollection::set_direction(char direction) {
|
||||
for(int i = m_point_list.size() - 1; i >= 0; i--) {
|
||||
m_point_list[i].m_direction = direction;
|
||||
}
|
||||
}
|
||||
|
||||
void PointCollection::move() {
|
||||
for(int i = m_point_list.size() - 1; i >= 0; i--) {
|
||||
if(m_point_list[i].m_direction == 'u') {
|
||||
if(m_point_list[i].m_row <= 0) continue;
|
||||
m_point_list[i].assign(m_point_list[i].m_row - 1, m_point_list[i].m_col);
|
||||
} else if(m_point_list[i].m_direction == 'd') {
|
||||
if(m_point_list[i].m_row >= ROW - 1) continue;
|
||||
m_point_list[i].assign(m_point_list[i].m_row + 1, m_point_list[i].m_col);
|
||||
} else if(m_point_list[i].m_direction == 'l') {
|
||||
if(m_point_list[i].m_col <= 0) continue;
|
||||
m_point_list[i].assign(m_point_list[i].m_row, m_point_list[i].m_col - 1);
|
||||
} else if(m_point_list[i].m_direction == 'r') {
|
||||
if(m_point_list[i].m_col >= COL - 1) continue;
|
||||
m_point_list[i].assign(m_point_list[i].m_row, m_point_list[i].m_col + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
+18
-2
@@ -1,7 +1,7 @@
|
||||
#include "input.hpp"
|
||||
#include <ncurses.h>
|
||||
|
||||
void Input::get_input(bool &running) {
|
||||
void Input::get_input(bool &running, PointCollection &collection) {
|
||||
while (running) {
|
||||
initscr();
|
||||
cbreak();
|
||||
@@ -10,6 +10,22 @@ void Input::get_input(bool &running) {
|
||||
_getch = getch();
|
||||
endwin();
|
||||
|
||||
if(_getch == 113) running = false;
|
||||
if(_getch == 65) {
|
||||
_getch = 0;
|
||||
collection.set_direction('u');
|
||||
}
|
||||
else if(_getch == 66) {
|
||||
_getch = 0;
|
||||
collection.set_direction('d');
|
||||
}
|
||||
else if(_getch == 67) {
|
||||
_getch = 0;
|
||||
collection.set_direction('r');
|
||||
}
|
||||
else if(_getch == 68) {
|
||||
_getch = 0;
|
||||
collection.set_direction('l');
|
||||
}
|
||||
else if(_getch == 113) running = false;
|
||||
}
|
||||
}
|
||||
|
||||
+6
-3
@@ -2,17 +2,20 @@
|
||||
#include <thread>
|
||||
#include "input.hpp"
|
||||
#include "window.hpp"
|
||||
#include "elements.hpp"
|
||||
|
||||
int main() {
|
||||
Input input;
|
||||
Window window;
|
||||
PointCollection collection;
|
||||
bool running = true;
|
||||
std::thread input_thread(&Input::get_input, &input, std::ref(running));
|
||||
std::thread input_thread(&Input::get_input, &input, std::ref(running), std::ref(collection));
|
||||
|
||||
collection.m_point_list.push_back(Point());
|
||||
|
||||
window.m_board[0][0] = 4;
|
||||
while (running) {
|
||||
window.draw_window();
|
||||
collection.move();
|
||||
window.draw_window(collection);
|
||||
}
|
||||
|
||||
input_thread.join();
|
||||
|
||||
+7
-3
@@ -1,10 +1,14 @@
|
||||
#include "window.hpp"
|
||||
#include <array>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
void Window::draw_window() {
|
||||
// for(std::array<int, COL> &row : m_board) { row.fill(0); }
|
||||
void Window::draw_window(PointCollection &collection) {
|
||||
for(std::array<int, COL> &row : m_board) { row.fill(0); }
|
||||
for(int i = collection.m_point_list.size() - 1; i >= 0; i--) {
|
||||
m_board[collection.m_point_list[i].m_row][collection.m_point_list[i].m_col] = collection.m_point_list[i].m_symbol;
|
||||
}
|
||||
std::cout << "\033[H" << std::flush;
|
||||
for(int i = 0; i < ROW; i++) {
|
||||
for(int j = 0; j < COL; j++) {
|
||||
@@ -13,5 +17,5 @@ void Window::draw_window() {
|
||||
}
|
||||
std::cout << "\r\n";
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(33));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user