<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Maratis forum - [PLUGIN] Movement Pattern Plugin]]></title>
	<link rel="self" href="http://forum.maratis3d.com/extern.php?action=feed&amp;tid=666&amp;type=atom"/>
	<updated>2013-05-14T22:45:01Z</updated>
	<generator>PunBB</generator>
	<id>http://forum.maratis3d.com/viewtopic.php?id=666</id>
		<entry>
			<title type="html"><![CDATA[Re: [PLUGIN] Movement Pattern Plugin]]></title>
			<link rel="alternate" href="http://forum.maratis3d.com/viewtopic.php?pid=4158#p4158"/>
			<content type="html"><![CDATA[<p>done...</p><div class="codebox"><pre><code>/////////////////////////////////////////////////////////////////////////////////////////////////////////
// MovementGamePlugin
// Movement.h
// 
// Code : Anael Seghezzi edited by Giuseppe Alfieri
/////////////////////////////////////////////////////////////////////////////////////////////////////////

#ifndef _MY_BEHAVIOR_H
#define _MY_BEHAVIOR_H

#include &lt;MEngine.h&gt;


class Movement : public MBehavior
{
public:

    // constructors / destructors
    Movement(MObject3d * parentObject);
    Movement(Movement &amp; 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 &quot;Movement&quot;; }
    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</code></pre></div><p>Movement.cpp<br /></p><div class="codebox"><pre><code>/////////////////////////////////////////////////////////////////////////////////////////////////////////
// SimpleGamePlugin
// Movement.cpp
// 
// Code : Anael Seghezzi edited by Giuseppe Alfieri
/////////////////////////////////////////////////////////////////////////////////////////////////////////

#include &quot;Movement.h&quot;


/////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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 &amp; 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(&quot;NULL&quot;, NULL, M_VARIABLE_NULL);
    case 0:
        return MVariable(&quot;animated&quot;, &amp;animated, M_VARIABLE_BOOL);
    case 1:
        return MVariable(&quot;rotationSpeed&quot;, &amp;m_rotationSpeed, M_VARIABLE_FLOAT);
    case 2:
        return MVariable(&quot;autoRotate&quot;, &amp;autoRotate, M_VARIABLE_BOOL);
    case 3:
        return MVariable(&quot;movementSpeed&quot;, &amp;m_speed, M_VARIABLE_FLOAT);
    case 4:
        return MVariable(&quot;flySpeed&quot;, &amp;m_flySpeed, M_VARIABLE_FLOAT);
    case 5:
        return MVariable(&quot;movementPattern&quot;, &amp;m_pattern, M_VARIABLE_STRING); 
    case 6:
        return MVariable(&quot;CycleMovement&quot;, &amp;cycle, M_VARIABLE_BOOL);

    }
}


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

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

    // check if the game is running, removing thif test will enable In-Editor update (like for the lookAt behavior)
    if(! game-&gt;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 &quot;rotationSpeed&quot; variable
    if(animated){
    printf(&quot;Strlen m_pattern %d\n&quot;, strlen(m_pattern));
    printf(&quot;INT i: %d\n&quot;, i);
    printf(&quot;Pattern: %s\n&quot;, m_pattern);
    if(autoRotate)
    parent-&gt;addAxisAngleRotation(MVector3(0, 0, 1), m_rotationSpeed);
    if(i&lt;strlen(m_pattern)){
    // a pattern string of wasd moves the object using rotationSpeed and moveSpeed
    if(m_pattern[i]==&#039;w&#039;){
    MVector3 oldPosition=parent-&gt;getPosition();
    MVector3* trnPosition=new MVector3(0,m_speed,0);
    MVector3 newPosition=oldPosition + *trnPosition;
    parent-&gt;setPosition(newPosition);
    }
    if(m_pattern[i]==&#039;a&#039;){
        parent-&gt;addAxisAngleRotation(MVector3(0, 0, m_rotationSpeed), 3);
    }
    if(m_pattern[i]==&#039;d&#039;){
        parent-&gt;addAxisAngleRotation(MVector3(0, 0, -m_rotationSpeed), 3);
    }
    if(m_pattern[i]==&#039;s&#039;){
        MVector3 oldPosition=parent-&gt;getPosition();
        MVector3* trnPosition=new MVector3(0,-m_speed,0);
        MVector3 newPosition=oldPosition + *trnPosition;
        parent-&gt;setPosition(newPosition);
    }
    i++;
    }
    else if(cycle)
        i=0;
    
    }
}</code></pre></div><p>Now I want to set the &quot;a&quot; and &quot;d&quot; patterns to rotate the object so that works like </p><div class="codebox"><pre><code>-- turn around
    if isKeyPressed(&quot;A&quot;) then
        rotate(Player, {0, 0, 0.5}, 3)
    end
    
    if isKeyPressed(&quot;D&quot;) then
        rotate(Player, {0, 0, 0.5}, -3)
    end</code></pre></div>]]></content>
			<author>
				<name><![CDATA[firelite]]></name>
				<uri>http://forum.maratis3d.com/profile.php?id=1537</uri>
			</author>
			<updated>2013-05-14T22:45:01Z</updated>
			<id>http://forum.maratis3d.com/viewtopic.php?pid=4158#p4158</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[[PLUGIN] Movement Pattern Plugin]]></title>
			<link rel="alternate" href="http://forum.maratis3d.com/viewtopic.php?pid=4155#p4155"/>
			<content type="html"><![CDATA[<p>I&#039;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&#039;m blocked... This is the code.</p><br /><br /><p>Movement.cpp<br /></p><div class="codebox"><pre><code>/////////////////////////////////////////////////////////////////////////////////////////////////////////
// SimpleGamePlugin
// Movement.cpp
// 
// Code : Anael Seghezzi edited by Giuseppe Alfieri
/////////////////////////////////////////////////////////////////////////////////////////////////////////

#include &quot;Movement.h&quot;


/////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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(&quot;&quot;),
cycle(0)
{}

// copy constructor
Movement::Movement(Movement &amp; 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(&quot;NULL&quot;, NULL, M_VARIABLE_NULL);
    case 0:
        return MVariable(&quot;animated&quot;, &amp;animated, M_VARIABLE_BOOL);
    case 1:
        return MVariable(&quot;rotationSpeed&quot;, &amp;m_rotationSpeed, M_VARIABLE_FLOAT);
    case 2:
        return MVariable(&quot;movementSpeed&quot;, &amp;m_speed, M_VARIABLE_FLOAT);
    case 3:
        return MVariable(&quot;flySpeed&quot;, &amp;m_flySpeed, M_VARIABLE_FLOAT);
    case 4:
        return MVariable(&quot;movementPattern&quot;, &amp;m_pattern, M_VARIABLE_STRING);   
    case 5:
        return MVariable(&quot;CycleMovement&quot;, &amp;cycle, M_VARIABLE_BOOL);

    }
}


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

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

    // check if the game is running, removing thif test will enable In-Editor update (like for the lookAt behavior)
    if(! game-&gt;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 &quot;rotationSpeed&quot; variable
    if(animated){
    patternLenght=strlen(m_pattern);
    printf(&quot;%d&quot;, patternLenght);
    parent-&gt;addAxisAngleRotation(MVector3(0, 0, 1), m_rotationSpeed);
    if(i&lt;patternLenght){
    // a pattern string of wasd moves the object using rotationSpeed and moveSpeed
    if(m_pattern[i]==&#039;w&#039;){
    MVector3 oldPosition=parent-&gt;getPosition();
    MVector3* trnPosition=new MVector3(0,m_speed,0);
    MVector3 newPosition=oldPosition + *trnPosition;
    parent-&gt;setPosition(newPosition);
    }
    if(m_pattern[i]==&#039;a&#039;){
        parent-&gt;addAxisAngleRotation(MVector3(0, 0, 1), m_rotationSpeed);
    }
    if(m_pattern[i]==&#039;d&#039;){
        parent-&gt;addAxisAngleRotation(MVector3(0, 0, -1), m_rotationSpeed);
    }
    if(m_pattern[i]==&#039;s&#039;){
        MVector3 oldPosition=parent-&gt;getPosition();
        MVector3* trnPosition=new MVector3(m_speed/2,0,0);
        MVector3 newPosition=oldPosition + *trnPosition;
        parent-&gt;setPosition(newPosition);
    }
    i++;
    }
    else if(cycle)
        i=0;
    
    }
}</code></pre></div><p>Movement.h</p><div class="codebox"><pre><code>/////////////////////////////////////////////////////////////////////////////////////////////////////////
// MovementGamePlugin
// Movement.h
// 
// Code : Anael Seghezzi edited by Giuseppe Alfieri
/////////////////////////////////////////////////////////////////////////////////////////////////////////

#ifndef _MY_BEHAVIOR_H
#define _MY_BEHAVIOR_H

#include &lt;MEngine.h&gt;


class Movement : public MBehavior
{
public:

    // constructors / destructors
    Movement(MObject3d * parentObject);
    Movement(Movement &amp; 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 &quot;Movement&quot;; }
    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</code></pre></div>]]></content>
			<author>
				<name><![CDATA[firelite]]></name>
				<uri>http://forum.maratis3d.com/profile.php?id=1537</uri>
			</author>
			<updated>2013-05-14T20:06:37Z</updated>
			<id>http://forum.maratis3d.com/viewtopic.php?pid=4155#p4155</id>
		</entry>
</feed>
