Advertisement
firelite

movement.cpp

May 16th, 2013
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. // RpgMakerPlugin
  3. // Movement.cpp
  4. //
  5. // Code : Giuseppe Alfieri (thanks Anael Seghezzi)
  6. //
  7. //
  8. /////////////////////////////////////////////////////////////////////////////////////////////////////////
  9.  
  10. #include "Movement.h"
  11.  
  12.  
  13. /////////////////////////////////////////////////////////////////////////////////////////////////////////
  14. // Init, this part is always similar, constructor, copy constructor etc
  15. /////////////////////////////////////////////////////////////////////////////////////////////////////////
  16.  
  17. // constructor
  18. Movement::Movement(MObject3d * parentObject):
  19. MBehavior(parentObject),
  20. animated(1),
  21. m_rotationSpeed(1),
  22. autoRotate(0),
  23. m_speed(0),
  24. m_flySpeed(0),
  25. m_pattern(),
  26. cycle(0)
  27. {}
  28.  
  29. // copy constructor
  30. Movement::Movement(Movement & behavior, MObject3d * parentObject):
  31. MBehavior(parentObject),
  32. animated(behavior.animated),
  33. m_rotationSpeed(behavior.m_rotationSpeed),
  34. autoRotate(0),
  35. m_speed(behavior.m_speed),
  36. m_flySpeed(behavior.m_flySpeed),
  37. m_pattern(behavior.m_pattern),
  38. cycle(behavior.cycle)
  39. {}
  40.  
  41. // destructor
  42. Movement::~Movement(void)
  43. {}
  44.  
  45. // destroy function : always similar
  46. void Movement::destroy(void)
  47. {
  48.     delete this;
  49. }
  50.  
  51. // getNew function : always similar
  52. MBehavior * Movement::getNew(MObject3d * parentObject)
  53. {
  54.     return new Movement(parentObject);
  55. }
  56.  
  57. // getCopy function : always similar
  58. MBehavior * Movement::getCopy(MObject3d * parentObject)
  59. {
  60.     return new Movement(*this, parentObject);
  61. }
  62.  
  63. /////////////////////////////////////////////////////////////////////////////////////////////////////////
  64. // Variables, allow to access custom variable from script and from Maratis Editor
  65. /////////////////////////////////////////////////////////////////////////////////////////////////////////
  66. int i=0;
  67. unsigned int Movement::getVariablesNumber(void){
  68.     return 7;
  69. }
  70.  
  71. MVariable Movement::getVariable(unsigned int id)
  72. {
  73.     switch(id)
  74.     {
  75.     default:
  76.         return MVariable("NULL", NULL, M_VARIABLE_NULL);
  77.     case 0:
  78.         return MVariable("animated", &animated, M_VARIABLE_BOOL);
  79.     case 1:
  80.         return MVariable("rotationSpeed", &m_rotationSpeed, M_VARIABLE_FLOAT);
  81.     case 2:
  82.         return MVariable("autoRotate", &autoRotate, M_VARIABLE_BOOL);
  83.     case 3:
  84.         return MVariable("movementSpeed", &m_speed, M_VARIABLE_FLOAT);
  85.     case 4:
  86.         return MVariable("flySpeed", &m_flySpeed, M_VARIABLE_FLOAT);
  87.     case 5:
  88.         return MVariable("movementPattern", &m_pattern, M_VARIABLE_STRING);
  89.     case 6:
  90.         return MVariable("CycleMovement", &cycle, M_VARIABLE_BOOL);
  91.  
  92.     }
  93. }
  94.  
  95.  
  96. /////////////////////////////////////////////////////////////////////////////////////////////////////////
  97. // Events
  98. /////////////////////////////////////////////////////////////////////////////////////////////////////////
  99.  
  100. // update function (called by MGame class by default)
  101. void Movement::update(void)
  102. {
  103.     MEngine * engine = MEngine::getInstance();
  104.     MGame * game = engine->getGame();
  105.  
  106.     // check if the game is running, removing thif test will enable In-Editor update (like for the lookAt behavior)
  107.     if(! game->isRunning())
  108.         return;
  109.  
  110.     // get the associated parent object (who is using this behavior)
  111.     MObject3d * parent = getParentObject();
  112.  
  113.     // lets rotate the parent object around the z axis, using our custom "rotationSpeed" variable
  114.     if(animated){
  115.     if(autoRotate)
  116.     parent->addAxisAngleRotation(MVector3(0, 0, 1), m_rotationSpeed);
  117.     if(i<strlen(m_pattern)){
  118.     // a pattern string of wasd moves the object using rotationSpeed and moveSpeed
  119.     if(m_pattern[i]=='w'){
  120.     MVector3 axis = parent->getRotatedVector(MVector3(0,m_speed,0));
  121.     parent->setPosition(parent->getPosition() + axis);
  122.     }
  123.     if(m_pattern[i]=='a'){
  124.         parent->addAxisAngleRotation(parent->getInverseRotatedVector(MVector3(0, 0, 0.5)), m_rotationSpeed);
  125.     }
  126.     if(m_pattern[i]=='d'){
  127.         parent->addAxisAngleRotation(parent->getInverseRotatedVector(MVector3(0, 0, 0.5)), -m_rotationSpeed);
  128.     }
  129.     if(m_pattern[i]=='s'){
  130.         MVector3 axis = parent->getRotatedVector(MVector3(0,-m_speed,0));
  131.         parent->setPosition(parent->getPosition() + axis);
  132.     }
  133.     i++;
  134.     }
  135.     else if(cycle)
  136.         i=0;
  137.    
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement