Ejemplo n.º 1
0
void
CompoundAbsorberSystem::update(int) {
    for (const auto& entry : m_impl->m_absorbers) {
        CompoundAbsorberComponent* absorber = std::get<0>(entry.second);
        absorber->m_absorbedCompounds.clear();
    }
    for (Collision collision : m_impl->m_compoundCollisions)
    {
        EntityId entityA = collision.entityId1;
        EntityId entityB = collision.entityId2;
        EntityId compoundEntity = NULL_ENTITY;
        EntityId absorberEntity = NULL_ENTITY;

        CompoundAbsorberComponent* absorber = nullptr;
        CompoundComponent* compound = nullptr;
        if (
            m_impl->m_compounds.containsEntity(entityA) and
            m_impl->m_absorbers.containsEntity(entityB)
        ) {
            compoundEntity = entityA;
            absorberEntity = entityB;
            compound = std::get<0>(
                m_impl->m_compounds.entities().at(entityA)
            );
            absorber = std::get<0>(
                m_impl->m_absorbers.entities().at(entityB)
            );
        }
        else if (
            m_impl->m_absorbers.containsEntity(entityA) and
            m_impl->m_compounds.containsEntity(entityB)
        ) {
            compoundEntity = entityB;
            absorberEntity = entityA;
            absorber = std::get<0>(
                m_impl->m_absorbers.entities().at(entityA)
            );
            compound = std::get<0>(
                m_impl->m_compounds.entities().at(entityB)
            );
        }
        if (compound and absorber and absorber->m_enabled == true and absorber->canAbsorbCompound(compound->m_compoundId)) {
            if (CompoundRegistry::isAgentType(compound->m_compoundId)){
                (*CompoundRegistry::getAgentEffect(compound->m_compoundId))(absorberEntity, compound->m_potency);
                this->entityManager()->removeEntity(compoundEntity);
            }
            else if(absorber->m_absorbtionCapacity >= compound->m_potency * CompoundRegistry::getCompoundUnitVolume(compound->m_compoundId)){
                absorber->m_absorbedCompounds[compound->m_compoundId] += compound->m_potency;
                this->entityManager()->removeEntity(compoundEntity);
            }
        }
    }

    m_impl->m_compoundCollisions.clearCollisions();
}
Ejemplo n.º 2
0
void
CompoundAbsorberSystem::update(int, int) {
    for (const auto& value : m_impl->m_absorbers) {
        CompoundAbsorberComponent* absorber = std::get<1>(value.second);
        absorber->m_absorbedCompounds.clear();
    }

    // For all entities that have a membrane and are able to absorb stuff do...
    for (auto& value : m_impl->m_absorbers)
    {
        //EntityId entity = value.first;
        MembraneComponent* membrane = std::get<0>(value.second);
        CompoundAbsorberComponent* absorber = std::get<1>(value.second);
        OgreSceneNodeComponent* sceneNode = std::get<2>(value.second);

        // Find the bounding box of the membrane.
        int sideLength = membrane->getCellDimensions();
        // Find the position of the membrane.
        Ogre::Vector3 origin = sceneNode->m_transform.position;


        // Each membrane absorbs a certain amount of each compound.
        for (auto& entry : m_impl->m_compounds)
        {
            CompoundCloudComponent* compoundCloud = std::get<0>(entry.second);
            CompoundId id = compoundCloud->m_compoundId;
            int x_start = (origin.x - sideLength/2 - compoundCloud->offsetX)/compoundCloud->gridSize + compoundCloud->width/2;
            x_start = x_start > 0 ? x_start : 0;
            int x_end = (origin.x + sideLength/2 - compoundCloud->offsetX)/compoundCloud->gridSize + compoundCloud->width/2;
            x_end = x_end < compoundCloud->width ? x_end : compoundCloud->width;

            int y_start = (origin.y - sideLength/2 - compoundCloud->offsetY)/compoundCloud->gridSize + compoundCloud->height/2;
            y_start = y_start > 0 ? y_start : 0;
            int y_end = (origin.y + sideLength/2 - compoundCloud->offsetY)/compoundCloud->gridSize + compoundCloud->height/2;
            y_end = y_end < compoundCloud->height ? y_end : compoundCloud->height;

            // Iterate though all of the points inside the bounding box.
            for (int x = x_start; x < x_end; x++)
            {
                for (int y = y_start; y < y_end; y++)
                {
                    if (membrane->contains((x-compoundCloud->width/2)*compoundCloud->gridSize-origin.x+compoundCloud->offsetX,(y-compoundCloud->height/2)*compoundCloud->gridSize-origin.y+compoundCloud->offsetY)) {
                        if (absorber->m_enabled == true && absorber->canAbsorbCompound(id)) {
                            float amount = compoundCloud->amountAvailable(x, y, .2) / 5000.0f;
                            //if (CompoundRegistry::isAgentType(id)){
                            //    (*CompoundRegistry::getAgentEffect(id))(entity, amount);
                            //    this->entityManager()->removeEntity(compoundEntity);
                            //}
                            //else
                                if(absorber->m_absorbtionCapacity >= amount * CompoundRegistry::getCompoundUnitVolume(id)){
                                absorber->m_absorbedCompounds[id] += compoundCloud->takeCompound(x, y, .2) / 5000.0f;
                                //this->entityManager()->removeEntity(compoundEntity);
                            }
                        }
                        // Absorb .2 (third parameter) of the available compounds.
                        //membrane->absorbCompounds();
                    }
                }
            }
        }

        // Each membrane absorbs a certain amount of each agent.
        for (auto& entry : m_impl->m_agents)
        {
            AgentCloudComponent* agent = std::get<0>(entry.second);
            OgreSceneNodeComponent* agentNode = std::get<1>(entry.second);
            CompoundId id = agent->m_compoundId;

            if (membrane->contains(agentNode->m_transform.position.x - sceneNode->m_transform.position.x, agentNode->m_transform.position.y - sceneNode->m_transform.position.y)) {
                if (absorber->m_enabled == true && absorber->canAbsorbCompound(id)) {
                    float amount = agent->getPotency();
                    if (CompoundRegistry::isAgentType(id)){
                        (*CompoundRegistry::getAgentEffect(id))(value.first, amount);
                        this->entityManager()->removeEntity(entry.first);
                    }
                }
                // Absorb .2 (third parameter) of the available compounds.
                //membrane->absorbCompounds();
            }
        }
    }
//
//    for (Collision collision : m_impl->m_compoundCollisions)
//    {
//        EntityId entityA = collision.entityId1;
//        EntityId entityB = collision.entityId2;
//        EntityId compoundEntity = NULL_ENTITY;
//        EntityId absorberEntity = NULL_ENTITY;
//
//        CompoundAbsorberComponent* absorber = nullptr;
//        CompoundComponent* compound = nullptr;
//        if (
//            m_impl->m_compounds.containsEntity(entityA) and
//            m_impl->m_absorbers.containsEntity(entityB)
//        ) {
//            compoundEntity = entityA;
//            absorberEntity = entityB;
//            compound = std::get<0>(
//                m_impl->m_compounds.entities().at(entityA)
//            );
//            absorber = std::get<0>(
//                m_impl->m_absorbers.entities().at(entityB)
//            );
//        }
//        else if (
//            m_impl->m_absorbers.containsEntity(entityA) and
//            m_impl->m_compounds.containsEntity(entityB)
//        ) {
//            compoundEntity = entityB;
//            absorberEntity = entityA;
//            absorber = std::get<0>(
//                m_impl->m_absorbers.entities().at(entityA)
//            );
//            compound = std::get<0>(
//                m_impl->m_compounds.entities().at(entityB)
//            );
//        }
//
//        if (compound and absorber and absorber->m_enabled == true and absorber->canAbsorbCompound(compound->m_compoundId)) {
//            if (CompoundRegistry::isAgentType(compound->m_compoundId)){
//                (*CompoundRegistry::getAgentEffect(compound->m_compoundId))(absorberEntity, compound->m_potency);
//                this->entityManager()->removeEntity(compoundEntity);
//            }
//            else if(absorber->m_absorbtionCapacity >= compound->m_potency * CompoundRegistry::getCompoundUnitVolume(compound->m_compoundId)){
//                absorber->m_absorbedCompounds[compound->m_compoundId] += compound->m_potency;
//                this->entityManager()->removeEntity(compoundEntity);
//            }
//        }
//    }

//    m_impl->m_compoundCollisions.clearCollisions();
}