added elements in unit, map, game

This commit is contained in:
ProgramSnail 2021-04-12 01:51:12 +03:00
parent 220dd71d8f
commit 733bcbd99d
10 changed files with 116 additions and 16 deletions

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,5 @@
#include "game.hpp"
#include "game_map.hpp"
Game::Game(size_t playersNum, std::pair<size_t, size_t> sz) :
playersNum(playersNum), gameMap(sz) {}

View file

@ -0,0 +1,19 @@
#include <vector>
#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<unit::Unit> units;
events::EventCenter eventCenter;
// vector<player data>
// vector<input> // for players
public:
// may be more than one map sizes ??
Game(size_t playersNum, std::pair<size_t, size_t> sz);
};

View file

@ -0,0 +1,48 @@
#include "game_map.hpp"
namespace map {
GameMap::GameMap(std::pair<size_t, size_t> sz) :
cells(sz.first, std::vector<Cell>(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(); // empty action;
freeActionsId.push_back(id);
}
const Cell& GameMap::getCell(std::pair<size_t, size_t> pos) {
return cells[pos.first][pos.second];
}
void GameMap::setCell(std::pair<size_t, size_t> pos,
const Cell& cell) {
cells[pos.first][pos.second] = cell;
}
std::vector<std::pair<size_t, size_t>> GameMap::getWay(
std::pair<size_t, size_t> start,
std::pair<size_t, size_t> end) {
// now only easiest realisation, then may be A*
std::vector<std::pair<size_t, size_t>> 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;
}
}

View file

@ -1,7 +1,6 @@
#include <vector> #include <vector>
#include "map_entities/cell.hpp" #include "map_entities/cell.hpp"
#include "map_entities/action.hpp" #include "map_entities/action.hpp"
#include "map_entities/unit_obj.hpp"
#pragma once #pragma once
@ -9,11 +8,23 @@ namespace map {
class GameMap { class GameMap {
private: private:
std::vector<std::vector<Cell> > cells; std::vector<std::vector<Cell> > cells;
std::vector<Action*> actions; std::vector<Action> actions;
std::vector<UnitObj> units; std::vector<size_t> freeActionsId;
public: public:
GameMap() { GameMap(std::pair<size_t, size_t> sz);
size_t addAction(const Action& action);
void removeAction(size_t id);
const Cell& getCell(std::pair<size_t, size_t> pos);
void setCell(std::pair<size_t, size_t> pos,
const Cell& cell);
std::vector<std::pair<size_t, size_t>> getWay(
std::pair<size_t, size_t> start,
std::pair<size_t, size_t> end);
}
}; };
} }

View file

@ -1,7 +1,7 @@
#pragma once #pragma once
namespace map { namespace map {
enum class CellType { // needed to choose final types enum class CellType {
ctNone, ctNone,
ctUnit, ctUnit,
ctWeapon, ctWeapon,
@ -18,7 +18,7 @@ namespace map {
}; };
struct Cell { struct Cell {
CellType type; CellType type = CellType::ctNone;
// int ??speed??; // speed when move inside CellPlayer player = CellPlayer::cpNone;
}; };
} }

View file

@ -1,6 +1,7 @@
#include <utility> #include <utility>
#include <cstdint> #include <cstdint>
#include "cell.hpp" #include "cell.hpp"
#include "../game_map.hpp"
#pragma once #pragma once
@ -12,10 +13,10 @@ namespace map {
Coord size; Coord size;
CellType cellType; CellType cellType;
public: public:
UnitObj(const Coord& pos, const Coord& size, const CellType& cellType) : UnitObj() {}
pos(pos), size(size), cellType(cellType) { UnitObj(const Coord& pos, const Coord& size,
const CellType& cellType, map::GameMap* map) :
} pos(pos), size(size), cellType(cellType) {}
void updateAll() { void updateAll() {
} }

View file

@ -1,5 +1,7 @@
#include "unit.hpp" #include "unit.hpp"
namespace unit { namespace unit {
Unit::Unit(map::GameMap* map, events::EventCenter* events) {
}
} }

View file

@ -1,22 +1,36 @@
#include <vector> #include <vector>
#include "../game_map.hpp" #include "../game_map.hpp"
#include "../game_events.hpp"
#include "unit_module.hpp" #include "unit_module.hpp"
#include "../map_entities/unit_obj.hpp"
#pragma once #pragma once
namespace unit { namespace unit {
class Unit { class Unit {
private: private:
const size_t MEMORY_SIZE = 100;
// parts of unit, that do something // parts of unit, that do something
std::vector<Module> modules; std::vector<Module> modules;
// memory, help modules communicate and handle events // memory, help modules communicate and handle events
std::vector<int> memory; std::vector<int> memory;
map::GameMap* map; map::UnitObj unitObj;
public:
Unit();
void update(); 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();
}; };
} }