Peter87:
Actually the Enemies are EventDispatchers. An event dispatcher looks like this:
EventDispatcher.h:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
class EventDispatcher
{
public:
void addHandler(EventHandler* handler);
void removeHandler(EventHandler* handler);
protected:
// Dispatches an event to all subscribed handlers
void dispatchEvent(const Event& e);
private:
std::vector<EventHandler*> handlers_;
};
|
EventDispatcher.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
void EventDispatcher::addHandler(EventHandler* handler)
{
handlers_.push_back(handler);
}
void EventDispatcher::removeHandler(EventHandler* handler)
{
handlers_.erase(std::find(handlers_.begin(), handlers_.end(), handler));
}
void EventDispatcher::dispatchEvent(const Event& e)
{
for (const auto &handler : handlers_)
{
handler->handleEvent(e);
}
}
|
Since the EventDispatcher store pointers to the enemies, this will be invalid when the enemies are erased and the other enemies change position within the vector? This could be solved by using pointers?
The error still feels kinda weird if this is the case. Wouldn't it just try to access enemies out of range etc.?
keskiverto:
Considering I've overloaded the copy constructor and equality (=) operator in Entity, I think it should be safe to copy considering that Enemy doesn't use any pointers etc. in the class?
Also, before in my GameManager class I had like this:
1 2 3
|
std::unique_ptr<Player> player_;
std::vector<std::shared_ptr<Enemy>> enemies_;
std::vector<std::unique_ptr<Projectile>> projectiles_;
|
but after reading that you should use the STACK rather than the HEAP when possible, I changed it to:
1 2 3
|
Player player_;
std::vector<Enemy> enemies_;
std::vector<std::unique_ptr<Projectile>> projectiles_;
|
¨
But I guess since both Player, Enemey and Projectile (the Projectile vector stores both Rockets and Bombs so pointers
must be used there) uses polymorphism it's perfectly valid (and safer) to use pointers (smart pointers) instead?