Exemple #1
0
void SpawnerProperty::createNewEntity(LocatedEntity * e, const Operation & op,
        OpVector & res, const std::string& locId)
{
    Anonymous create_arg;
    if (!m_entity.empty()) {
        create_arg = smart_dynamic_cast<Anonymous>(
                Factories::instance()->createObject(m_entity));
        if (!create_arg.isValid()) {
            log(ERROR,
                    "Could not parse 'entity' data on spawner into Entity instance.");
            return;
        }
    } else {
        create_arg->setParents(std::list<std::string>(1, m_type));
    }
    create_arg->setLoc(locId);

    WFMath::MTRand& rand = WFMath::MTRand::instance;
    if (m_mode_external) {
        if (!e->m_location.pos().isValid()) {
            log(ERROR,
                    "Tried to spawn entity for which parent has no valid position.");
            return;
        }
        //randomize position and rotation
        float angle = rand.randf(WFMath::numeric_constants<float>::pi() * 2);
        //place it between 0 and 2 meters away
        float distance = rand.randf(2.0f);
        //if we're solid we should make sure it's not within our own radius
        if (e->m_location.isSolid() && e->m_location.bBox().isValid()) {
            distance += e->m_location.radius();
        }
        //and finally make sure that it's not beyond the radius for checking
        if (m_radius != 0.0f) {
            distance = std::min(m_radius, distance);
        }

        float x = (distance * std::cos(angle));
        float y = (distance * std::sin(angle));

        ::addToEntity(
                WFMath::Point<3>(e->m_location.pos()).shift(
                        WFMath::Vector<3>(x, y, 0)), create_arg->modifyPos());
    } else {
        //If it's an internal spawner, spawn anywhere within the bounding box.
        const BBox bbox = e->m_location.m_bBox;
        if (bbox.isValid()) {
            float x = rand.rand(bbox.highCorner().x() - bbox.lowCorner().x())
                    + bbox.lowCorner().x();
            float y = rand.rand(bbox.highCorner().y() - bbox.lowCorner().y())
                    + bbox.lowCorner().y();
            ::addToEntity(WFMath::Point<3>(x, y, 0), create_arg->modifyPos());
        } else {
            ::addToEntity(WFMath::Point<3>::ZERO(), create_arg->modifyPos());
        }
    }
    float rotation = rand.randf(WFMath::numeric_constants<float>::pi() * 2);
    WFMath::Quaternion orientation(WFMath::Vector<3>(0, 0, 1), rotation);
    create_arg->setAttr("orientation", orientation.toAtlas());

    Create create;
    create->setTo(e->m_location.m_loc->getId());
    create->setArgs1(create_arg);
    res.push_back(create);

    debug(log(NOTICE, compose("Spawner belonging to entity %1 creating new"
            " entity of type %2", e->getId(), m_type))
    ;);