Пример #1
0
void GameLevel::dieImpl() {
  mDieFlag = false;

  pause();

  // Camera effect.
  getGameLayer()->getBlockRoot()->runAction(CameraShake::create(0.3f, 10));

  auto hero = getHero();

  // Particle effect.
  ParticleSystem *ps = ParticleSystemQuad::create(DIE_FX_NAME);
  ParticleBatchNode *batch = ParticleBatchNode::createWithTexture(ps->getTexture());
  batch->addChild(ps);
  batch->setPosition(hero->getRenderer()->getPosition());
  batch->setCameraMask((unsigned short) CameraFlag::USER2);
  mGameLayer->getBlockRoot()->addChild(batch, ZORDER_DIE_FX, DIE_FX_TAG);

  hero->getRenderer()->getNode()->runAction(Sequence::create(ScaleTo::create(0.2, 0.1, 0.1),
                                                             CallFunc::create([this] {
    getHero()->getRenderer()->setVisible(false);
  }), nullptr));

  mGameLayer->runAction(Sequence::create(DelayTime::create(0.5), CallFunc::create([this] {
    enableGame(false);
    enableGame(true);
  }), nullptr));
}
Пример #2
0
void GameLevel::loadFx() {
  for (auto i : mEffects) {
    ParticleSystem *ps = ParticleSystemQuad::create(i);
    ParticleBatchNode *batch = ParticleBatchNode::createWithTexture(ps->getTexture());
    batch->addChild(ps);
    mGameLayer->getBlockRoot()->addChild(batch, ZORDER_PARTICLE);
    batch->setCameraMask((unsigned short) CameraFlag::USER2);
    mFxNodes.push_back(batch);
  }
}
ParticleBatchNode* ParticleBatchNode::create(const std::string& imageFile, int capacity/* = kParticleDefaultCapacity*/)
{
    ParticleBatchNode * p = new (std::nothrow) ParticleBatchNode();
    if( p && p->initWithFile(imageFile, capacity))
    {
        p->autorelease();
        return p;
    }
    CC_SAFE_DELETE(p);
    return nullptr;
}
ParticleBatchNode* ParticleBatchNode::createWithTexture(Texture2D *tex, int capacity/* = kParticleDefaultCapacity*/)
{
    ParticleBatchNode * p = new (std::nothrow) ParticleBatchNode();
    if( p && p->initWithTexture(tex, capacity))
    {
        p->autorelease();
        return p;
    }
    CC_SAFE_DELETE(p);
    return nullptr;
}
Пример #5
0
ParticleBatchNode* ParticleBatchNode::create(const char* imageFile, unsigned int capacity/* = kParticleDefaultCapacity*/)
{
    ParticleBatchNode * p = new ParticleBatchNode();
    if( p && p->initWithFile(imageFile, capacity))
    {
        p->autorelease();
        return p;
    }
    CC_SAFE_DELETE(p);
    return NULL;
}
Пример #6
0
ParticleBatchNode* ParticleBatchNode::createWithTexture(Texture2D *tex, unsigned int capacity/* = kParticleDefaultCapacity*/)
{
    ParticleBatchNode * p = new ParticleBatchNode();
    if( p && p->initWithTexture(tex, capacity))
    {
        p->autorelease();
        return p;
    }
    CC_SAFE_DELETE(p);
    return NULL;
}
void ParticleSystemQuad::setBatchNode(ParticleBatchNode * batchNode)
{
    if( _batchNode != batchNode )
    {
        ParticleBatchNode* oldBatch = _batchNode;

        ParticleSystem::setBatchNode(batchNode);

        // NEW: is self render ?
        if( ! batchNode )
        {
            allocMemory();
            initIndices();
            setTexture(oldBatch->getTexture());
            if (Configuration::getInstance()->supportsShareableVAO())
            {
                setupVBOandVAO();
            }
            else
            {
                setupVBO();
            }
        }
        // OLD: was it self render ? cleanup
        else if( !oldBatch )
        {
            // copy current state to batch
            V3F_C4B_T2F_Quad *batchQuads = _batchNode->getTextureAtlas()->getQuads();
            V3F_C4B_T2F_Quad *quad = &(batchQuads[_atlasIndex] );
            memcpy( quad, _quads, _totalParticles * sizeof(_quads[0]) );

            CC_SAFE_FREE(_quads);
            CC_SAFE_FREE(_indices);

            glDeleteBuffers(2, &_buffersVBO[0]);
            memset(_buffersVBO, 0, sizeof(_buffersVBO));
            if (Configuration::getInstance()->supportsShareableVAO())
            {
                glDeleteVertexArrays(1, &_VAOname);
                GL::bindVAO(0);
                _VAOname = 0;
            }
        }
    }
}
Пример #8
0
void GameScene::onTouchEnded(cocos2d::Touch *touch, cocos2d::Event *unused_event)
{
    Vec2 size = Director::getInstance()->getVisibleSize();
    
    auto HandAnimation = Animation::createWithSpriteFrames(mHandAnimations, 0.05);
    auto HandAnimate = Animate::create(HandAnimation);
    mHandSprite->runAction(HandAnimate);

    if (mKnife == nullptr) return;
    
    CCLOG ("%f\n", mKnife->getKnife()->getPositionY());
    if (mKnife->isInCutterEdge())
    {
        
        // 失败脸部表情
        auto FaceAnimation = Animation::createWithSpriteFrames(mFaceAnimationFail, 1);
        auto FaceAnimate = Animate::create(FaceAnimation);
        mFaceSprite->runAction(FaceAnimate);
        
        MusicButton::getInstance()->playCatchCutterEdge();
        
        // 停止刀子
        mKnife->getKnife()->getPhysicsBody()->setVelocity(Vec2(0, 0));
        mKnife->getKnife()->getPhysicsBody()->setGravityEnable(false);
        
        // 流血效果
        ParticleSystem* BloodPar = ParticleSystemQuad::create("blood.plist");
        BloodPar->retain();
        ParticleBatchNode* BloodNode = ParticleBatchNode::createWithTexture(BloodPar->getTexture());
        BloodNode->addChild(BloodPar);
        BloodNode->setAnchorPoint(Vec2::ZERO);
        BloodNode->setPosition(Vec2(250, 300));
        addChild(BloodNode, 10);
        BloodPar->release();
        
        // NG
        scheduleOnce(schedule_selector(GameScene::gameOver), 1);
    }
    else if (mKnife->isInHilt())
    {
        // 成功脸部表情
        auto FaceAnimation = Animation::createWithSpriteFrames(mFaceAnimationSucc, 0.5);
        auto FaceAnimate = Animate::create(FaceAnimation);
        mFaceSprite->runAction(FaceAnimate);
        
        MusicButton::getInstance()->playCatchHilt();
        
        // 刀子动作
        mKnife->getKnife()->getPhysicsBody()->setVelocity(Vec2(0, 0));
        mKnife->getKnife()->getPhysicsBody()->setGravityEnable(false);
        auto KnifeMove = MoveTo::create(0.3, Vec2(Vec2(size.x-140, size.y-82)));
        auto KnifeScale = ScaleTo::create(0.4, 0.1);
        auto KnifeSpa = Spawn::create(KnifeMove, KnifeScale, NULL);
        mKnife->getKnife()->runAction(KnifeSpa);
        
        scheduleOnce(schedule_selector(GameScene::gotKnife), 0.5);
    }
    else
    {
        // Miss表情
        auto FaceAnimation = Animation::createWithSpriteFrames(mFaceAnimationMiss, 0.2);
        auto FaceAnimate = Animate::create(FaceAnimation);
        mFaceSprite->runAction(FaceAnimate);
    }
}
Пример #9
0
bool GameScene::init()
{
    if (!Layer::init()) {
        return false;
    }
    Size winSize =Director::getInstance()->getWinSize();
    ParticleSystemQuad* qual = ParticleSystemQuad::create("res/effect/dots_birth.plist");
    ParticleBatchNode* bNode = ParticleBatchNode::createWithTexture(qual->getTexture());
    bNode->addChild(qual);
    this->addChild(bNode);
    qual->setPosition(Vec2(320,568));
    auto spawnHero = CallFunc::create([=]()
    {
        m_hero = Hero::create();
        this->addChild(m_hero);
        addJointForHero();
        bNode->removeFromParent();
    });
    auto spawnMobs = CallFunc::create([=]()
    {
        Enemy* enemy = Enemy::create();
        enemy->setPosition(Vec2(400,200));
        addChild(enemy);
        enemy->moveToHero(m_hero->getCurMovingNode()->getPosition());
    });
    auto spawnHeroSequ = Sequence::create(DelayTime::create(0.8),spawnHero,spawnMobs,NULL);
    runAction(spawnHeroSequ);
    EventListenerTouchOneByOne* listenner = EventListenerTouchOneByOne::create();
    listenner->onTouchBegan = [=](Touch* touch,Event* evt)
    {
        m_hero->swapCircle();
        Vec2 pos = m_hero->getCurMovingNode()->getPosition();
        if( shoudMoveCameraPos(pos) )
            m_camera->runAction(MoveTo::create(0.3,pos));
//        m_hero->changeState();
        return true;
    };
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listenner,this);


    Sprite* bg = Sprite::create("res/bg_1.png");
    addChild(bg,-1);
    bg->setPosition(winSize/2);
    bg->setScale(2,3);

    auto edge = Node::create();
    edge->setPosition(winSize/2);
    PhysicsBody* edgeBody = PhysicsBody::createEdgeBox(winSize*1.3,PhysicsMaterial(PHYSICS_INFINITY,0,0),10,Vec2(0,0));
    edge->setPhysicsBody(edgeBody);
    edgeBody->setDynamic(true);
    addChild(edge);
    edgeBody->setCategoryBitmask(0xFFFFFFFF);
    edgeBody->setCollisionBitmask(0xFFFFFFFF);
    edgeBody->setContactTestBitmask(0X00001000);
    EventListenerPhysicsContact* physicsListenner = EventListenerPhysicsContact::create();
    physicsListenner->onContactBegin=[=](PhysicsContact& contact)
    {
        //英雄和墙发生碰撞
        if ((contact.getShapeA()->getContactTestBitmask()|contact.getShapeB()->getContactTestBitmask()) == 0x11001000)
        {
            m_hero->reverse();
        }
        //英雄和食物发生碰撞
        else if ((contact.getShapeA()->getContactTestBitmask()|contact.getShapeB()->getContactTestBitmask()) == 0x11000100)
        {
            exchangeSide();
            if ((contact.getShapeA()->getContactTestBitmask()) == 0x00000100)
            {
                contact.getShapeA()->getBody()->getNode()->setVisible(false);
            }
            else if((contact.getShapeB()->getContactTestBitmask()) == 0x00000100)
            {
                contact.getShapeB()->getBody()->getNode()->setVisible(false);
            }
        }
        //英雄和怪物发生碰撞
        else if ((contact.getShapeA()->getContactTestBitmask()|contact.getShapeB()->getContactTestBitmask())== 0x11000110)
        {
            Vec2 contactPos = contact.getContactData()->points[0];
            ParticleSystemQuad* quad = ParticleSystemQuad::create("res/effect/die.plist");
            quad->setPosition(contactPos);
            ParticleBatchNode* batch = ParticleBatchNode::createWithTexture(quad->getTexture());
            batch->addChild(quad);
            addChild(batch);
            auto removeFunc = CallFunc::create([=](){
                batch->removeFromParent();
            });
            Sequence* sequ = Sequence::create(DelayTime::create(0.8f),removeFunc,NULL);
            runAction(sequ);
        }
        return true;
    };
    physicsListenner->onContactPreSolve = [=](PhysicsContact& contact,PhysicsContactPreSolve& preSlove)
    {
        CCLOG("PreSlove");
        return false;
    };
    physicsListenner->onContactPostSolve = [=](PhysicsContact& contact,const PhysicsContactPostSolve& postSlove)
    {
        CCLOG("PostSlove");
    };
    physicsListenner->onContactSeperate = [=](PhysicsContact& contact)
    {
        if ((contact.getShapeA()->getContactTestBitmask()) == 0x00000100)
        {
            contact.getShapeA()->getBody()->getNode()->removeFromParent();
        }
        else if((contact.getShapeB()->getContactTestBitmask()) == 0x00000100)
        {
            contact.getShapeB()->getBody()->getNode()->removeFromParent();
        }
    };
    _eventDispatcher->addEventListenerWithSceneGraphPriority(physicsListenner,this);
    m_camera = Camera::create();
    m_camera->setPosition(winSize/2);
    addChild(m_camera);

    Enhance* en =Enhance::create();
    en->setPosition(Vec2(100,100));
    addChild(en);

    return true;
}