SyndicatQuebecoisdelaConstr.../SQCSim2021/player.cpp
2022-04-06 23:07:24 -04:00

190 lines
6.2 KiB
C++
Raw Blame History

#include "player.h"
#include "world.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_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 > 80) m_rotX = 80;
else if (m_rotX < -80) m_rotX = -80;
}
Vector3f Player::GetInput(bool front, bool back, bool left, bool right, bool jump, bool shoot, float elapsedTime) {
Vector3f delta = Vector3f(0, 0, 0);
float yrotrad = (m_rotY / 57.2957795056f); // 180/Pi = 57.295...
float xrotrad = (m_rotX / 57.2957795056f);
m_direction = Vector3f(cos(xrotrad) * sin(yrotrad),
-sin(xrotrad),
cos(xrotrad) * -cos(yrotrad));
m_direction.Normalize();
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;
}
delta.Normalize();
delta.x *= .6f;
delta.z *= .6f;
if ((jump || shoot ) && !m_airborne) {
delta.y += jump? .32f: shoot? .1f : 0.f;
m_airborne = true;
}
if (shoot) // Recoil!
TurnTopBottom(-1);
return delta;
}
void Player::ApplyPhysics(Vector3f input, World* world, float elapsedTime, Audio* audio) {
static irrklang::ISound* step; // Pour les sons de pas.
static float timing = 0.f;
/* Gestion de collisions */
BlockType bt1, bt2, bt3;
bt1 = world->BlockAt(GetPosition().x, GetPosition().y + input.y, GetPosition().z);
bt2 = world->BlockAt(GetPosition().x, GetPosition().y + input.y - 0.9f, GetPosition().z);
bt3 = world->BlockAt(GetPosition().x, GetPosition().y + input.y - 1.7f, GetPosition().z);
if ((bt1 != BTYPE_AIR || bt2 != BTYPE_AIR || bt3 != BTYPE_AIR) && m_position.y < 129.7f) {
bt1 = world->BlockAt(GetPosition().x, GetPosition().y + .3f, GetPosition().z);
if (bt1 == BTYPE_AIR) m_position.y = (int)m_position.y + .7f;
m_velocity.y = input.y = 0;
m_airborne = false;
}
else {
if (abs(m_velocity.y) < 1.1f) m_velocity.y += input.y - 1.1f * elapsedTime;
bt3 = world->BlockAt(GetPosition().x, GetPosition().y + m_velocity.y - 1.7f, GetPosition().z);
bt1 = world->BlockAt(GetPosition().x, GetPosition().y + .3f, GetPosition().z);
if (bt3 != BTYPE_AIR) {
m_velocity.y = 0;
if (timing == 0.f) {
if (m_airborne) audio->Create3DAudioObj(step, AUDIO_PATH "hit.wav", GetPosition(), GetVelocity(), 1.f);
timing = .3f;
}
m_airborne = false;
}
else if (bt1 != BTYPE_AIR) {
m_velocity.y = -.1f;
}
else m_airborne = true;
}
if (timing > 0.f) timing -= elapsedTime;
if (timing < 0.f) timing = 0.f;
bt1 = world->BlockAt(GetPosition().x + input.x, GetPosition().y, GetPosition().z);
bt2 = world->BlockAt(GetPosition().x + input.x, GetPosition().y - 0.9f, GetPosition().z);
bt3 = world->BlockAt(GetPosition().x + input.x, GetPosition().y - 1.7f, GetPosition().z);
if (bt1 != BTYPE_AIR || bt2 != BTYPE_AIR || bt3 != BTYPE_AIR) {
input.x = m_velocity.x = 0;
m_velocity.z *= .5f;
}
bt1 = world->BlockAt(GetPosition().x, GetPosition().y, GetPosition().z + input.z);
bt2 = world->BlockAt(GetPosition().x, GetPosition().y - 0.9f, GetPosition().z + input.z);
bt3 = world->BlockAt(GetPosition().x, GetPosition().y - 1.7f, GetPosition().z + input.z);
if (bt1 != BTYPE_AIR || bt2 != BTYPE_AIR || bt3 != BTYPE_AIR) {
input.z = m_velocity.z = 0;
m_velocity.x *= .5f;
}
/* Fin gestion de collisions */
/* Gestion de la friction */
if (!m_airborne) {
m_velocity.x += input.x * 2.f * elapsedTime;
m_velocity.z += input.z * 2.f * elapsedTime;
if (input.x == 0.f)
m_velocity.x *= .8f;
if (input.z == 0.f)
m_velocity.z *= .8f;
}
else {
m_velocity.x += input.x * .4f * elapsedTime; // Techniquement contre les lois de la physique, mais c'est beaucoup moins chiant pour grimper sur les blocs.
m_velocity.z += input.z * .4f * elapsedTime;
m_velocity.x *= .99f;
m_velocity.z *= .99f;
}
/* Fin gestion de la friction */
float vy = m_velocity.y;
m_velocity.y = 1.f; // Padding pour limiter le x et z lors du Normalize().
if (m_velocity.Length() >= 1.f) m_velocity.Normalize(); // Limiteur de vitesse en x/z.
m_velocity.y = 0;
if (m_velocity.Length() < .005f) m_velocity.Zero(); // Threshold en x/z.
m_velocity.y = vy;
m_position += m_velocity;
static float bobbingtime = 0; // Gestion de la cam<61>ra
static bool leftright = false;
static bool isStep = false;
if (bobbingtime <= 360.f)
bobbingtime += elapsedTime * 20.f; else bobbingtime = 0;
if ((sin(bobbingtime) - 0.5f) * (abs(m_velocity.x) + abs(m_velocity.z)) < -.2f && !m_airborne) {
Vector3f vstep;
if (leftright)
vstep = Vector3f(GetPosition().x + GetDirection().z, GetPosition().y - 1.7f, GetPosition().z + GetDirection().x);
else vstep = Vector3f(GetPosition().x - GetDirection().z, GetPosition().y - 1.7f, GetPosition().z - GetDirection().x);
if (!isStep) {
audio->Create3DAudioObj(step, AUDIO_PATH "step.wav", vstep, GetVelocity(), .8f);
leftright = !leftright;
}
isStep = true;
}
else isStep = false;
m_POV = m_position.y;
m_POV += m_airborne ? 0 : (sin(bobbingtime) - 0.5f) * (abs(m_velocity.x) + abs(m_velocity.z)) * .2f;
}
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(-GetPOV());
}
Vector3f Player::GetPosition() const { return Vector3f(m_position.x + CHUNK_SIZE_X * WORLD_SIZE_X / 2, m_position.y, m_position.z + CHUNK_SIZE_Z * WORLD_SIZE_Y / 2); }
Vector3f Player::GetVelocity() const { return m_velocity; }
Vector3f Player::GetPOV() const { return Vector3f(GetPosition().x, m_POV, GetPosition().z); }
Vector3f Player::GetDirection() const { return m_direction; }
void Player::Teleport(int& x, int& z) {
m_position.x -= x * CHUNK_SIZE_X;
m_position.z -= z * CHUNK_SIZE_Z;
}