Code: Select all
#pragma once
#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace gui;
using namespace io;
class CSceneNodeAnimatorCameraRTS : public ISceneNodeAnimator
{
public:
CSceneNodeAnimatorCameraRTS(f32 pMoveSpeed = 5.0f, bool pEnableKeys = true, ICursorControl* pCursorControl = NULL, rect<s32> pMouseRect = rect<s32>(50, 50, 1024-50, 768-50));
~CSceneNodeAnimatorCameraRTS();
virtual bool hasFinished(void) const;
virtual bool isEventReceiverEnabled() const;
virtual bool OnEvent(const SEvent& event);
virtual ESCENE_NODE_ANIMATOR_TYPE getType() const;
virtual ISceneNodeAnimator* createClone(ISceneNode* node, ISceneManager* newManager=0);
virtual void animateNode(ISceneNode* node, u32 timeMs);
private:
bool mFirstRun;
bool mKeysEnabled;
bool mKeys[KEY_KEY_CODES_COUNT];
f32 mMoveSpeed;
unsigned int mLastAnimationTime;
ICursorControl* mCursorControl;
rect<s32> mMouseRect;
};
Code: Select all
#include "CSceneNodeAnimatorCameraRTS.h"
CSceneNodeAnimatorCameraRTS::CSceneNodeAnimatorCameraRTS(f32 pMoveSpeed, bool pEnableKeys, ICursorControl* pCursorControl, rect<s32> pMouseRect) : mFirstRun(true), mKeysEnabled(pEnableKeys), mMoveSpeed(pMoveSpeed), mCursorControl(pCursorControl), mMouseRect(pMouseRect)
{
for(int i = 0; i < KEY_KEY_CODES_COUNT; ++i)
mKeys[i] = false;
if(mCursorControl)
mCursorControl->grab();
}
CSceneNodeAnimatorCameraRTS::~CSceneNodeAnimatorCameraRTS()
{
if(mCursorControl)
mCursorControl->drop();
}
bool CSceneNodeAnimatorCameraRTS::hasFinished(void) const
{
return false;
}
bool CSceneNodeAnimatorCameraRTS::isEventReceiverEnabled() const
{
return true;
}
bool CSceneNodeAnimatorCameraRTS::OnEvent(const SEvent& event)
{
if(event.EventType == EET_KEY_INPUT_EVENT)
{
mKeys[event.KeyInput.Key] = event.KeyInput.PressedDown;
return true;
}
return false;
}
ESCENE_NODE_ANIMATOR_TYPE CSceneNodeAnimatorCameraRTS::getType() const
{
return ESNAT_UNKNOWN;
}
ISceneNodeAnimator* CSceneNodeAnimatorCameraRTS::createClone(ISceneNode* node, ISceneManager* newManager)
{
return new CSceneNodeAnimatorCameraRTS();
}
void CSceneNodeAnimatorCameraRTS::animateNode(ISceneNode* node, u32 timeMs)
{
if (!node || node->getType() != ESNT_CAMERA)
return;
if(mFirstRun)
{
mLastAnimationTime = timeMs;
mFirstRun = false;
}
f32 timeDiff = (f32) (timeMs - mLastAnimationTime);
mLastAnimationTime = timeMs;
ICameraSceneNode* Camera = static_cast<ICameraSceneNode*>(node);
vector3df pos = Camera->getPosition();
vector2di curspos = mCursorControl?mCursorControl->getPosition():vector2di(0, 0);
dimension2du screensize = Camera->getSceneManager()->getVideoDriver()->getScreenSize();
if((mKeysEnabled && mKeys[KEY_KEY_W]) || (mCursorControl && mMouseRect.UpperLeftCorner.Y > curspos.Y))
pos.Z+=mMoveSpeed*timeDiff;
if((mKeysEnabled && mKeys[KEY_KEY_S]) || (mCursorControl && mMouseRect.LowerRightCorner.Y < curspos.Y))
pos.Z-=mMoveSpeed*timeDiff;
if((mKeysEnabled && mKeys[KEY_KEY_A]) || (mCursorControl && mMouseRect.UpperLeftCorner.X > curspos.X))
pos.X-=mMoveSpeed*timeDiff;
if((mKeysEnabled && mKeys[KEY_KEY_D]) || (mCursorControl && mMouseRect.LowerRightCorner.X < curspos.X))
pos.X+=mMoveSpeed*timeDiff;
node->setPosition(pos);
Camera->setTarget(vector3df(pos.X, pos.Y-2, pos.Z+2));
}
Code: Select all
ISceneNodeAnimator* anim = new CSceneNodeAnimatorCameraRTS();
ICameraSceneNode* Camera = SceneMgr->addCameraSceneNode();
Camera->setFarValue(10000.0f);
Camera->setPosition(vector3df(100, 1600, 100));
Camera->addAnimator(anim);
anim->drop();
Code: Select all
ISceneNodeAnimator* anim = new CSceneNodeAnimatorCameraRTS(5, false, Device->getCursorControl(), rect<s32>(25, 25, SCREENX-25, SCREENY-25));
Camera = SceneMgr->addCameraSceneNode();
Camera->setFarValue(10000.0f);
Camera->setPosition(vector3df(100, 2500, 100));
Camera->addAnimator(anim);
anim->drop();
Todo:
Rotation and zoom
Constructive criticism/feedback appreciated
