2011-01-08 15:11:37 +01:00
|
|
|
#include "player.hpp"
|
|
|
|
|
2012-07-03 15:32:38 +02:00
|
|
|
#include <OgreSceneNode.h>
|
2012-08-09 00:15:52 +04:00
|
|
|
#include <OgreCamera.h>
|
2012-08-12 15:50:37 +04:00
|
|
|
#include <OgreRay.h>
|
2012-08-09 00:15:52 +04:00
|
|
|
|
2012-08-09 17:01:03 +04:00
|
|
|
#include "../mwbase/environment.hpp"
|
2012-08-09 16:12:10 +02:00
|
|
|
#include "../mwbase/soundmanager.hpp"
|
2012-08-09 17:01:03 +04:00
|
|
|
|
2012-08-09 00:15:52 +04:00
|
|
|
#include "../mwworld/ptr.hpp"
|
|
|
|
#include "../mwworld/refdata.hpp"
|
2012-07-03 15:32:38 +02:00
|
|
|
|
2011-01-08 15:11:37 +01:00
|
|
|
namespace MWRender
|
|
|
|
{
|
2011-11-16 21:15:49 -05:00
|
|
|
Player::Player (Ogre::Camera *camera, Ogre::SceneNode* node)
|
2012-08-12 15:50:37 +04:00
|
|
|
: mCamera(camera),
|
|
|
|
mPlayerNode(node),
|
|
|
|
mCameraNode(mPlayerNode->createChildSceneNode()),
|
2012-08-14 02:36:18 +04:00
|
|
|
mVanityNode(mPlayerNode->createChildSceneNode()),
|
2012-08-09 00:15:52 +04:00
|
|
|
mFirstPersonView(true),
|
2012-08-12 15:50:37 +04:00
|
|
|
mVanityMode(false),
|
2012-08-14 02:36:18 +04:00
|
|
|
mPreviewMode(false),
|
|
|
|
mHeight(40.f)
|
2012-05-30 15:52:39 +02:00
|
|
|
{
|
2012-08-14 02:36:18 +04:00
|
|
|
mCameraNode->attachObject(mCamera);
|
|
|
|
mCameraNode->setPosition(0.f, 0.f, mHeight);
|
|
|
|
|
|
|
|
mPreviewCam.yaw = 0.f;
|
2012-08-12 15:50:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Player::rotate(const Ogre::Vector3 &rot, bool adjust)
|
|
|
|
{
|
2012-08-13 08:37:32 +04:00
|
|
|
rotateCamera(rot, adjust);
|
2012-05-30 15:52:39 +02:00
|
|
|
|
2012-08-12 18:35:35 +04:00
|
|
|
mUpdates = 0;
|
|
|
|
mTimeIdle = 0.f;
|
2012-05-30 15:52:39 +02:00
|
|
|
|
2012-08-12 18:35:35 +04:00
|
|
|
if (mVanityMode) {
|
2012-08-14 02:36:18 +04:00
|
|
|
toggleVanityMode(false);
|
2012-08-12 15:50:37 +04:00
|
|
|
}
|
2012-08-09 10:24:18 +04:00
|
|
|
|
2012-08-12 18:35:35 +04:00
|
|
|
return !mVanityMode && !mPreviewMode;
|
2012-05-30 15:52:39 +02:00
|
|
|
}
|
2012-07-03 15:32:38 +02:00
|
|
|
|
2012-08-13 08:37:32 +04:00
|
|
|
void Player::rotateCamera(const Ogre::Vector3 &rot, bool adjust)
|
2012-07-03 15:32:38 +02:00
|
|
|
{
|
2012-08-12 15:50:37 +04:00
|
|
|
Ogre::SceneNode *pitchNode = mCamera->getParentSceneNode();
|
|
|
|
Ogre::SceneNode *yawNode = pitchNode->getParentSceneNode();
|
2012-08-09 00:15:52 +04:00
|
|
|
|
2012-08-12 15:50:37 +04:00
|
|
|
if (adjust) {
|
2012-08-14 02:36:18 +04:00
|
|
|
setYaw(getYaw() + rot.z);
|
|
|
|
setPitch(getPitch() + rot.x);
|
2012-08-12 15:50:37 +04:00
|
|
|
} else {
|
2012-08-14 02:36:18 +04:00
|
|
|
setYaw(rot.z);
|
|
|
|
setPitch(rot.x);
|
2012-08-12 15:50:37 +04:00
|
|
|
}
|
2012-08-14 02:36:18 +04:00
|
|
|
Ogre::Quaternion xr(
|
|
|
|
Ogre::Radian(getPitch() + Ogre::Math::HALF_PI),
|
|
|
|
Ogre::Vector3::UNIT_X
|
|
|
|
);
|
|
|
|
Ogre::Quaternion zr(Ogre::Radian(getYaw()), Ogre::Vector3::UNIT_Z);
|
|
|
|
|
|
|
|
pitchNode->setOrientation(xr);
|
|
|
|
yawNode->setOrientation(zr);
|
|
|
|
|
2012-08-13 08:37:32 +04:00
|
|
|
updateListener();
|
2012-08-09 00:15:52 +04:00
|
|
|
}
|
2012-08-09 10:24:18 +04:00
|
|
|
|
2012-08-12 15:50:37 +04:00
|
|
|
std::string Player::getHandle() const
|
2012-08-09 10:24:18 +04:00
|
|
|
{
|
2012-08-12 15:50:37 +04:00
|
|
|
return mPlayerNode->getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::attachTo(const MWWorld::Ptr &ptr)
|
|
|
|
{
|
|
|
|
ptr.getRefData().setBaseNode(mPlayerNode);
|
|
|
|
}
|
|
|
|
|
2012-08-09 17:01:03 +04:00
|
|
|
void Player::updateListener()
|
|
|
|
{
|
|
|
|
Ogre::Vector3 pos = mCamera->getRealPosition();
|
|
|
|
Ogre::Vector3 dir = mCamera->getRealDirection();
|
|
|
|
|
|
|
|
Ogre::Real xch;
|
|
|
|
xch = pos.y, pos.y = -pos.z, pos.z = xch;
|
|
|
|
xch = dir.y, dir.y = -dir.z, dir.z = xch;
|
|
|
|
|
|
|
|
MWBase::Environment::get().getSoundManager()->setListenerPosDir(pos, dir);
|
|
|
|
}
|
2012-08-12 15:50:37 +04:00
|
|
|
|
|
|
|
void Player::update(float duration)
|
|
|
|
{
|
2012-08-12 18:35:35 +04:00
|
|
|
if (!mVanityMode) {
|
|
|
|
++mUpdates;
|
|
|
|
mTimeIdle += duration;
|
|
|
|
if (mTimeIdle > 30.f) {
|
2012-08-14 02:36:18 +04:00
|
|
|
toggleVanityMode(true);
|
2012-08-12 18:35:35 +04:00
|
|
|
}
|
|
|
|
}
|
2012-08-12 15:50:37 +04:00
|
|
|
if (mFirstPersonView && !mVanityMode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (mVanityMode) {
|
2012-08-12 18:35:35 +04:00
|
|
|
Ogre::Vector3 rot(0.f, 0.f, 0.f);
|
|
|
|
rot.z = Ogre::Degree(3.f * duration).valueRadians();
|
2012-08-13 08:37:32 +04:00
|
|
|
rotateCamera(rot, true);
|
2012-08-12 15:50:37 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::toggleViewMode()
|
|
|
|
{
|
|
|
|
mFirstPersonView = !mFirstPersonView;
|
|
|
|
if (mFirstPersonView) {
|
2012-08-13 08:37:32 +04:00
|
|
|
mCamera->setPosition(0.f, 0.f, 0.f);
|
2012-08-12 15:50:37 +04:00
|
|
|
} else {
|
2012-08-13 08:37:32 +04:00
|
|
|
mCamera->setPosition(0.f, 0.f, 400.f);
|
2012-08-12 15:50:37 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-14 02:36:18 +04:00
|
|
|
void Player::toggleVanityMode(bool enable, bool force)
|
2012-08-12 15:50:37 +04:00
|
|
|
{
|
2012-08-14 02:36:18 +04:00
|
|
|
if (mVanityMode == enable) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mVanityMode = enable;
|
2012-08-12 18:35:35 +04:00
|
|
|
|
2012-08-13 08:37:32 +04:00
|
|
|
float offset = 300.f;
|
2012-08-12 18:35:35 +04:00
|
|
|
Ogre::Vector3 rot(0.f, 0.f, 0.f);
|
|
|
|
if (mVanityMode) {
|
|
|
|
rot.x = Ogre::Degree(-30.f).valueRadians();
|
2012-08-14 02:36:18 +04:00
|
|
|
mMainCam.offset = mCamera->getPosition().z;
|
|
|
|
|
|
|
|
mPlayerNode->removeChild(mCameraNode);
|
|
|
|
mVanityNode->addChild(mCameraNode);
|
2012-08-12 18:35:35 +04:00
|
|
|
} else {
|
2012-08-14 02:36:18 +04:00
|
|
|
rot.x = getPitch();
|
|
|
|
offset = mMainCam.offset;
|
|
|
|
|
|
|
|
mVanityNode->removeChild(mCameraNode);
|
|
|
|
mPlayerNode->addChild(mCameraNode);
|
2012-08-12 18:35:35 +04:00
|
|
|
}
|
2012-08-14 02:36:18 +04:00
|
|
|
rot.z = getYaw();
|
2012-08-13 08:37:32 +04:00
|
|
|
mCamera->setPosition(0.f, 0.f, offset);
|
|
|
|
rotateCamera(rot, false);
|
2012-08-12 15:50:37 +04:00
|
|
|
}
|
|
|
|
|
2012-08-14 02:36:18 +04:00
|
|
|
void Player::togglePreviewMode(bool enable)
|
2012-08-12 15:50:37 +04:00
|
|
|
{
|
|
|
|
/// \todo move camera
|
2012-08-14 02:36:18 +04:00
|
|
|
if (mPreviewMode == enable) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mPreviewMode = enable;
|
2012-08-12 15:50:37 +04:00
|
|
|
}
|
2012-08-12 18:35:35 +04:00
|
|
|
|
2012-08-14 02:36:18 +04:00
|
|
|
float Player::getYaw()
|
2012-08-12 18:35:35 +04:00
|
|
|
{
|
2012-08-14 02:36:18 +04:00
|
|
|
if (mVanityMode || mPreviewMode) {
|
|
|
|
return mPreviewCam.yaw;
|
|
|
|
}
|
|
|
|
return mMainCam.yaw;
|
|
|
|
}
|
2012-08-12 18:35:35 +04:00
|
|
|
|
2012-08-14 02:36:18 +04:00
|
|
|
void Player::setYaw(float angle)
|
|
|
|
{
|
|
|
|
if (angle > Ogre::Math::PI) {
|
|
|
|
angle -= Ogre::Math::TWO_PI;
|
|
|
|
} else if (angle < -Ogre::Math::PI) {
|
|
|
|
angle += Ogre::Math::TWO_PI;
|
|
|
|
}
|
|
|
|
if (mVanityMode || mPreviewMode) {
|
|
|
|
mPreviewCam.yaw = angle;
|
|
|
|
} else {
|
|
|
|
mMainCam.yaw = angle;
|
|
|
|
}
|
2012-08-12 18:35:35 +04:00
|
|
|
}
|
|
|
|
|
2012-08-14 02:36:18 +04:00
|
|
|
float Player::getPitch()
|
2012-08-12 18:35:35 +04:00
|
|
|
{
|
2012-08-14 02:36:18 +04:00
|
|
|
if (mVanityMode || mPreviewMode) {
|
|
|
|
return mPreviewCam.pitch;
|
|
|
|
}
|
|
|
|
return mMainCam.pitch;
|
|
|
|
}
|
2012-08-12 18:35:35 +04:00
|
|
|
|
2012-08-14 02:36:18 +04:00
|
|
|
void Player::setPitch(float angle)
|
|
|
|
{
|
|
|
|
if (angle > Ogre::Math::HALF_PI) {
|
|
|
|
angle = Ogre::Math::HALF_PI - 0.01;
|
|
|
|
} else if (angle < -Ogre::Math::HALF_PI) {
|
|
|
|
angle = -Ogre::Math::HALF_PI + 0.01;
|
|
|
|
}
|
|
|
|
if (mVanityMode || mPreviewMode) {
|
|
|
|
mPreviewCam.pitch = angle;
|
|
|
|
} else {
|
|
|
|
mMainCam.pitch = angle;
|
2012-08-12 18:35:35 +04:00
|
|
|
}
|
|
|
|
}
|
2011-01-08 15:11:37 +01:00
|
|
|
}
|