Пример #1
0
void CCArmature::drawContour()
{
    for(auto& element : _boneDic)
    {
        Bone *bone = element.second;
        ColliderDetector *detector = bone->getColliderDetector();

        if (!detector)
            continue;

        const cocos2d::Vector<ColliderBody*>& bodyList = detector->getColliderBodyList();

        for (auto& object : bodyList)
        {
            ColliderBody *body = static_cast<ColliderBody*>(object);
            const std::vector<Vec2> &vertexList = body->getCalculatedVertexList();

            unsigned long length = vertexList.size();
            Vec2 *points = new Vec2[length];
            for (unsigned long i = 0; i<length; i++)
            {
                Vec2 p = vertexList.at(i);
                points[i].x = p.x;
                points[i].y = p.y;
            }
            DrawPrimitives::drawPoly( points, (unsigned int)length, true );

            delete []points;
        }
    }
}
Пример #2
0
void TestColliderDetector::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
{
    for(auto& element : armature2->getBoneDic())
    {
        Bone *bone = element.second;
        ColliderDetector *detector = bone->getColliderDetector();
        
        if (!detector)
            continue;
        
        const cocos2d::Vector<ColliderBody*>& bodyList = detector->getColliderBodyList();
        
        for (auto& object : bodyList)
        {
            ColliderBody *body = static_cast<ColliderBody*>(object);
            const std::vector<Vec2> &vertexList = body->getCalculatedVertexList();
            
            unsigned long length = vertexList.size();
            Vec2 *points = new Vec2[length];
            for (unsigned long i = 0; i<length; i++)
            {
                Vec2 p = vertexList.at(i);
                points[i].x = p.x;
                points[i].y = p.y;
            }
            drawNode->clear();
            drawNode->drawPoly(points, (unsigned int)length, true, Color4F(1.0, 1.0, 1.0, 1.0));
            
            delete []points;
        }
    }
}
Пример #3
0
void TestColliderDetector::update(float delta)
{
    armature2->setVisible(true);

    Rect rect = bullet->getBoundingBox();

    // This code is just telling how to get the vertex.
    // For a more accurate collider detection, you need to implemente yourself.
    const Map<std::string, cocostudio::Bone*>& map = armature2->getBoneDic();
    for(const auto& element : map)
    {
        cocostudio::Bone *bone = element.second;
        ColliderDetector *detector = bone->getColliderDetector();

        if (!detector)
            continue;

        const cocos2d::Vector<ColliderBody*>& bodyList = detector->getColliderBodyList();

        for (const auto& object : bodyList)
        {
            ColliderBody *body = static_cast<ColliderBody*>(object);
            const std::vector<Vec2> &vertexList = body->getCalculatedVertexList();

            float minx = 0, miny = 0, maxx = 0, maxy = 0;
            size_t length = vertexList.size();
            for (size_t i = 0; i<length; i++)
            {
                Vec2 vertex = vertexList.at(i);
                if (i == 0)
                {
                    minx = maxx = vertex.x;
                    miny = maxy = vertex.y;
                }
                else
                {
                    minx = vertex.x < minx ? vertex.x : minx;
                    miny = vertex.y < miny ? vertex.y : miny;
                    maxx = vertex.x > maxx ? vertex.x : maxx;
                    maxy = vertex.y > maxy ? vertex.y : maxy;
                }
            }
            Rect temp = Rect(minx, miny, maxx - minx, maxy - miny);

            if (temp.intersectsRect(rect))
            {
                armature2->setVisible(false);
            }
        }
    }
}
Пример #4
0
void Armature::drawContour()
{
    for(auto& element : _boneDic)
    {
        Bone *bone = element.second;
        ColliderDetector *detector = bone->getColliderDetector();

        if (!detector)
            continue;

        const cocos2d::Vector<ColliderBody*>& bodyList = detector->getColliderBodyList();

        for (auto& object : bodyList)
        {
            ColliderBody *body = static_cast<ColliderBody*>(object);
            const std::vector<Vec2> &vertexList = body->getCalculatedVertexList();

            unsigned long length = vertexList.size();
            Vec2 *points = new (std::nothrow) Vec2[length];
            for (unsigned long i = 0; i<length; i++)
            {
                Vec2 p = vertexList.at(i);
                points[i].x = p.x;
                points[i].y = p.y;
            }
            
#if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#elif _MSC_VER >= 1400 //vs 2005 or higher
#pragma warning (push)
#pragma warning (disable: 4996)
#endif
            
            DrawPrimitives::drawPoly( points, (unsigned int)length, true );

#if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
#pragma GCC diagnostic warning "-Wdeprecated-declarations"
#elif _MSC_VER >= 1400 //vs 2005 or higher
#pragma warning (pop)
#endif
            
            delete []points;
        }
    }
}
Пример #5
0
bool CocAnimationTest::init(Layer* layer){
    do{
        m_layer = layer;
        CC_BREAK_IF(!m_layer);
        ArmatureDataManager::getInstance()->addArmatureFileInfo("hero.ExportJson");
        m_armature = Armature::create("hero");
       // Armature* gun = Armature::create("man");
        //gun->setPosition(Point(100,100));
        
        //m_layer->addChild(gun,10);
        
//        Sprite* s = Sprite::create("CloseNormal.png");
//        s->setPosition(Point(_pCenterX,_pCenterY+100));
//        s->setPhysicsBody(PhysicsBody::createBox(Size(50/_screenScale, 50/_screenScale)));
//        m_layer->addChild(s,10);
        
//        Bone* bone1 = gun->getBone("manBone");
//        gunBone->addChild(gun);
        
        
        
        CC_BREAK_IF(!m_armature);
       
        m_node = Node::create();
        m_layer->addChild(m_node);
        m_node->setPosition(Point(_pCenterX,_pCenterY));
        m_node->setAnchorPoint(Point::ANCHOR_MIDDLE);
        m_node->addChild(m_armature);
        m_node->setPhysicsBody(PhysicsBody::create());
        
        m_armature->setPosition(Point::ZERO);
        
        Bone* _bulletout = m_armature->getBone("bulletout");
        BaseData* data = _bulletout->getWorldInfo();
        m_node->getPhysicsBody()->addShape(PhysicsShapeBox::create(Size(20,20),PHYSICSSHAPE_MATERIAL_DEFAULT,Point(data->x,data->y)));
       // Point points[4] = {Point(0,0),Point(100,100),Point(200,100)};
       // m_node->getPhysicsBody()->addShape(PhysicsShapePolygon::create(points, 3));
        
        //m_armature->setAnchorPoint(Point::ANCHOR_MIDDLE_LEFT);
        
//        m_armature->setPhysicsBody(PhysicsBody::create());
//        m_armature->getPhysicsBody()->addShape(PhysicsShapeBox::create(Size(50/_screenScale, 50/_screenScale)));
////        Point points[4] = {Point(0,0),Point(100,100),Point(0,100)};
////        m_armature->getPhysicsBody()->addShape(PhysicsShapePolygon::create(points, 3));
//        
//       log("pos x=%f,y = %f",m_armature->getBone("shoulder_l")->getWorldInfo()-> x,m_armature->getBone("shoulder_r")->getWorldInfo()->y);
//        m_armature->getPhysicsBody()->addShape(PhysicsShapeBox::create(Size(20,20),PHYSICSSHAPE_MATERIAL_DEFAULT,Point(125,-5)));
//        
//        
        Map<string, Bone*> dict = m_armature->getBoneDic();
        for (auto iter = dict.begin(); iter!=dict.end(); ++iter) {
            Bone* bone = iter->second;
            ColliderDetector* detector = bone->getColliderDetector();
            if (!detector) {
                continue;
            }
            
            Vector<ColliderBody*> vec = detector->getColliderBodyList();
            for (Vector<ColliderBody*>::iterator it = vec.begin(); it!=vec.end(); it++) {
                ColliderBody* body = dynamic_cast<ColliderBody*>(*it);
                
                vector<Point> vertexList = body->getCalculatedVertexList();
                int length = vertexList.size();
                log("length %d",length);
                Point* points = new Point[length];
                for (int i=0; i<length; i++) {
                    points[i] = vertexList[length-i-1];
                    log("point %d %f,%f",i,vertexList[i].x,vertexList[i].y);
                }
                
                m_node->getPhysicsBody()->addShape(PhysicsShapePolygon::create(points, length));
                
            }
        }
        
        m_node->getPhysicsBody()->setRotationEnable(false);
        //Bone* manBone = m_armature->getBone("manBone");
        //m_armature->setPhysicsBody(PhysicsBody::createBox(Size(50/_screenScale, 50/_screenScale)));
        //m_armature->setPosition(Point(100,100));
       // m_armature->setRotation(60);
      //  m_armature->getPhysicsBody()->setVelocity(Point(0,500));
     //   m_armature->getPhysicsBody()->getShape(0)->setTag(99);
      //  m_armature->getPhysicsBody()->setContactTestBitmask(10);
      //  m_armature->getPhysicsBody()->setRotationEnable(false);
      //  m_armature->getPhysicsBody()->setCategoryBitmask(0xf);
       // m_armature->getPhysicsBody()->getShape(99)->setRestitution(0);
 //       Bone* gunBone = gun->getBone("manBone");

//        manBone->addChildBone(gunBone);
        
 //       m_armature->setPhysicsBody(PhysicsBody::createEdgeBox(Size(50/_screenScale, 50/_screenScale)));
//        m_armature->getAnimation()->play("run");
//        m_armature->getAnimation()->setSpeedScale(0.5f);
 //       manBone->addChild(gunBone);
        
        function<void(Armature*,MovementEventType,string)> callback = [](Armature* armature, MovementEventType evt, string animationName){
            switch (evt) {
                case cocostudio::COMPLETE:
                    log("%s %s complete",armature->getName().c_str(),animationName.c_str());
                    break;
                case cocostudio::LOOP_COMPLETE:
                    log("%s %s loop complete", armature->getName().c_str(),animationName.c_str());
                    break;
                case cocostudio::START:
                    log("%s %s start", armature->getName().c_str(),animationName.c_str());
                    break;
                default:
                    break;
            }
        };
        
       // m_armature->getAnimation()->setMovementEventCallFunc(callback);
        //armature->setRotation(60);
        
        
        
        
        return true;
        //m_layer->addChild(gun,11);
    }while (0);
    return false;
}