-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.hpp
133 lines (101 loc) · 3.19 KB
/
Player.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#ifndef GAME3D_PLAYER_HPP
#define GAME3D_PLAYER_HPP
#include <Ogre.h>
#include "Camera.hpp"
#include "Node.hpp"
#include "Object.hpp"
#include "World.hpp"
namespace Game3D {
class Player: public Object {
private:
Ogre::Vector3 translateVector_;
Ogre::Real currentSpeed_;
float moveScale_;
float speedLimit_;
Ogre::Degree rotateScale_;
Ogre::Radian rotX_, rotY_;
Ogre::Real moveSpeed_;
Ogre::Degree rotateSpeed_;
CameraPtr camera_;
public:
inline Player(CameraPtr camera)
: translateVector_(Ogre::Vector3::ZERO), currentSpeed_(0),
moveScale_(0.0f), rotateScale_(0.0f),
moveSpeed_(200), rotateSpeed_(36),
camera_(camera){ }
inline void onEvent(Node& node, Event& event) {
switch(event.type) {
case Event::FRAME_RENDERING: {
const Ogre::Vector3 lastMotion = translateVector_;
if(!event.mouse.buffered() || !event.keyboard.buffered()) {
moveScale_ = moveSpeed_ * event.frameEvent.timeSinceLastFrame;
rotateScale_ = rotateSpeed_ * event.frameEvent.timeSinceLastFrame;
rotX_ = 0;
rotY_ = 0;
translateVector_ = Ogre::Vector3::ZERO;
}
if(!event.keyboard.buffered()){
processUnbufferedKeyInput(event);
}
if(!event.mouse.buffered()){
processUnbufferedMouseInput(event);
}
if(translateVector_ == Ogre::Vector3::ZERO) {
currentSpeed_ -= event.frameEvent.timeSinceLastFrame * 0.3;
translateVector_ = lastMotion;
} else {
currentSpeed_ += event.frameEvent.timeSinceLastFrame;
}
// Limit speed.
if(currentSpeed_ > 1.0) {
currentSpeed_ = 1.0;
} else if(currentSpeed_ < 0.0) {
currentSpeed_ = 0.0;
}
translateVector_ *= currentSpeed_;
if(!event.mouse.buffered() || !event.keyboard.buffered()) {
moveCamera();
}
break;
}
default:
{
break;
}
}
}
inline void processUnbufferedKeyInput(Event& event) {
OIS::Keyboard& keyboard = event.keyboard;
if(keyboard.isKeyDown(OIS::KC_W)) {
translateVector_.z = moveScale_;
}
if(keyboard.isKeyDown(OIS::KC_S)) {
translateVector_.z = -(moveScale_ * 0.25);
}
if(keyboard.isKeyDown(OIS::KC_A)) {
translateVector_.x = moveScale_ * 0.5;
}
if(keyboard.isKeyDown(OIS::KC_D)) {
translateVector_.x = -(moveScale_ * 0.5);
}
}
inline void processUnbufferedMouseInput(Event& event) {
const OIS::MouseState& ms = event.mouse.getMouseState();
rotX_ = Ogre::Degree(-ms.X.rel * 0.26);
rotY_ = Ogre::Degree(-ms.Y.rel * 0.26);
}
inline void moveCamera() {
camera_->rotate(AngleVector(-rotY_.valueDegrees(), rotX_.valueDegrees(), 0.0));
camera_->translate(camera_->getOrientation().yawOrientation * translateVector_);
const double maxPitch = 60.0;
AngleVector rotation = camera_->getRotation();
const double pitch = rotation.pitch;
// Limit the pitch.
if(fabs(pitch) > maxPitch) {
rotation.pitch = (pitch > 0.0) ? maxPitch : -maxPitch;
camera_->setRotation(rotation);
}
}
};
}
#endif