SyndicatQuebecoisdelaConstr.../SQCSim2021/player.cpp
MarcEricMartel bb62651c62 HALLELUJAH!!
2021-11-15 20:58:13 -05:00

252 lines
8.2 KiB
C++
Raw Blame History

#include "player.h"
Player::Player(const Vector3f& position, float rotX, float rotY) : m_position(position), m_rotX(rotX), m_rotY(rotY) {
m_velocity = Vector3f(0, 0, 0);
m_dbljump = 0;
m_airborne = true;
}
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;
}
Vector3f Player::GetInput(bool front, bool back, bool left, bool right, bool jump, bool dash, float elapsedTime) {
static float yrotrad = 0;
static float xrotrad = 0;
static float dashtimeout = 0;
Vector3f delta = Vector3f(0, 0, 0);
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 (m_airborne && !jump) m_airborne = false; // Anti-rebondissement du saut, pour pouvoir rebondir.
if ((jump || dash) && !m_airborne) {
delta.y += jump ? m_jumpforce : 0.01f;
m_airborne = true;
m_dbljump++;
}
if (dashtimeout > 0.f) { // Gestion du timeout de dash pour ne pas pouvoir spammer le dash.
dash = false;
dashtimeout -= elapsedTime;
}
else dashtimeout = 0;
if (dash) dashtimeout = 2;
if ((m_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) {
delta.x += float(sin(yrotrad)) * elapsedTime;
delta.z += float(-cos(yrotrad)) * elapsedTime;
}
}
if (!m_airborne) {
if (front) {
delta.x += float(sin(yrotrad)) * elapsedTime * 10.f;
delta.z += float(-cos(yrotrad)) * elapsedTime * 10.f;
}
else if (back) {
delta.x -= float(-sin(yrotrad)) * elapsedTime * -10.f;
delta.z -= float(cos(yrotrad)) * elapsedTime * -10.f;
}
if (left) {
delta.x += float(-cos(yrotrad)) * elapsedTime * 10.f;
delta.z += float(-sin(yrotrad)) * elapsedTime * 10.f;
}
else if (right) {
delta.x -= float(cos(yrotrad)) * elapsedTime * -10.f;
delta.z -= float(sin(yrotrad)) * elapsedTime * -10.f;
}
}
return delta;
}
//
//Vector3f Player::GetInput(bool front, bool back, bool left, bool right, bool jump, bool dash, float elapsedTime) {
// static float accWS = 0;
// static float accAD = 0;
// static float yrotrad = 0;
// static float xrotrad = 0;
// static float dashtimeout = 0;
//
// Vector3f movepos = m_velocity;
//
// 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 (dashtimeout > 0.f) { // Gestion du timeout de dash pour ne pas pouvoir spammer le dash.
// dash = false;
// dashtimeout -= elapsedTime;
// }
// else dashtimeout = 0;
// if (dash) dashtimeout = 2;
//
// if (m_airborne && !jump) m_airborne = false; // Anti-rebondissement du saut, pour pouvoir rebondir.
//
// if (m_dbljump >= 2) jump = false;
//
// if ((jump || dash) && !m_airborne ) {
// m_velocity.y += jump? m_jumpforce: 0.1f;
// movepos.y += m_velocity.y;
// m_airborne = true;
// m_dbljump++;
// }
//
// if ((m_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.
// }
//
// if (front) {
// if (m_dbljump == 0)
// if (accWS < m_topspeed) accWS += elapsedTime * 30; else accWS = m_topspeed;
// if (dash) accWS *= accWS > 0.f ? 3.f : -1.f;
// m_velocity.x = float(sin(yrotrad)) * elapsedTime * accWS;
// movepos.x += m_velocity.x;
// m_velocity.z = float(-cos(yrotrad)) * elapsedTime * accWS;
// movepos.z += m_velocity.z;
// }
// else if (back) {
// if (m_dbljump == 0)
// if (accWS > -m_topspeed) accWS -= elapsedTime * 30; else accWS = -m_topspeed;
// if (dash) accWS *= accWS < 0.f? 3.f: -1.f;
// m_velocity.x = float(-sin(yrotrad)) * elapsedTime * -accWS;
// movepos.x += m_velocity.x;
// m_velocity.z = float(cos(yrotrad)) * elapsedTime * -accWS;
// movepos.z += m_velocity.z;
// }
// 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);
//
// m_velocity.x = float(sin(yrotrad)) * elapsedTime * accWS;
// movepos.x += m_velocity.x;
// m_velocity.z = float(-cos(yrotrad)) * elapsedTime * accWS;
// movepos.z += m_velocity.z;
// if (accWS < 1 && accWS > -1) accWS = 0;
// }
//
// if (left) {
// if (m_dbljump == 0)
// if (accAD < m_topspeed) accAD += elapsedTime * 30; else accAD = m_topspeed;
// if (dash) accAD *= accAD > 0.f? 3.f: -1.f;
// m_velocity.x = float(-cos(yrotrad)) * elapsedTime * accAD;
// movepos.x += m_velocity.x;
// m_velocity.z = float(-sin(yrotrad)) * elapsedTime * accAD;
// movepos.z += m_velocity.z;
// }
// else if (right) {
// if (m_dbljump == 0)
// if (accAD > -m_topspeed) accAD -= elapsedTime * 30; else accAD = -m_topspeed;
// if (dash) accAD *= accAD < 0.f ? 3.f : -1.f;
// m_velocity.x = float(cos(yrotrad)) * elapsedTime * -accAD;
// movepos.x += m_velocity.x;
// m_velocity.z = float(sin(yrotrad)) * elapsedTime * -accAD;
// movepos.z += m_velocity.z;
// }
// 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);
//
// m_velocity.x = float(-cos(yrotrad)) * elapsedTime * accAD;
// movepos.x += m_velocity.x;
// m_velocity.z = float(-sin(yrotrad)) * elapsedTime * accAD;
// movepos.z += m_velocity.z;
// if (accAD < 1.f && accAD > -1.f) 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 (abs(accAD) + abs(accWS) > sqrtf(exp2f(m_topspeed) * 2)) {
// accWS *= 0.8f;
// accAD *= 0.8f;
// }
//
// return movepos;
//}
void Player::ApplyPhysics(Vector3f input, World world, float elapsedTime) {
/* Gestion de collisions */
BlockType bt1, bt2, bt3;
bt1 = world.BlockAt(m_position.x, m_position.y + input.y, m_position.z);
bt2 = world.BlockAt(m_position.x, m_position.y + input.y - 0.9f, m_position.z);
bt3 = world.BlockAt(m_position.x, m_position.y + input.y - 1.7f, m_position.z);
if (bt1 != BTYPE_AIR || bt2 != BTYPE_AIR || bt3 != BTYPE_AIR) {
m_dbljump = 0;
if (input.y < 0.f) {
input.y = 0;
}
m_velocity.y = input.y;
if (!m_airborne) m_position.y = (int)m_position.y + .7f;
m_airborne = false;
}
else {
if (abs(m_velocity.y) < 1.1f) m_velocity.y += input.y - 1.1f * elapsedTime;
m_airborne = true;
}
bt1 = world.BlockAt(m_position.x + input.x, m_position.y, m_position.z);
bt2 = world.BlockAt(m_position.x + input.x, m_position.y - 0.9f, m_position.z);
bt3 = world.BlockAt(m_position.x + input.x, m_position.y - 1.7f, m_position.z);
if (bt1 != BTYPE_AIR || bt2 != BTYPE_AIR || bt3 != BTYPE_AIR) {
input.x = 0;
}
bt1 = world.BlockAt(m_position.x, m_position.y, m_position.z + input.z);
bt2 = world.BlockAt(m_position.x, m_position.y - 0.9f, m_position.z + input.z);
bt3 = world.BlockAt(m_position.x, m_position.y - 1.7f, m_position.z + input.z);
if (bt1 != BTYPE_AIR || bt2 != BTYPE_AIR || bt3 != BTYPE_AIR) {
input.z = 0;
}
/* Fin gestion de collisions */
m_position += input;
m_POV = input;
m_position.y += m_velocity.y;
//static float bobbingtime = 0; // Gestion de la cam<61>ra
//if (bobbingtime <= 360.f) bobbingtime += elapsedTime * m_topspeed / 2; else bobbingtime = 0;
//m_POV.x = m_position.x;
//m_POV.z = m_position.z;
//m_POV.y = m_position.y - 1.7f + m_airborne ? 0 : ((sin(bobbingtime) - 0.5f) * (abs(m_velocity.x * 40) + abs(m_velocity.z * 40)) / (10.f * m_topspeed));
}
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);
}
Vector3f Player::GetPosition() const { return m_position; }
Vector3f Player::GetVelocity() const { return m_velocity; }
Vector3f Player::GetPOV() const
{
return m_POV;
}
Vector3f Player::GetDirection() const { return m_direction; }