diff --git a/.gitignore b/.gitignore index 2c1fa6b..2cc25a6 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,5 @@ install_manifest.txt compile_commands.json CTestTestfile.cmake _deps -testing \ No newline at end of file +testing +.vscode \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..0db5873 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "cmake.configureOnOpen": true +} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index c91e6d9..4f03271 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,7 +29,6 @@ set(GameMainSource set(GameUnitSource src/game/unit/module_builder.cpp - src/game/unit/unit_builder.cpp src/game/unit/unit_module.cpp src/game/unit/unit.cpp) diff --git a/lib/libGameMain.a b/lib/libGameMain.a index 8a17feb..e163600 100644 Binary files a/lib/libGameMain.a and b/lib/libGameMain.a differ diff --git a/lib/libGameUnit.a b/lib/libGameUnit.a index 8cf3156..04bd9a9 100644 Binary files a/lib/libGameUnit.a and b/lib/libGameUnit.a differ diff --git a/src/game/game.cpp b/src/game/game.cpp index e69de29..614b3c8 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -0,0 +1,22 @@ +#include "game.hpp" +#include "game_map.hpp" + +Game::Game(size_t playersNum, std::pair sz) : + playersNum(playersNum), gameMap(sz) {} + +void Game::begin() { + // init map + // create basic units +} + +void Game::step() { + // gameMap.step(); + for (size_t i = 0; i < units.size(); ++i) { + units[i].step(); + } + // all game step +} + +void Game::end() { + // end all +} \ No newline at end of file diff --git a/src/game/game.hpp b/src/game/game.hpp index e69de29..941a53d 100644 --- a/src/game/game.hpp +++ b/src/game/game.hpp @@ -0,0 +1,22 @@ +#include +#include "game_events.hpp" +#include "game_map.hpp" +#include "unit/unit.hpp" + +#pragma once + +class Game { +private: + size_t playersNum; + map::GameMap gameMap; + std::vector units; + events::EventCenter eventCenter; + // vector + // vector // for players +public: + // may be more than one map sizes ?? + Game(size_t playersNum, std::pair sz); + void begin(); + void step(); + void end(); +}; \ No newline at end of file diff --git a/src/game/game_map.cpp b/src/game/game_map.cpp index e69de29..54e6871 100644 --- a/src/game/game_map.cpp +++ b/src/game/game_map.cpp @@ -0,0 +1,71 @@ +#include "game_map.hpp" + +namespace map { + GameMap::GameMap(Coord sz) : + cells(sz.first, std::vector(sz.second)) {} + + size_t GameMap::addAction(const Action& action) { + if (freeActionsId.size() == 0) { + actions.push_back(action); + return actions.size() - 1; + } + else { + size_t id = freeActionsId.back(); + actions[id] = action; + actions.pop_back(); + return id; + } + } + + void GameMap::removeAction(size_t id) { + actions[id] = Action(); + freeActionsId.push_back(id); + } + + const Cell& GameMap::getCell(Coord pos) { + return cells[pos.first][pos.second]; + } + + void GameMap::setCell(Coord pos, const Cell& cell) { + cells[pos.first][pos.second] = cell; + } + + void GameMap::setCellBlock(Coord startPos, + Coord endPos, const Cell& cell) { + for (size_t i = startPos.first; i < endPos.first; ++i) { // ?? or <= better ?? + for (size_t j = startPos.second; j < endPos.second; ++j) { + setCell({i, j}, cell); + } + } + } + + void GameMap::eraseCell(Coord pos) { + setCell(pos, Cell()); + } + + void GameMap::eraseCellBlock(Coord startPos, Coord endPos) { + setCellBlock(startPos, endPos, Cell()); + } + + std::vector GameMap::getWay( + Coord start, Coord end) { + // now only easiest realisation, then may be A* + std::vector way; + for (size_t i = start.first; i < end.first; + start.first < end.first ? ++i : --i) { + for (size_t j = start.second; i < end.second; + start.second < end.second ? ++i : --i) { + way.push_back({i, j}); + } + } + return way; + } + + void GameMap::generate() { + for (size_t i = 0; cells.size(); ++i) { + for (size_t j = 0; j < cells[i].size(); ++i) { + cells[i][j] = Cell(); + } + } + } +} \ No newline at end of file diff --git a/src/game/game_map.hpp b/src/game/game_map.hpp index 55a82ad..a67ceaa 100644 --- a/src/game/game_map.hpp +++ b/src/game/game_map.hpp @@ -1,19 +1,39 @@ #include #include "map_entities/cell.hpp" #include "map_entities/action.hpp" -#include "map_entities/unit_obj.hpp" #pragma once namespace map { class GameMap { + public: + using Coord = std::pair; private: std::vector > cells; - std::vector actions; - std::vector units; + std::vector actions; + std::vector freeActionsId; public: - GameMap() { + GameMap(Coord sz); + + size_t addAction(const Action& action); - } + void removeAction(size_t id); + + const Cell& getCell(Coord pos); + + void setCell(Coord pos, const Cell& cell); + + void setCellBlock(Coord startPos, + Coord endPos, const Cell& cell); + + void eraseCell(Coord pos); + + void eraseCellBlock(Coord startPos, Coord endPos); + + std::vector getWay( + Coord start, + Coord end); + + void generate(); }; } \ No newline at end of file diff --git a/src/game/map_entities/cell.hpp b/src/game/map_entities/cell.hpp index 08e03a7..739fa79 100644 --- a/src/game/map_entities/cell.hpp +++ b/src/game/map_entities/cell.hpp @@ -1,24 +1,27 @@ #pragma once namespace map { - enum class CellType { // needed to choose final types - ctNone, - ctUnit, - ctWeapon, - ctForest, - ctMoutain + enum class CellType { + none, + unit, + weapon, + forest, + moutain }; enum class CellPlayer { - cpNone, - cpPlayer0, - cpPlayer1, - cpPlayer2, - cpPlayer3 + none, + player0, + player1, + player2, + player3 }; struct Cell { CellType type; - // int ??speed??; // speed when move inside + CellPlayer player; + Cell(CellType type = CellType::none, + CellPlayer player = CellPlayer::none) + : type(type), player(player) {} }; } diff --git a/src/game/map_entities/unit_obj.cpp b/src/game/map_entities/unit_obj.cpp index cc7f083..1bf7940 100644 --- a/src/game/map_entities/unit_obj.cpp +++ b/src/game/map_entities/unit_obj.cpp @@ -1,5 +1,23 @@ #include "unit_obj.hpp" namespace map { - + void UnitObj::resize(Coord newSize) { + gameMap->eraseCellBlock(pos, getEndPos()); + size = newSize; + gameMap->setCellBlock(pos, getEndPos(), cellPrototype); + } + + void UnitObj::move(Coord newPos) { + gameMap->eraseCellBlock(pos, getEndPos()); + pos = newPos; + gameMap->setCellBlock(pos, getEndPos(), cellPrototype); + } + + void UnitObj::setPlayer(int player) { + cellPrototype.player = static_cast(player); + } + + void UnitObj::setCellType(int cellType) { + cellPrototype.type = static_cast(cellType); + } } \ No newline at end of file diff --git a/src/game/map_entities/unit_obj.hpp b/src/game/map_entities/unit_obj.hpp index fd06e51..bd89400 100644 --- a/src/game/map_entities/unit_obj.hpp +++ b/src/game/map_entities/unit_obj.hpp @@ -1,6 +1,7 @@ #include #include #include "cell.hpp" +#include "../game_map.hpp" #pragma once @@ -9,15 +10,29 @@ namespace map { private: using Coord = std::pair; Coord pos; - Coord size; - CellType cellType; + Coord size; + GameMap* gameMap; + Cell cellPrototype; + + + Coord getEndPos() { + return {pos.first + size.first, + pos.second + size.second}; + } public: - UnitObj(const Coord& pos, const Coord& size, const CellType& cellType) : - pos(pos), size(size), cellType(cellType) { + UnitObj() = default; + UnitObj(const Coord& pos, const Coord& size, + const CellType& cellType, + const CellPlayer& cellPlayer, GameMap* gameMap) : + pos(pos), size(size), gameMap(gameMap), + cellPrototype(cellType, cellPlayer) {} - } - void updateAll() { + void resize(Coord newSize); - } + void move(Coord newPos); + + void setPlayer(int player); + + void setCellType(int cellType); }; } \ No newline at end of file diff --git a/src/game/player_data.cpp b/src/game/player_data.cpp new file mode 100644 index 0000000..e03a62c --- /dev/null +++ b/src/game/player_data.cpp @@ -0,0 +1,3 @@ +#include "player_data.hpp" + +int PlayerData::maxPlayerId = 1; \ No newline at end of file diff --git a/src/game/player_data.hpp b/src/game/player_data.hpp index e69de29..b7b88ab 100644 --- a/src/game/player_data.hpp +++ b/src/game/player_data.hpp @@ -0,0 +1,12 @@ +#pragma once + +struct PlayerData { // may be class better + static int maxPlayerId; + int player; + int resources = 0; + + PlayerData() { + player = maxPlayerId; + ++maxPlayerId; + } +}; \ No newline at end of file diff --git a/src/game/unit/_constructable_unit.cpp b/src/game/unit/_constructable_unit.cpp new file mode 100644 index 0000000..dea4de6 --- /dev/null +++ b/src/game/unit/_constructable_unit.cpp @@ -0,0 +1,7 @@ +#include "unit.hpp" + +namespace unit { + Unit::Unit(map::GameMap* map, events::EventCenter* events) { + + } +} \ No newline at end of file diff --git a/src/game/unit/_constructable_unit.hpp b/src/game/unit/_constructable_unit.hpp new file mode 100644 index 0000000..f7efb11 --- /dev/null +++ b/src/game/unit/_constructable_unit.hpp @@ -0,0 +1,36 @@ +#include +#include "../game_map.hpp" +#include "../game_events.hpp" +#include "unit_module.hpp" +#include "../map_entities/unit_obj.hpp" + +#pragma once + +namespace unit { + class Unit { + private: + const size_t MEMORY_SIZE = 100; + + // parts of unit, that do something + std::vector modules; + + // memory, help modules communicate and handle events + std::vector memory; + + map::UnitObj unitObj; + + map::GameMap* map; + + events::EventCenter* eventCenter; + public: + Unit(map::GameMap* map, events::EventCenter* eventCenter); + + void step(); + + size_t addModule(Module module); + + void removeModule(size_t id); + + void damage(); + }; +} diff --git a/src/game/unit/unit.cpp b/src/game/unit/unit.cpp index 5420efd..dea4de6 100644 --- a/src/game/unit/unit.cpp +++ b/src/game/unit/unit.cpp @@ -1,5 +1,7 @@ #include "unit.hpp" namespace unit { - + Unit::Unit(map::GameMap* map, events::EventCenter* events) { + + } } \ No newline at end of file diff --git a/src/game/unit/unit.hpp b/src/game/unit/unit.hpp index 6ef2c1c..f755d03 100644 --- a/src/game/unit/unit.hpp +++ b/src/game/unit/unit.hpp @@ -1,22 +1,95 @@ #include #include "../game_map.hpp" +#include "../game_events.hpp" #include "unit_module.hpp" +#include "../map_entities/unit_obj.hpp" #pragma once namespace unit { + class UnitFactory; + class Unit { private: - // parts of unit, that do something - std::vector modules; - - // memory, help modules communicate and handle events - std::vector memory; - - map::GameMap* map; - public: - Unit(); + friend class UnitFactory; - void update(); + int basicStrength; + int strength; + int shield; + int speed; + int player; + int cellType; + + map::UnitObj unitObj; + + map::GameMap* gameMap = nullptr; + + events::EventCenter* eventCenter = nullptr; + + void setCellType(int newCellType) { + cellType = newCellType; + unitObj.setCellType(cellType); + } + public: + Unit() = default; + + Unit(map::GameMap* gameMap, events::EventCenter* eventCenter); + + void step(); + + void damage(int damage, int power); + + bool isDestroyed() { + return strength <= 0; + } + + void setPlayer(int newPlayer) { + player = newPlayer; + unitObj.setPlayer(player); + } + }; + + class UnitFactory { + private: + Unit unit; + public: + UnitFactory() = default; + + UnitFactory& setBasicStrength(int basicStrength) { + unit.basicStrength = basicStrength; + unit.strength = basicStrength; + return *this; + } + + UnitFactory& setShield(int shield) { + unit.shield = shield; + return *this; + } + + UnitFactory& setSpeed(int speed) { + unit.speed = speed; + return *this; + } + + UnitFactory& setCellType(int cellType) { + unit.setCellType(cellType); + return *this; + } + + UnitFactory& setEventCenter(events::EventCenter* eventCenter) { + unit.eventCenter = eventCenter; + return *this; + } + + UnitFactory& setGameMap(map::GameMap* gameMap) { + unit.gameMap = gameMap; + return *this; + } + + Unit build(int player) { + Unit cp = unit; + cp.setPlayer(player); + return cp; + } }; } diff --git a/src/game/unit/unit_builder.cpp b/src/game/unit/unit_builder.cpp deleted file mode 100644 index 91e5fd0..0000000 --- a/src/game/unit/unit_builder.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "unit_builder.hpp" \ No newline at end of file diff --git a/src/game/unit/unit_builder.hpp b/src/game/unit/unit_builder.hpp deleted file mode 100644 index 5a7baa6..0000000 --- a/src/game/unit/unit_builder.hpp +++ /dev/null @@ -1,16 +0,0 @@ -#include -#include "unit.hpp" - -#pragma once - -namespace unit { - class UnitBuilder { - private: - public: - UnitBuilder(); - - Unit create(); - - void config(std::string& file); - }; -} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 22791c9..efda5b0 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,14 +1,19 @@ #include #include +#include "game/game.hpp" #include "out_api/init.hpp" #include "out_api/menu_draw.hpp" #include "out_api/game_draw.hpp" int main() { + Game game(2, {100, 100}); + game.begin(); init::begin(); while (!init::stop()) { game_draw::drawAll(); + game.step(); init::step(); } - init::end(); + init::end(); + game.end(); } diff --git a/src/out_api/init.cpp b/src/out_api/init.cpp index c9e452c..746a683 100644 --- a/src/out_api/init.cpp +++ b/src/out_api/init.cpp @@ -6,10 +6,13 @@ #include "input.hpp" namespace init { + WINDOW* stdscr; + void begin() { - initscr(); + stdscr = initscr(); noecho(); curs_set(false); + nodelay(stdscr, true); draw::begin(); input::begin(); } diff --git a/src/out_api/input.cpp b/src/out_api/input.cpp index ad88df2..57c85ce 100644 --- a/src/out_api/input.cpp +++ b/src/out_api/input.cpp @@ -14,7 +14,7 @@ namespace input { } void step() { - + lastInput = getch(); } void end() { diff --git a/src/out_api/input.hpp b/src/out_api/input.hpp index e3a753a..25b592f 100644 --- a/src/out_api/input.hpp +++ b/src/out_api/input.hpp @@ -1,8 +1,10 @@ #include +#include #pragma once namespace input { + char lastInput = ERR; void begin(); diff --git a/src/out_api/input_controller.cpp b/src/out_api/input_controller.cpp index e69de29..74b591e 100644 --- a/src/out_api/input_controller.cpp +++ b/src/out_api/input_controller.cpp @@ -0,0 +1 @@ +#include "input_controller.hpp" \ No newline at end of file diff --git a/src/out_api/input_controller.hpp b/src/out_api/input_controller.hpp index e69de29..ed2f68e 100644 --- a/src/out_api/input_controller.hpp +++ b/src/out_api/input_controller.hpp @@ -0,0 +1,12 @@ +#include + +#pragma once + +// add keyboard to keys rebinding for each InputController + +class InputController { +private: + +public: + InputController(); +}; \ No newline at end of file