CCPUDynamicAttributeTranslator.cpp 8.9 KB

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