CCPUAffectorTranslator.cpp 7.5 KB

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