Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ find_package(SDL2_ttf REQUIRED)

set(BIN_NAME dungeon_rush)

file(GLOB SRC src/*.cpp src/*.c)
file(GLOB_RECURSE SRC src/*.cpp src/*.c)

add_executable(${BIN_NAME} ${SRC})

Expand Down
79 changes: 79 additions & 0 deletions src/entities/entity_manager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include "entities/entity_manager.h"

namespace snake {
namespace entities {

void EntityManager::addSnake(const std::shared_ptr<Snake>& snake) {
if (spritesCount_ < kSpritesMaxNum) {
snakes_[spritesCount_++] = snake;
}
}

void EntityManager::removeSnake(int index) {
if (index >= 0 && index < spritesCount_) {
snakes_[index] = nullptr;
}
}

std::shared_ptr<Snake> EntityManager::getSnake(int index) const {
if (index >= 0 && index < kSpritesMaxNum) {
return snakes_[index];
}
return nullptr;
}

int EntityManager::snakeCount() const {
return spritesCount_;
}

int EntityManager::playerCount() const {
return playersCount_;
}

void EntityManager::setPlayerCount(int count) {
playersCount_ = count;
}

void EntityManager::incrementSpriteCount() {
++spritesCount_;
}

int EntityManager::spriteCount() const {
return spritesCount_;
}

void EntityManager::addBullet(const std::shared_ptr<Bullet>& bullet) {
bullets_.push_back(bullet);
}

void EntityManager::removeBullet(const std::shared_ptr<Bullet>& bullet) {
bullets_.remove(bullet);
}

BulletList& EntityManager::bullets() {
return bullets_;
}

const BulletList& EntityManager::bullets() const {
return bullets_;
}

std::array<std::shared_ptr<Snake>, kSpritesMaxNum>& EntityManager::snakes() {
return snakes_;
}

const std::array<std::shared_ptr<Snake>, kSpritesMaxNum>& EntityManager::snakes() const {
return snakes_;
}

void EntityManager::clear() {
for (int i = 0; i < kSpritesMaxNum; ++i) {
snakes_[i] = nullptr;
}
bullets_.clear();
spritesCount_ = 0;
playersCount_ = 0;
}

} // namespace entities
} // namespace snake
61 changes: 61 additions & 0 deletions src/entities/entity_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#ifndef SNAKE_ENTITIES_ENTITY_MANAGER_H_
#define SNAKE_ENTITIES_ENTITY_MANAGER_H_

#include "player.h"
#include "sprite.h"
#include "bullet.h"
#include "adt.h"

#include <array>
#include <memory>

namespace snake {
namespace entities {

constexpr int kSpritesMaxNum = 1024;

// ============================================================================
// EntityManager - Manages all game entities (snakes, bullets)
// ============================================================================
class EntityManager {
public:
EntityManager() = default;
EntityManager(const EntityManager&) = delete;
EntityManager& operator=(const EntityManager&) = delete;
EntityManager(EntityManager&&) = default;
EntityManager& operator=(EntityManager&&) = default;
~EntityManager() = default;

// Snake management
void addSnake(const std::shared_ptr<Snake>& snake);
void removeSnake(int index);
std::shared_ptr<Snake> getSnake(int index) const;
int snakeCount() const;
int playerCount() const;
void setPlayerCount(int count);
void incrementSpriteCount();
int spriteCount() const;

// Bullet management
void addBullet(const std::shared_ptr<Bullet>& bullet);
void removeBullet(const std::shared_ptr<Bullet>& bullet);
BulletList& bullets();
const BulletList& bullets() const;

// Entity access
std::array<std::shared_ptr<Snake>, kSpritesMaxNum>& snakes();
const std::array<std::shared_ptr<Snake>, kSpritesMaxNum>& snakes() const;

void clear();

private:
std::array<std::shared_ptr<Snake>, kSpritesMaxNum> snakes_{};
BulletList bullets_{};
int spritesCount_ = 0;
int playersCount_ = 0;
};

} // namespace entities
} // namespace snake

#endif // SNAKE_ENTITIES_ENTITY_MANAGER_H_
Loading
Loading