added factory, unit maiden easier

This commit is contained in:
ProgramSnail 2021-05-18 17:43:03 +03:00
parent 52024e4b06
commit 1c87c30dda
13 changed files with 230 additions and 69 deletions

View file

@ -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)

View file

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

View file

@ -6,25 +6,33 @@
namespace map {
class GameMap {
public:
using Coord = std::pair<size_t, size_t>;
private:
std::vector<std::vector<Cell> > cells;
std::vector<Action> actions;
std::vector<size_t> freeActionsId;
public:
GameMap(std::pair<size_t, size_t> sz);
GameMap(Coord sz);
size_t addAction(const Action& action);
void removeAction(size_t id);
const Cell& getCell(std::pair<size_t, size_t> pos);
const Cell& getCell(Coord pos);
void setCell(std::pair<size_t, size_t> pos,
const Cell& cell);
void setCell(Coord 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);
void setCellBlock(Coord startPos,
Coord endPos, const Cell& cell);
void eraseCell(Coord pos);
void eraseCellBlock(Coord startPos, Coord endPos);
std::vector<Coord> getWay(
Coord start,
Coord end);
void generate();
};

View file

@ -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) {}
};
}

View file

@ -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<CellPlayer>(player);
}
void UnitObj::setCellType(int cellType) {
cellPrototype.type = static_cast<CellType>(cellType);
}
}

View file

@ -10,15 +10,29 @@ namespace map {
private:
using Coord = std::pair<int32_t, int32_t>;
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);
};
}

3
src/game/player_data.cpp Normal file
View file

@ -0,0 +1,3 @@
#include "player_data.hpp"
int PlayerData::maxPlayerId = 1;

View file

@ -0,0 +1,12 @@
#pragma once
struct PlayerData { // may be class better
static int maxPlayerId;
int player;
int resources = 0;
PlayerData() {
player = maxPlayerId;
++maxPlayerId;
}
};

View file

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

View file

@ -0,0 +1,36 @@
#include <vector>
#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<Module> modules;
// memory, help modules communicate and handle events
std::vector<int> 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();
};
}

View file

@ -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<Module> modules;
// memory, help modules communicate and handle events
std::vector<int> 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;
}
};
}

View file

@ -1 +0,0 @@
#include "unit_builder.hpp"

View file

@ -1,16 +0,0 @@
#include <string>
#include "unit.hpp"
#pragma once
namespace unit {
class UnitBuilder {
private:
public:
UnitBuilder();
Unit create();
void config(std::string& file);
};
}