I've downloaded the source code and tried to compile it, but unfortunately my Visual Studio 2013 is sending back these errors:
Error 1 error C3848: expression having type 'const std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Spaceship::* )(float,float),void,Spaceship,float,float>,std::_Ph<1> &,float,float>' would lose some const-volatile qualifiers in order to call 'void std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Spaceship::* )(float,float),void,Spaceship,float,float>,std::_Ph<1> &,float,float>::operator ()<GameObject&>(GameObject &)' c:\users\bane\documents\visual studio 2013\projects\mortal_space_invaders_clone\mortal_space_invaders_clone\command.hpp 33
Error 2 error C3848: expression having type 'const std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Spaceship::* )(void),void,Spaceship,>,std::_Ph<1> &>' would lose some const-volatile qualifiers in order to call 'void std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall Spaceship::* )(void),void,Spaceship,>,std::_Ph<1> &>::operator ()<GameObject&>(GameObject &)' c:\users\bane\documents\visual studio 2013\projects\mortal_space_invaders_clone\mortal_space_invaders_clone\command.hpp 33
After searching a bit, I found out (http://en.sfml-dev.org/forums/index.php?topic=11992.msg85540#msg85540) that it is a compiler error. So if anybody is using VS 2013 (like me), he is going to have a tough time compiling it. :(
i added new video for latest update, after implemented sprite masking and pixel collision detection.
here snippet from sprite masking by using sf::RenderTexture and sf::Image
void Shield::updateCurrent(sf::Time dt, CommandQueue& commands)
{
if (!mOnHit)
return;
mRenderTexture.clear();
mRenderTexture.draw(*this, sf::BlendNone);
mRenderTexture.display();
float radius = (mRectOnHit.height > ExplosionRadius) ? mRectOnHit.height : ExplosionRadius;
sf::CircleShape circle(radius, 10);
circle.setPosition(mPositionOnHit.x, mPositionOnHit.y + circle.getRadius() / 2.f * mSign);
circle.setFillColor(sf::Color::Transparent);
centerOrigin(circle);
mRenderTexture.draw(circle, sf::BlendNone);
mRenderTexture.display();
sf::Vector2u position(static_cast<std::size_t>(getPosition().x - mTexture.getSize().x / 2u), static_cast<std::size_t>(getPosition().y - mTexture.getSize().y / 2u));
mImage.copy(mRenderTexture.getTexture().copyToImage(), 0u, 0u, sf::IntRect(position.x, position.y, mTexture.getSize().x, mTexture.getSize().y));
mImage.createMaskFromColor(sf::Color::Transparent);
updateSprite();
}
and here the pixel collision detection for bullets:
bool PixelcollidesPair(const Shield& shield, const Projectile& bullet)
{
auto bulletBounds = static_cast<sf::Rect<std::size_t>>(bullet.getBoundingRect());
auto shieldBounds = static_cast<sf::Rect<std::size_t>>(shield.getBoundingRect());
auto width = bulletBounds.left + bulletBounds.width;
auto height = bulletBounds.top + bulletBounds.height;
sf::Vector2u position(bulletBounds.left, bulletBounds.top);
if (!bulletBounds.intersects(shieldBounds))
return false;
for (auto x = position.x; x < width; ++x)
{
for (auto y = position.y; y < height; ++y)
{
auto relX = x - shieldBounds.left;
auto relY = (bullet.getCategory() & Category::PlayerProjectile) ? y - shieldBounds.top : y - shieldBounds.top - bulletBounds.height;
if (shield.getPixel(relX, relY))
return true;
}
}
return false;
}
update
i add a new video in main post for implement a new movement algorithm for invaders. result seems okay so far. inspired by what Hapax did to his latest practice games. thanks millions Hapax
here code snippet of adapt enemy movements
void World::adaptEnemyMovements()
{
bool changeDirection = false;
for (const auto& i : mEnemyNodes)
{
Spaceship& enemy = static_cast<Spaceship&>(*i);
if (enemy.getType() == Spaceship::Boss)
continue;
if (!getBattlefieldBounds().contains(enemy.getPosition()))
changeDirection = true;
}
// let invaders moving down and update condition of change direction
for (const auto& i : mEnemyNodes)
{
Spaceship& enemy = static_cast<Spaceship&>(*i);
if (enemy.getType() == Spaceship::Boss)
continue;
enemy.requestChangeDirection(changeDirection);
}
}
and here update movement pattern it's exactly same mostly
void Spaceship::updateMovementPattern(sf::Time dt)
{
// Enemy Spaceships: Movement pattern
const std::vector<Direction>& directions = Table[mType].directions;
if (!directions.empty())
{
// Moved long enough in horizontal direction: Change direction to move down
if(mChaneDirction && directions[mDirectionIndex].distance == 0.f)
{
mDirectionIndex = (mDirectionIndex + 1) % directions.size();
mTravelledDistance = 0.f;
mChaneDirction = false;
}
// Moved long enough in vertical direction: Change direction to move aside
if (directions[mDirectionIndex].distance != 0.f && mTravelledDistance > directions[mDirectionIndex].distance)
{
mDirectionIndex = (mDirectionIndex + 1) % directions.size();
mTravelledDistance = 0.f;
}
// Compute velocity from direction
auto radians = toRadian(directions[mDirectionIndex].angle + 90.f);
auto vx = getMaxSpeed() * std::cos(radians);
auto vy = getMaxSpeed() * std::sin(radians);
setVelocity(vx, vy);
mTravelledDistance += getMaxSpeed() * dt.asSeconds();
applyAnimation(dt);
}
}
unfortunately, there is a bug, invaders some time drop down, especially when it got hit at both edges right and left of screen.