axmol/extensions/Particle3D/ParticleUniverse/ParticleAffectors/CCPUParticle3DAffectorTrans...

196 lines
7.8 KiB
C++
Raw Normal View History

2015-02-11 18:14:22 +08:00
/****************************************************************************
2015-02-12 16:09:08 +08:00
Copyright (c) 2015 Chukong Technologies Inc.
2015-02-11 18:14:22 +08:00
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "CCPUParticle3DAffectorTranslator.h"
2015-02-13 16:57:00 +08:00
#include "extensions/Particle3D/ParticleUniverse/CCPUParticleSystem3D.h"
#include "extensions/Particle3D/ParticleUniverse/ParticleAffectors/CCPUParticle3DAffectorManager.h"
2015-02-11 18:14:22 +08:00
NS_CC_BEGIN
PUParticle3DAffectorTranslator::PUParticle3DAffectorTranslator()
:_affector(nullptr)
{
}
//-------------------------------------------------------------------------
void PUParticle3DAffectorTranslator::translate(PUScriptCompiler* compiler, PUAbstractNode *node)
{
PUObjectAbstractNode* obj = reinterpret_cast<PUObjectAbstractNode*>(node);
PUObjectAbstractNode* parent = obj->parent ? reinterpret_cast<PUObjectAbstractNode*>(obj->parent) : 0;
// The name of the obj is the type of the affector
// Remark: This can be solved by using a listener, so that obj->values is filled with type + name. Something for later
std::string type;
if(!obj->name.empty())
{
type = obj->name;
}
//// Get the factory
//ParticleAffectorFactory* particleAffectorFactory = ParticleSystemManager::getSingletonPtr()->getAffectorFactory(type);
//if (!particleAffectorFactory)
//{
// compiler->addError(ScriptCompiler::CE_INVALIDPARAMETERS, obj->file, obj->line);
// return;
//}
PUScriptTranslator *particleAffectorTranlator = PUParticle3DAffectorManager::Instance()->getTranslator(type);
if (!particleAffectorTranlator) return;
//// Create the affector
//mAffector = ParticleSystemManager::getSingletonPtr()->createAffector(type);
//if (!mAffector)
//{
// compiler->addError(ScriptCompiler::CE_INVALIDPARAMETERS, obj->file, obj->line);
// return;
//}
_affector = PUParticle3DAffectorManager::Instance()->createAffector(type);
if (!_affector) return;
if (parent && parent->context)
{
PUParticleSystem3D* system = static_cast<PUParticleSystem3D*>(parent->context);
system->addAffector(_affector);
}
// The first value is the (optional) name
std::string name;
if(!obj->values.empty())
{
getString(*obj->values.front(), &name);
_affector->setName(name);
}
// Set it in the context
obj->context = _affector;
// Run through properties
for(PUAbstractNodeList::iterator i = obj->children.begin(); i != obj->children.end(); ++i)
{
if((*i)->type == ANT_PROPERTY)
{
PUPropertyAbstractNode* prop = reinterpret_cast<PUPropertyAbstractNode*>((*i));
if (prop->name == token[TOKEN_ENABLED])
{
// Property: enabled
if (passValidateProperty(compiler, prop, token[TOKEN_ENABLED], VAL_BOOL))
{
bool val;
if(getBoolean(*prop->values.front(), &val))
{
_affector->setEnabled(val);
}
}
}
else if (prop->name == token[TOKEN_POSITION])
{
// Property: positon
if (passValidateProperty(compiler, prop, token[TOKEN_POSITION], VAL_VECTOR3))
{
Vec3 val;
if(getVector3(prop->values.begin(), prop->values.end(), &val))
{
//mAffector->position = val;
//mAffector->originalPosition = val;
_affector->setLocalPosition(val);
}
}
}
else if (prop->name == token[TOKEN_AFFECTOR_MASS])
{
if (passValidateProperty(compiler, prop, token[TOKEN_AFFECTOR_MASS], VAL_REAL))
{
float val = 0.0f;
if(getFloat(*prop->values.front(), &val))
{
_affector->setMass(val);
}
}
}
else if (prop->name == token[TOKEN_AFFECTOR_SPECIALISATION])
{
if (passValidateProperty(compiler, prop, token[TOKEN_AFFECTOR_SPECIALISATION], VAL_STRING))
{
std::string val;
if(getString(*prop->values.front(), &val))
{
if (val == token[TOKEN_AFFECTOR_SPEC_DEFAULT])
{
_affector->setAffectSpecialisation(PUParticle3DAffector::AFSP_DEFAULT);
}
else if (val == token[TOKEN_AFFECTOR_SPEC_TTL_INCREASE])
{
_affector->setAffectSpecialisation(PUParticle3DAffector::AFSP_TTL_INCREASE);
}
else if (val == token[TOKEN_AFFECTOR_SPEC_TTL_DECREASE])
{
_affector->setAffectSpecialisation(PUParticle3DAffector::AFSP_TTL_DECREASE);
}
}
}
}
else if (prop->name == token[TOKEN_AFFECTOR_EXCLUDE_EMITTER])
{
//if (passValidatePropertyNoValues(compiler, prop, token[TOKEN_AFFECTOR_EXCLUDE_EMITTER]))
//{
// for(AbstractNodeList::iterator j = prop->values.begin(); j != prop->values.end(); ++j)
// {
// String val;
// if(getString(*j, &val))
// {
// mAffector->addEmitterToExclude(val);
// }
// else
// {
// compiler->addError(ScriptCompiler::CE_NUMBEREXPECTED, prop->file, prop->line,
// "PU Compiler: Error in exclude_emitter arguments");
// }
// }
//}
}
else if (particleAffectorTranlator->translateChildProperty(compiler, *i))
{
// Parsed the property by another translator; do nothing
}
else
{
errorUnexpectedProperty(compiler, prop);
}
}
else if((*i)->type == ANT_OBJECT)
{
if (particleAffectorTranlator->translateChildObject(compiler, *i))
{
// Parsed the object by another translator; do nothing
}
else
{
processNode(compiler, *i);
}
}
else
{
errorUnexpectedToken(compiler, *i);
}
}
}
NS_CC_END