Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #if defined(linux) || defined(__linux) || defined(UNIX)
- #include <ncurses.h>
- #endif
- #if defined(_WIN32) || defined (_WIN64)
- #include <curses.h>
- #endif
- #include <clocale>
- #include <fstream>
- #include <cstdlib>
- #include <ctime>
- #include "gameinterface.h"
- using std::ifstream;
- GameInterface::GameInterface(const Hero& hero, const Boss& boss, const char path[])
- {
- if (readMap(path, n, m)) {
- setlocale(LC_ALL, "");
- initscr();
- refresh();
- clear();
- attron(A_NORMAL);
- if (has_colors())
- initColors();
- drawMap(n, m);
- drawInfoAboutHero(hero, m);
- drawInOutBuf(hero, boss, n, m);
- endwin();
- }
- else error(1);
- }
- inline void GameInterface::initColors() const
- {
- start_color();
- init_pair(1, COLOR_RED, COLOR_BLACK);
- init_pair(2, COLOR_GREEN, COLOR_BLACK);
- init_pair(3, COLOR_BLUE, COLOR_BLACK);
- init_pair(4, COLOR_YELLOW, COLOR_BLACK);
- init_pair(5, COLOR_MAGENTA, COLOR_BLACK);
- init_pair(6, COLOR_CYAN, COLOR_BLACK);
- }
- bool GameInterface::readMap(const char path[], const int n, const int m)
- {
- ifstream file(path);
- if (file.is_open()) {
- file >> _x1 >> _y1;
- file >> _x2 >> _y2;
- for (int i = 0; i < n; i++)
- for (int j = 0; j < m; j++)
- file >> map[i][j];
- file.close();
- }
- else return false;
- return true;
- }
- void GameInterface::drawMap(const int n, const int m) const
- {
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++)
- printw("%c", map[i][j]);
- printw("\n");
- }
- mvaddch(_y1, _x1, 'H' | COLOR_PAIR(2) | A_BOLD);
- mvaddch(_y2, _x2, 'B' | COLOR_PAIR(1) | A_BOLD);
- }
- void GameInterface::drawInfoAboutHero(const Hero& obj, const int m) const
- {
- char hp[10];
- char mp[10];
- char pp[10];
- char sp[10];
- char lvl[10];
- to_string(obj.hp, hp);
- to_string(obj.mp, mp);
- to_string(obj.pp, pp);
- to_string(obj.sp, sp);
- mvaddstr(0, m + 2, "HP: ");
- addstr(hp);
- mvaddstr(1, m + 2, "MP: ");
- addstr(mp);
- mvaddstr(2, m + 2, "PP: ");
- addstr(pp);
- mvaddstr(3, m + 2, "SP: ");
- addstr(sp);
- mvaddstr(4, m + 2, "LVL: ");
- addstr(lvl);
- }
- void to_string(const int num, char* buf)
- {
- int tmp = num, n = 0;
- while (tmp > 0)
- {
- buf[n] = tmp % 10 + '0';
- tmp /= 10;
- n++;
- }
- for (int i = 0, j = n - 1; i < j; i++, j--)
- {
- tmp = buf[i];
- buf[i] = buf[j];
- buf[j] = tmp;
- }
- buf[n++] = '\0';
- }
- void GameInterface::drawInOutBuf(const Hero& hero, const Boss& boss, const int n, const int m)
- {
- while (true) {
- raw();
- move(n + 1, 0);
- clrtoeol();
- printw ("> ");
- char str[10];
- getstr(str);
- if (str[0] == '/')
- checkCommand(str + 1);
- else
- moveHero(str, n, m);
- }
- }
- void GameInterface::checkCommand(const string str) const
- {
- if (str == "help" || str == "h")
- help();
- else if (str == "skills" || str == "s")
- skills();
- else if (str == "about")
- about();
- else if (str == "exit")
- {
- // придумать адекватный выход из игры
- }
- }
- void GameInterface::moveHero(const string direction, const int n, const int m)
- {
- bool moved = false;
- if (direction == "left" || direction == "l")
- if ((_x1 > 0) && map[_y1][_x1-1] == '.'){
- mvaddch(_y1, _x1, '.');
- _x1--;
- mvaddch(_y1, _x1, 'H' | COLOR_PAIR(2) | A_BOLD);
- moved = true;
- }
- if (direction == "right" || direction == "r")
- if ((_x1 < m - 1) && map[_y1][_x1+1] == '.'){
- mvaddch(_y1, _x1, '.');
- _x1++;
- mvaddch(_y1, _x1, 'H' | COLOR_PAIR(2) | A_BOLD);
- moved = true;
- }
- if (direction == "down" || direction == "d")
- if ((_y1 < n - 1) && map[_y1+1][_x1] == '.') {
- mvaddch(_y1, _x1, '.');
- _y1++;
- mvaddch(_y1, _x1, 'H' | COLOR_PAIR(2) | A_BOLD);
- moved = true;
- }
- if (direction == "up" || direction == "u")
- if ((_y1 > 0) && map[_y1-1][_x1] == '.') {
- mvaddch(_y1, _x1, '.');
- _y1--;
- mvaddch(_y1, _x1, 'H' | COLOR_PAIR(2) | A_BOLD);
- moved = true;
- }
- if (moved)
- {
- if (_x1 == _x2 && _y1 == _y2)
- fightWithBoss();
- else if (hasEnemy())
- {
- // тут ещё надо проверить врага на враждебность
- }
- }
- }
- bool GameInterface::hasEnemy() const {}
- void GameInterface::fightWithBoss() const {}
- void GameInterface::help() const{}
- void GameInterface::about() const
- {
- printw("Cat's Comedy\n");
- printw("Разработчики: Демченко Антон, Киселёв Дмитрий, Маркова Юлия, Пудашкин Роман\n");
- printw("Все права защищены. 2016 год.\n");
- getch();
- move(n + 1, 0);
- clrtobot(); // реализовать "умную" очистку экрана
- }
- void GameInterface::skills() const{}
- void GameInterface::error(const int err) const
- {
- switch(err)
- {
- case 1:
- {
- printf("Отсутствует или некорректнен файл с картой.\n");
- exit(0);
- }
- }
- }
- GameInterface::~GameInterface(){}
Advertisement
Add Comment
Please, Sign In to add comment