Fixed now. I had a typo in my scenenode categories.
Everytime any character pushes attackCommand to the commandqueue, that command gets executed 4 times.
3 projectiles are added where character is currently and 4. as an invisible child to the character. And if I enable players text node, 5th one is added relative to that. What could cause this?
Category changes behaviour, but I still haven't completely understood the command structure.
Attack command
mAttackCommand.category = Category::Scene;
mAttackCommand.action = [this, &textures] (SceneNode& node, sf::Time)
{
std::cout << " -> create projectile" << std::endl;
createProjectile(node, textures);
};
void Character::createProjectile(SceneNode& node, const TextureHolder& textures) const
{
std::unique_ptr<Projectile> projectile(new Projectile(Projectile::Placeholder, textures));
projectile->setPosition(getPosition());
node.attachChild(std::move(projectile));
}
Character update
void Character::updateCurrent(sf::Time dt, CommandQueue& commands)
{
if (getCategory() == Category::Player)
updateTexts();
checkAttackCooldown(dt, commands);
Entity::updateCurrent(dt, commands);
}
void Character::checkAttackCooldown(sf::Time dt, CommandQueue& commands)
{
if (getCategory() & Category::Enemy)
attack();
// Check for automatic attacking, allow only in intervals
if (mIsAttacking && mAttackCooldown <= sf::Time::Zero)
{
std::cout << "push attack command" << std::endl;
// Interval expired: Attack
commands.push(mAttackCommand);
mAttackCooldown += Table[mType].attackInterval;
mIsAttacking = false;
}
else if (mAttackCooldown > sf::Time::Zero)
{
// Interval not expired: Decrease it further
mAttackCooldown -= dt;
mIsAttacking = false;
}
}
stdout
push attack command
-> create projectile
-> create projectile
-> create projectile
-> create projectile
-> create projectile