From d987781b24c6f505a61a01794b6bc76b07c9b25d Mon Sep 17 00:00:00 2001 From: Joursoir Date: Mon, 8 Mar 2021 23:28:31 +0000 Subject: add infinity vehicle ride --- Vehicle.cpp | 5 +++++ Vehicle.hpp | 10 ++++++++++ main.cpp | 48 ++++++++++++++++++++++++++++++++---------------- 3 files changed, 47 insertions(+), 16 deletions(-) diff --git a/Vehicle.cpp b/Vehicle.cpp index 3e71db4..9a5153c 100644 --- a/Vehicle.cpp +++ b/Vehicle.cpp @@ -23,3 +23,8 @@ bool Vehicle::Draw(int bound_x) return ret; } + +int Vehicle::GetLength() +{ + return veh_info[type].length; +} \ No newline at end of file diff --git a/Vehicle.hpp b/Vehicle.hpp index 418a532..4e5e854 100644 --- a/Vehicle.hpp +++ b/Vehicle.hpp @@ -9,8 +9,18 @@ public: : type(a_type), pos_y(a_y), pos_x(a_x) { } ~Vehicle() { } + int GetLength(); + void MoveRight() { pos_x++; } bool Draw(int bound_x); /* return false if not draw anything */ }; +struct ll_vehicle { + Vehicle *data; + struct ll_vehicle *next; + + ll_vehicle(Vehicle *a_data, struct ll_vehicle *a_ptr) + : data(a_data), next(a_ptr) { } +}; + #endif /* ASCIIROAD_VEHICLE_H */ diff --git a/main.cpp b/main.cpp index ca406f4..e5a781e 100644 --- a/main.cpp +++ b/main.cpp @@ -39,15 +39,6 @@ void draw_background() mvprintw(total_rows-3, 40, " | |"); mvprintw(total_rows-2, 40, " | |"); mvprintw(total_rows-1, 40, "______// \\\\"); - - refresh(); -} - -Vehicle *get_random_vehicle(int y, int x) -{ - Vehicle *car; - car = new Vehicle(rand() % veh_types_max, y, x); - return car; } int main(int argc, char *argv[]) @@ -63,24 +54,49 @@ int main(int argc, char *argv[]) getmaxyx(stdscr, total_rows, total_cols); Vehicle *car = 0; + struct ll_vehicle *ptr_first = new struct ll_vehicle(0, 0); + struct ll_vehicle *ptr; + int wait_move = 0; for(;;) { clear(); draw_background(); - if(!car) - car = get_random_vehicle(total_rows, -1); + ptr = ptr_first; + while(ptr) + { + car = ptr->data; + if(!car) { + if(wait_move > 0) + break; - car->MoveRight(); - if(!(car->Draw(total_cols))) { - delete car; - car = 0; + ptr->data = new Vehicle( + rand() % veh_types_max, total_rows, -1); + car = ptr->data; + wait_move = car->GetLength() + 3 + rand() % 10; + + ptr->next = new struct ll_vehicle(0, 0); + } + + car->MoveRight(); + if(!car->Draw(total_cols)) { + ptr = ptr->next; + delete car; + delete ptr_first; + ptr_first = ptr; + continue; + } + + ptr = ptr->next; } + if(wait_move > 0) + wait_move--; + refresh(); nanosleep(&my_timer, NULL); } - + endwin(); return 0; } -- cgit v1.2.3-18-g5258