CCPUBehaviourTranslator.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 "CCPUBehaviourTranslator.h"
  22. #include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
  23. #include "extensions/Particle3D/PU/CCPUBehaviourManager.h"
  24. NS_CC_BEGIN
  25. PUBehaviourTranslator::PUBehaviourTranslator()
  26. :_behaviour(nullptr)
  27. {
  28. }
  29. //-------------------------------------------------------------------------
  30. void PUBehaviourTranslator::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 Behaviour
  35. std::string type;
  36. if(!obj->name.empty())
  37. {
  38. type = obj->name;
  39. }
  40. else
  41. {
  42. //compiler->addError(ScriptCompiler::CE_INVALIDPARAMETERS, obj->file, obj->line);
  43. return;
  44. }
  45. //// Get the factory
  46. //ParticleBehaviourFactory* behaviourFactory = ParticleSystemManager::getSingletonPtr()->getBehaviourFactory(type);
  47. //if (!behaviourFactory)
  48. //{
  49. // //compiler->addError(ScriptCompiler::CE_INVALIDPARAMETERS, obj->file, obj->line);
  50. // return;
  51. //}
  52. PUScriptTranslator *particleBehaviourTranlator = PUBehaviourManager::Instance()->getTranslator(type);
  53. if (!particleBehaviourTranlator) return;
  54. // Create the Behaviour
  55. _behaviour = PUBehaviourManager::Instance()->createBehaviour(type);
  56. if (!_behaviour)
  57. {
  58. //compiler->addError(ScriptCompiler::CE_INVALIDPARAMETERS, obj->file, obj->line);
  59. return;
  60. }
  61. _behaviour->setBehaviourType(type);
  62. if (parent && parent->context)
  63. {
  64. PUParticleSystem3D* system = static_cast<PUParticleSystem3D*>(parent->context);
  65. system->addBehaviourTemplate(_behaviour);
  66. }
  67. else
  68. {
  69. //// It is an alias
  70. //_behaviour->setAliasName(parent->name);
  71. //ParticleSystemManager::getSingletonPtr()->addAlias(mBehaviour);
  72. }
  73. // Set it in the context
  74. obj->context = _behaviour;
  75. // Run through properties
  76. for(PUAbstractNodeList::iterator i = obj->children.begin(); i != obj->children.end(); ++i)
  77. {
  78. // No properties of its own
  79. if((*i)->type == ANT_PROPERTY)
  80. {
  81. PUPropertyAbstractNode* prop = reinterpret_cast<PUPropertyAbstractNode*>((*i));
  82. if (particleBehaviourTranlator->translateChildProperty(compiler, *i))
  83. {
  84. // Parsed the property by another translator; do nothing
  85. }
  86. else
  87. {
  88. errorUnexpectedProperty(compiler, prop);
  89. }
  90. }
  91. else if((*i)->type == ANT_OBJECT)
  92. {
  93. if (particleBehaviourTranlator->translateChildObject(compiler, *i))
  94. {
  95. // Parsed the object by another translator; do nothing
  96. }
  97. else
  98. {
  99. processNode(compiler, *i);
  100. }
  101. }
  102. else
  103. {
  104. errorUnexpectedToken(compiler, *i);
  105. }
  106. }
  107. }
  108. NS_CC_END