Compare commits
2 Commits
7c848cd09f
...
61efa711c9
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
61efa711c9 | ||
|
|
fdfee95426 |
10
README.md
10
README.md
@@ -19,8 +19,10 @@
|
||||
- [x] create a dedicated python script
|
||||
- [ ] C++
|
||||
- [x] re-use 2D game engine
|
||||
- [ ] add map and tiles with cost
|
||||
- [ ] add tiles (with cost) to map
|
||||
- [ ] conversion functions from tile coords to world coords
|
||||
- [ ] drawing tiles
|
||||
- [ ] add "terrain tiles" with different costs
|
||||
- [ ] add mouse-click movement (direct, no pathfinding, player always selected)
|
||||
- [ ] add "terrain tiles" with different costs
|
||||
- [ ] add different sprites
|
||||
- [ ] implement pathfinding and
|
||||
- [ ] implement pathfinding
|
||||
- [ ] windows build?
|
||||
|
||||
300
cpp/src/main.cpp
300
cpp/src/main.cpp
@@ -40,11 +40,8 @@ class UserAction;
|
||||
class Sound {
|
||||
public:
|
||||
Sound() { LOG_DEBUG("."); }
|
||||
|
||||
Sound(const Sound &x) { LOG_DEBUG("."); }
|
||||
|
||||
Sound(Sound &&x) noexcept { LOG_DEBUG("."); }
|
||||
|
||||
~Sound() { LOG_DEBUG("."); }
|
||||
};
|
||||
|
||||
@@ -201,6 +198,14 @@ public:
|
||||
SDL_RenderTexture(m_Renderer.get(), s.GetTexture(), nullptr, &rect);
|
||||
}
|
||||
|
||||
void DrawRect(const Vec2D<float> &position, const Vec2D<float> size,
|
||||
uint8_t R, uint8_t G, uint8_t B, uint8_t A) {
|
||||
SDL_FRect rect = {position.x - size.x / 2.0f, position.y - size.y / 2.0f,
|
||||
size.x, size.y};
|
||||
SDL_SetRenderDrawColor(m_Renderer.get(), R, G, B, A);
|
||||
SDL_RenderFillRect(m_Renderer.get(), &rect);
|
||||
}
|
||||
|
||||
void ClearWindow() {
|
||||
SDL_SetRenderDrawColor(m_Renderer.get(), 50, 50, 50, 255);
|
||||
SDL_RenderClear(m_Renderer.get());
|
||||
@@ -346,11 +351,7 @@ public:
|
||||
NONE,
|
||||
PLAYER,
|
||||
WALL,
|
||||
BOMB,
|
||||
ZOMBIE,
|
||||
ITEM,
|
||||
BOX,
|
||||
EXPLOSION,
|
||||
TILE,
|
||||
COUNT // must be last
|
||||
};
|
||||
|
||||
@@ -361,7 +362,7 @@ public:
|
||||
friend std::ostream &operator<<(std::ostream &os, const Entity &obj) {
|
||||
static constexpr std::array<std::string_view,
|
||||
static_cast<size_t>(Entity::Type::COUNT)>
|
||||
type_name{"NONE", "PLAYER", "WALL", "BOMB", "ZOMBIE", "ITEM", "BOX"};
|
||||
type_name{"NONE", "PLAYER", "WALL", "TILE"};
|
||||
size_t idx = static_cast<size_t>(obj.GetType());
|
||||
assert(idx < type_name.size());
|
||||
os << type_name[idx];
|
||||
@@ -373,7 +374,6 @@ public:
|
||||
virtual constexpr float GetCollisionRadiusSquared() {
|
||||
return GetCollisionRadius() * GetCollisionRadius();
|
||||
}
|
||||
// TODO virtual float GetCollisionRadius() const
|
||||
|
||||
virtual constexpr Type GetType() const = 0;
|
||||
void SetFlagExpired() { m_FlagExpired = true; }
|
||||
@@ -432,106 +432,6 @@ private:
|
||||
static constexpr float m_CollisionRadiusSq = 1000.0f;
|
||||
};
|
||||
|
||||
class Explosion final : public Entity {
|
||||
public:
|
||||
Explosion(Vec2D<float> position) : Entity(position) {
|
||||
LOG_DEBUG(".");
|
||||
if (m_Sprite == nullptr) {
|
||||
LoadResources();
|
||||
}
|
||||
}
|
||||
Explosion(const Explosion &) = delete;
|
||||
Explosion(Explosion &&) = delete;
|
||||
~Explosion() = default;
|
||||
|
||||
Sprite &GetSprite() override {
|
||||
assert(m_Sprite != nullptr);
|
||||
return *m_Sprite;
|
||||
}
|
||||
|
||||
void Update(float time_delta) override {
|
||||
m_ExpirationTime -= time_delta;
|
||||
if (m_ExpirationTime < 0.0f) {
|
||||
this->SetFlagExpired();
|
||||
}
|
||||
Entity::Update(time_delta);
|
||||
}
|
||||
|
||||
constexpr float GetCollisionRadius() const override { return 30.0f; }
|
||||
constexpr Type GetType() const { return Entity::Type::EXPLOSION; }
|
||||
bool IsMovable() const override { return false; }
|
||||
bool IsCollidable() const override { return false; }
|
||||
|
||||
private:
|
||||
void LoadResources() {
|
||||
m_Sprite = std::make_unique<Sprite>("resources/explosion.png",
|
||||
Vec2D<float>{25.0f, 25.0f});
|
||||
}
|
||||
static std::unique_ptr<Sprite> m_Sprite;
|
||||
float m_ExpirationTime = 100.0f;
|
||||
};
|
||||
std::unique_ptr<Sprite> Explosion::m_Sprite = nullptr;
|
||||
|
||||
class Player;
|
||||
|
||||
class Bomb final : public Entity {
|
||||
public:
|
||||
Bomb(Vec2D<float> position, const Player& creator) :
|
||||
Entity(position),
|
||||
m_CreatingPlayer(creator)
|
||||
{
|
||||
LOG_DEBUG(".");
|
||||
if (m_Sprite == nullptr) {
|
||||
LoadResources();
|
||||
}
|
||||
}
|
||||
Bomb(const Bomb &x) = delete;
|
||||
Bomb(Bomb &&x) = delete;
|
||||
|
||||
Sprite &GetSprite() override {
|
||||
assert(m_Sprite != nullptr);
|
||||
return *m_Sprite;
|
||||
}
|
||||
void Update(float time_delta) override {
|
||||
m_ExpirationTime -= time_delta;
|
||||
if (m_ExpirationTime < 0.0f) {
|
||||
this->SetFlagExpired();
|
||||
}
|
||||
Entity::Update(time_delta);
|
||||
}
|
||||
|
||||
constexpr float GetCollisionRadius() const override { return 30.0f; }
|
||||
constexpr Type GetType() const { return Entity::Type::BOMB; }
|
||||
bool IsMovable() const override { return false; }
|
||||
bool IsCollidable() const override { return true; }
|
||||
void SpawnExplosions()
|
||||
{
|
||||
// TODO spawn 4 explosions
|
||||
}
|
||||
|
||||
const Player& GetCreatingPlayer() { return m_CreatingPlayer; }
|
||||
|
||||
private:
|
||||
void LoadResources() {
|
||||
m_Sprite = std::make_unique<Sprite>("resources/bomb.png",
|
||||
Vec2D<float>{25.0f, 40.0f});
|
||||
}
|
||||
static std::unique_ptr<Sprite> m_Sprite;
|
||||
float m_ExpirationTime = 100.0f;
|
||||
const Player& m_CreatingPlayer;
|
||||
};
|
||||
|
||||
std::unique_ptr<Sprite> Bomb::m_Sprite;
|
||||
|
||||
class Item final : public Entity {
|
||||
public:
|
||||
void OnPickup(Entity &other);
|
||||
|
||||
constexpr float GetCollisionRadius() const override { return 50.0f; }
|
||||
bool IsMovable() const override { return false; }
|
||||
bool IsCollidable() const override { return true; }
|
||||
};
|
||||
|
||||
class Wall final : public Entity {
|
||||
public:
|
||||
Wall(Vec2D<float> pos = {0.0f, 0.0f}) : Entity(pos) {
|
||||
@@ -577,10 +477,6 @@ public:
|
||||
return *m_Sprite;
|
||||
}
|
||||
|
||||
std::shared_ptr<Bomb> CreateBomb() {
|
||||
return std::make_shared<Bomb>(this->GetPosition(), *this);
|
||||
}
|
||||
|
||||
constexpr Entity::Type GetType() const override {
|
||||
return Entity::Type::PLAYER;
|
||||
}
|
||||
@@ -600,22 +496,80 @@ std::unique_ptr<Sprite> Player::m_Sprite;
|
||||
|
||||
using Collision = std::pair<std::shared_ptr<Entity>, std::shared_ptr<Entity>>;
|
||||
|
||||
// Game class - TODO Game will be interface,
|
||||
// implementations will be LocalGame and RemoteGame (server)?
|
||||
// Class with game logic should be identical for remote and local games
|
||||
// GameLoop <--> Game
|
||||
// GameLoop <--> GameClient <========> GameServer <--> Game
|
||||
struct Tile {
|
||||
float cost;
|
||||
uint8_t R, G, B, A;
|
||||
};
|
||||
|
||||
static const std::map<std::string_view, Tile> tile_types = {
|
||||
{"Grass", {1.0, 0, 200, 0, 255}},
|
||||
{"Mud", {2.0, 100, 100, 100, 255}},
|
||||
{"Road", {0.5, 200, 200, 200, 255}},
|
||||
};
|
||||
|
||||
using TilePos = Vec2D<int>;
|
||||
using WorldPos = Vec2D<float>;
|
||||
|
||||
class Map {
|
||||
|
||||
// TODO using = ... for tile vector
|
||||
|
||||
class Game {
|
||||
public:
|
||||
Game(int width, int height) : m_Width(width), m_Height(height) {
|
||||
static constexpr float TILE_SIZE = 100.0f; // tile size in world
|
||||
|
||||
Map(int width, int height) : m_Width(width), m_Height(height) {
|
||||
bool sw = true;
|
||||
LOG_DEBUG("width = ", width, " height = ", height);
|
||||
m_Tiles = std::vector<std::vector<const Tile *>>{};
|
||||
for (int i = 0; i < m_Width; i++) {
|
||||
m_Tiles.push_back(std::vector<const Tile *>{});
|
||||
for (int j = 0; j < m_Height; j++) {
|
||||
if (sw)
|
||||
m_Tiles[i].push_back(&tile_types.at("Grass"));
|
||||
else
|
||||
m_Tiles[i].push_back(&tile_types.at("Mud"));
|
||||
sw = !sw;
|
||||
}
|
||||
sw = !sw;
|
||||
}
|
||||
}
|
||||
Map() : Map(0, 0) {}
|
||||
|
||||
const std::vector<std::vector<const Tile *>> &GetMapTiles() const {
|
||||
return m_Tiles;
|
||||
}
|
||||
|
||||
WorldPos TileToWorld(TilePos p) const {
|
||||
return WorldPos{p.x * TILE_SIZE, p.y * TILE_SIZE};
|
||||
}
|
||||
|
||||
TilePos WorldToTile(WorldPos p) const {
|
||||
return TilePos{p.x / TILE_SIZE, p.y / TILE_SIZE};
|
||||
}
|
||||
|
||||
Vec2D<float> GetTileSize() const {
|
||||
return Vec2D<float>{TILE_SIZE, TILE_SIZE};
|
||||
}
|
||||
|
||||
private:
|
||||
// std::vector<std::vector<const Tile*>> m_Tiles;
|
||||
std::vector<std::vector<const Tile *>> m_Tiles;
|
||||
int m_Width = 0;
|
||||
int m_Height = 0;
|
||||
};
|
||||
|
||||
class PathFindingDemo {
|
||||
public:
|
||||
PathFindingDemo(int width, int height)
|
||||
: m_Width(width), m_Height(height), // TODO delete width, height
|
||||
m_Map(width, height) {
|
||||
LOG_DEBUG(".");
|
||||
}
|
||||
|
||||
~Game() { LOG_DEBUG("."); }
|
||||
~PathFindingDemo() { LOG_DEBUG("."); }
|
||||
|
||||
Game(const Game &m) = delete;
|
||||
Game(Game &&m) = delete;
|
||||
PathFindingDemo(const PathFindingDemo &m) = delete;
|
||||
PathFindingDemo(PathFindingDemo &&m) = delete;
|
||||
|
||||
void AddEntity(std::shared_ptr<Entity> e) {
|
||||
// TODO emplace_back
|
||||
@@ -627,33 +581,6 @@ public:
|
||||
m_Player = std::make_shared<Player>();
|
||||
m_Player->SetPosition(Vec2D<float>{200.0f, 200.0f});
|
||||
m_Entities.push_back(m_Player);
|
||||
LOG_INFO("Re-creating random map");
|
||||
// add some entities
|
||||
size_t map_size_bricks = 8;
|
||||
auto wall = std::make_shared<Wall>();
|
||||
auto wall_size = wall->GetSprite().GetSize();
|
||||
Vec2D<float> offset{100.0f, 100.0f};
|
||||
for (size_t u = 0; u < map_size_bricks; u++) {
|
||||
Vec2D<float> pos{offset.x + wall_size.x * u, offset.y};
|
||||
m_Entities.push_back(std::make_shared<Wall>(pos));
|
||||
}
|
||||
for (size_t u = 1; u < map_size_bricks; u++) {
|
||||
Vec2D<float> pos{offset.x, offset.y + u * wall_size.y};
|
||||
m_Entities.push_back(std::make_shared<Wall>(pos));
|
||||
}
|
||||
for (size_t u = 0; u < map_size_bricks; u++) {
|
||||
Vec2D<float> pos{offset.x + wall_size.x * u,
|
||||
offset.y + map_size_bricks * wall_size.y};
|
||||
m_Entities.push_back(std::make_shared<Wall>(pos));
|
||||
}
|
||||
for (size_t u = 0; u < map_size_bricks; u++) {
|
||||
Vec2D<float> pos{offset.x + map_size_bricks * wall_size.x,
|
||||
offset.y + u * wall_size.y};
|
||||
m_Entities.push_back(std::make_shared<Wall>(pos));
|
||||
}
|
||||
// TODO tady jsi skoncil - problem s tim ze ne vsechny steny se spawnou, bud
|
||||
// jsou spatne cykly, nebo to souvisi s tim ze se obcas nespawnou bomby -
|
||||
// investigovat
|
||||
}
|
||||
|
||||
std::shared_ptr<Player> GetPlayer() { return m_Player; }
|
||||
@@ -709,36 +636,11 @@ public:
|
||||
}
|
||||
|
||||
void CollisionGameLogic(Entity &A, Entity &B) {
|
||||
if (A.GetType() == Entity::Type::PLAYER) {
|
||||
// LOG_DEBUG("got player");
|
||||
if (B.GetType() == Entity::Type::EXPLOSION) {
|
||||
LOG_DEBUG("got player and bomb");
|
||||
}
|
||||
}
|
||||
// not used for path finding demo
|
||||
}
|
||||
|
||||
void ExpiryGameLogic(Entity &entity)
|
||||
{
|
||||
switch (entity.GetType()) {
|
||||
case Entity::Type::BOMB:
|
||||
{
|
||||
Bomb& bomb = static_cast<Bomb&>(entity);
|
||||
bomb.SpawnExplosions();
|
||||
break;
|
||||
}
|
||||
case Entity::Type::EXPLOSION:
|
||||
|
||||
break;
|
||||
// non-expiring classes
|
||||
case Entity::Type::PLAYER:
|
||||
case Entity::Type::WALL:
|
||||
case Entity::Type::ZOMBIE:
|
||||
case Entity::Type::ITEM:
|
||||
case Entity::Type::BOX:
|
||||
default:
|
||||
assert(false);
|
||||
break;
|
||||
}
|
||||
void ExpiryGameLogic(Entity &entity) {
|
||||
// not used for path finding demo
|
||||
}
|
||||
|
||||
const std::vector<Collision> &GetEntityCollisions() {
|
||||
@@ -775,7 +677,7 @@ public:
|
||||
m_ExitRequested = true;
|
||||
} else if (action.type == UserAction::Type::FIRE) {
|
||||
LOG_INFO("Fire");
|
||||
AddEntity(m_Player->CreateBomb());
|
||||
// AddEntity(m_Player->CreateBomb());
|
||||
} else if (action.type == UserAction::Type::MOVE) {
|
||||
LOG_INFO("Move direction ", action.Argument.position);
|
||||
m_Player->SetRequestedVelocity(action.Argument.position * 4.0f);
|
||||
@@ -783,6 +685,8 @@ public:
|
||||
};
|
||||
}
|
||||
|
||||
const Map &GetMap() const { return m_Map; }
|
||||
|
||||
bool IsCollisionBoxVisible() const { return m_DrawCollisionBox; }
|
||||
bool IsExitRequested() const { return m_ExitRequested; }
|
||||
|
||||
@@ -793,6 +697,7 @@ private:
|
||||
bool m_DrawCollisionBox = true;
|
||||
std::vector<std::shared_ptr<Entity>> m_Entities;
|
||||
std::shared_ptr<Player> m_Player;
|
||||
Map m_Map;
|
||||
};
|
||||
|
||||
// GameLoop class handles user input and audio/video output,
|
||||
@@ -809,6 +714,21 @@ public:
|
||||
m_Game->UpdateEntities();
|
||||
|
||||
m_Window->ClearWindow();
|
||||
|
||||
// draw the map (terrain tiles)
|
||||
const Map &map = m_Game->GetMap();
|
||||
const auto &tiles = map.GetMapTiles();
|
||||
for (int row = 0; row < tiles.size(); row++) {
|
||||
for (int col = 0; col < tiles[row].size(); col++) {
|
||||
// LOG_DEBUG("Drawing rect (", row, ", ", col, ")");
|
||||
m_Window->DrawRect(
|
||||
map.TileToWorld(TilePos{row, col}) + Vec2D<float>{100.0f, 100.0f},
|
||||
map.GetTileSize(), tiles[row][col]->R, tiles[row][col]->G,
|
||||
tiles[row][col]->B, tiles[row][col]->A);
|
||||
}
|
||||
}
|
||||
|
||||
// draw all the entities (player etc)
|
||||
for (auto &entity : m_Game->GetEntities()) {
|
||||
m_Window->DrawSprite(entity->GetPosition(), entity->GetSprite());
|
||||
if (m_Game->IsCollisionBoxVisible()) {
|
||||
@@ -816,13 +736,16 @@ public:
|
||||
entity->GetCollisionRadius());
|
||||
}
|
||||
}
|
||||
|
||||
m_Window->Flush();
|
||||
// TODO measure fps
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(30));
|
||||
}
|
||||
}
|
||||
|
||||
inline void SetGame(std::unique_ptr<Game> x) { m_Game = std::move(x); }
|
||||
inline void SetGame(std::unique_ptr<PathFindingDemo> x) {
|
||||
m_Game = std::move(x);
|
||||
}
|
||||
inline void SetWindow(std::unique_ptr<Window> x) { m_Window = std::move(x); }
|
||||
inline void SetUserInput(std::unique_ptr<UserInput> x) {
|
||||
m_UserInput = std::move(x);
|
||||
@@ -832,7 +755,7 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
std::unique_ptr<Game> m_Game;
|
||||
std::unique_ptr<PathFindingDemo> m_Game;
|
||||
std::unique_ptr<Window> m_Window;
|
||||
std::unique_ptr<UserInput> m_UserInput;
|
||||
std::unique_ptr<AudioOutput> m_AudioOutput;
|
||||
@@ -865,21 +788,16 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialize the map and run the game
|
||||
* Initialize the map and run the pathfinding demo
|
||||
*/
|
||||
|
||||
// some menu should be used to configure the game itself,
|
||||
// e.g. set map size, type, local or remote game,
|
||||
// optionally game server...
|
||||
// for now we go directly to the game
|
||||
|
||||
auto game = std::make_unique<Game>(50, 50);
|
||||
game->CreateMap();
|
||||
auto demo = std::make_unique<PathFindingDemo>(10, 10);
|
||||
demo->CreateMap();
|
||||
|
||||
auto game_loop = GameLoop{};
|
||||
game_loop.SetWindow(std::move(window));
|
||||
game_loop.SetUserInput(std::move(user_input));
|
||||
game_loop.SetAudioOutput(std::move(audio_output));
|
||||
game_loop.SetGame(std::move(game));
|
||||
game_loop.SetGame(std::move(demo));
|
||||
game_loop.Run();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user