CCPUDynamicAttributeTranslator.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 "CCPUDynamicAttributeTranslator.h"
  23. #include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
  24. NS_CC_BEGIN
  25. PUDynamicAttributeTranslator::PUDynamicAttributeTranslator()
  26. {
  27. }
  28. PUDynamicAttributeTranslator::~PUDynamicAttributeTranslator()
  29. {
  30. }
  31. void PUDynamicAttributeTranslator::translate(PUScriptCompiler* compiler, PUAbstractNode *node)
  32. {
  33. PUObjectAbstractNode* obj = reinterpret_cast<PUObjectAbstractNode*>(node);
  34. // The first value is the type
  35. std::string type = obj->name;
  36. if (type == token[TOKEN_DYN_RANDOM])
  37. {
  38. _dynamicAttribute = new (std::nothrow) PUDynamicAttributeRandom();
  39. }
  40. else if (type == token[TOKEN_DYN_CURVED_LINEAR])
  41. {
  42. _dynamicAttribute = new (std::nothrow) PUDynamicAttributeCurved();
  43. }
  44. else if (type == token[TOKEN_DYN_CURVED_SPLINE])
  45. {
  46. _dynamicAttribute = new (std::nothrow) PUDynamicAttributeCurved();
  47. }
  48. else if (type == token[TOKEN_DYN_OSCILLATE])
  49. {
  50. _dynamicAttribute = new (std::nothrow) PUDynamicAttributeOscillate();
  51. }
  52. else
  53. {
  54. // Create a fixed one.
  55. _dynamicAttribute = new (std::nothrow) PUDynamicAttributeFixed();
  56. }
  57. // Run through properties
  58. for(PUAbstractNodeList::iterator i = obj->children.begin(); i != obj->children.end(); ++i)
  59. {
  60. if((*i)->type == ANT_PROPERTY)
  61. {
  62. PUPropertyAbstractNode* prop = reinterpret_cast<PUPropertyAbstractNode*>((*i));
  63. if (prop->name == token[TOKEN_DYN_MIN])
  64. {
  65. // Property: min
  66. if (_dynamicAttribute->getType() == PUDynamicAttribute::DAT_RANDOM)
  67. {
  68. if (passValidateProperty(compiler, prop, token[TOKEN_DYN_MIN], VAL_REAL))
  69. {
  70. float val = 0.0f;
  71. if(getFloat(*prop->values.front(), &val))
  72. {
  73. (static_cast<PUDynamicAttributeRandom*>(_dynamicAttribute))->setMin(val);
  74. }
  75. }
  76. }
  77. }
  78. else if (prop->name == token[TOKEN_DYN_MAX])
  79. {
  80. // Property: max
  81. if (_dynamicAttribute->getType() == PUDynamicAttribute::DAT_RANDOM)
  82. {
  83. if (passValidateProperty(compiler, prop, token[TOKEN_DYN_MAX], VAL_REAL))
  84. {
  85. float val = 0.0f;
  86. if(getFloat(*prop->values.front(), &val))
  87. {
  88. (static_cast<PUDynamicAttributeRandom*>(_dynamicAttribute))->setMax(val);
  89. }
  90. }
  91. }
  92. }
  93. else if (prop->name == token[TOKEN_DYN_CONTROL_POINT])
  94. {
  95. // Property: control_point
  96. if (_dynamicAttribute->getType() == PUDynamicAttribute::DAT_CURVED)
  97. {
  98. if (passValidateProperty(compiler, prop, token[TOKEN_DYN_CONTROL_POINT], VAL_VECTOR2))
  99. {
  100. Vec2 val;
  101. if(getVector2(prop->values.begin(), prop->values.end(), &val))
  102. {
  103. (static_cast<PUDynamicAttributeCurved*>(_dynamicAttribute))->addControlPoint(val.x, val.y);
  104. }
  105. }
  106. }
  107. }
  108. else if (prop->name == token[TOKEN_DYN_OSCILLATE_FREQUENCY])
  109. {
  110. // Property: oscillate_frequency
  111. if (_dynamicAttribute->getType() == PUDynamicAttribute::DAT_OSCILLATE)
  112. {
  113. if (passValidateProperty(compiler, prop, token[TOKEN_DYN_OSCILLATE_FREQUENCY], VAL_REAL))
  114. {
  115. float val = 0.0f;
  116. if(getFloat(*prop->values.front(), &val))
  117. {
  118. (static_cast<PUDynamicAttributeOscillate*>(_dynamicAttribute))->setFrequency(val);
  119. }
  120. }
  121. }
  122. }
  123. else if (prop->name == token[TOKEN_DYN_OSCILLATE_PHASE])
  124. {
  125. // Property: oscillate_phase
  126. if (_dynamicAttribute->getType() == PUDynamicAttribute::DAT_OSCILLATE)
  127. {
  128. if (passValidateProperty(compiler, prop, token[TOKEN_DYN_OSCILLATE_PHASE], VAL_REAL))
  129. {
  130. float val = 0.0f;
  131. if(getFloat(*prop->values.front(), &val))
  132. {
  133. (static_cast<PUDynamicAttributeOscillate*>(_dynamicAttribute))->setPhase(val);
  134. }
  135. }
  136. }
  137. }
  138. else if (prop->name == token[TOKEN_DYN_OSCILLATE_BASE])
  139. {
  140. // Property: oscillate_base
  141. if (_dynamicAttribute->getType() == PUDynamicAttribute::DAT_OSCILLATE)
  142. {
  143. if (passValidateProperty(compiler, prop, token[TOKEN_DYN_OSCILLATE_BASE], VAL_REAL))
  144. {
  145. float val = 0.0f;
  146. if(getFloat(*prop->values.front(), &val))
  147. {
  148. (static_cast<PUDynamicAttributeOscillate*>(_dynamicAttribute))->setBase(val);
  149. }
  150. }
  151. }
  152. }
  153. else if (prop->name == token[TOKEN_DYN_OSCILLATE_AMPLITUDE])
  154. {
  155. // Property: oscillate_amplitude
  156. if (_dynamicAttribute->getType() == PUDynamicAttribute::DAT_OSCILLATE)
  157. {
  158. if (passValidateProperty(compiler, prop, token[TOKEN_DYN_OSCILLATE_AMPLITUDE], VAL_REAL))
  159. {
  160. float val = 0.0f;
  161. if(getFloat(*prop->values.front(), &val))
  162. {
  163. (static_cast<PUDynamicAttributeOscillate*>(_dynamicAttribute))->setAmplitude(val);
  164. }
  165. }
  166. }
  167. }
  168. else if (prop->name == token[TOKEN_DYN_OSCILLATE_TYPE])
  169. {
  170. // Property: oscillate_type
  171. if (_dynamicAttribute->getType() == PUDynamicAttribute::DAT_OSCILLATE)
  172. {
  173. if (passValidateProperty(compiler, prop, token[TOKEN_DYN_OSCILLATE_TYPE], VAL_STRING))
  174. {
  175. std::string val;
  176. if(getString(*prop->values.front(), &val))
  177. {
  178. if (val == token[TOKEN_DYN_SINE])
  179. {
  180. (static_cast<PUDynamicAttributeOscillate*>(_dynamicAttribute))->setOscillationType(
  181. PUDynamicAttributeOscillate::OSCT_SINE);
  182. }
  183. else if (val == token[TOKEN_DYN_SQUARE])
  184. {
  185. (static_cast<PUDynamicAttributeOscillate*>(_dynamicAttribute))->setOscillationType(
  186. PUDynamicAttributeOscillate::OSCT_SQUARE);
  187. }
  188. }
  189. }
  190. }
  191. }
  192. else
  193. {
  194. errorUnexpectedProperty(compiler, prop);
  195. }
  196. }
  197. else if((*i)->type == ANT_OBJECT)
  198. {
  199. processNode(compiler, *i);
  200. }
  201. else
  202. {
  203. errorUnexpectedToken(compiler, *i);
  204. }
  205. }
  206. // Set it in the context
  207. obj->context = _dynamicAttribute;
  208. }
  209. NS_CC_END