SyndicatQuebecoisdelaConstr.../SQCSim2021/player.cpp

107 lines
3.7 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
}
2021-10-02 15:00:48 -04:00
void Player::TurnTopBottom(float value) { 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
2021-10-02 15:00:48 -04:00
if (jump && !jumped) {
m_accjmp = m_jumpheight;
2021-10-01 10:52:33 -04:00
jumped = true;
}
2021-10-02 15:00:48 -04:00
else if (m_position.y > -0.3f && m_accjmp != 0) {
2021-10-01 10:52:33 -04:00
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 {
2021-10-01 11:04:10 -04:00
if (jump) m_accjmp -= elapsedTime * 0.7;
else m_accjmp -= elapsedTime * 0.9;
2021-10-01 10:52:33 -04:00
}
2021-10-02 15:00:48 -04:00
front = false; // Emp<6D>che le d<>placement dans les airs.
back = false;
left = false;
right = false;
}
2021-10-01 10:52:33 -04:00
else m_accjmp = 0;
2021-10-01 11:04:10 -04:00
if (m_position.y < 0) { // Suivi de mouvement pour l'atterrissage.
2021-10-01 10:52:33 -04:00
m_position.y += elapsedTime * 5;
2021-10-02 15:00:48 -04:00
if (m_position.y > 0.f) {
jumped = false;
m_position.y = 0;
}
2021-10-01 10:52:33 -04:00
}
m_position.y += m_accjmp;
2021-10-02 15:00:48 -04:00
if (!jumped || left || right || front || back) {
m_yrotrad = (m_rotY / 57.2957795056f); // 180/Pi = 57.295...
m_xrotrad = (m_rotX / 57.2957795056f);
}
2021-09-27 13:15:57 -04:00
if (front) {
2021-10-02 15:00:48 -04:00
if (m_accWS < m_topspeed) m_accWS += elapsedTime * 30; else m_accWS = m_topspeed;
m_position.x += float(sin(m_yrotrad)) * elapsedTime * m_accWS;
m_position.z += float(-cos(m_yrotrad)) * elapsedTime * m_accWS;
2021-10-01 10:52:33 -04:00
// 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-02 15:00:48 -04:00
if (m_accWS > -m_topspeed) m_accWS -= elapsedTime * 30; else m_accWS = -m_topspeed;
m_position.x += float(-sin(m_yrotrad)) * elapsedTime * -m_accWS;
m_position.z += float(cos(m_yrotrad)) * elapsedTime * -m_accWS;
2021-10-01 10:52:33 -04:00
// m_position.y += float(sin(xrotrad)) * elapsedTime * 20; // <20> remettre pour impl<70>menter une fonction de vol.
}
else if (m_accWS != 0) {
2021-10-02 15:00:48 -04:00
m_accWS = m_accWS > 0 ? m_accWS - elapsedTime * (jumped? 10 : 60) : m_accWS + elapsedTime * (jumped ? 10 : 60);
m_position.x += float(sin(m_yrotrad)) * elapsedTime * m_accWS;
m_position.z += float(-cos(m_yrotrad)) * elapsedTime * m_accWS;
2021-10-01 10:52:33 -04:00
if (m_accWS < 1 && m_accWS > -1) m_accWS = 0;
2021-09-27 13:15:57 -04:00
}
if (left) {
2021-10-02 15:00:48 -04:00
if (m_accAD < m_topspeed) m_accAD += elapsedTime * 30; else m_accAD = m_topspeed;
m_position.x += float(-cos(m_yrotrad)) * elapsedTime * m_accAD;
m_position.z += float(-sin(m_yrotrad)) * elapsedTime * m_accAD;
2021-09-27 13:15:57 -04:00
}
else if (right) {
2021-10-02 15:00:48 -04:00
if (m_accAD > -m_topspeed) m_accAD -= elapsedTime * 30; else m_accAD = -m_topspeed;
m_position.x += float(cos(m_yrotrad)) * elapsedTime * -m_accAD;
m_position.z += float(sin(m_yrotrad)) * elapsedTime * -m_accAD;
2021-10-01 10:52:33 -04:00
}
else if (m_accAD != 0) {
2021-10-02 15:00:48 -04:00
m_accAD = m_accAD > 0 ? m_accAD - elapsedTime * (jumped ? 10 : 60) : m_accAD + elapsedTime * (jumped ? 10 : 60);
m_position.x += float(-cos(m_yrotrad)) * elapsedTime * m_accAD;
m_position.z += float(-sin(m_yrotrad)) * elapsedTime * m_accAD;
2021-10-01 10:52:33 -04:00
if (m_accAD < 1 && m_accAD > -1) m_accAD = 0;
}
2021-10-02 15:00:48 -04:00
// Gestion de si le personnage va en diagonale pour qu'il n'aille pas plus vite que s'il allait en ligne droite: SQRT(15^2+15^2) = ~21.2
if ((m_accWS >= 0? m_accWS: -m_accWS + m_accAD >= 0 ? m_accAD : -m_accAD) > sqrtf(m_topspeed * m_topspeed + m_topspeed * m_topspeed)) {
2021-10-01 11:04:10 -04:00
m_accWS *= 0.9f;
m_accAD *= 0.9f;
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
}