Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Attack implementation from the book: 4 projectiles instead of 1  (Read 1200 times)

0 Members and 1 Guest are viewing this topic.

warbaque

  • Newbie
  • *
  • Posts: 18
    • View Profile
Attack implementation from the book: 4 projectiles instead of 1
« on: December 17, 2013, 05:44:44 pm »
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
« Last Edit: December 17, 2013, 06:14:17 pm by warbaque »