Lugdunum  0.1.0
FreeMovement.cpp
Go to the documentation of this file.
4 
5 namespace lug {
6 namespace Core {
7 
8 void FreeMovement::onFrame(const System::Time& elapsedTime) {
9  if (_eventSource == nullptr) {
10  LUG_LOG.warn("FreeMovement::onFrame: No event source, call setEventSource()");
11  return;
12  }
13 
14  // Move Forward
16  _target->translate({0.0f, 0.0f, _speed * elapsedTime.getMilliseconds<float>()});
17  }
18  // Move Backward
20  _target->translate({0.0f, 0.0f, -_speed * elapsedTime.getMilliseconds<float>()});
21  }
22  // Strafe Left
24  _target->translate({-_speed * elapsedTime.getMilliseconds<float>(), 0.0f, 0.0f});
25  }
26  // Strafe Right
28  _target->translate({_speed * elapsedTime.getMilliseconds<float>(), 0.0f, 0.0f});
29  }
30 
31  // Move Upward
33  _target->translate({0.0f, _speed * elapsedTime.getMilliseconds<float>(), 0.0f});
34  }
35  // Move Downward
37  _target->translate({0.0f, -_speed * elapsedTime.getMilliseconds<float>(), 0.0f});
38  }
39 
40  // GamePad Movement
41  {
42  float axisLeftX = _eventSource->_gamePadState.axisLeft.x();
43  float axisLeftY = _eventSource->_gamePadState.axisLeft.y();
44 
45  if (axisLeftX) {
46  _target->translate({ ((axisLeftX > 0) ? 1 : -1) * _speed * 0.5f * elapsedTime.getMilliseconds<float>(), 0.0f, 0.0f});
47  }
48  if (axisLeftY) {
49  _target->translate({0.0f, 0.0f, ((axisLeftY > 0) ? 1 : -1) * _speed * 0.5f * elapsedTime.getMilliseconds<float>()});
50  }
51  }
52  // GamePad View
53  {
54  float axisRightX = _eventSource->_gamePadState.axisRight.x();
55  float axisRightY = _eventSource->_gamePadState.axisRight.y();
56 
57  if (axisRightX || axisRightY) {
59  _target->rotate(-_speed * axisRightY, {1, 0, 0});
60  }
61  }
62  // TouchScreen
63  {
67  }
68  }
69 
70  // Capture / Release the mouse cursor
73  _hasFocus = true;
75  }
77  _hasFocus = false;
79  }
80 
81  if (_hasFocus) {
82  auto mousePos = _eventSource->getMousePos();
83 
84  // Only if the mouse moved since the last time
85  if (_lastMousePos != mousePos) {
86  Math::Vec2i delta = mousePos - _lastMousePos;
87  _lastMousePos = mousePos;
88 
89  // Rotate in world space to freeze the rotation on the X axis
90  _target->rotate(_speed * 0.4f * -delta.x(), {0, 1, 0}, lug::Graphics::Node::TransformSpace::World);
91  _target->rotate(_speed * 0.4f * -delta.y(), {1, 0, 0});
92 
93  Math::Vec2i windowSize = _eventSource->getWindowSize();
94 
95  // If the mouse escapes a 30% zone on the edges of the screen, we reset-it on the middle
96  if (mousePos.x() < windowSize.width() * 0.30 || mousePos.x() > windowSize.width() * 0.70
97  || mousePos.y() < windowSize.height() * 0.30 || mousePos.y() > windowSize.height() * 0.70) {
98  Math::Vec2i middle{windowSize.width() / 2, windowSize.height() / 2};
99  _eventSource->setMousePos(middle);
100  _lastMousePos = middle;
101  }
102  }
103  }
104 }
105 
106 }
107 }
108 
T getMilliseconds() const
TouchScreenEvent _touchScreenState
Definition: Window.hpp:162
void setMousePos(const Math::Vec2i &mousePosition)
Sets the mouse position.
Definition: Window.cpp:275
void onFrame(const System::Time &elapsedTime)
Definition: FreeMovement.cpp:8
lug::Math::Vec2f coordinates[2]
The Touch coordinate.
Definition: Event.hpp:59
const Math::Vec2i & getMousePos() const
Retrieves the mouses position.
Definition: Window.cpp:271
lug::Graphics::Scene::Node * _target
void translate(const Math::Vec3f &direction, TransformSpace space=TransformSpace::Local)
Definition: Node.cpp:46
lug::Math::Vec2f axisLeft
Definition: Event.hpp:54
void rotate(float angle, const Math::Vec3f &axis, TransformSpace space=TransformSpace::Local)
Definition: Node.cpp:62
lug::Math::Vec2f axisRight
Definition: Event.hpp:55
bool isKeyPressed(Keyboard::Key key) const
Determines if a key pressed.
Definition: Window.cpp:263
GamePadEvent _gamePadState
Definition: Window.hpp:161
#define LUG_LOG
Definition: Logger.hpp:73
Math::Vec2i getWindowSize() const
Gets the window size.
Definition: Window.cpp:282
void setMouseCursorVisible(bool visible)
Sets the visibility of the mouse cursor (hide/show)
Definition: Window.cpp:249
lug::Window::Window * _eventSource