diff --git a/SQCSim-common/bullet.cpp b/SQCSim-common/bullet.cpp index 5c4e6c4..873879d 100644 --- a/SQCSim-common/bullet.cpp +++ b/SQCSim-common/bullet.cpp @@ -7,8 +7,9 @@ Bullet::Bullet(Vector3f pos, Vector3f dir, uint64_t tid): m_startpos(pos), m_cur Bullet::~Bullet() {} -bool Bullet::Update(World* world, float elapsedtime) { - for (int x = 0; x < 1000; ++x) { +bool Bullet::Update(World* world, float elapsedtime, int perframe) { + int max = 100 / perframe; + for (int x = 0; x < max; ++x) { m_currentpos += m_velocity * elapsedtime; if (!world->ChunkAt(m_currentpos)) @@ -30,10 +31,14 @@ void Bullet::Transpose(int& x, int& z) { m_startpos.z -= z * CHUNK_SIZE_Z; } -Vector3f Bullet::getPos() { +Vector3f Bullet::getPos() const { return m_currentpos; } +Vector3f Bullet::getVel() const { + return m_velocity; +} + uint64_t Bullet::getTeamID(){ return m_tid; } diff --git a/SQCSim-common/bullet.h b/SQCSim-common/bullet.h index e3ae989..1b5e227 100644 --- a/SQCSim-common/bullet.h +++ b/SQCSim-common/bullet.h @@ -12,9 +12,10 @@ public: Bullet(Vector3f pos, Vector3f dir, uint64_t tid); ~Bullet(); - bool Update(World* world, float elapsedtime); + bool Update(World* world, float elapsedtime, int perframe); void Transpose(int& x, int& z); - Vector3f getPos(); + Vector3f getPos() const; + Vector3f getVel() const; uint64_t getTeamID(); private: diff --git a/SQCSim2021/audio.cpp b/SQCSim2021/audio.cpp index b6a066b..e849c6f 100644 --- a/SQCSim2021/audio.cpp +++ b/SQCSim2021/audio.cpp @@ -2,17 +2,17 @@ Audio::Audio() { m_engine = irrklang::createIrrKlangDevice(); - m_engine->setDopplerEffectParameters(1); - m_engine->setRolloffFactor(1); - m_engine->setDefault3DSoundMinDistance(1); + m_engine->setDopplerEffectParameters(10); + m_engine->setRolloffFactor(2); + m_engine->setDefault3DSoundMinDistance(.1); m_engine->setDefault3DSoundMaxDistance(1000); } Audio::Audio(const char * music) { m_engine = irrklang::createIrrKlangDevice(); m_engine->setDopplerEffectParameters(1); - m_engine->setRolloffFactor(1); - m_engine->setDefault3DSoundMinDistance(1); + m_engine->setRolloffFactor(2); + m_engine->setDefault3DSoundMinDistance(.1); m_engine->setDefault3DSoundMaxDistance(1000); m_music = m_engine->play2D(music, false, true, true, irrklang::ESM_STREAMING); } @@ -28,10 +28,11 @@ void Audio::Update3DAudio(Vector3f pos, Vector3f dir, Vector3f vel) { irrklang::vec3df(vel.x, vel.y, vel.z)); } -void Audio::Create3DAudioObj(irrklang::ISound* sound, const char* name, Vector3f pos, Vector3f vel, float volume = 1) { - sound = m_engine->play3D(name, irrklang::vec3df(pos.x, pos.y, pos.z), false, false, true, irrklang::ESM_NO_STREAMING, true); +irrklang::ISound* Audio::Create3DAudioObj(irrklang::ISound* sound, const char* name, Vector3f pos, Vector3f vel, bool is_looped = false, float volume = 1) { + sound = m_engine->play3D(name, irrklang::vec3df(pos.x, pos.y, pos.z), is_looped, false, true, is_looped? irrklang::ESM_STREAMING: irrklang::ESM_NO_STREAMING, true); sound->setVelocity(irrklang::vec3df(vel.x, vel.y, vel.z)); sound->setVolume(volume); + return sound; } void Audio::Render3DAudioObj(irrklang::ISound* sound, Vector3f& pos, Vector3f& vel, float volume = 1) { diff --git a/SQCSim2021/audio.h b/SQCSim2021/audio.h index b0ff044..78c01b4 100644 --- a/SQCSim2021/audio.h +++ b/SQCSim2021/audio.h @@ -8,17 +8,17 @@ class Audio { private: - irrklang::ISoundEngine* m_engine; irrklang::ISound* m_music; public: Audio(); Audio(const char* music); ~Audio(); + irrklang::ISoundEngine* m_engine; void Update3DAudio(Vector3f pos, Vector3f dir, Vector3f speed); - void Create3DAudioObj(irrklang::ISound* sound, const char* name, Vector3f pos, Vector3f vel, float volume); + irrklang::ISound* Create3DAudioObj(irrklang::ISound* sound, const char* name, Vector3f pos, Vector3f vel, bool is_looped, float volume); void Render3DAudioObj(irrklang::ISound* sound, Vector3f& pos, Vector3f& vel, float volume); diff --git a/SQCSim2021/define.h b/SQCSim2021/define.h index 9c4a040..d775f14 100644 --- a/SQCSim2021/define.h +++ b/SQCSim2021/define.h @@ -20,6 +20,8 @@ #define SRV_ADDR "127.0.0.1" #define COUNTDOWN 300 +#define BULLET_UPDATES_PER_FRAME 20 + #define BASE_WIDTH 1920 #define BASE_HEIGHT 1080 diff --git a/SQCSim2021/engine.cpp b/SQCSim2021/engine.cpp index f44eb96..6b8dfd3 100644 --- a/SQCSim2021/engine.cpp +++ b/SQCSim2021/engine.cpp @@ -61,8 +61,10 @@ void Engine::Init() { //m_audio.ToggleMusicState(); // Array pour les balles. - for (int x = 0; x < MAX_BULLETS; ++x) + for (int x = 0; x < MAX_BULLETS; ++x) { m_bullets[x] = nullptr; + m_whoosh[x] = nullptr; // = m_audio.m_engine.m_audio.m_engine->addSoundSourceFromFile(AUDIO_PATH "noise.ogg", irrklang::ESM_AUTO_DETECT, false); + } uint64_t seed = SEED; std::string playname = "La Chienne à Jacques"; @@ -84,6 +86,8 @@ void Engine::Init() { // Init Chunks m_world.GetChunks().Reset(nullptr); + + // Gestion de souris. CenterMouse(); HideCursor(); @@ -474,11 +478,11 @@ void Engine::Render(float elapsedTime) { if (leftright) vstep = Vector3f(m_player.GetPosition().x + m_player.GetDirection().z, m_player.GetPosition().y - 1.7f, m_player.GetPosition().z + m_player.GetDirection().x); else vstep = Vector3f(m_player.GetPosition().x - m_player.GetDirection().z, m_player.GetPosition().y - 1.7f, m_player.GetPosition().z - m_player.GetDirection().x); - m_audio.Create3DAudioObj(step, AUDIO_PATH "step.wav", vstep, m_player.GetVelocity(), .8f); + m_audio.Create3DAudioObj(step, AUDIO_PATH "step.wav", vstep, m_player.GetVelocity(), false, .8f); leftright = !leftright; break; case Player::Sound::FALL: - m_audio.Create3DAudioObj(step, AUDIO_PATH "hit.wav", m_player.GetPosition(), m_player.GetVelocity(), 1.f); + m_audio.Create3DAudioObj(step, AUDIO_PATH "hit.wav", m_player.GetPosition(), m_player.GetVelocity(), false, 1.f); break; default: break; } @@ -512,7 +516,7 @@ void Engine::Render(float elapsedTime) { m_bullets[0] = new Bullet(m_player.GetPOV() + m_player.GetDirection(), m_player.GetDirection()); } bulletTime = .1f; - m_audio.Create3DAudioObj(m_powpow, AUDIO_PATH "windowsaccount.wav", m_player.GetPOV(), m_player.GetDirection() * 10, .5f); + m_audio.Create3DAudioObj(m_powpow, AUDIO_PATH "windowsaccount.wav", m_player.GetPOV(), m_player.GetDirection() * 10, false, .5f); if (m_flash) { // Coupe le rendering et affiche un frame blanc, pour simuler un flash. glClearColor(.8f, .8f, .8f, 1.f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); @@ -523,13 +527,24 @@ void Engine::Render(float elapsedTime) { } else if (m_mouseR) m_world.ChangeBlockAtCursor(BTYPE_AIR, m_player.GetPosition(), m_player.GetDirection(), m_block); - - for (int x = 0; x < MAX_BULLETS; ++x) // Array de bullets en jeu. - if (m_bullets[x]) - if (m_bullets[x]->Update(&m_world, elapsedTime)) { - m_bullets[x]->~Bullet(); - m_bullets[x] = nullptr; - } + + for (int x = 0; x < MAX_BULLETS; ++x) { // Array de bullets en jeu. + if (m_bullets[x]) { + for (int b = 0; b < BULLET_UPDATES_PER_FRAME; ++b) + if (m_bullets[x]->Update(&m_world, elapsedTime, BULLET_UPDATES_PER_FRAME)) { + m_bullets[x]->~Bullet(); + if (m_whoosh[x]) + m_whoosh[x]->drop(); + m_bullets[x] = nullptr; + m_whoosh[x] = nullptr; + break; + } + else if (!m_whoosh[x]) { + m_whoosh[x] = m_audio.Create3DAudioObj(m_whoosh[x], AUDIO_PATH "noise.wav", m_bullets[x]->getPos(), m_bullets[x]->getVel(), true, (m_bullets[x]->getPos() - m_player.GetPosition()).Length()); + } + else m_audio.Render3DAudioObj(m_whoosh[x], m_bullets[x]->getPos(), m_bullets[x]->getVel(), 5 - (m_bullets[x]->getPos() - m_player.GetPosition()).Length()); + } + } m_wrenderer.RenderWorld(&m_world, m_renderCount, m_player.GetPosition(), m_player.GetDirection(), all, m_shader01, m_textureAtlas); m_world.Update(m_bullets, m_player.GetPosition(), m_blockinfo); @@ -542,7 +557,7 @@ void Engine::Render(float elapsedTime) { static bool fell = false; if (m_player.GetPosition().y < 1.7f && !fell) { - m_audio.Create3DAudioObj(m_scream, AUDIO_PATH "scream.wav", m_player.GetPOV(), m_player.GetVelocity(), 1.f); + m_audio.Create3DAudioObj(m_scream, AUDIO_PATH "scream.wav", m_player.GetPOV(), m_player.GetVelocity(), false, 1.f); fell = true; } else if (m_player.GetPosition().y < -20.f) { @@ -604,6 +619,8 @@ void Engine::KeyPressEvent(unsigned char key) { break; case 8: // I - Ignorer break; + case 15: // P - Ignorer + break; case 17: // R - Ignorer break; case 19: // T - Ignorer @@ -653,6 +670,17 @@ void Engine::KeyReleaseEvent(unsigned char key) { case 12: // M - Toggle music m_audio.ToggleMusicState(); break; + case 15: + for (int x = 0; x < MAX_BULLETS; ++x) // Ajouter une balle dans l'array (aussi connu sous le nom de "faire pow pow"). + if (!m_bullets[x]) { + m_bullets[x] = new Bullet(m_player.GetPOV() - Vector3f(1.f, 0.f, 1.f), Vector3f(1.f,0.f,1.f)); + break; + } + else if (x == MAX_BULLETS - 1) { // S'il y a pas d'espace dans l'array, prendre la place de la première balle de l'array. + m_bullets[0]->~Bullet(); + m_bullets[0] = new Bullet(m_player.GetPOV() - Vector3f(1.f, 0.f, 1.f), Vector3f(1.f, 0.f, 1.f)); + } + break; case 17: // R - Toggle skybox m_isSkybox = !m_isSkybox; break; diff --git a/SQCSim2021/engine.h b/SQCSim2021/engine.h index fa3a088..e5e395d 100644 --- a/SQCSim2021/engine.h +++ b/SQCSim2021/engine.h @@ -70,8 +70,9 @@ private: Skybox m_skybox; Audio m_audio = Audio(AUDIO_PATH "start.wav"); - irrklang::ISound* m_powpow; - irrklang::ISound* m_scream; + irrklang::ISound* m_powpow, + * m_scream; + irrklang::ISound *m_whoosh[MAX_BULLETS]; Player m_player = Player(Vector3f(.5f, CHUNK_SIZE_Y + 1.8f, .5f)); diff --git a/SQCSim2021/media/audio/noise.wav b/SQCSim2021/media/audio/noise.wav new file mode 100644 index 0000000..6c22f27 Binary files /dev/null and b/SQCSim2021/media/audio/noise.wav differ diff --git a/SQCSim2021/worldrenderer.cpp b/SQCSim2021/worldrenderer.cpp index dcf6d45..e4e0333 100644 --- a/SQCSim2021/worldrenderer.cpp +++ b/SQCSim2021/worldrenderer.cpp @@ -211,3 +211,4 @@ void WorldRenderer::UpdateWorld(World* origin, const Vector3f& player, BlockInfo } } } +