SQCSimulator2023/SQCSim2021/player.cpp
Marc-Eric Martel 1371d62b3a !
2021-10-01 11:04:10 -04:00

105 lines
3.7 KiB
C++

#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;
if (m_rotX > 45) m_rotX = 45;
else if (m_rotX < -45) m_rotX = -45;
}
void Player::TurnTopBottom(float value) {
m_rotY += value;
}
void Player::Move(bool front, bool back, bool left, bool right, bool jump, float elapsedTime) {
float xrotrad, yrotrad;
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; // Bloque les sauts à répétition en tenant le bouton de saut.
if (m_position.y < 0) { // Suivi de mouvement pour l'atterrissage.
m_position.y += elapsedTime * 5;
if (m_position.y > 0.f) m_position.y = 0;
}
m_position.y += m_accjmp;
if (front) {
if (m_accWS < 10) m_accWS += elapsedTime * 30; else m_accWS = 10;
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;
// m_position.y += float(-sin(xrotrad)) * elapsedTime * 20; // à remettre pour implémenter une fonction de vol.
}
else if (back) {
if (m_accWS > -10) m_accWS -= elapsedTime * 30; else m_accWS = -10;
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;
// m_position.y += float(sin(xrotrad)) * elapsedTime * 20; // à remettre pour implé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;
}
if (left) {
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 (right) {
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;
yrotrad = (m_rotY / 180 * 3.141592654f);
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;
}
// Gestion de si le personnage va en diagonale pour qu'il n'aille pas plus vite que s'il allait en ligne droite: SQRT(10^2+10^2) = ~12.
if ((m_accWS >= 0? m_accWS: -m_accWS + m_accAD >= 0 ? m_accAD : -m_accAD) > 12) {
m_accWS *= 0.9f;
m_accAD *= 0.9f;
}
}
void Player::ApplyTransformation(Transformation& transformation, bool rel) const {
transformation.ApplyRotation(-m_rotX, 1, 0, 0);
transformation.ApplyRotation(-m_rotY, 0, 1, 0);
if (rel) transformation.ApplyTranslation(-m_position);
}