Guest User

Untitled

a guest
Mar 28th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.88 KB | None | 0 0
  1. #if defined(linux) || defined(__linux) || defined(UNIX)
  2. #include <ncurses.h>
  3. #endif
  4.  
  5. #if defined(_WIN32) || defined (_WIN64)
  6. #include <curses.h>
  7. #endif
  8.  
  9. #include <clocale>
  10. #include <fstream>
  11. #include <cstdlib>
  12. #include <ctime>
  13.  
  14. #include "gameinterface.h"
  15. using std::ifstream;
  16.  
  17. GameInterface::GameInterface(const Hero& hero, const Boss& boss, const char path[])
  18. {
  19.     if (readMap(path, n, m)) {
  20.         setlocale(LC_ALL, "");
  21.         initscr();
  22.         refresh();
  23.         clear();
  24.         attron(A_NORMAL);
  25.  
  26.         if (has_colors())
  27.             initColors();
  28.  
  29.         drawMap(n, m);
  30.         drawInfoAboutHero(hero, m);
  31.         drawInOutBuf(hero, boss, n, m);
  32.         endwin();
  33.     }
  34.     else error(1);
  35. }
  36.  
  37. inline void GameInterface::initColors() const
  38. {
  39.     start_color();
  40.     init_pair(1, COLOR_RED,     COLOR_BLACK);
  41.     init_pair(2, COLOR_GREEN,   COLOR_BLACK);
  42.     init_pair(3, COLOR_BLUE,    COLOR_BLACK);
  43.     init_pair(4, COLOR_YELLOW,  COLOR_BLACK);
  44.     init_pair(5, COLOR_MAGENTA, COLOR_BLACK);
  45.     init_pair(6, COLOR_CYAN,    COLOR_BLACK);
  46. }
  47.  
  48. bool GameInterface::readMap(const char path[], const int n, const int m)
  49. {
  50.     ifstream file(path);
  51.     if (file.is_open()) {
  52.         file >> _x1 >> _y1;
  53.         file >> _x2 >> _y2;
  54.  
  55.         for (int i = 0; i < n; i++)
  56.             for (int j = 0; j < m; j++)
  57.                 file >> map[i][j];
  58.         file.close();
  59.     }
  60.     else return false;
  61.  
  62.     return true;
  63. }
  64.  
  65. void GameInterface::drawMap(const int n, const int m) const
  66. {
  67.     for (int i = 0; i < n; i++) {
  68.         for (int j = 0; j < m; j++)
  69.             printw("%c", map[i][j]);
  70.         printw("\n");
  71.     }
  72.     mvaddch(_y1, _x1, 'H' | COLOR_PAIR(2) | A_BOLD);
  73.     mvaddch(_y2, _x2, 'B' | COLOR_PAIR(1) | A_BOLD);
  74. }
  75.  
  76. void GameInterface::drawInfoAboutHero(const Hero& obj, const int m) const
  77. {
  78.     char hp[10];
  79.     char mp[10];
  80.     char pp[10];
  81.     char sp[10];
  82.     char lvl[10];
  83.     to_string(obj.hp, hp);
  84.     to_string(obj.mp, mp);
  85.     to_string(obj.pp, pp);
  86.     to_string(obj.sp, sp);
  87.  
  88.     mvaddstr(0, m + 2, "HP: ");
  89.     addstr(hp);
  90.  
  91.     mvaddstr(1, m + 2, "MP: ");
  92.     addstr(mp);
  93.  
  94.     mvaddstr(2, m + 2, "PP: ");
  95.     addstr(pp);
  96.  
  97.     mvaddstr(3, m + 2, "SP: ");
  98.     addstr(sp);
  99.  
  100.     mvaddstr(4, m + 2, "LVL: ");
  101.     addstr(lvl);
  102. }
  103.  
  104. void to_string(const int num, char* buf)
  105. {
  106.     int tmp = num, n = 0;
  107.     while (tmp > 0)
  108.     {
  109.         buf[n] = tmp % 10 + '0';
  110.         tmp /= 10;
  111.         n++;
  112.     }
  113.  
  114.     for (int i = 0, j = n - 1; i < j; i++, j--)
  115.     {
  116.         tmp = buf[i];
  117.         buf[i] = buf[j];
  118.         buf[j] = tmp;
  119.     }
  120.     buf[n++] = '\0';
  121. }
  122.  
  123. void GameInterface::drawInOutBuf(const Hero& hero, const Boss& boss, const int n, const int m)
  124. {
  125.     while (true) {
  126.         raw();
  127.         move(n + 1, 0);
  128.         clrtoeol();
  129.         printw ("> ");
  130.  
  131.         char str[10];
  132.         getstr(str);
  133.        
  134.         if (str[0] == '/')
  135.             checkCommand(str + 1);
  136.         else
  137.             moveHero(str, n, m);
  138.     }
  139. }
  140.  
  141. void GameInterface::checkCommand(const string str) const
  142. {
  143.     if (str == "help" || str == "h")
  144.         help();
  145.     else if (str == "skills" || str == "s")
  146.         skills();
  147.     else if (str == "about")
  148.         about();
  149.     else if (str == "exit")
  150.     {
  151.         // придумать адекватный выход из игры
  152.     }
  153. }
  154.  
  155. void GameInterface::moveHero(const string direction, const int n, const int m)
  156. {
  157.     bool moved = false;
  158.  
  159.     if (direction == "left" || direction == "l")
  160.         if ((_x1 > 0) && map[_y1][_x1-1] == '.'){
  161.             mvaddch(_y1, _x1, '.');
  162.             _x1--;
  163.             mvaddch(_y1, _x1, 'H' | COLOR_PAIR(2) | A_BOLD);
  164.             moved = true;
  165.         }
  166.  
  167.     if (direction == "right" || direction == "r")
  168.  
  169.         if ((_x1 < m - 1) && map[_y1][_x1+1] == '.'){
  170.             mvaddch(_y1, _x1, '.');
  171.             _x1++;
  172.             mvaddch(_y1, _x1, 'H' | COLOR_PAIR(2) | A_BOLD);
  173.             moved = true;
  174.         }
  175.  
  176.     if (direction == "down" || direction == "d")
  177.         if ((_y1 < n - 1) && map[_y1+1][_x1] == '.') {
  178.             mvaddch(_y1, _x1, '.');
  179.             _y1++;
  180.             mvaddch(_y1, _x1, 'H' | COLOR_PAIR(2) | A_BOLD);
  181.             moved = true;
  182.         }
  183.  
  184.     if (direction == "up" || direction == "u")
  185.         if ((_y1 > 0) && map[_y1-1][_x1] == '.') {
  186.             mvaddch(_y1, _x1, '.');
  187.             _y1--;
  188.             mvaddch(_y1, _x1, 'H' | COLOR_PAIR(2) | A_BOLD);
  189.             moved = true;
  190.         }
  191.  
  192.     if (moved)
  193.     {
  194.         if (_x1 == _x2 && _y1 == _y2)
  195.             fightWithBoss();
  196.         else if (hasEnemy())
  197.         {
  198.             // тут ещё надо проверить врага на враждебность  
  199.         }
  200.     }
  201. }
  202.  
  203. bool GameInterface::hasEnemy() const {}
  204. void GameInterface::fightWithBoss() const {}
  205. void GameInterface::help() const{}
  206.  
  207. void GameInterface::about() const
  208. {
  209.     printw("Cat's Comedy\n");
  210.     printw("Разработчики: Демченко Антон, Киселёв Дмитрий, Маркова Юлия, Пудашкин Роман\n");
  211.     printw("Все права защищены. 2016 год.\n");
  212.     getch();
  213.     move(n + 1, 0);
  214.     clrtobot(); // реализовать "умную" очистку экрана
  215. }
  216. void GameInterface::skills() const{}
  217.  
  218. void GameInterface::error(const int err) const
  219. {
  220.     switch(err)
  221.     {
  222.         case 1:
  223.         {
  224.             printf("Отсутствует или некорректнен файл с картой.\n");
  225.             exit(0);
  226.         }
  227.     }
  228. }
  229.  
  230. GameInterface::~GameInterface(){}
Advertisement
Add Comment
Please, Sign In to add comment