141 lines
4.8 KiB
C++
141 lines
4.8 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_rotY += value;
|
||
if (m_rotY > 360) m_rotY = 0;
|
||
else if (m_rotY < -360) m_rotY = 0;
|
||
}
|
||
|
||
void Player::TurnTopBottom(float value) {
|
||
m_rotX += value;
|
||
if (m_rotX > 45) m_rotX = 45;
|
||
else if (m_rotX < -45) m_rotX = -45;
|
||
}
|
||
|
||
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;
|
||
static bool jumped = true;
|
||
static int dbljump = 0; // Peut sauter ou dasher tant que la variable est en dessous de 2.
|
||
static int dashtimeout = 0;
|
||
static float gametime = 0;
|
||
|
||
if (gametime <= 180.f) gametime += elapsedTime*m_topspeed/2; else gametime = 0;
|
||
|
||
if (dashtimeout > 0) {
|
||
dash = false;
|
||
--dashtimeout;
|
||
}
|
||
|
||
if (jumped && !jump) jumped = false; // Anti-rebondissement du saut, pour pouvoir rebondir.
|
||
|
||
if (dash) dashtimeout = 100;
|
||
|
||
if ((jump || dash) && dbljump < 2 && !jumped ) {
|
||
accjmp += jump? m_jumpforce: 0.1f;
|
||
jumped = true;
|
||
dbljump++;
|
||
}
|
||
else if (m_position.y > -0.2f && accjmp != 0) {
|
||
if (accjmp > 0.f) {
|
||
if (jump) {
|
||
accjmp *= 0.95f - accjmp * elapsedTime;
|
||
accjmp -= elapsedTime * 1.1;
|
||
}
|
||
else {
|
||
accjmp *= 0.75f - accjmp * elapsedTime;
|
||
accjmp -= elapsedTime * 1.1;
|
||
}
|
||
}
|
||
else {
|
||
if (jump) accjmp -= elapsedTime * 0.7;
|
||
else accjmp -= elapsedTime * 0.9;
|
||
}
|
||
}
|
||
else accjmp = 0;
|
||
|
||
if (m_position.y < 0.f) { // Suivi de mouvement pour l'atterrissage.
|
||
m_position.y += elapsedTime * 5.f;
|
||
if (m_position.y > 0.f) {
|
||
dbljump = 0;
|
||
m_position.y = 0;
|
||
}
|
||
}
|
||
|
||
if (dbljump < 1 && ( left || right || front || back) ||
|
||
dash && !(left || right || front || back))
|
||
{
|
||
yrotrad = (m_rotY / 57.2957795056f); // 180/Pi = 57.295...
|
||
xrotrad = (m_rotX / 57.2957795056f);
|
||
if (dash) accWS = m_topspeed; // Pour avoir un boost de vitesse vers l'avant si le dash est appuy<75> seul.
|
||
}
|
||
|
||
m_position.y += accjmp;
|
||
|
||
if (front) {
|
||
if (dbljump == 0)
|
||
if (accWS < m_topspeed) accWS += elapsedTime * 30; else accWS = m_topspeed;
|
||
if (dash) accWS *= accWS > 0.f ? 3.f : -1.f;
|
||
m_position.x += float(sin(yrotrad)) * elapsedTime * accWS;
|
||
m_position.z += float(-cos(yrotrad)) * elapsedTime * accWS;
|
||
m_position.y += (sin(gametime) - 0.5f) * accWS / (20.f * m_topspeed);
|
||
}
|
||
else if (back) {
|
||
if (dbljump == 0)
|
||
if (accWS > -m_topspeed) accWS -= elapsedTime * 30; else accWS = -m_topspeed;
|
||
if (dash) accWS *= accWS < 0.f? 3.f: -1.f;
|
||
m_position.x += float(-sin(yrotrad)) * elapsedTime * -accWS;
|
||
m_position.z += float(cos(yrotrad)) * elapsedTime * -accWS;
|
||
m_position.y += (sin(gametime) - 0.5f) * accWS / (20.f * m_topspeed);
|
||
}
|
||
else if (accWS != 0) {
|
||
accWS = accWS > 0 ? accWS - elapsedTime * (m_position.y > 0.f ? 10 : 120) : accWS + elapsedTime * (m_position.y > 0.f ? 10 : 120);
|
||
|
||
m_position.x += float(sin(yrotrad)) * elapsedTime * accWS;
|
||
m_position.z += float(-cos(yrotrad)) * elapsedTime * accWS;
|
||
m_position.y += (sin(gametime) - 0.5f) * accWS / (30.f * m_topspeed);
|
||
if (accWS < 1 && accWS > -1) accWS = 0;
|
||
}
|
||
|
||
if (left) {
|
||
if (dbljump == 0)
|
||
if (accAD < m_topspeed) accAD += elapsedTime * 30; else accAD = m_topspeed;
|
||
if (dash) accAD *= accAD > 0.f? 3.f: -1.f;
|
||
m_position.x += float(-cos(yrotrad)) * elapsedTime * accAD;
|
||
m_position.z += float(-sin(yrotrad)) * elapsedTime * accAD;
|
||
if (accWS == 0.f) m_position.y += (sin(gametime) - 0.5f) * accAD / (20.f * m_topspeed);
|
||
}
|
||
else if (right) {
|
||
if (dbljump == 0)
|
||
if (accAD > -m_topspeed) accAD -= elapsedTime * 30; else accAD = -m_topspeed;
|
||
if (dash) accAD *= accAD < 0.f ? 3.f : -1.f;
|
||
m_position.x += float(cos(yrotrad)) * elapsedTime * -accAD;
|
||
m_position.z += float(sin(yrotrad)) * elapsedTime * -accAD;
|
||
if (accWS == 0.f) m_position.y += (sin(gametime) - 0.5f) * accAD / (20.f * m_topspeed);
|
||
}
|
||
else if (accAD != 0) {
|
||
accAD = accAD > 0 ? accAD - elapsedTime * (m_position.y > 0.f ? 10 : 120) : accAD + elapsedTime * (m_position.y > 0.f ? 10 : 120);
|
||
m_position.x += float(-cos(yrotrad)) * elapsedTime * accAD;
|
||
m_position.z += float(-sin(yrotrad)) * elapsedTime * accAD;
|
||
if (accWS == 0.f) m_position.y += (sin(gametime) - 0.5f) * accAD / (20.f * m_topspeed);
|
||
if (accAD < 1 && accAD > -1) accAD = 0;
|
||
}
|
||
|
||
// Gestion de si le personnage va en diagonale, qu'il n'aille pas plus vite que s'il allait en ligne droite.
|
||
if ((accWS >= 0.f ? accWS : -accWS + accAD >= 0.f ? accAD : -accAD) > sqrtf(m_topspeed * m_topspeed + m_topspeed * m_topspeed)) {
|
||
accWS *= 0.8f;
|
||
accAD *= 0.8f;
|
||
}
|
||
}
|
||
|
||
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);
|
||
}
|