2012年7月19日 星期四

Ogre Sinbad Moving

需要加入model資料夾和resource.cfg和plugins.cfg

#include "Ogre\ExampleApplication.h"
class Example25FrameListener : public Ogre::FrameListener
{
    public:
    Example25FrameListener(Ogre::SceneNode* node,Ogre::Entity* ent,RenderWindow* win,Ogre::Camera* cam)
    {
        _node = node;
        _Cam = cam;
        _ent = ent;
        _WalkingSpeed = 50.0f;
        _rotation = 0.0f;
        _isActionKeyPress = false;
        size_t windowHnd = 0;
        std::stringstream windowHndStr;
        win->getCustomAttribute("WINDOW", &windowHnd);
        windowHndStr << windowHnd;
        OIS::ParamList pl;
        pl.insert(std::make_pair(std::string("WINDOW"),windowHndStr.str()));
        _man = OIS::InputManager::createInputSystem( pl );
        _key = static_cast( _man->createInputObject(OIS::OISKeyboard, false ));
        _mouse = static_cast(_man->createInputObject( OIS::OISMouse, false ));
        translate = Ogre::Vector3(0,0,0);

        _aniState = _ent->getAnimationState("RunBase");
        _aniState->setLoop(false);
        _aniStateTop = _ent->getAnimationState("RunTop");
        _aniStateTop->setLoop(false);
     }

    bool frameStarted(const Ogre::FrameEvent    &evt)
    {
        bool walked = false;
        Ogre::Vector3 SinbadTranslate(0,0,0);
        _key->capture();
        if(_key->isKeyDown(OIS::KC_ESCAPE))
        {
            return false;
        }
        if(_key->isKeyDown(OIS::KC_SPACE))
        {
          _isActionKeyPress = true;
          _aniStateTop = _ent->getAnimationState("SliceHorizontal");
          _aniStateTop->setLoop(false);
          _aniStateTop->setTimePosition(0.0f);
        }
        if(_key->isKeyDown(OIS::KC_W))
        {
        }
        if(_key->isKeyDown(OIS::KC_S))
        {
        }
        if(_key->isKeyDown(OIS::KC_A))
        {
        }
        if(_key->isKeyDown(OIS::KC_D))
        {
        }
        if(_key->isKeyDown(OIS::KC_UP))
        {
            SinbadTranslate += Ogre::Vector3(0,0,-1);
            _rotation = 3.14f;
            walked = true;
        }
        if(_key->isKeyDown(OIS::KC_DOWN))
        {
            SinbadTranslate += Ogre::Vector3(0,0,1);
            _rotation = 0.0f;
            walked = true;
        }
        if(_key->isKeyDown(OIS::KC_LEFT))
        {
            SinbadTranslate += Ogre::Vector3(-1,0,0);
            _rotation = -1.57f;
            walked = true;
        }
        if(_key->isKeyDown(OIS::KC_RIGHT))
        {
            SinbadTranslate += Ogre::Vector3(1,0,0);
            _rotation = 1.57f;
            walked = true;
        }

        if(walked)
        {
            _aniState->setEnabled(true);
            _aniStateTop->setEnabled(true);
            if(_aniState->hasEnded())
            {
                _aniState->setTimePosition(0.0f);
            }
            if(_aniStateTop->hasEnded())
            {
                _aniStateTop->setTimePosition(0.0f);
            }
        }
        else
        {
            if(_isActionKeyPress)
            {
                _aniStateTop->setEnabled(true);
                if(_aniStateTop->hasEnded())
                {
                    _isActionKeyPress = false;
                    _aniStateTop = _ent->getAnimationState("RunTop");
                    _aniStateTop->setLoop(false);
                    _aniStateTop->setTimePosition(0.0f);
                    _aniStateTop->setEnabled(false);
                }
            }
            else
            {
                _aniState->setTimePosition(0.0f);
                _aniState->setEnabled(false);
                _aniStateTop->setTimePosition(0.0f);
                _aniStateTop->setEnabled(false);
            }
        }
        _mouse->capture();
        float rotX = _mouse->getMouseState().X.rel * evt.timeSinceLastFrame* -1;
        float rotY = _mouse->getMouseState().Y.rel * evt.timeSinceLastFrame * -1;
        _node->translate(SinbadTranslate * evt.timeSinceLastFrame * _WalkingSpeed);
        _node->resetOrientation();
        _node->yaw(Ogre::Radian(_rotation));

        _Cam->yaw(Ogre::Radian(rotX));
        _Cam->pitch(Ogre::Radian(rotY));
        _Cam->moveRelative(translate*evt.timeSinceLastFrame * 10);

        _aniState->addTime(evt.timeSinceLastFrame*1);
        _aniStateTop->addTime(evt.timeSinceLastFrame*0.4);
        return true;
    }

