Topic: [PLUGIN] Movement Pattern Plugin

I'm trying to implement a plugin for moving objects. This plugin could make easier to manage object movement by simply writing a string with the specific movement an object could do. This is the idea, now I'm blocked... This is the code.



Movement.cpp

/////////////////////////////////////////////////////////////////////////////////////////////////////////
// SimpleGamePlugin
// Movement.cpp
// 
// Code : Anael Seghezzi edited by Giuseppe Alfieri
/////////////////////////////////////////////////////////////////////////////////////////////////////////

#include "Movement.h"


/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Init, this part is always similar, constructor, copy constructor etc
/////////////////////////////////////////////////////////////////////////////////////////////////////////
int patternLenght, i=0;

// constructor
Movement::Movement(MObject3d * parentObject):
MBehavior(parentObject),
animated(1),
m_rotationSpeed(1),
m_speed(0),
m_flySpeed(0),
m_pattern(""),
cycle(0)
{}

// copy constructor
Movement::Movement(Movement & behavior, MObject3d * parentObject):
MBehavior(parentObject),
animated(behavior.animated),
m_rotationSpeed(behavior.m_rotationSpeed),
m_speed(behavior.m_speed),
m_flySpeed(behavior.m_flySpeed),
m_pattern(behavior.m_pattern),
cycle(behavior.cycle)
{}

// destructor
Movement::~Movement(void)
{}

// destroy function : always similar
void Movement::destroy(void)
{
    delete this;
}

// getNew function : always similar
MBehavior * Movement::getNew(MObject3d * parentObject)
{
    return new Movement(parentObject);
}

