Component* ComAudioReader::createComAudioWithFlatBuffers(const flatbuffers::Table *comAudioOptions)
 {
     auto options = (ComAudioOptions*)comAudioOptions;
     
     Component* component = ComAudio::create();
     ComAudio* audio = static_cast<ComAudio*>(component);
     
     auto fileNameData = options->fileNameData();
     
     int resourceType = fileNameData->resourceType();
     switch (resourceType)
     {
         case 0:
         {
             std::string path = fileNameData->path()->c_str();
             audio->setFile(path.c_str());
             break;
         }
             
         default:
             break;
     }
     
     bool loop = options->loop() != 0;
     audio->setLoop(loop);
     
     audio->setName(options->name()->c_str());
     
     return component;
 }
void BulletController::onAdd()
{
    ComController::onAdd();

    ComAudio* pSceneAudio = static_cast<ComAudio*>(_owner->getComponent("CCComAudio"));
    if (pSceneAudio)
    {
        pSceneAudio->playEffect("res/sound/pew-pew-lei.wav");
    }
    
    LayerMainGame* pMainGameLyr = dynamic_cast<LayerMainGame*>(_owner->getParent());
    if (pMainGameLyr == nullptr)
    {
        return;
    }
    Vec2 playerPosition = pMainGameLyr->getPlayerSprite()->getPosition();
    Size winSize = Director::getInstance()->getWinSize();
    
    Vec2 touchDir = m_ptTouch-playerPosition;
    
    _owner->setPosition(playerPosition);
    float targetDis = fabs(touchDir.length() * winSize.width / (m_ptTouch.x - playerPosition.x));
    touchDir.normalize();
    Vec2 ptTarget = playerPosition + targetDis*touchDir;
    
    float velocity = 480/1; // 480pixels/1sec
    float realMoveDuration = targetDis/velocity;
    ActionInterval* pMoveAction = MoveTo::create(realMoveDuration, ptTarget);
    _owner->runAction(Sequence::create(pMoveAction, RemoveSelf::create(), nullptr));
}
Example #3
0
void SceneEditorTestLayer::toExtensionsMainLayer(cocos2d::Object *sender)
{
    ComAudio *pBackMusic = (ComAudio*)(_curNode->getComponent("CCBackgroundAudio"));
    pBackMusic->stopBackgroundMusic();
	ExtensionsTestScene *pScene = new ExtensionsTestScene();
	pScene->runThisTest();
	pScene->release();
}  
Example #4
0
cocos2d::Node* BackgroundComponentTest::createGameScene()
{
    Node *node = SceneReader::getInstance()->createNodeWithSceneFile("scenetest/BackgroundComponentTest/BackgroundComponentTest.json");
	if (node == nullptr)
	{
		return nullptr;
	}

	ComAudio *Audio = static_cast<ComAudio*>(node->getComponent("CCBackgroundAudio"));
	Audio->playBackgroundMusic();
    return node;
}
Example #5
0
ComAudio* ComAudio::create(void)
{
    ComAudio * pRet = new ComAudio();
    if (pRet && pRet->init())
    {
        pRet->autorelease();
    }
    else
    {
        CC_SAFE_DELETE(pRet);
    }
	return pRet;
}
Example #6
0
void EffectComponentTest::animationEvent(Armature *armature, MovementEventType movementType, const std::string& movementID)
{
	 std::string id = movementID;

	if (movementType == LOOP_COMPLETE)
	{
		if (id.compare("Fire") == 0)
		{
			ComAudio *pAudio = static_cast<ComAudio*>(_node->getChildByTag(10015)->getComponent("CCComAudio"));
			pAudio->playEffect();
		}
	}
}
Example #7
0
ComAudio* ComAudio::create()
{
    ComAudio * pRet = new (std::nothrow) ComAudio();
    if (pRet && pRet->init())
    {
        pRet->autorelease();
    }
    else
    {
        CC_SAFE_DELETE(pRet);
    }
    return pRet;
}
Example #8
0
Component* CSLoader::loadComAudio(const rapidjson::Value &json)
{
    ComAudio* audio = ComAudio::create();
    
    const char* name = DICTOOL->getStringValue_json(json, COMPONENT_NAME);
    bool enabled = DICTOOL->getBooleanValue_json(json, COMPONENT_ENABLED);
    
    audio->setName(name);
    audio->setEnabled(enabled);
    
    const char* filePath = DICTOOL->getStringValue_json(json, COMPONENT_AUDIO_FILE_PATH);
    bool loop = DICTOOL->getBooleanValue_json(json, COMPONENT_LOOP);
    
    audio->setFile(filePath);
    audio->setLoop(loop);
    
    
    return audio;
}
Example #9
0
void PlayMusic::done()
{
	do 
	{
		Node *node = SceneReader::getInstance()->getNodeByTag(_tag);
		CC_BREAK_IF(node == nullptr);
		ComAudio *audio = (ComAudio*)(node->getComponent(_comName.c_str()));
		CC_BREAK_IF(audio == nullptr);
		if (_type == 0)
		{
			audio->playBackgroundMusic();
		}
		else if (_type == 1)
		{
			audio->playEffect();
		}

	} while (0);
}
Example #10
0
void BackgroundComponentTest::defaultPlay()
{
	ComAudio *Audio = static_cast<ComAudio*>(_rootNode->getComponent("CCBackgroundAudio"));
	Audio->playBackgroundMusic();
}
Example #11
0
	Node* SceneReader::createObject(cs::JsonDictionary * inputFiles, Node* parenet, ISceneReaderListener* listener)
    {
        const char *className = inputFiles->getItemStringValue("classname"); 
        if(strcmp(className, "CCNode") == 0)
        {
            Node* gb = NULL;
            if(NULL == parenet)
            {
                gb = Node::create();
            }
            else
            {
                gb = Node::create();
                parenet->addChild(gb);
            }
            
            setPropertyFromJsonDict(gb, inputFiles);
    
            int count = inputFiles->getArrayItemCount("components");
            for (int i = 0; i < count; i++)
            {
                cs::JsonDictionary * subDict = inputFiles->getSubItemFromArray("components", i);
                if (!subDict)
				{
				   CC_SAFE_DELETE(subDict);
                   break;
				}
                const char *comName = subDict->getItemStringValue("classname");
				const char *pComName = subDict->getItemStringValue("name");
                
				cs::JsonDictionary *fileData = subDict->getSubDictionary("fileData");
				std::string pPath;
                std::string pPlistFile;
				int nResType = 0;
				if (fileData != NULL)
                {
					const char *file = fileData->getItemStringValue("path");
					nResType = fileData->getItemIntValue("resourceType", -1);
					const char *plistFile = fileData->getItemStringValue("plistFile");
					if (file != NULL)
					{
						pPath.append(cocos2d::FileUtils::getInstance()->fullPathForFilename(file));
					}

					if (plistFile != NULL)
					{
						pPlistFile.append(cocos2d::FileUtils::getInstance()->fullPathForFilename(plistFile));
					}
					CC_SAFE_DELETE(fileData);
                }

                if (comName != NULL && strcmp(comName, "CCSprite") == 0)
                {
					cocos2d::Sprite *pSprite = NULL;

					if (nResType == 0)
					{
						if (pPath.find(".png") == pPath.npos)
						{
							continue;
						}
						pSprite = Sprite::create(pPath.c_str());
					}
					else if (nResType == 1)
					{
						std::string pngFile = pPlistFile;
						std::string::size_type pos = pngFile.find(".plist");
						if (pos  == pPath.npos)
						{
							continue;
						}
						pngFile.replace(pos, pngFile.length(), ".png");
						CCSpriteFrameCache::getInstance()->addSpriteFramesWithFile(pPlistFile.c_str(), pngFile.c_str());
						pSprite = Sprite::createWithSpriteFrameName(pPath.c_str());
					}
					else
					{
						continue;
					}
					
                    ComRender *pRender = ComRender::create(pSprite, "CCSprite");
                    if (pComName != NULL)
                    {
                        pRender->setName(pComName);
                    }
                    
                    gb->addComponent(pRender);
                }
                else if(comName != NULL && strcmp(comName, "CCTMXTiledMap") == 0)
                {
					cocos2d::TMXTiledMap *pTmx = NULL;
					if (nResType == 0)
					{
						if (pPath.find(".tmx") == pPath.npos)
						{
							continue;
						}
						pTmx = TMXTiledMap::create(pPath.c_str());
					}
					else
					{
						continue;
					}

                    ComRender *pRender = ComRender::create(pTmx, "CCTMXTiledMap");
                    if (pComName != NULL)
                    {
                        pRender->setName(pComName);
                    }
                    gb->addComponent(pRender);
                }
                else if(comName != NULL && strcmp(comName, "CCParticleSystemQuad") == 0)
                {
                    std::string::size_type pos =  pPath.find(".plist");
                    if (pos  == pPath.npos)
                    {
                        continue;
                    }

					cocos2d::ParticleSystemQuad *pParticle = NULL;
					if (nResType == 0)
					{
						pParticle = ParticleSystemQuad::create(pPath.c_str());
					}
					else
					{
						CCLOG("unknown resourcetype on CCParticleSystemQuad!");
					}

					pParticle->setPosition(0, 0);
                    ComRender *pRender = ComRender::create(pParticle, "CCParticleSystemQuad");
                    if (pComName != NULL)
                    {
                        pRender->setName(pComName);
                    }
                    gb->addComponent(pRender);
                }
                else if(comName != NULL && strcmp(comName, "CCArmature") == 0)
                {
					if (nResType != 0)
					{
						continue;
					}
					std::string reDir = pPath;
					std::string file_path = "";
					size_t pos = reDir.find_last_of('/');
					if (pos != std::string::npos)
					{
						file_path = reDir.substr(0, pos+1);
					}
					unsigned long size = 0;
					const char *des = (char*)(cocos2d::FileUtils::getInstance()->getFileData(pPath.c_str(),"r" , &size));
					cs::JsonDictionary *jsonDict = new cs::JsonDictionary();
					jsonDict->initWithDescription(des);
					if(NULL == des || strcmp(des, "") == 0)
					{
						CCLOG("read json file[%s] error!\n", pPath.c_str());
					}

					int childrenCount = DICTOOL->getArrayCount_json(jsonDict, "armature_data");
					cs::JsonDictionary* subData = DICTOOL->getDictionaryFromArray_json(jsonDict, "armature_data", 0);
					const char *name = DICTOOL->getStringValue_json(subData, "name");

					childrenCount = DICTOOL->getArrayCount_json(jsonDict, "config_file_path");
					for (int i = 0; i < childrenCount; ++i)
					{
						const char* plist = DICTOOL->getStringValueFromArray_json(jsonDict, "config_file_path", i);
						std::string plistpath;
						plistpath += file_path;
						plistpath.append(plist);
						cocos2d::Dictionary *root = Dictionary::createWithContentsOfFile(plistpath.c_str());
						Dictionary* metadata = DICTOOL->getSubDictionary(root, "metadata");
						const char* textureFileName = DICTOOL->getStringValue(metadata, "textureFileName");

						std::string textupath;
						textupath += file_path;
						textupath.append(textureFileName);

						cocos2d::extension::armature::ArmatureDataManager::getInstance()->addArmatureFileInfo(textupath.c_str(), plistpath.c_str(), pPath.c_str());

					}

					cocos2d::extension::armature::Armature *pAr = cocos2d::extension::armature::Armature::create(name);
					ComRender *pRender = ComRender::create(pAr, "CCArmature");
					if (pComName != NULL)
					{
						pRender->setName(pComName);
					}
					gb->addComponent(pRender);

					const char *actionName = subDict->getItemStringValue("selectedactionname");
					if (actionName != NULL && pAr->getAnimation() != NULL)
					{
						pAr->getAnimation()->play(actionName);
					}

					CC_SAFE_DELETE(jsonDict);
					CC_SAFE_DELETE(subData);
					CC_SAFE_DELETE_ARRAY(des);
                }
                else if(comName != NULL && strcmp(comName, "CCComAudio") == 0)
                {
					ComAudio *pAudio = NULL;
					if (nResType == 0)
					{
						pAudio = ComAudio::create();
					}
					else
					{
						continue;
					}
                    pAudio->preloadEffect(pPath.c_str());
                    gb->addComponent(pAudio);
                }
                else if(comName != NULL && strcmp(comName, "CCComAttribute") == 0)
                {
                    ComAttribute *pAttribute = NULL;
					if (nResType == 0)
					{
						pAttribute = ComAttribute::create();
						unsigned long size = 0;
						const char* pData = 0;
						pData = (char*)(cocos2d::FileUtils::getInstance()->getFileData(pPath.c_str(), "r", &size));
						if(pData != NULL && strcmp(pData, "") != 0)
						{
							pAttribute->getDict()->initWithDescription(pData);
						}
					}
					else
					{
						CCLOG("unknown resourcetype on CCComAttribute!");
						continue;
					}
                    gb->addComponent(pAttribute);
                }
                else if (comName != NULL && strcmp(comName, "CCBackgroundAudio") == 0)
                {
					ComAudio *pAudio = NULL;
					if (nResType == 0)
					{
						pAudio = ComAudio::create();
					}
					else
					{
						continue;
					}
                    pAudio->preloadBackgroundMusic(pPath.c_str());
					pAudio->setFile(pPath.c_str());
					bool bLoop = subDict->getItemIntValue("loop", 0);
					pAudio->setLoop(bLoop);
                    gb->addComponent(pAudio);
					pAudio->playBackgroundMusic(pPath.c_str(), bLoop);
                }
				else if(comName != NULL && strcmp(comName, "GUIComponent") == 0)
				{
                    cocos2d::extension::UILayer *pLayer = cocos2d::extension::UILayer::create();
					pLayer->scheduleUpdate();
					UIWidget* widget = cocos2d::extension::UIHelper::instance()->createWidgetFromJsonFile(pPath.c_str(), listener);
					pLayer->addWidget(widget);
					ComRender *pRender = ComRender::create(pLayer, "GUIComponent");
					if (pComName != NULL)
					{
					pRender->setName(pComName);
					}
					gb->addComponent(pRender);
				}
                
                CC_SAFE_DELETE(subDict);
            }

            for (int i = 0; i < inputFiles->getArrayItemCount("gameobjects"); i++)
            {
                cs::JsonDictionary * subDict = inputFiles->getSubItemFromArray("gameobjects", i);
                if (!subDict)
                {
                    break;
                }
                createObject(subDict, gb,listener);
                CC_SAFE_DELETE(subDict);
            }
			if (listener)
				listener->OnNodeLoaded(gb);
            return gb;
        }
        
        return NULL;
    }