    Example25FrameListener()
    {
        FrameListener = NULL;
    }
    ~Example25FrameListener()
    {
        _man->destroyInputObject(_key);
        OIS::InputManager::destroyInputSystem(_man);
        _man->destroyInputObject(_mouse);
        if(FrameListener)
        {
            delete FrameListener;
        }
    }

    private:
    Ogre::FrameListener* FrameListener;
    Ogre::SceneNode* _node;
    OIS::InputManager* _man;
    OIS::Keyboard* _key;
    Ogre::Vector3 translate;
    Ogre::Degree rotate;
    Ogre::Camera* _Cam;
    OIS::Mouse* _mouse;
    Ogre::Entity* _ent;
    Ogre::AnimationState* _aniState;
    Ogre::AnimationState* _aniStateTop;
    float _WalkingSpeed;
    float _rotation;
    bool _isActionKeyPress;
 };

class Example1 : public ExampleApplication
{
    public:
    void createScene()
    {
        Ogre::Plane plane(Vector3::UNIT_Y,  -10);
        Ogre::MeshManager::getSingleton().createPlane("plane",ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,plane,1500,1500,20,20,true,1,5,5,Vector3::UNIT_Z );
        Ogre::Entity* ent = mSceneMgr->createEntity("LightPlaneEntity", "plane");
        mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(ent);
        ent->setMaterialName("Examples/BeachStones");
        Ogre::Light* light = mSceneMgr->createLight("Light1");
        light->setType(Ogre::Light::LT_DIRECTIONAL);
        light->setDirection(Ogre::Vector3(1,-1,0));

        Ogre::SceneNode* node = mSceneMgr->createSceneNode("Node1");
        mSceneMgr->getRootSceneNode()->addChild(node);
        _SinbadEnt = mSceneMgr->createEntity("Sinbad", "Sinbad.mesh");
        Ogre::Entity* sword1 = mSceneMgr->createEntity("Sword1","Sword.mesh");
        Ogre::Entity* sword2 = mSceneMgr->createEntity("Sword2","Sword.mesh");
        _SinbadEnt->attachObjectToBone("Handle.L",sword1);
        _SinbadEnt->attachObjectToBone("Handle.R", sword2);
        _SinbadNode = node->createChildSceneNode("SinbadNode");
        _SinbadNode->setScale(3.0f,3.0f,3.0f);
        _SinbadNode->setPosition(Ogre::Vector3(0.0f,4.0f,0.0f));
        _SinbadNode->attachObject(_SinbadEnt);
    }
    void createCamera()
    {
        mCamera = mSceneMgr->createCamera("MyCamera1");
        mCamera->setPosition(0,100,200);
        mCamera->lookAt(0,0,0);
        mCamera->setNearClipDistance(5);
    }
    void createFrameListener()
    {
        Ogre::FrameListener* FrameListener = new Example25FrameListener(_SinbadNode,_SinbadEnt,mWindow,mCamera);
        mRoot->addFrameListener(FrameListener);
    }
    private:
    Ogre::SceneNode* _SinbadNode;
    Ogre::Entity* _SinbadEnt;
};
int main (void)
{
    Example1 app;
    app.go();
    return 0;
}

沒有留言:

張貼留言