#include "Robot.h"
#include <iostream>
using namespace std;
int Robot::sNextID = 0;
Random Robot::sRand;
Robot::Robot(Ogre::SceneManager* sceneMgr):
mWalkSpeed(35.0f), mId(sNextID++)
{
stringstream sstr;
sstr << "Robot" << mId;
mName = sstr.str();
string nodeName = mName + "_Node";
mEntity = sceneMgr->createEntity(mName, "robot.mesh");
float x = sRand.nextInt(-100, 500);
float y = 0.0f;
float z = sRand.nextInt(-200, 200);
mNode = sceneMgr->getRootSceneNode()->
createChildSceneNode(nodeName, Ogre::Vector3(x, y, z));
mNode->attachObject(mEntity);
for(int i = 1; i <= 4; i++){
mWalkList.push_back(Ogre::Vector3(x, y, z ));
Ogre::Entity *ent;
Ogre::SceneNode *node;
stringstream knotStream;
knotStream << mName << "_" << i;
string knotName = knotStream.str();
ent = sceneMgr->createEntity(knotName, "knot.mesh");
node = sceneMgr->getRootSceneNode()->createChildSceneNode(knotName + "Node",
Ogre::Vector3(x, -10.0f, z));
node->attachObject(ent);
node->setScale(0.1f, 0.1f, 0.1f);
x = sRand.nextInt(-100, 500);
z = sRand.nextInt(-200, 200);
}
}
Robot::~Robot() {
}
void Robot::update(const Ogre::FrameEvent &evt){
if (!mIsWalking)
{
if (nextLocation())
{
mAnimationState = mEntity->getAnimationState("Walk");
mAnimationState->setLoop(true);
mAnimationState->setEnabled(true);
}
}
else
{
Ogre::Real move = mWalkSpeed * evt.timeSinceLastFrame;
mDistance -= move;
if (mDistance <= 0.0f)
{
mNode->setPosition(mDestination);
mDirection = Ogre::Vector3::ZERO;
mIsWalking = false;
if (! nextLocation())
{
mAnimationState = mEntity->getAnimationState("Die");
mAnimationState->setLoop(false);
mAnimationState->setEnabled(true);
}
else
{
Ogre::Vector3 src = mNode->getOrientation() * Ogre::Vector3::UNIT_X;
if ((1.0f + src.dotProduct(mDirection)) < 0.0001f)
{
mNode->yaw(Ogre::Degree(180));
}
else
{
Ogre::Quaternion quat = src.getRotationTo(mDirection);
mNode->rotate(quat);
} }
}
else
{
mNode->translate(mDirection * move);
} } mAnimationState->addTime(evt.timeSinceLastFrame);
}
void Robot::setupAnimation(){
mAnimationState = mEntity->getAnimationState("Idle");
mAnimationState->setLoop(true);
mAnimationState->setEnabled(true);
mWalkSpeed = 35.0f;
mDirection = Ogre::Vector3::ZERO;
mIsWalking = false;
}
bool Robot::nextLocation(void){
if (mWalkList.empty())
return false;
mDestination = mWalkList.front(); mWalkList.pop_front();
mDirection = mDestination - mNode->getPosition();
mDistance = mDirection.normalise();
mIsWalking = true;
return true;
}
int Robot::getId(){
return mId;
}
string Robot::getName(){
return mName;
}