2
Copyright (C) 2013 Erik Ogenvik
4
This program is free software; you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation; either version 2 of the License, or
7
(at your option) any later version.
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with this program; if not, write to the Free Software
16
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23
#include "AvatarCameraWarper.h"
24
#include "MovementController.h"
25
#include "EmberEntity.h"
26
#include "camera/MainCamera.h"
27
#include "framework/MainLoopController.h"
28
#include "framework/Tokeniser.h"
29
#include "framework/LoggingInstance.h"
31
#include <Eris/View.h>
32
#include <OgreSceneNode.h>
41
AvatarCameraWarper::AvatarCameraWarper(MovementController& movementController, const Camera::MainCamera& camera, Eris::View& view, float movementThreshold) :
42
AvatarFollowsCamera("toggle_avatar_follows_camera", this, "Toggles the avatar being moved when the first person camera is moved. An optional threshold value can also be specified."), mMovementController(movementController), mCamera(camera), mView(view), mMovementThreshold(movementThreshold), mLastPosition(camera.getPosition()), mEnabled(false)
44
MainLoopController::getSingleton().EventFrameProcessed.connect(sigc::mem_fun(*this, &AvatarCameraWarper::frameProcessed));
47
AvatarCameraWarper::~AvatarCameraWarper()
51
void AvatarCameraWarper::setEnabled(bool enabled)
56
void AvatarCameraWarper::runCommand(const std::string& command, const std::string& args)
58
if (AvatarFollowsCamera == command) {
60
S_LOG_INFO("Setting avatar camera warper to " << (mEnabled ? "enabled" : "disabled") << ".");
61
Tokeniser tokeniser(args);
62
if (tokeniser.hasRemainingTokens()) {
64
std::istringstream(tokeniser.nextToken()) >> mMovementThreshold;
65
} catch (const std::exception& e) {
66
S_LOG_WARNING("Could not update movement threshold in avatar camera warper." << e);
72
void AvatarCameraWarper::frameProcessed(const TimeFrame&, unsigned int)
74
if (mEnabled && mMovementController.isCameraFreeFlying()) {
75
const Ogre::Vector3& cameraPosition = mCamera.getPosition();
76
if (cameraPosition.distance(mLastPosition) > mMovementThreshold) {
77
updateAvatarPosition(cameraPosition);
78
mLastPosition = cameraPosition;
83
void AvatarCameraWarper::updateAvatarPosition(const Ogre::Vector3& worldPosition)
85
if (mView.getTopLevel()) {
86
mMovementController.teleportTo(worldPosition, static_cast<EmberEntity*>(mView.getTopLevel()));