CCPUAffectorTranslator.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /****************************************************************************
  2. Copyright (C) 2013 Henry van Merode. All rights reserved.
  3. Copyright (c) 2015-2017 Chukong Technologies Inc.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "CCPUAffectorTranslator.h"
  22. #include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
  23. #include "extensions/Particle3D/PU/CCPUAffectorManager.h"
  24. NS_CC_BEGIN
  25. PUAffectorTranslator::PUAffectorTranslator()
  26. :_affector(nullptr)
  27. {
  28. }
  29. //-------------------------------------------------------------------------
  30. void PUAffectorTranslator::translate(PUScriptCompiler* compiler, PUAbstractNode *node)
  31. {
  32. PUObjectAbstractNode* obj = reinterpret_cast<PUObjectAbstractNode*>(node);
  33. PUObjectAbstractNode* parent = obj->parent ? reinterpret_cast<PUObjectAbstractNode*>(obj->parent) : 0;
  34. // The name of the obj is the type of the affector
  35. // Remark: This can be solved by using a listener, so that obj->values is filled with type + name. Something for later
  36. std::string type;
  37. if(!obj->name.empty())
  38. {
  39. type = obj->name;
  40. }
  41. //// Get the factory
  42. //ParticleAffectorFactory* particleAffectorFactory = ParticleSystemManager::getSingletonPtr()->getAffectorFactory(type);
  43. //if (!particleAffectorFactory)
  44. //{
  45. // compiler->addError(ScriptCompiler::CE_INVALIDPARAMETERS, obj->file, obj->line);
  46. // return;
  47. //}
  48. PUScriptTranslator *particleAffectorTranlator = PUAffectorManager::Instance()->getTranslator(type);
  49. if (!particleAffectorTranlator) return;
  50. //// Create the affector
  51. //mAffector = ParticleSystemManager::getSingletonPtr()->createAffector(type);
  52. //if (!mAffector)
  53. //{
  54. // compiler->addError(ScriptCompiler::CE_INVALIDPARAMETERS, obj->file, obj->line);
  55. // return;
  56. //}
  57. _affector = PUAffectorManager::Instance()->createAffector(type);
  58. if (!_affector) return;
  59. _affector->setAffectorType(type);
  60. if (parent && parent->context)
  61. {
  62. PUParticleSystem3D* system = static_cast<PUParticleSystem3D*>(parent->context);
  63. system->addAffector(_affector);
  64. }
  65. // The first value is the (optional) name
  66. std::string name;
  67. if(!obj->values.empty())
  68. {
  69. getString(*obj->values.front(), &name);
  70. _affector->setName(name);
  71. }
  72. // Set it in the context
  73. obj->context = _affector;
  74. // Run through properties
  75. for(PUAbstractNodeList::iterator i = obj->children.begin(); i != obj->children.end(); ++i)
  76. {
  77. if((*i)->type == ANT_PROPERTY)
  78. {
  79. PUPropertyAbstractNode* prop = reinterpret_cast<PUPropertyAbstractNode*>((*i));
  80. if (prop->name == token[TOKEN_ENABLED])
  81. {
  82. // Property: enabled
  83. if (passValidateProperty(compiler, prop, token[TOKEN_ENABLED], VAL_BOOL))
  84. {
  85. bool val;
  86. if(getBoolean(*prop->values.front(), &val))
  87. {
  88. _affector->setEnabled(val);
  89. }
  90. }
  91. }
  92. else if (prop->name == token[TOKEN_POSITION])
  93. {
  94. // Property: position
  95. if (passValidateProperty(compiler, prop, token[TOKEN_POSITION], VAL_VECTOR3))
  96. {
  97. Vec3 val;
  98. if(getVector3(prop->values.begin(), prop->values.end(), &val))
  99. {
  100. //mAffector->position = val;
  101. //mAffector->originalPosition = val;
  102. _affector->setLocalPosition(val);
  103. }
  104. }
  105. }
  106. else if (prop->name == token[TOKEN_AFFECTOR_MASS])
  107. {
  108. if (passValidateProperty(compiler, prop, token[TOKEN_AFFECTOR_MASS], VAL_REAL))
  109. {
  110. float val = 0.0f;
  111. if(getFloat(*prop->values.front(), &val))
  112. {
  113. _affector->setMass(val);
  114. }
  115. }
  116. }
  117. else if (prop->name == token[TOKEN_AFFECTOR_SPECIALISATION])
  118. {
  119. if (passValidateProperty(compiler, prop, token[TOKEN_AFFECTOR_SPECIALISATION], VAL_STRING))
  120. {
  121. std::string val;
  122. if(getString(*prop->values.front(), &val))
  123. {
  124. if (val == token[TOKEN_AFFECTOR_SPEC_DEFAULT])
  125. {
  126. _affector->setAffectSpecialisation(PUAffector::AFSP_DEFAULT);
  127. }
  128. else if (val == token[TOKEN_AFFECTOR_SPEC_TTL_INCREASE])
  129. {
  130. _affector->setAffectSpecialisation(PUAffector::AFSP_TTL_INCREASE);
  131. }
  132. else if (val == token[TOKEN_AFFECTOR_SPEC_TTL_DECREASE])
  133. {
  134. _affector->setAffectSpecialisation(PUAffector::AFSP_TTL_DECREASE);
  135. }
  136. }
  137. }
  138. }
  139. else if (prop->name == token[TOKEN_AFFECTOR_EXCLUDE_EMITTER])
  140. {
  141. if (passValidatePropertyNoValues(compiler, prop, token[TOKEN_AFFECTOR_EXCLUDE_EMITTER]))
  142. {
  143. for(PUAbstractNodeList::iterator j = prop->values.begin(); j != prop->values.end(); ++j)
  144. {
  145. std::string val;
  146. if(getString(**j, &val))
  147. {
  148. _affector->addEmitterToExclude(val);
  149. }
  150. }
  151. }
  152. }
  153. else if (particleAffectorTranlator->translateChildProperty(compiler, *i))
  154. {
  155. // Parsed the property by another translator; do nothing
  156. }
  157. else
  158. {
  159. errorUnexpectedProperty(compiler, prop);
  160. }
  161. }
  162. else if((*i)->type == ANT_OBJECT)
  163. {
  164. if (particleAffectorTranlator->translateChildObject(compiler, *i))
  165. {
  166. // Parsed the object by another translator; do nothing
  167. }
  168. else
  169. {
  170. processNode(compiler, *i);
  171. }
  172. }
  173. else
  174. {
  175. errorUnexpectedToken(compiler, *i);
  176. }
  177. }
  178. }
  179. NS_CC_END