SQCSimulator2023/SQCSim2021/player.cpp

172 lines
5.5 KiB
C++
Raw Normal View History

2021-09-27 13:15:57 -04:00
#include "player.h"
2021-10-26 17:28:37 -04:00
Player::Player(const Vector3f& position, float rotX, float rotY) : m_position(position), m_rotX(rotX), m_rotY(rotY) {
m_velocity = Vector3f(0, 0, 0);
}
2021-09-27 13:15:57 -04:00
2021-10-04 12:29:10 -04:00
void Player::TurnLeftRight(float value) {
m_rotY += value;
if (m_rotY > 360) m_rotY = 0;
else if (m_rotY < -360) m_rotY = 0;
}
void Player::TurnTopBottom(float value) {
2021-09-27 13:15:57 -04:00
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-04 12:29:10 -04:00
void Player::Move(bool front, bool back, bool left, bool right, bool jump, bool dash, float elapsedTime) {
static float accWS = 0;
static float accAD = 0;
static float accjmp = 0;
static float yrotrad = 0;
static float xrotrad = 0;
2021-10-04 21:50:54 -04:00
static bool jumped = true;
static int dbljump = 0; // Peut sauter ou dasher tant que la variable est en dessous de 2.
static float dashtimeout = 0;
2021-10-31 00:31:08 -04:00
static float bobbingtime = 0;
2021-10-08 08:14:23 -04:00
2021-10-31 00:31:08 -04:00
m_direction = Vector3f(cos(m_rotY / 57.2957795056f) * cos(m_rotX / 57.2957795056f),
-sin(m_rotX / 57.2957795056f),
sin(m_rotY / 57.2957795056f) * cos(m_rotX / 57.2957795056f));
if (bobbingtime <= 360.f) bobbingtime += elapsedTime * m_topspeed / 2; else bobbingtime = 0;
2021-09-27 13:15:57 -04:00
if (dashtimeout > 0.f) {
2021-10-04 15:01:25 -04:00
dash = false;
dashtimeout -= elapsedTime;
2021-10-04 15:01:25 -04:00
}
else dashtimeout = 0;
2021-10-04 15:01:25 -04:00
2021-10-04 21:50:54 -04:00
if (jumped && !jump) jumped = false; // Anti-rebondissement du saut, pour pouvoir rebondir.
2021-10-08 09:27:52 -04:00
if (dash) dashtimeout = 2;
2021-10-04 21:50:54 -04:00
2021-10-12 15:58:54 -04:00
if (dbljump >= 2) jump = false;
if ((jump || dash) && !jumped ) {
2021-10-04 21:50:54 -04:00
accjmp += jump? m_jumpforce: 0.1f;
2021-10-01 10:52:33 -04:00
jumped = true;
2021-10-04 21:50:54 -04:00
dbljump++;
2021-10-01 10:52:33 -04:00
}
2021-10-04 12:29:10 -04:00
else if (m_position.y > -0.2f && accjmp != 0) {
if (accjmp > 0.f) {
2021-10-01 10:52:33 -04:00
if (jump) {
2021-10-04 12:29:10 -04:00
accjmp *= 0.95f - accjmp * elapsedTime;
accjmp -= elapsedTime * 1.1;
2021-10-01 10:52:33 -04:00
}
else {
2021-10-04 12:29:10 -04:00
accjmp *= 0.75f - accjmp * elapsedTime;
accjmp -= elapsedTime * 1.1;
2021-10-01 10:52:33 -04:00
}
}
else {
2021-10-04 12:29:10 -04:00
if (jump) accjmp -= elapsedTime * 0.7;
else accjmp -= elapsedTime * 0.9;
2021-10-01 10:52:33 -04:00
}
2021-10-02 15:00:48 -04:00
}
2021-10-04 12:29:10 -04:00
else accjmp = 0;
2021-10-01 10:52:33 -04:00
2021-10-04 21:50:54 -04:00
if (m_position.y < 0.f) { // Suivi de mouvement pour l'atterrissage.
2021-10-04 12:29:10 -04:00
m_position.y += elapsedTime * 5.f;
2021-10-02 15:00:48 -04:00
if (m_position.y > 0.f) {
2021-10-04 21:50:54 -04:00
dbljump = 0;
2021-10-02 15:00:48 -04:00
m_position.y = 0;
}
2021-10-01 10:52:33 -04:00
}
2021-10-04 12:29:10 -04:00
2021-10-25 10:50:08 -04:00
if ((dbljump < 1 && ( left || right || front || back)) ||
2021-10-31 00:31:08 -04:00
(dash && !(left || right || front || back)) ) {
2021-10-04 12:29:10 -04:00
yrotrad = (m_rotY / 57.2957795056f); // 180/Pi = 57.295...
xrotrad = (m_rotX / 57.2957795056f);
2021-10-04 21:50:54 -04:00
if (dash) accWS = m_topspeed; // Pour avoir un boost de vitesse vers l'avant si le dash est appuy<75> seul.
2021-10-02 15:00:48 -04:00
}
2021-10-08 09:09:04 -04:00
// Ajoute l'acc<63>l<EFBFBD>ration de saut et le view bobbing.
2021-10-31 00:31:08 -04:00
m_velocity.y = accjmp + (sin(bobbingtime) - 0.5f) * ((abs(accWS) + abs(accAD)) / 2.f) / (10.f * m_topspeed);
2021-10-26 17:28:37 -04:00
m_position.y += m_velocity.y;
2021-09-27 13:15:57 -04:00
if (front) {
2021-10-04 22:02:05 -04:00
if (dbljump == 0)
2021-10-04 15:01:25 -04:00
if (accWS < m_topspeed) accWS += elapsedTime * 30; else accWS = m_topspeed;
2021-10-04 22:02:05 -04:00
if (dash) accWS *= accWS > 0.f ? 3.f : -1.f;
2021-10-26 17:28:37 -04:00
m_velocity.x = float(sin(yrotrad)) * elapsedTime * accWS;
m_position.x += m_velocity.x;
m_velocity.z = float(-cos(yrotrad)) * elapsedTime * accWS;
m_position.z += m_velocity.z;
2021-09-27 13:15:57 -04:00
}
2021-10-04 12:29:10 -04:00
else if (back) {
2021-10-04 22:02:05 -04:00
if (dbljump == 0)
2021-10-04 15:01:25 -04:00
if (accWS > -m_topspeed) accWS -= elapsedTime * 30; else accWS = -m_topspeed;
2021-10-04 22:02:05 -04:00
if (dash) accWS *= accWS < 0.f? 3.f: -1.f;
2021-10-26 17:28:37 -04:00
m_velocity.x = float(-sin(yrotrad)) * elapsedTime * -accWS;
m_position.x += m_velocity.x;
m_velocity.z = float(cos(yrotrad)) * elapsedTime * -accWS;
m_position.z += m_velocity.z;
2021-10-01 10:52:33 -04:00
}
2021-10-04 12:29:10 -04:00
else if (accWS != 0) {
accWS = accWS > 0 ? accWS - elapsedTime * (m_position.y > 0.1f ? 10 : 120)
: accWS + elapsedTime * (m_position.y > 0.1f ? 10 : 120);
2021-10-02 15:00:48 -04:00
2021-10-26 17:28:37 -04:00
m_velocity.x = float(sin(yrotrad)) * elapsedTime * accWS;
m_position.x += m_velocity.x;
m_velocity.z = float(-cos(yrotrad)) * elapsedTime * accWS;
m_position.z += m_velocity.z;
2021-10-04 12:29:10 -04:00
if (accWS < 1 && accWS > -1) accWS = 0;
2021-09-27 13:15:57 -04:00
}
2021-10-04 12:29:10 -04:00
if (left) {
2021-10-04 22:02:05 -04:00
if (dbljump == 0)
2021-10-04 15:01:25 -04:00
if (accAD < m_topspeed) accAD += elapsedTime * 30; else accAD = m_topspeed;
2021-10-04 22:02:05 -04:00
if (dash) accAD *= accAD > 0.f? 3.f: -1.f;
2021-10-26 17:28:37 -04:00
m_velocity.x = float(-cos(yrotrad)) * elapsedTime * accAD;
m_position.x += m_velocity.x;
m_velocity.z = float(-sin(yrotrad)) * elapsedTime * accAD;
m_position.z += m_velocity.z;
2021-09-27 13:15:57 -04:00
}
else if (right) {
2021-10-04 22:02:05 -04:00
if (dbljump == 0)
2021-10-04 15:01:25 -04:00
if (accAD > -m_topspeed) accAD -= elapsedTime * 30; else accAD = -m_topspeed;
2021-10-04 22:02:05 -04:00
if (dash) accAD *= accAD < 0.f ? 3.f : -1.f;
2021-10-26 17:28:37 -04:00
m_velocity.x = float(cos(yrotrad)) * elapsedTime * -accAD;
m_position.x += m_velocity.x;
m_velocity.z = float(sin(yrotrad)) * elapsedTime * -accAD;
m_position.z += m_velocity.z;
2021-10-01 10:52:33 -04:00
}
2021-10-04 12:29:10 -04:00
else if (accAD != 0) {
accAD = accAD > 0 ? accAD - elapsedTime * (m_position.y > 0.1f ? 10 : 120)
: accAD + elapsedTime * (m_position.y > 0.1f ? 10 : 120);
2021-10-26 17:28:37 -04:00
m_velocity.x = float(-cos(yrotrad)) * elapsedTime * accAD;
m_position.x += m_velocity.x;
m_velocity.z = float(-sin(yrotrad)) * elapsedTime * accAD;
m_position.z += m_velocity.z;
if (accAD < 1.f && accAD > -1.f) accAD = 0;
2021-10-01 10:52:33 -04:00
}
2021-10-04 12:29:10 -04:00
// Gestion de si le personnage va en diagonale, qu'il n'aille pas plus vite que s'il allait en ligne droite.
2021-10-31 00:31:08 -04:00
if (abs(accAD) + abs(accWS) > sqrtf(exp2f(m_topspeed) * 2)) {
2021-10-08 08:46:39 -04:00
accWS *= 0.8f;
accAD *= 0.8f;
2021-09-27 13:15:57 -04:00
}
2021-10-31 00:31:08 -04:00
// Threshold de v<>locit<69>.
if (abs(m_velocity.x) < 0.02f) m_velocity.x = 0;
if (abs(m_velocity.y) < 0.02f) m_velocity.y = 0;
if (abs(m_velocity.z) < 0.02f) m_velocity.z = 0;
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
}
2021-10-25 10:50:08 -04:00
Vector3f Player::GetPosition() const { return m_position; }
2021-10-26 17:28:37 -04:00
Vector3f Player::GetVelocity() const { return m_velocity; }
2021-10-31 00:31:08 -04:00
Vector3f Player::GetDirection() const { return m_direction; }