Merge pull request #3 from ProgramSnail/checkpoint_2

added elements in unit, map, game
This commit is contained in:
ProgramSnail 2021-08-30 08:38:08 +00:00 committed by GitHub
commit 8415ec6e21
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 371 additions and 58 deletions

1
.gitignore vendored
View file

@ -10,3 +10,4 @@ compile_commands.json
CTestTestfile.cmake CTestTestfile.cmake
_deps _deps
testing testing
.vscode

3
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,3 @@
{
"cmake.configureOnOpen": true
}

View file

@ -29,7 +29,6 @@ set(GameMainSource
set(GameUnitSource set(GameUnitSource
src/game/unit/module_builder.cpp src/game/unit/module_builder.cpp
src/game/unit/unit_builder.cpp
src/game/unit/unit_module.cpp src/game/unit/unit_module.cpp
src/game/unit/unit.cpp) src/game/unit/unit.cpp)

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,22 @@
#include "game.hpp"
#include "game_map.hpp"
Game::Game(size_t playersNum, std::pair<size_t, size_t> 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
}

View file

@ -0,0 +1,22 @@
#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);
void begin();
void step();
void end();
};

View file

@ -0,0 +1,71 @@
#include "game_map.hpp"
namespace map {
GameMap::GameMap(Coord 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();
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::Coord> GameMap::getWay(
Coord start, Coord end) {
// now only easiest realisation, then may be A*
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;
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();
}
}
}
}

View file

@ -1,19 +1,39 @@
#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
namespace map { namespace map {
class GameMap { class GameMap {
public:
using Coord = std::pair<size_t, size_t>;
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(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<Coord> getWay(
Coord start,
Coord end);
void generate();
}; };
} }

View file

@ -1,24 +1,27 @@
#pragma once #pragma once
namespace map { namespace map {
enum class CellType { // needed to choose final types enum class CellType {
ctNone, none,
ctUnit, unit,
ctWeapon, weapon,
ctForest, forest,
ctMoutain moutain
}; };
enum class CellPlayer { enum class CellPlayer {
cpNone, none,
cpPlayer0, player0,
cpPlayer1, player1,
cpPlayer2, player2,
cpPlayer3 player3
}; };
struct Cell { struct Cell {
CellType type; CellType type;
// int ??speed??; // speed when move inside 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" #include "unit_obj.hpp"
namespace map { 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

@ -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
@ -10,14 +11,28 @@ namespace map {
using Coord = std::pair<int32_t, int32_t>; using Coord = std::pair<int32_t, int32_t>;
Coord pos; Coord pos;
Coord size; Coord size;
CellType cellType; GameMap* gameMap;
Cell cellPrototype;
Coord getEndPos() {
return {pos.first + size.first,
pos.second + size.second};
}
public: public:
UnitObj(const Coord& pos, const Coord& size, const CellType& cellType) : UnitObj() = default;
pos(pos), size(size), cellType(cellType) { 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 updateAll() {
} 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

@ -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,95 @@
#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 UnitFactory;
class Unit { class Unit {
private: private:
// parts of unit, that do something friend class UnitFactory;
std::vector<Module> modules;
// memory, help modules communicate and handle events int basicStrength;
std::vector<int> memory; int strength;
int shield;
int speed;
int player;
int cellType;
map::GameMap* map; map::UnitObj unitObj;
map::GameMap* gameMap = nullptr;
events::EventCenter* eventCenter = nullptr;
void setCellType(int newCellType) {
cellType = newCellType;
unitObj.setCellType(cellType);
}
public: public:
Unit(); Unit() = default;
void update(); 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;
}
}; };
} }

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

View file

@ -1,14 +1,19 @@
#include <curses.h> #include <curses.h>
#include <cstdlib> #include <cstdlib>
#include "game/game.hpp"
#include "out_api/init.hpp" #include "out_api/init.hpp"
#include "out_api/menu_draw.hpp" #include "out_api/menu_draw.hpp"
#include "out_api/game_draw.hpp" #include "out_api/game_draw.hpp"
int main() { int main() {
Game game(2, {100, 100});
game.begin();
init::begin(); init::begin();
while (!init::stop()) { while (!init::stop()) {
game_draw::drawAll(); game_draw::drawAll();
game.step();
init::step(); init::step();
} }
init::end(); init::end();
game.end();
} }

View file

@ -6,10 +6,13 @@
#include "input.hpp" #include "input.hpp"
namespace init { namespace init {
WINDOW* stdscr;
void begin() { void begin() {
initscr(); stdscr = initscr();
noecho(); noecho();
curs_set(false); curs_set(false);
nodelay(stdscr, true);
draw::begin(); draw::begin();
input::begin(); input::begin();
} }

View file

@ -14,7 +14,7 @@ namespace input {
} }
void step() { void step() {
lastInput = getch();
} }
void end() { void end() {

View file

@ -1,8 +1,10 @@
#include <cstdint> #include <cstdint>
#include <curses.h>
#pragma once #pragma once
namespace input { namespace input {
char lastInput = ERR;
void begin(); void begin();

View file

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

View file

@ -0,0 +1,12 @@
#include <set>
#pragma once
// add keyboard to keys rebinding for each InputController
class InputController {
private:
public:
InputController();
};