firelite

Movement.cpp

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