draw game improvement

This commit is contained in:
ProgramSnail 2021-04-11 20:14:23 +03:00
parent bee718ad9b
commit 220dd71d8f
15 changed files with 274 additions and 75 deletions

View file

@ -9,34 +9,34 @@ include_directories(${CURSES_INCLUDE_DIR})
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin) set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib) set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
set(GameSource main.cpp) set(GameSource src/main.cpp)
set(OutApiSource set(OutApiSource
out_api/draw.cpp src/out_api/draw.cpp
out_api/game_draw.cpp src/out_api/game_draw.cpp
out_api/init.cpp src/out_api/init.cpp
out_api/input_controller.cpp src/out_api/input_controller.cpp
out_api/input.cpp src/out_api/input.cpp
out_api/menu_draw.cpp) src/out_api/menu_draw.cpp)
set(MenuSource set(MenuSource
menu/menu.cpp) src/menu/menu.cpp)
set(GameMainSource set(GameMainSource
game/game_events.cpp src/game/game_events.cpp
game/game_map.cpp src/game/game_map.cpp
game/game_menu.cpp) src/game/game_menu.cpp)
set(GameUnitSource set(GameUnitSource
game/unit/module_builder.cpp src/game/unit/module_builder.cpp
game/unit/unit_builder.cpp src/game/unit/unit_builder.cpp
game/unit/unit_module.cpp src/game/unit/unit_module.cpp
game/unit/unit.cpp) src/game/unit/unit.cpp)
set(GameMapEntitiesSource set(GameMapEntitiesSource
game/map_entities/cell.cpp src/game/map_entities/cell.cpp
game/map_entities/unit_obj.cpp src/game/map_entities/unit_obj.cpp
game/map_entities/actions/attack_action.cpp) src/game/map_entities/actions/attack_action.cpp)
add_library(OutApi ${OutApiSource}) add_library(OutApi ${OutApiSource})
add_library(Menu ${MenuSource}) add_library(Menu ${MenuSource})

BIN
bin/Game Executable file

Binary file not shown.

BIN
lib/libGameMain.a Normal file

Binary file not shown.

BIN
lib/libGameMapEntities.a Normal file

Binary file not shown.

BIN
lib/libGameUnit.a Normal file

Binary file not shown.

BIN
lib/libMenu.a Normal file

Binary file not shown.

BIN
lib/libOutApi.a Normal file

Binary file not shown.

View file

@ -1,7 +1,7 @@
#include <vector> #include <vector>
#include <functional> #include <functional>
#include <string> #include <string>
#include <unordered_map> #include <map>
#pragma once #pragma once
@ -25,13 +25,30 @@ namespace events {
bool operator!=(const EventId& eId) const { bool operator!=(const EventId& eId) const {
return !operator==(eId); 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 class EventData { // may be changed
private: private:
using T = int; using T = int;
std::unordered_map<std::string, T> data; std::map<std::string, T> data;
public: public:
EventData() {} EventData() {}
@ -99,7 +116,7 @@ namespace events {
class EventCenter { class EventCenter {
private: private:
std::unordered_map<EventId, std::vector<EventHandler>> handlers; std::map<EventId, std::vector<EventHandler>> handlers;
public: public:
void addEventHandler(const EventHandler& handler) { void addEventHandler(const EventHandler& handler) {
handlers[handler.getEventId()].push_back(handler); handlers[handler.getEventId()].push_back(handler);

View file

@ -1,7 +1,6 @@
#include <vector> #include <vector>
#include "../game_map.hpp" #include "../game_map.hpp"
#include "unit_module.hpp" #include "unit_module.hpp"
#include "unit_memory_elem.hpp"
#pragma once #pragma once

View file

@ -1,8 +1,8 @@
#include <curses.h> #include <curses.h>
#include <cstdlib> #include <cstdlib>
#include "src/out_api/init.hpp" #include "out_api/init.hpp"
#include "src/out_api/menu_draw.hpp" #include "out_api/menu_draw.hpp"
#include "src/out_api/game_draw.hpp" #include "out_api/game_draw.hpp"
int main() { int main() {
init::begin(); init::begin();

View file

@ -4,18 +4,18 @@
namespace draw { namespace draw {
void initColorPairs() { void initColorPairs() {
init_pair(ColorScheme::simple, COLOR_BLACK, COLOR_WHITE); init_pair(static_cast<short>(ColorScheme::simple), COLOR_WHITE, COLOR_BLACK);
init_pair(ColorScheme::map_simple, COLOR_BLACK, COLOR_WHITE); init_pair(static_cast<short>(ColorScheme::map_simple), COLOR_WHITE, COLOR_BLACK);
init_pair(ColorScheme::map_active, COLOR_WHITE, COLOR_BLACK); init_pair(static_cast<short>(ColorScheme::map_active), COLOR_BLACK, COLOR_WHITE);
init_pair(ColorScheme::menu_simple, COLOR_BLACK, COLOR_WHITE); init_pair(static_cast<short>(ColorScheme::menu_simple), COLOR_WHITE, COLOR_BLACK);
init_pair(ColorScheme::menu_active, COLOR_WHITE, COLOR_BLACK); init_pair(static_cast<short>(ColorScheme::menu_active), COLOR_BLACK, COLOR_WHITE);
init_pair(ColorScheme::player0, COLOR_BLACK, COLOR_RED); init_pair(static_cast<short>(ColorScheme::player0), COLOR_RED, COLOR_BLACK);
init_pair(ColorScheme::player1, COLOR_BLACK, COLOR_BLUE); init_pair(static_cast<short>(ColorScheme::player1), COLOR_BLUE, COLOR_BLACK);
init_pair(ColorScheme::player2, COLOR_BLACK, COLOR_MAGENTA); init_pair(static_cast<short>(ColorScheme::player2), COLOR_MAGENTA, COLOR_BLACK);
init_pair(ColorScheme::player3, COLOR_BLACK, COLOR_YELLOW); init_pair(static_cast<short>(ColorScheme::player3), COLOR_YELLOW, COLOR_BLACK);
init_pair(ColorScheme::neutral, COLOR_WHITE, COLOR_GREEN); init_pair(static_cast<short>(ColorScheme::neutral), COLOR_GREEN, COLOR_WHITE);
init_pair(ColorScheme::enviroment, COLOR_BLACK, COLOR_WHITE); init_pair(static_cast<short>(ColorScheme::enviroment), COLOR_WHITE, COLOR_BLACK);
init_pair(ColorScheme::damaged, COLOR_RED, COLOR_WHITE); init_pair(static_cast<short>(ColorScheme::damaged), COLOR_WHITE, COLOR_RED);
} }
void begin() { void begin() {
@ -36,7 +36,14 @@ namespace draw {
void drawCh(size_t x, size_t y, void drawCh(size_t x, size_t y,
Cell cell, ColorScheme colorScheme) { Cell cell, ColorScheme colorScheme) {
attron(COLOR_PAIR(colorScheme)); attron(COLOR_PAIR(colorScheme));
mvaddch(y, x, cell); mvaddch(y, x, static_cast<short>(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)); attroff(COLOR_PAIR(colorScheme));
} }

View file

@ -5,29 +5,32 @@
namespace draw { namespace draw {
using std::size_t; using std::size_t;
enum ColorScheme { enum class ColorScheme {
simple, simple = 0,
map_simple, map_simple = 1,
map_active, map_active = 2,
menu_simple, menu_simple = 3,
menu_active, menu_active = 4,
player0, player0 = 5,
player1, player1 = 6,
player2, player2 = 7,
player3, player3 = 8,
neutral, neutral = 9,
damaged, damaged = 10,
enviroment, enviroment = 11,
}; };
enum Cell { enum class Cell {
blank = ' ', blank = ' ',
menu_hb = '-', menu_hb = '-',
menu_vb = '|', menu_vb = '|',
menu_c = ' ', menu_c = ' ',
field = '.', field = '.',
mountain = '^', mountain = '^',
unit = '#' unit = '#',
gold = 'G',
iron = 'I',
tech = 'T'
}; };
void begin(); void begin();
@ -41,6 +44,9 @@ namespace draw {
void drawCh(size_t x, size_t y, void drawCh(size_t x, size_t y,
Cell cell, ColorScheme colorScheme = ColorScheme::simple); 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 setxy(uint32_t x, uint32_t y);
void getSize(size_t& x, size_t& y); void getSize(size_t& x, size_t& y);

View file

@ -1,32 +1,168 @@
#include "draw.hpp"
#include "game_draw.hpp" #include "game_draw.hpp"
#include "draw.hpp"
namespace game_draw { namespace game_draw {
const size_t MENU_WIDTH = 16; 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<size_t, size_t> start,
std::pair<size_t, size_t> 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<size_t, size_t> start,
std::pair<size_t, size_t> end) {
}
void drawParams(std::pair<size_t, size_t> start,
const std::vector<std::pair<int, draw::Cell>> &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<size_t, size_t> end,
const std::vector<draw::Cell> &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() { void drawAll() {
size_t width; size_t width;
size_t height; size_t height;
draw::getSize(width, height); draw::getSize(width, height);
// check width && height // check width && height
for(size_t i = 0; i < height; ++i) {
draw::drawCh(i, 0, draw::Cell::menu_hb); size_t menuBorderLeft = width - MENU_WIDTH - 3;
draw::drawCh(i, width - 1, draw::Cell::menu_hb); size_t menuBorderDown = height - 3;
draw::drawCh(i, width - MENU_WIDTH, draw::Cell::menu_hb); 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 borders
draw::drawCh(height - 1, i, draw::Cell::menu_vb); 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 // draw corners
} {
std::vector<std::pair<size_t, size_t>> corners = {
void drawMenu(std::pair<size_t, size_t> start, std::pair<size_t, size_t> end) { {borderLeft, borderUp},
{menuBorderLeft, borderUp},
{borderRight, borderUp},
} {menuBorderLeft, menuBorderDown},
{borderRight, menuBorderDown},
void drawMap(std::pair<size_t, size_t> start, std::pair<size_t, size_t> end) { {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<std::
pair<int, draw::Cell>>({
{1023, draw::Cell::gold},
{324, draw::Cell::iron},
{545, draw::Cell::tech}
})); // test
// draw build requirements
drawRequirements({width - 1, 0}, std::
vector<draw::Cell>(5, draw::Cell::unit)); // test
// cost
drawParams({menuBorderLeft + 1, menuBorderDown + 1},
std::vector<std::pair<int, draw::Cell>>({
{120, draw::Cell::gold},
{400, draw::Cell::iron},
{15, draw::Cell::tech}
})); // test
lastWidth = width;
lastHeight = height;
} }
} }

View file

@ -1,13 +1,48 @@
#include <bits/c++config.h>
#include <cstdlib> #include <cstdlib>
#include <utility> #include <utility>
#include <vector>
#include <string>
#include "draw.hpp"
#pragma once #pragma once
namespace game_draw { namespace game_draw {
using std::size_t; 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<size_t, size_t> start,
std::pair<size_t, size_t> end);
void drawMap(std::pair<size_t, size_t> start,
std::pair<size_t, size_t> end);
void drawParams(std::pair<size_t, size_t> pos,
const std::vector<std::pair<int, draw::Cell> >& res);
void drawRequirements(std::pair<size_t, size_t> pos,
const std::vector<draw::Cell>& requirements);
void drawAll(); 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|
*/

View file

@ -3,7 +3,6 @@
#include "input.hpp" #include "input.hpp"
namespace input { namespace input {
bool stopValue = false; bool stopValue = false;
void begin() { void begin() {