Example #1
0
void Unit::damage(i32 value)
{
    hp -= value;
    if (hp <= 0) {
        Vec2 pos = _rootNode->getPhysicsBody()->getPosition();
        Color4F col = Color4F::WHITE;
        if (auto player = getPlayer()) {
            col = player->color;
        }

        // Create boom trash
        Vec2 up = -_game->physicsWorld()->getForceField()->getGravity(pos).getNormalized();
        for (int i = 0; i < 7; i++) {
            Shell* shell = Shell::create(_game);
            shell->setColor(col);
            shell->setDamage(10);
            shell->setPosition(pos);

            float angle = CC_DEGREES_TO_RADIANS(random<float>(-60, 60));
            Vec2 j = up * random<float>(10.0f, 25.0f);
            j = j.rotate(Vec2::forAngle(angle));
            shell->getNode()->getPhysicsBody()->applyTorque(random<float>(-100.0f, 100.0f));
            shell->getNode()->getPhysicsBody()->applyImpulse(j);
        }

        destroy();
    }
}