// getCopy function : always similar
MBehavior * Movement::getCopy(MObject3d * parentObject)
{
    return new Movement(*this, parentObject);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Variables, allow to access custom variable from script and from Maratis Editor
/////////////////////////////////////////////////////////////////////////////////////////////////////////

unsigned int Movement::getVariablesNumber(void){
    return 6;
}

MVariable Movement::getVariable(unsigned int id)
{
    switch(id)
    {
    default:
        return MVariable("NULL", NULL, M_VARIABLE_NULL);
    case 0:
        return MVariable("animated", &animated, M_VARIABLE_BOOL);
    case 1:
        return MVariable("rotationSpeed", &m_rotationSpeed, M_VARIABLE_FLOAT);
    case 2:
        return MVariable("movementSpeed", &m_speed, M_VARIABLE_FLOAT);
    case 3:
        return MVariable("flySpeed", &m_flySpeed, M_VARIABLE_FLOAT);
    case 4:
        return MVariable("movementPattern", &m_pattern, M_VARIABLE_STRING);   
    case 5:
        return MVariable("CycleMovement", &cycle, M_VARIABLE_BOOL);

    }
}


/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Events
/////////////////////////////////////////////////////////////////////////////////////////////////////////

// update function (called by MGame class by default)
void Movement::update(void)
{
    MEngine * engine = MEngine::getInstance();
    MGame * game = engine->getGame();

    // check if the game is running, removing thif test will enable In-Editor update (like for the lookAt behavior)
    if(! game->isRunning())
        return;

    // get the associated parent object (who is using this behavior)
    MObject3d * parent = getParentObject();

    // lets rotate the parent object around the z axis, using our custom "rotationSpeed" variable
    if(animated){
    patternLenght=strlen(m_pattern);
    printf("%d", patternLenght);
    parent->addAxisAngleRotation(MVector3(0, 0, 1), m_rotationSpeed);
    if(i<patternLenght){
    // a pattern string of wasd moves the object using rotationSpeed and moveSpeed
    if(m_pattern[i]=='w'){
    MVector3 oldPosition=parent->getPosition();
    MVector3* trnPosition=new MVector3(0,m_speed,0);
    MVector3 newPosition=oldPosition + *trnPosition;
    parent->setPosition(newPosition);
    }
    if(m_pattern[i]=='a'){
        parent->addAxisAngleRotation(MVector3(0, 0, 1), m_rotationSpeed);
    }
    if(m_pattern[i]=='d'){
        parent->addAxisAngleRotation(MVector3(0, 0, -1), m_rotationSpeed);
    }
    if(m_pattern[i]=='s'){
        MVector3 oldPosition=parent->getPosition();
        MVector3* trnPosition=new MVector3(m_speed/2,0,0);
        MVector3 newPosition=oldPosition + *trnPosition;
        parent->setPosition(newPosition);
    }
    i++;
    }
    else if(cycle)
        i=0;
    
    }
}

Movement.h

/////////////////////////////////////////////////////////////////////////////////////////////////////////
// MovementGamePlugin
// Movement.h
// 
// Code : Anael Seghezzi edited by Giuseppe Alfieri
/////////////////////////////////////////////////////////////////////////////////////////////////////////

#ifndef _MY_BEHAVIOR_H
#define _MY_BEHAVIOR_H

#include <MEngine.h>


class Movement : public MBehavior
{
public:

    // constructors / destructors
    Movement(MObject3d * parentObject);
    Movement(Movement & behavior, MObject3d * parentObject);
    ~Movement(void);

private:
    
    // custom variables
    bool animated;
    float m_rotationSpeed;
    float m_speed;
    float m_flySpeed;
    char  *m_pattern;
    bool cycle;
    
public:

    // destroy
    void destroy(void);

    // get new
    static MBehavior * getNew(MObject3d * parentObject);

    // get copy
    MBehavior * getCopy(MObject3d * parentObject);

    // name
    static const char * getStaticName(void){ return "Movement"; }
    const char * getName(void){ return getStaticName(); }

    // variables
    unsigned int getVariablesNumber(void);
    MVariable getVariable(unsigned int id);

    // events (virtuals from MBehavior class)
    void update(void);
    void runEvent(int param){}
};

#endif

Re: [PLUGIN] Movement Pattern Plugin

done...

/////////////////////////////////////////////////////////////////////////////////////////////////////////
// MovementGamePlugin
// Movement.h
// 
// Code : Anael Seghezzi edited by Giuseppe Alfieri
/////////////////////////////////////////////////////////////////////////////////////////////////////////

#ifndef _MY_BEHAVIOR_H
#define _MY_BEHAVIOR_H

#include <MEngine.h>


class Movement : public MBehavior
{
public:

    // constructors / destructors
    Movement(MObject3d * parentObject);
    Movement(Movement & behavior, MObject3d * parentObject);
    ~Movement(void);

private:
    
    // custom variables
    bool animated;
    float m_rotationSpeed;
    bool autoRotate;
    float m_speed;
    float m_flySpeed;
    char  *m_pattern;
    bool cycle;
    
public:

    // destroy
    void destroy(void);

    // get new
    static MBehavior * getNew(MObject3d * parentObject);

    // get copy
    MBehavior * getCopy(MObject3d * parentObject);

    // name
    static const char * getStaticName(void){ return "Movement"; }
    const char * getName(void){ return getStaticName(); }

    // variables
    unsigned int getVariablesNumber(void);
    MVariable getVariable(unsigned int id);

    // events (virtuals from MBehavior class)
    void update(void);
    void runEvent(int param){}
};

#endif

Movement.cpp

/////////////////////////////////////////////////////////////////////////////////////////////////////////
// SimpleGamePlugin
// Movement.cpp
// 
// Code : Anael Seghezzi edited by Giuseppe Alfieri
/////////////////////////////////////////////////////////////////////////////////////////////////////////

#include "Movement.h"


/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Init, this part is always similar, constructor, copy constructor etc
/////////////////////////////////////////////////////////////////////////////////////////////////////////
int i=0;
//char tmp[128];
// constructor
Movement::Movement(MObject3d * parentObject):
MBehavior(parentObject),
animated(1),
m_rotationSpeed(1),
autoRotate(0),
m_speed(0),
m_flySpeed(0),
m_pattern(),
cycle(0)
{}

// copy constructor
Movement::Movement(Movement & behavior, MObject3d * parentObject):
MBehavior(parentObject),
animated(behavior.animated),
m_rotationSpeed(behavior.m_rotationSpeed),
autoRotate(0),
m_speed(behavior.m_speed),
m_flySpeed(behavior.m_flySpeed),
m_pattern(behavior.m_pattern),
cycle(behavior.cycle)
{}

// destructor
Movement::~Movement(void)
{}

// destroy function : always similar
void Movement::destroy(void)
{
    delete this;
}

// getNew function : always similar
MBehavior * Movement::getNew(MObject3d * parentObject)
{
    return new Movement(parentObject);
}

// getCopy function : always similar
MBehavior * Movement::getCopy(MObject3d * parentObject)
{
    return new Movement(*this, parentObject);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Variables, allow to access custom variable from script and from Maratis Editor
/////////////////////////////////////////////////////////////////////////////////////////////////////////

unsigned int Movement::getVariablesNumber(void){
    return 7;
}

MVariable Movement::getVariable(unsigned int id)
{
    switch(id)
    {
    default:
        return MVariable("NULL", NULL, M_VARIABLE_NULL);
    case 0:
        return MVariable("animated", &animated, M_VARIABLE_BOOL);
    case 1:
        return MVariable("rotationSpeed", &m_rotationSpeed, M_VARIABLE_FLOAT);
    case 2:
        return MVariable("autoRotate", &autoRotate, M_VARIABLE_BOOL);
    case 3:
        return MVariable("movementSpeed", &m_speed, M_VARIABLE_FLOAT);
    case 4:
        return MVariable("flySpeed", &m_flySpeed, M_VARIABLE_FLOAT);
    case 5:
        return MVariable("movementPattern", &m_pattern, M_VARIABLE_STRING); 
    case 6:
        return MVariable("CycleMovement", &cycle, M_VARIABLE_BOOL);

    }
}


/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Events
/////////////////////////////////////////////////////////////////////////////////////////////////////////

// update function (called by MGame class by default)
void Movement::update(void)
{
    MEngine * engine = MEngine::getInstance();
    MGame * game = engine->getGame();

    // check if the game is running, removing thif test will enable In-Editor update (like for the lookAt behavior)
    if(! game->isRunning())
        return;

    // get the associated parent object (who is using this behavior)
    MObject3d * parent = getParentObject();

    // lets rotate the parent object around the z axis, using our custom "rotationSpeed" variable
    if(animated){
    printf("Strlen m_pattern %d\n", strlen(m_pattern));
    printf("INT i: %d\n", i);
    printf("Pattern: %s\n", m_pattern);
    if(autoRotate)
    parent->addAxisAngleRotation(MVector3(0, 0, 1), m_rotationSpeed);
    if(i<strlen(m_pattern)){
    // a pattern string of wasd moves the object using rotationSpeed and moveSpeed
    if(m_pattern[i]=='w'){
    MVector3 oldPosition=parent->getPosition();
    MVector3* trnPosition=new MVector3(0,m_speed,0);
    MVector3 newPosition=oldPosition + *trnPosition;
    parent->setPosition(newPosition);
    }
    if(m_pattern[i]=='a'){
        parent->addAxisAngleRotation(MVector3(0, 0, m_rotationSpeed), 3);
    }
    if(m_pattern[i]=='d'){
        parent->addAxisAngleRotation(MVector3(0, 0, -m_rotationSpeed), 3);
    }
    if(m_pattern[i]=='s'){
        MVector3 oldPosition=parent->getPosition();
        MVector3* trnPosition=new MVector3(0,-m_speed,0);
        MVector3 newPosition=oldPosition + *trnPosition;
        parent->setPosition(newPosition);
    }
    i++;
    }
    else if(cycle)
        i=0;
    
    }
}

Now I want to set the "a" and "d" patterns to rotate the object so that works like

-- turn around
    if isKeyPressed("A") then
        rotate(Player, {0, 0, 0.5}, 3)
    end
    
    if isKeyPressed("D") then
        rotate(Player, {0, 0, 0.5}, -3)
    end