SQCSimulator2023/SQCSim-common/player.cpp

303 lines
8.0 KiB
C++
Raw Normal View History

2021-09-27 13:15:57 -04:00
#include "player.h"
2021-12-01 21:21:45 -05:00
#include "world.h"
2021-09-27 13:15:57 -04:00
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_airborne = true;
m_hp = 1.0f; //TODO: Remettre <20> 1.0f
m_username = "Zelda Bee-Bop56";
2021-10-26 17:28:37 -04:00
}
2021-09-27 13:15:57 -04:00
2023-10-03 12:43:54 -04:00
Player::~Player() {}
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;
2023-12-05 13:44:54 -05:00
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();
2021-10-04 12:29:10 -04:00
}
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-11-27 13:35:39 -05:00
if (m_rotX > 80) m_rotX = 80;
else if (m_rotX < -80) m_rotX = -80;
2021-10-08 08:14:23 -04:00
2021-11-16 20:48:52 -05:00
float yrotrad = (m_rotY / 57.2957795056f); // 180/Pi = 57.295...
float xrotrad = (m_rotX / 57.2957795056f);
2021-10-31 00:31:08 -04:00
2021-11-27 13:35:39 -05:00
m_direction = Vector3f(cos(xrotrad) * sin(yrotrad),
2023-12-05 13:44:54 -05:00
-sin(xrotrad),
cos(xrotrad) * -cos(yrotrad));
2021-11-27 13:35:39 -05:00
m_direction.Normalize();
2023-12-05 13:44:54 -05:00
}
Vector3f Player::GetInput(bool front, bool back, bool left, bool right, bool jump, bool shoot, float elapsedTime) {
Vector3f delta = Vector3f(0, 0, 0);
Vector3f dir = m_direction;
dir.y = 0;
2021-09-27 13:15:57 -04:00
2021-11-16 20:48:52 -05:00
if (front) {
2023-12-05 13:44:54 -05:00
delta += dir;
2021-11-16 20:48:52 -05:00
}
else if (back) {
2023-12-05 13:44:54 -05:00
delta -= dir;
2021-10-04 15:01:25 -04:00
}
2021-10-04 21:50:54 -04:00
2021-11-16 20:48:52 -05:00
if (left) {
2023-12-05 13:44:54 -05:00
delta.x += dir.z;
delta.z += -dir.x;
2021-11-16 20:48:52 -05:00
}
else if (right) {
2023-12-05 13:44:54 -05:00
delta.x -= dir.z;
delta.z -= -dir.x;
2021-10-02 15:00:48 -04:00
}
2021-10-01 10:52:33 -04:00
2021-11-16 20:48:52 -05:00
delta.Normalize();
delta.x *= .6f;
delta.z *= .6f;
2021-10-04 12:29:10 -04:00
2021-12-07 18:59:50 -05:00
if ((jump || shoot ) && !m_airborne) {
delta.y += jump? .32f: shoot? .1f : 0.f;
2021-11-16 20:48:52 -05:00
m_airborne = true;
2021-10-02 15:00:48 -04:00
}
2023-10-30 14:03:10 -04:00
if (boostspeed)
{
delta.x += STRENGTH_SPEED_BOOST / 100 * delta.x;
delta.z += STRENGTH_SPEED_BOOST / 100 * delta.z;
}
2021-10-02 15:00:48 -04:00
2021-12-07 18:59:50 -05:00
if (shoot) // Recoil!
TurnTopBottom(-1);
2021-11-15 20:58:13 -05:00
return delta;
}
2021-10-26 17:28:37 -04:00
2023-09-30 14:46:54 -04:00
Player::Sound Player::ApplyPhysics(Vector3f input, World* world, float elapsedTime) {
Player::Sound snd = Player::Sound::NOSOUND;
2021-12-07 18:59:50 -05:00
static float timing = 0.f;
2021-11-16 20:48:52 -05:00
/* Gestion de collisions */
2021-11-15 20:58:13 -05:00
BlockType bt1, bt2, bt3;
2021-11-15 22:54:01 -05:00
2022-04-06 23:07:24 -04:00
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);
2021-11-27 14:14:05 -05:00
if ((bt1 != BTYPE_AIR || bt2 != BTYPE_AIR || bt3 != BTYPE_AIR) && m_position.y < 129.7f) {
2022-04-06 23:07:24 -04:00
bt1 = world->BlockAt(GetPosition().x, GetPosition().y + .3f, GetPosition().z);
2021-11-15 22:54:01 -05:00
if (bt1 == BTYPE_AIR) m_position.y = (int)m_position.y + .7f;
m_velocity.y = input.y = 0;
2021-11-15 20:58:13 -05:00
m_airborne = false;
2021-09-27 13:15:57 -04:00
}
2021-11-15 22:54:01 -05:00
else {
2021-11-15 20:58:13 -05:00
if (abs(m_velocity.y) < 1.1f) m_velocity.y += input.y - 1.1f * elapsedTime;
2022-04-06 23:07:24 -04:00
bt3 = world->BlockAt(GetPosition().x, GetPosition().y + m_velocity.y - 1.7f, GetPosition().z);
bt1 = world->BlockAt(GetPosition().x, GetPosition().y + .3f, GetPosition().z);
2021-11-15 22:54:01 -05:00
if (bt3 != BTYPE_AIR) {
m_velocity.y = 0;
2021-12-07 18:59:50 -05:00
if (timing == 0.f) {
2023-09-30 14:46:54 -04:00
if (m_airborne) snd = Player::Sound::FALL;
2021-12-07 18:59:50 -05:00
timing = .3f;
}
2021-11-15 22:54:01 -05:00
m_airborne = false;
}
2021-11-16 20:48:52 -05:00
else if (bt1 != BTYPE_AIR) {
m_velocity.y = -.1f;
}
2021-11-15 22:54:01 -05:00
else m_airborne = true;
2021-09-27 13:15:57 -04:00
}
2021-12-07 18:59:50 -05:00
if (timing > 0.f) timing -= elapsedTime;
if (timing < 0.f) timing = 0.f;
2022-04-06 23:07:24 -04:00
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) {
if (input.x > 0)
input.x = m_velocity.x = 0.5f;
else
input.x = m_velocity.x = -0.5f;
m_velocity.y = 0.3;
m_velocity.z *= .5f;
2023-09-24 08:45:40 -04:00
}
else if (bt1 != BTYPE_AIR || bt2 != BTYPE_AIR || bt3 != BTYPE_AIR) {
2021-11-16 20:48:52 -05:00
input.x = m_velocity.x = 0;
m_velocity.z *= .5f;
2021-10-01 10:52:33 -04:00
}
2022-04-06 23:07:24 -04:00
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) {
if (input.z > 0)
input.z = m_velocity.z = 0.5f;
else
input.z = m_velocity.z = -0.5f;
m_velocity.y = 0.3;
m_velocity.x *= .5f;
2023-09-24 08:45:40 -04:00
}
else if (bt1 != BTYPE_AIR || bt2 != BTYPE_AIR || bt3 != BTYPE_AIR) {
2021-11-16 20:48:52 -05:00
input.z = m_velocity.z = 0;
m_velocity.x *= .5f;
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 */
2021-11-16 20:48:52 -05:00
/* Gestion de la friction */
if (!m_airborne) {
m_velocity.x += input.x * 2.f * elapsedTime;
m_velocity.z += input.z * 2.f * elapsedTime;
2023-09-30 14:46:54 -04:00
if (input.x == 0.f)
m_velocity.x *= .8f;
2023-09-30 14:46:54 -04:00
if (input.z == 0.f)
m_velocity.z *= .8f;
2021-11-16 20:48:52 -05:00
}
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;
2021-11-15 20:58:13 -05:00
2021-11-15 22:54:01 -05:00
m_position += m_velocity;
static float bobbingtime = 0; // Gestion de la cam<61>ra
2021-12-07 18:59:50 -05:00
static bool leftright = false;
static bool isStep = false;
2023-09-30 14:46:54 -04:00
if (bobbingtime <= 360.f)
2021-12-07 18:59:50 -05:00
bobbingtime += elapsedTime * 20.f; else bobbingtime = 0;
2023-09-30 14:46:54 -04:00
2021-12-07 18:59:50 -05:00
if ((sin(bobbingtime) - 0.5f) * (abs(m_velocity.x) + abs(m_velocity.z)) < -.2f && !m_airborne) {
if (!isStep) {
2023-09-30 14:46:54 -04:00
snd = Player::Sound::STEP;
2021-12-07 18:59:50 -05:00
}
isStep = true;
}
else isStep = false;
2021-11-16 20:48:52 -05:00
m_POV = m_position.y;
m_POV += m_airborne ? 0 : (sin(bobbingtime) - 0.5f) * (abs(m_velocity.x) + abs(m_velocity.z)) * .2f;
2023-10-30 14:03:10 -04:00
RemoveBooster(elapsedTime);
2023-09-30 14:46:54 -04:00
return snd;
2021-09-27 13:15:57 -04:00
}
2023-11-19 16:46:13 -05:00
void Player::ApplyTransformation(Transformation& transformation, bool rel, bool rot) const {
2021-10-01 10:52:33 -04:00
transformation.ApplyRotation(-m_rotX, 1, 0, 0);
transformation.ApplyRotation(-m_rotY, 0, 1, 0);
2023-12-04 16:45:46 -05:00
2021-12-01 21:21:45 -05:00
if (rel) transformation.ApplyTranslation(-GetPOV());
2023-12-04 16:45:46 -05:00
2021-09-27 13:15:57 -04:00
}
2021-10-25 10:50:08 -04:00
2023-10-30 14:03:10 -04:00
void Player::GetBooster(Booster boosttype)
{
if (boosttype == SPEED)
{
boostspeed = true;
timeboostspeed = 0;
}
if (boosttype == HEAL)
{
m_hp = 100;
}
if (boosttype == DAMAGE)
{
boostdamage = true;
timeboostdamage = 0;
}
if (boosttype == INVINCIBLE)
{
boostinvincible = true;
boostinvincible = 0;
}
}
void Player::RemoveBooster(float elapsedtime)
{
if (boostspeed)
{
timeboostspeed += elapsedtime;
if (timeboostspeed >= TIME_SPEED_BOOST)
boostspeed = false;
}
if (boostdamage)
{
timeboostdamage += elapsedtime;
if (timeboostdamage >= TIME_DAMAGE_BOOST)
boostdamage = false;
}
if (boostinvincible)
{
timeboostinvincible += elapsedtime;
if (timeboostinvincible >= TIME_INVINCIBLE_BOOST)
boostinvincible = false;
}
}
void Player::SetDirection(Vector3f dir) { m_direction = dir; }
2023-10-30 14:03:10 -04:00
2023-12-05 13:44:54 -05:00
void Player::Move(Vector3f diff) { m_position -= diff; }
2021-12-06 12:05:05 -05:00
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); }
2021-10-25 10:50:08 -04:00
2023-12-05 13:44:54 -05:00
Vector3f Player::GetPositionAbs() const { return m_position; }
2021-10-26 17:28:37 -04:00
Vector3f Player::GetVelocity() const { return m_velocity; }
2021-12-01 21:21:45 -05:00
Vector3f Player::GetPOV() const { return Vector3f(GetPosition().x, m_POV, GetPosition().z); }
2021-11-15 20:58:13 -05:00
2021-10-31 00:31:08 -04:00
Vector3f Player::GetDirection() const { return m_direction; }
2021-12-01 21:21:45 -05:00
std::string Player::GetUsername() const { return m_username; }
float Player::GetHP() const { return m_hp; }
2021-12-03 11:49:59 -05:00
void Player::Teleport(int& x, int& z) {
2021-12-01 21:21:45 -05:00
m_position.x -= x * CHUNK_SIZE_X;
m_position.z -= z * CHUNK_SIZE_Z;
}
2023-12-07 12:32:36 -05:00
bool Player::GetIsAirborne() const { return m_airborne; }
2023-10-30 14:36:44 -04:00
2023-12-07 12:32:36 -05:00
bool Player::AmIDead() { return m_hp <= 0; }
2023-10-23 15:43:55 -04:00
2023-12-06 11:16:39 -05:00
void Player::InflictDamage(float hitPoints) {
2023-10-30 14:36:44 -04:00
m_hp -= hitPoints;
2023-12-06 13:42:15 -05:00
if (m_hp < 0)
m_hp == 0;
2023-12-06 11:16:39 -05:00
//if (AmIDead())
//{ // Quand le joueur est mort.
//}
}
2023-10-23 15:43:55 -04:00
2023-12-06 11:16:39 -05:00
int Player::getScore() const { return m_score; }
2023-10-30 14:56:04 -04:00
2023-12-06 11:16:39 -05:00
void Player::addPoint() { ++m_score; }
2023-10-23 15:43:55 -04:00
2023-10-03 12:43:54 -04:00
2023-10-27 13:55:15 -04:00
uint64_t Player::getId() const { return id; }
2023-10-03 12:43:54 -04:00
Vector3f Player::InterpolatePosition(const Vector3f& vec1, const Vector3f& vec2, const Timestamp& tim1, const Timestamp& tim2, const Timestamp& now) {
return Vector3f();
}