diff --git a/CMakeLists.txt b/CMakeLists.txt index 44d18b7..c91e6d9 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,34 +9,34 @@ include_directories(${CURSES_INCLUDE_DIR}) set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin) set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib) -set(GameSource main.cpp) +set(GameSource src/main.cpp) set(OutApiSource - out_api/draw.cpp - out_api/game_draw.cpp - out_api/init.cpp - out_api/input_controller.cpp - out_api/input.cpp - out_api/menu_draw.cpp) + src/out_api/draw.cpp + src/out_api/game_draw.cpp + src/out_api/init.cpp + src/out_api/input_controller.cpp + src/out_api/input.cpp + src/out_api/menu_draw.cpp) set(MenuSource - menu/menu.cpp) + src/menu/menu.cpp) set(GameMainSource - game/game_events.cpp - game/game_map.cpp - game/game_menu.cpp) + src/game/game_events.cpp + src/game/game_map.cpp + src/game/game_menu.cpp) set(GameUnitSource - game/unit/module_builder.cpp - game/unit/unit_builder.cpp - game/unit/unit_module.cpp - game/unit/unit.cpp) + src/game/unit/module_builder.cpp + src/game/unit/unit_builder.cpp + src/game/unit/unit_module.cpp + src/game/unit/unit.cpp) set(GameMapEntitiesSource - game/map_entities/cell.cpp - game/map_entities/unit_obj.cpp - game/map_entities/actions/attack_action.cpp) + src/game/map_entities/cell.cpp + src/game/map_entities/unit_obj.cpp + src/game/map_entities/actions/attack_action.cpp) add_library(OutApi ${OutApiSource}) add_library(Menu ${MenuSource}) diff --git a/bin/Game b/bin/Game new file mode 100755 index 0000000..5052616 Binary files /dev/null and b/bin/Game differ diff --git a/lib/libGameMain.a b/lib/libGameMain.a new file mode 100644 index 0000000..8a17feb Binary files /dev/null and b/lib/libGameMain.a differ diff --git a/lib/libGameMapEntities.a b/lib/libGameMapEntities.a new file mode 100644 index 0000000..9a6bb1c Binary files /dev/null and b/lib/libGameMapEntities.a differ diff --git a/lib/libGameUnit.a b/lib/libGameUnit.a new file mode 100644 index 0000000..8cf3156 Binary files /dev/null and b/lib/libGameUnit.a differ diff --git a/lib/libMenu.a b/lib/libMenu.a new file mode 100644 index 0000000..d7ef929 Binary files /dev/null and b/lib/libMenu.a differ diff --git a/lib/libOutApi.a b/lib/libOutApi.a new file mode 100644 index 0000000..4e687fa Binary files /dev/null and b/lib/libOutApi.a differ diff --git a/src/game/game_events.hpp b/src/game/game_events.hpp index 4cea773..bee5137 100644 --- a/src/game/game_events.hpp +++ b/src/game/game_events.hpp @@ -1,7 +1,7 @@ #include #include #include -#include +#include #pragma once @@ -25,13 +25,30 @@ namespace events { bool operator!=(const EventId& eId) const { return !operator==(eId); } + + bool operator<(const EventId& eId) const { + return nameHash < eId.nameHash || + (nameHash == eId.nameHash && name < eId.name); + } + + bool operator>(const EventId& eId) const { + return eId < *this; + } + + bool operator<=(const EventId& eId) const { + return !operator>(eId); + } + + bool operator>=(const EventId& eId) const { + return !operator<(eId); + } }; class EventData { // may be changed private: using T = int; - std::unordered_map data; + std::map data; public: EventData() {} @@ -99,7 +116,7 @@ namespace events { class EventCenter { private: - std::unordered_map> handlers; + std::map> handlers; public: void addEventHandler(const EventHandler& handler) { handlers[handler.getEventId()].push_back(handler); diff --git a/src/game/unit/unit.hpp b/src/game/unit/unit.hpp index 103979c..6ef2c1c 100644 --- a/src/game/unit/unit.hpp +++ b/src/game/unit/unit.hpp @@ -1,7 +1,6 @@ #include #include "../game_map.hpp" #include "unit_module.hpp" -#include "unit_memory_elem.hpp" #pragma once diff --git a/main.cpp b/src/main.cpp similarity index 63% rename from main.cpp rename to src/main.cpp index e309c7e..22791c9 100755 --- a/main.cpp +++ b/src/main.cpp @@ -1,8 +1,8 @@ #include #include -#include "src/out_api/init.hpp" -#include "src/out_api/menu_draw.hpp" -#include "src/out_api/game_draw.hpp" +#include "out_api/init.hpp" +#include "out_api/menu_draw.hpp" +#include "out_api/game_draw.hpp" int main() { init::begin(); diff --git a/src/out_api/draw.cpp b/src/out_api/draw.cpp index 522d13c..d77496f 100644 --- a/src/out_api/draw.cpp +++ b/src/out_api/draw.cpp @@ -4,18 +4,18 @@ namespace draw { void initColorPairs() { - init_pair(ColorScheme::simple, COLOR_BLACK, COLOR_WHITE); - init_pair(ColorScheme::map_simple, COLOR_BLACK, COLOR_WHITE); - init_pair(ColorScheme::map_active, COLOR_WHITE, COLOR_BLACK); - init_pair(ColorScheme::menu_simple, COLOR_BLACK, COLOR_WHITE); - init_pair(ColorScheme::menu_active, COLOR_WHITE, COLOR_BLACK); - init_pair(ColorScheme::player0, COLOR_BLACK, COLOR_RED); - init_pair(ColorScheme::player1, COLOR_BLACK, COLOR_BLUE); - init_pair(ColorScheme::player2, COLOR_BLACK, COLOR_MAGENTA); - init_pair(ColorScheme::player3, COLOR_BLACK, COLOR_YELLOW); - init_pair(ColorScheme::neutral, COLOR_WHITE, COLOR_GREEN); - init_pair(ColorScheme::enviroment, COLOR_BLACK, COLOR_WHITE); - init_pair(ColorScheme::damaged, COLOR_RED, COLOR_WHITE); + init_pair(static_cast(ColorScheme::simple), COLOR_WHITE, COLOR_BLACK); + init_pair(static_cast(ColorScheme::map_simple), COLOR_WHITE, COLOR_BLACK); + init_pair(static_cast(ColorScheme::map_active), COLOR_BLACK, COLOR_WHITE); + init_pair(static_cast(ColorScheme::menu_simple), COLOR_WHITE, COLOR_BLACK); + init_pair(static_cast(ColorScheme::menu_active), COLOR_BLACK, COLOR_WHITE); + init_pair(static_cast(ColorScheme::player0), COLOR_RED, COLOR_BLACK); + init_pair(static_cast(ColorScheme::player1), COLOR_BLUE, COLOR_BLACK); + init_pair(static_cast(ColorScheme::player2), COLOR_MAGENTA, COLOR_BLACK); + init_pair(static_cast(ColorScheme::player3), COLOR_YELLOW, COLOR_BLACK); + init_pair(static_cast(ColorScheme::neutral), COLOR_GREEN, COLOR_WHITE); + init_pair(static_cast(ColorScheme::enviroment), COLOR_WHITE, COLOR_BLACK); + init_pair(static_cast(ColorScheme::damaged), COLOR_WHITE, COLOR_RED); } void begin() { @@ -36,7 +36,14 @@ namespace draw { void drawCh(size_t x, size_t y, Cell cell, ColorScheme colorScheme) { attron(COLOR_PAIR(colorScheme)); - mvaddch(y, x, cell); + mvaddch(y, x, static_cast(cell)); + attroff(COLOR_PAIR(colorScheme)); + } + + void drawKey(size_t x, size_t y, + char key, ColorScheme colorScheme) { + attron(COLOR_PAIR(colorScheme)); + mvaddch(y, x, key); attroff(COLOR_PAIR(colorScheme)); } diff --git a/src/out_api/draw.hpp b/src/out_api/draw.hpp index 4c6f799..619f7cd 100644 --- a/src/out_api/draw.hpp +++ b/src/out_api/draw.hpp @@ -5,29 +5,32 @@ namespace draw { using std::size_t; - enum ColorScheme { - simple, - map_simple, - map_active, - menu_simple, - menu_active, - player0, - player1, - player2, - player3, - neutral, - damaged, - enviroment, + enum class ColorScheme { + simple = 0, + map_simple = 1, + map_active = 2, + menu_simple = 3, + menu_active = 4, + player0 = 5, + player1 = 6, + player2 = 7, + player3 = 8, + neutral = 9, + damaged = 10, + enviroment = 11, }; - enum Cell { + enum class Cell { blank = ' ', menu_hb = '-', menu_vb = '|', menu_c = ' ', field = '.', mountain = '^', - unit = '#' + unit = '#', + gold = 'G', + iron = 'I', + tech = 'T' }; void begin(); @@ -41,6 +44,9 @@ namespace draw { void drawCh(size_t x, size_t y, Cell cell, ColorScheme colorScheme = ColorScheme::simple); + void drawKey(size_t x, size_t y, + char key, ColorScheme colorScheme = ColorScheme::simple); + // void setxy(uint32_t x, uint32_t y); void getSize(size_t& x, size_t& y); diff --git a/src/out_api/game_draw.cpp b/src/out_api/game_draw.cpp index 97130e3..c6e49fb 100644 --- a/src/out_api/game_draw.cpp +++ b/src/out_api/game_draw.cpp @@ -1,32 +1,168 @@ -#include "draw.hpp" #include "game_draw.hpp" +#include "draw.hpp" namespace game_draw { const size_t MENU_WIDTH = 16; + + const size_t MENU_ENTITY_EXTRA = 4; + + const draw::ColorScheme menuSimpleScheme = + draw::ColorScheme::menu_simple; + + const draw::ColorScheme menuActiveScheme = + draw::ColorScheme::menu_active; + + size_t lastWidth = 0; + + size_t lastHeight = 0; + + void drawMenuEntity(size_t pos, size_t start, size_t end, + char shortkey, draw::Cell type, + const std::string& name, bool isActive) { + draw::ColorScheme colorScheme = + isActive ? menuActiveScheme : menuSimpleScheme; + draw::drawKey(start, pos, shortkey, colorScheme); + draw::drawCh(start + 1, pos, draw::Cell::blank, colorScheme); + for (size_t i = 0; i <= end - start - MENU_ENTITY_EXTRA; ++i) { + if (i < name.size()) { + draw::drawKey(start + 2 + i, pos, + name[i], colorScheme); + } else { + draw::drawCh(start + 2 + i, pos, + draw::Cell::blank, colorScheme); + } + } + // ?? add move of name ?? + draw::drawCh(end - 1, pos, draw::Cell::blank, colorScheme); + draw::drawCh(end, pos, type, colorScheme); + } + void drawMenu(std::pair start, + std::pair end) { + for (size_t i = start.second; i <= end.second; ++i) { + // add real entities + drawMenuEntity(i, start.first, end.first, 'A', + draw::Cell::unit, "Tank", false); + } + } + + void drawMap(std::pair start, + std::pair end) { + + } + + void drawParams(std::pair start, + const std::vector> &res) { + size_t pos = start.first; + for (size_t i = 0; i < res.size(); ++i) { + std::string s = std::to_string(res[i].first); + for (size_t j = 0; j < s.size(); ++j, ++pos) { + draw::drawKey(pos, start.second, + s[j], menuSimpleScheme); + } + draw::drawCh(pos++, start.second, + res[i].second, menuSimpleScheme); + draw::drawCh(pos++, start.second, + draw::Cell::blank, menuSimpleScheme); + } + } + + void drawRequirements(std::pair end, + const std::vector &requirements) { + size_t pos = end.first; + for (size_t i = 0; i < requirements.size(); ++i) { + draw::drawCh(pos--, end.second, + requirements[i], menuSimpleScheme); + draw::drawCh(pos--, end.second, + draw::Cell::blank, menuSimpleScheme); + } + draw::drawCh(pos, end.second, + draw::Cell::menu_vb, menuSimpleScheme); + } + void drawAll() { size_t width; size_t height; draw::getSize(width, height); // check width && height - for(size_t i = 0; i < height; ++i) { - draw::drawCh(i, 0, draw::Cell::menu_hb); - draw::drawCh(i, width - 1, draw::Cell::menu_hb); - draw::drawCh(i, width - MENU_WIDTH, draw::Cell::menu_hb); + + size_t menuBorderLeft = width - MENU_WIDTH - 3; + size_t menuBorderDown = height - 3; + size_t borderUp = 1; + size_t borderDown = height - 1; + size_t borderLeft = 0; + size_t borderRight = width - 1; + + if (width != lastWidth || height != lastHeight) { + draw::clearAll(); } - for(size_t i = 0; i < width; ++i) { - draw::drawCh(0, i, draw::Cell::menu_vb); - draw::drawCh(height - 1, i, draw::Cell::menu_vb); + + // draw borders + for(size_t i = borderUp + 1; i < borderDown; ++i) { + draw::drawCh(0, i, + draw::Cell::menu_vb, menuSimpleScheme); + draw::drawCh(menuBorderLeft, i, + draw::Cell::menu_vb, menuSimpleScheme); + draw::drawCh(borderRight, i, + draw::Cell::menu_vb, menuSimpleScheme); } + for(size_t i = borderLeft + 1; i < borderRight; ++i) { + draw::drawCh(i, borderUp, draw::Cell::menu_hb, + menuSimpleScheme); + draw::drawCh(i, borderDown, draw::Cell::menu_hb, + menuSimpleScheme); + } + for (size_t i = menuBorderLeft + 1; i < borderRight; ++i) { + draw::drawCh(i, menuBorderDown, draw::Cell::menu_hb, + menuSimpleScheme); + } + // draw corners + { + std::vector> corners = { + {borderLeft, borderUp}, + {menuBorderLeft, borderUp}, + {borderRight, borderUp}, + {menuBorderLeft, menuBorderDown}, + {borderRight, menuBorderDown}, + {borderLeft, borderDown}, + {menuBorderLeft, borderDown}, + {borderRight, borderDown} + }; + for (size_t i = 0; i < corners.size(); ++i) { + draw::drawCh(corners[i].first, + corners[i].second, draw::Cell::menu_c, + menuSimpleScheme); + } + } + + drawMenu({menuBorderLeft + 1, borderUp + 1}, + {borderRight - 1, menuBorderDown - 1}); + + drawMap({borderLeft + 1, borderUp + 1}, + {menuBorderLeft - 1, borderDown - 1}); + + // resources + drawParams({0, 0}, std::vector>({ + {1023, draw::Cell::gold}, + {324, draw::Cell::iron}, + {545, draw::Cell::tech} + })); // test + + // draw build requirements + drawRequirements({width - 1, 0}, std:: + vector(5, draw::Cell::unit)); // test + + // cost + drawParams({menuBorderLeft + 1, menuBorderDown + 1}, + std::vector>({ + {120, draw::Cell::gold}, + {400, draw::Cell::iron}, + {15, draw::Cell::tech} + })); // test + + lastWidth = width; + lastHeight = height; } - - void drawMenu(std::pair start, std::pair end) { - - - } - - void drawMap(std::pair start, std::pair end) { - - } } diff --git a/src/out_api/game_draw.hpp b/src/out_api/game_draw.hpp index 40ce220..1d9bec9 100644 --- a/src/out_api/game_draw.hpp +++ b/src/out_api/game_draw.hpp @@ -1,13 +1,48 @@ +#include #include #include +#include +#include +#include "draw.hpp" + #pragma once namespace game_draw { using std::size_t; + + void drawMenuEntity(size_t pos, size_t start, size_t end, + char shortkey, draw::Cell type, + const std::string& name, bool isActive); + void drawMenu(std::pair start, + std::pair end); + + void drawMap(std::pair start, + std::pair end); + + void drawParams(std::pair pos, + const std::vector >& res); + + void drawRequirements(std::pair pos, + const std::vector& requirements); + void drawAll(); - - void drawMenu(size_t width, size_t height); - - void drawMap(size_t width, size_t height); } + +/* +game scheme: + +1234A 1024B 1010011C | D F S W - resources | unit dependences +.--------------------------.----------. +| | | +| |X Tank I| - unit: symbol, name, shortkey +| | | +| | | +| Map | Menu | +| | | +| | | +| .----------. +| |103A 104B | - cost +.--------------------------.----------. + |MENU_WIDTH| +*/ \ No newline at end of file diff --git a/src/out_api/input.cpp b/src/out_api/input.cpp index 4774856..ad88df2 100644 --- a/src/out_api/input.cpp +++ b/src/out_api/input.cpp @@ -3,7 +3,6 @@ #include "input.hpp" namespace input { - bool stopValue = false; void begin() {