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/src/game/game_map.cpp b/src/game/game_map.cpp index 25d291d..54e6871 100644 --- a/src/game/game_map.cpp +++ b/src/game/game_map.cpp @@ -1,7 +1,7 @@ #include "game_map.hpp" namespace map { - GameMap::GameMap(std::pair sz) : + GameMap::GameMap(Coord sz) : cells(sz.first, std::vector(sz.second)) {} size_t GameMap::addAction(const Action& action) { @@ -18,24 +18,39 @@ namespace map { } void GameMap::removeAction(size_t id) { - actions[id] = Action(); // empty action; + actions[id] = Action(); freeActionsId.push_back(id); } - const Cell& GameMap::getCell(std::pair pos) { + const Cell& GameMap::getCell(Coord pos) { return cells[pos.first][pos.second]; } - void GameMap::setCell(std::pair pos, - const Cell& cell) { + void GameMap::setCell(Coord pos, const Cell& cell) { cells[pos.first][pos.second] = cell; } - std::vector> GameMap::getWay( - std::pair start, - std::pair end) { + 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; + 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; @@ -46,7 +61,11 @@ namespace map { return way; } - void generate() { - + 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 4d2365d..a67ceaa 100644 --- a/src/game/game_map.hpp +++ b/src/game/game_map.hpp @@ -6,25 +6,33 @@ namespace map { class GameMap { + public: + using Coord = std::pair; private: std::vector > cells; std::vector actions; std::vector freeActionsId; public: - GameMap(std::pair sz); + GameMap(Coord sz); size_t addAction(const Action& action); void removeAction(size_t id); - const Cell& getCell(std::pair pos); + const Cell& getCell(Coord pos); - void setCell(std::pair pos, - const Cell& cell); + void setCell(Coord pos, const Cell& cell); - std::vector> getWay( - std::pair start, - std::pair end); + 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(); }; diff --git a/src/game/map_entities/cell.hpp b/src/game/map_entities/cell.hpp index 5874b93..739fa79 100644 --- a/src/game/map_entities/cell.hpp +++ b/src/game/map_entities/cell.hpp @@ -2,23 +2,26 @@ namespace map { enum class CellType { - ctNone, - ctUnit, - ctWeapon, - ctForest, - ctMoutain + none, + unit, + weapon, + forest, + moutain }; enum class CellPlayer { - cpNone, - cpPlayer0, - cpPlayer1, - cpPlayer2, - cpPlayer3 + none, + player0, + player1, + player2, + player3 }; struct Cell { - CellType type = CellType::ctNone; - CellPlayer player = CellPlayer::cpNone; + CellType type; + 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 6bcb084..bd89400 100644 --- a/src/game/map_entities/unit_obj.hpp +++ b/src/game/map_entities/unit_obj.hpp @@ -10,15 +10,29 @@ namespace map { private: using Coord = std::pair; Coord pos; - Coord size; - CellType cellType; - public: - UnitObj() {} - UnitObj(const Coord& pos, const Coord& size, - const CellType& cellType, map::GameMap* map) : - pos(pos), size(size), cellType(cellType) {} - void updateAll() { + Coord size; + GameMap* gameMap; + Cell cellPrototype; + + Coord getEndPos() { + return {pos.first + size.first, + pos.second + size.second}; } + public: + 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 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.hpp b/src/game/unit/unit.hpp index f7efb11..f755d03 100644 --- a/src/game/unit/unit.hpp +++ b/src/game/unit/unit.hpp @@ -7,30 +7,89 @@ #pragma once namespace unit { + class UnitFactory; + class Unit { private: - const size_t MEMORY_SIZE = 100; + friend class UnitFactory; - // parts of unit, that do something - std::vector modules; - - // memory, help modules communicate and handle events - std::vector memory; + int basicStrength; + int strength; + int shield; + int speed; + int player; + int cellType; map::UnitObj unitObj; - map::GameMap* map; + map::GameMap* gameMap = nullptr; - events::EventCenter* eventCenter; + events::EventCenter* eventCenter = nullptr; + + void setCellType(int newCellType) { + cellType = newCellType; + unitObj.setCellType(cellType); + } public: - Unit(map::GameMap* map, events::EventCenter* eventCenter); + Unit() = default; + + Unit(map::GameMap* gameMap, events::EventCenter* eventCenter); void step(); - size_t addModule(Module module); + void damage(int damage, int power); - void removeModule(size_t id); + bool isDestroyed() { + return strength <= 0; + } - void damage(); + 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