SQCSimulator2023/SQCSim2021/player.cpp

252 lines
8.3 KiB
C++
Raw Normal View History

2021-09-27 13:15:57 -04:00
#include "player.h"
2021-11-15 20:58:13 -05:00
Player::Player(const Vector3f& position, float rotX, float rotY) : m_position(position), m_rotX(rotX), m_rotY(rotY) {
2021-10-26 17:28:37 -04:00
m_velocity = Vector3f(0, 0, 0);
2021-11-15 20:58:13 -05:00
m_dbljump = 0;
m_airborne = true;
2021-10-26 17:28:37 -04:00
}
2021-09-27 13:15:57 -04:00
2021-11-15 20:58:13 -05:00
void Player::TurnLeftRight(float value) {
m_rotY += value;
2021-10-04 12:29:10 -04:00
if (m_rotY > 360) m_rotY = 0;
else if (m_rotY < -360) m_rotY = 0;
}
2021-11-15 20:58:13 -05:00
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-11-15 20:58:13 -05:00
Vector3f Player::GetInput(bool front, bool back, bool left, bool right, bool jump, bool dash, float elapsedTime) {
2021-10-04 12:29:10 -04:00
static float yrotrad = 0;
static float xrotrad = 0;
static float dashtimeout = 0;
2021-11-15 20:58:13 -05:00
Vector3f delta = Vector3f(0, 0, 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),
2021-11-15 20:58:13 -05:00
-sin(m_rotX / 57.2957795056f),
2021-10-31 00:31:08 -04:00
sin(m_rotY / 57.2957795056f) * cos(m_rotX / 57.2957795056f));
//if (m_airborne && !jump) m_airborne = false; // Anti-rebondissement du saut, pour pouvoir rebondir.
2021-11-15 20:58:13 -05:00
if ((jump || dash) && !m_airborne) {
delta.y += jump ? m_jumpforce : 0.01f;
m_airborne = true;
//m_jumped = true;
2021-11-15 20:58:13 -05:00
m_dbljump++;
}
2021-09-27 13:15:57 -04:00
2021-11-15 20:58:13 -05:00
if (dashtimeout > 0.f) { // Gestion du timeout de dash pour ne pas pouvoir spammer le dash.
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-08 09:27:52 -04:00
if (dash) dashtimeout = 2;
2021-10-04 21:50:54 -04:00
2021-11-15 20:58:13 -05:00
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;
2021-10-01 10:52:33 -04:00
}
2021-10-02 15:00:48 -04:00
}
2021-10-01 10:52:33 -04:00
2021-11-15 20:58:13 -05:00
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;
2021-10-02 15:00:48 -04:00
}
2021-10-04 12:29:10 -04:00
2021-11-15 20:58:13 -05:00
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;
}
2021-10-02 15:00:48 -04:00
}
2021-11-15 20:58:13 -05:00
return delta;
}
2021-10-26 17:28:37 -04:00
2021-11-15 20:58:13 -05:00
//
//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;
2021-09-27 13:15:57 -04:00
}
else if (m_airborne) {
2021-11-15 20:58:13 -05:00
if (abs(m_velocity.y) < 1.1f) m_velocity.y += input.y - 1.1f * elapsedTime;
m_airborne = true;
2021-09-27 13:15:57 -04:00
}
2021-11-15 20:58:13 -05:00
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;
2021-10-01 10:52:33 -04:00
}
2021-11-15 20:58:13 -05:00
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;
2021-09-27 13:15:57 -04:00
}
2021-10-31 00:31:08 -04:00
2021-11-15 20:58:13 -05:00
/* Fin gestion de collisions */
m_position += input;
if (m_airborne) 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));
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-11-15 20:58:13 -05:00
Vector3f Player::GetPOV() const
{
return m_POV;
}
2021-10-31 00:31:08 -04:00
Vector3f Player::GetDirection() const { return m_direction; }