SyndicatQuebecoisdelaConstr.../SQCSim2021/player.cpp

104 lines
3.4 KiB
C++
Raw Normal View History

2021-09-27 13:15:57 -04:00
#include "player.h"
Player::Player(const Vector3f& position, float rotX, float rotY) : m_position(position), m_rotX(rotX), m_rotY(rotY) { }
void Player::TurnLeftRight(float value) {
m_rotX += value;
2021-10-01 10:52:33 -04:00
if (m_rotX > 45) m_rotX = 45;
else if (m_rotX < -45) m_rotX = -45;
2021-09-27 13:15:57 -04:00
}
void Player::TurnTopBottom(float value) {
2021-10-01 10:52:33 -04:00
m_rotY += value;
2021-09-27 13:15:57 -04:00
}
2021-10-01 10:52:33 -04:00
void Player::Move(bool front, bool back, bool left, bool right, bool jump, float elapsedTime) {
2021-09-27 13:15:57 -04:00
float xrotrad, yrotrad;
2021-10-01 10:52:33 -04:00
if (m_accjmp == 0 && jump && !jumped) {
m_accjmp = 0.3;
jumped = true;
}
else if (m_position.y > -0.2f && m_accjmp != 0)
if (m_accjmp > 0.1f) {
if (jump) {
m_accjmp *= 0.95 - m_accjmp * elapsedTime;
m_accjmp -= elapsedTime * 1.1;
}
else {
m_accjmp *= 0.75 - m_accjmp * elapsedTime;
m_accjmp -= elapsedTime * 1.1;
}
}
else {
if (jump) m_accjmp -= elapsedTime* 0.7;
else m_accjmp -= elapsedTime*0.9;
}
else m_accjmp = 0;
if (!jump) jumped = false;
if (m_position.y < 0) {
m_position.y += elapsedTime * 5;
if (m_position.y > 0.f) m_position.y = 0;
}
m_position.y += m_accjmp;
2021-09-27 13:15:57 -04:00
if (front) {
2021-10-01 10:52:33 -04:00
if (m_accWS < 10) m_accWS += elapsedTime * 30; else m_accWS = 10;
2021-09-27 13:15:57 -04:00
yrotrad = (m_rotY / 180 * 3.141592654f);
xrotrad = (m_rotX / 180 * 3.141592654f);
2021-10-01 10:52:33 -04:00
m_position.x += float(sin(yrotrad)) * elapsedTime * m_accWS;
m_position.z += float(-cos(yrotrad)) * elapsedTime * m_accWS;
// m_position.y += float(-sin(xrotrad)) * elapsedTime * 20; // <20> remettre pour impl<70>menter une fonction de vol.
2021-09-27 13:15:57 -04:00
}
else if (back) {
2021-10-01 10:52:33 -04:00
if (m_accWS > -10) m_accWS -= elapsedTime * 30; else m_accWS = -10;
2021-09-27 13:15:57 -04:00
yrotrad = (m_rotY / 180 * 3.141592654f);
xrotrad = (m_rotX / 180 * 3.141592654f);
2021-10-01 10:52:33 -04:00
m_position.x += float(-sin(yrotrad)) * elapsedTime * -m_accWS;
m_position.z += float(cos(yrotrad)) * elapsedTime * -m_accWS;
// m_position.y += float(sin(xrotrad)) * elapsedTime * 20; // <20> remettre pour impl<70>menter une fonction de vol.
}
else if (m_accWS != 0) {
m_accWS = m_accWS > 0 ? m_accWS - elapsedTime * 60 : m_accWS + elapsedTime * 60;
yrotrad = (m_rotY / 180 * 3.141592654f);
xrotrad = (m_rotX / 180 * 3.141592654f);
m_position.x += float(sin(yrotrad)) * elapsedTime * m_accWS;
m_position.z += float(-cos(yrotrad)) * elapsedTime * m_accWS;
if (m_accWS < 1 && m_accWS > -1) m_accWS = 0;
2021-09-27 13:15:57 -04:00
}
if (left) {
2021-10-01 10:52:33 -04:00
if (m_accAD < 10) m_accAD += elapsedTime * 30; else m_accAD = 10;
2021-09-27 13:15:57 -04:00
yrotrad = (m_rotY / 180 * 3.141592654f);
2021-10-01 10:52:33 -04:00
m_position.x += float(-cos(yrotrad)) * elapsedTime * m_accAD;
m_position.z += float(-sin(yrotrad)) * elapsedTime * m_accAD;
2021-09-27 13:15:57 -04:00
}
else if (right) {
2021-10-01 10:52:33 -04:00
if (m_accAD > -10) m_accAD -= elapsedTime * 30; else m_accAD = -10;
yrotrad = (m_rotY / 180 * 3.141592654f);
m_position.x += float(cos(yrotrad)) * elapsedTime * -m_accAD;
m_position.z += float(sin(yrotrad)) * elapsedTime * -m_accAD;
}
else if (m_accAD != 0) {
m_accAD = m_accAD > 0 ? m_accAD - elapsedTime * 60 : m_accAD + elapsedTime * 60;
2021-09-27 13:15:57 -04:00
yrotrad = (m_rotY / 180 * 3.141592654f);
2021-10-01 10:52:33 -04:00
m_position.x += float(-cos(yrotrad)) * elapsedTime * m_accAD;
m_position.z += float(-sin(yrotrad)) * elapsedTime * m_accAD;
if (m_accAD < 1 && m_accAD > -1) m_accAD = 0;
}
if ((m_accWS >= 0? m_accWS: -m_accWS + m_accAD >= 0 ? m_accAD : -m_accAD) > 12) {
m_accWS *= 0.9;
m_accAD *= 0.9;
2021-09-27 13:15:57 -04:00
}
}
2021-09-27 13:56:29 -04:00
void Player::ApplyTransformation(Transformation& transformation, bool rel) const {
2021-10-01 10:52:33 -04:00
transformation.ApplyRotation(-m_rotX, 1, 0, 0);
transformation.ApplyRotation(-m_rotY, 0, 1, 0);
2021-09-27 13:56:29 -04:00
if (rel) transformation.ApplyTranslation(-m_position);
2021-09-27 13:15:57 -04:00
}