Skip to content

Commit

Permalink
[Forest] Added foosteps sound when walking
Browse files Browse the repository at this point in the history
  • Loading branch information
PanosK92 committed Dec 2, 2023
1 parent f64cdd1 commit 27a3fc9
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 11 deletions.
Binary file added assets/music/footsteps_grass.mp3
Binary file not shown.
4 changes: 2 additions & 2 deletions data/shaders/light.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ float3 subsurface_scattering(Surface surface, Light light)
float3 color_a = float3(0.6, 0.8, 0.5);
float3 color_b = float3(0.3, 0.6, 0.2);
float angle_factor = pow(backlit, 1.5f);
float3 sss_color = lerp(color_a, color_b, angle_factor);
float3 sss_color = lerp(color_a, color_b, angle_factor) * sss_effect;

float fresnel = dot(surface.normal, -light.to_pixel);

return surface.albedo * sss_color * sss_effect * fresnel * light.radiance;
return surface.albedo * sss_color * fresnel * light.radiance;
}

[numthreads(THREAD_GROUP_COUNT_X, THREAD_GROUP_COUNT_Y, 1)]
Expand Down
8 changes: 8 additions & 0 deletions runtime/World/Components/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,14 @@ namespace Spartan
m_physics_body_to_control = physics_body;
}

bool Camera::IsWalking()
{
if (!m_physics_body_to_control || !m_physics_body_to_control->RayTraceIsGrounded())
return false;

return m_physics_body_to_control->GetLinearVelocity().LengthSquared() > 0.01f;
}

Matrix Camera::ComputeViewMatrix() const
{
const auto position = GetEntity()->GetPosition();
Expand Down
1 change: 1 addition & 0 deletions runtime/World/Components/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ namespace Spartan
void SetPhysicsBodyToControl(PhysicsBody* physics_body);

// Misc
bool IsWalking();
void MakeDirty() { m_is_dirty = true; }
void SetSelectedEntity(std::shared_ptr<Spartan::Entity> entity) { m_selected_entity = entity; }
std::shared_ptr<Spartan::Entity> GetSelectedEntity() { return m_selected_entity.lock(); }
Expand Down
51 changes: 42 additions & 9 deletions runtime/World/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ namespace Spartan
m_resolve = false;
}

// default world logic
// forest default world logic (maybe we could create a default_world class to house all logic and objects)
{
if (!m_default_terrain)
return;
Expand All @@ -511,18 +511,40 @@ namespace Spartan
if (!terrain)
return;

AudioSource* audio_source = m_default_terrain->GetDescendantByName("underwater")->GetComponent<AudioSource>().get();
if (!audio_source)
return;

bool is_below_water_level = camera->GetEntity()->GetPosition().y < terrain->GetWaterLevel();
if (is_below_water_level && !audio_source->IsPlaying())

// underwater sound
{
audio_source->Play();
AudioSource* audio_source = m_default_terrain->GetDescendantByName("underwater")->GetComponent<AudioSource>().get();
if (!audio_source)
return;


if (is_below_water_level && !audio_source->IsPlaying())
{
audio_source->Play();
}
else if (!is_below_water_level && audio_source->IsPlaying())
{
audio_source->Stop();
}
}
else if (!is_below_water_level && audio_source->IsPlaying())

// footsteps
if (!is_below_water_level)
{
audio_source->Stop();
AudioSource* audio_source = m_default_terrain->GetDescendantByName("footsteps")->GetComponent<AudioSource>().get();
if (!audio_source)
return;

if (camera->IsWalking() && !audio_source->IsPlaying())
{
audio_source->Play();
}
else if (!camera->IsWalking() && audio_source->IsPlaying())
{
audio_source->Stop();
}
}
}
}
Expand Down Expand Up @@ -846,6 +868,17 @@ namespace Spartan
entity->SetObjectName("audio");
entity->SetParent(m_default_terrain.get());

// footsteps grass
{
shared_ptr<Entity> sound = World::CreateEntity();
sound->SetObjectName("footsteps");
sound->SetParent(entity);

shared_ptr<AudioSource> audio_source = sound->AddComponent<AudioSource>();
audio_source->SetAudioClip("project\\music\\footsteps_grass.mp3");
audio_source->SetPlayOnStart(false);
}

// forest and river sounds
{
shared_ptr<Entity> sound = World::CreateEntity();
Expand Down

0 comments on commit 27a3fc9

Please sign in to comment.