Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/new-api/assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ var Audio = gomp.CreateAssetLibrary(

assert.True(rl.IsSoundValid(sound), "Error loading sound")

// Mute sound by default. Later it controlled by Aduio systems
rl.SetSoundVolume(sound, 0)

return sound
},
func(path string, asset *rl.Sound) {
Expand Down
Binary file added examples/new-api/assets/satellite_main_sound.wav
Binary file not shown.
18 changes: 17 additions & 1 deletion examples/new-api/components/audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ import (
)

type SoundEffect struct {
Clip *rl.Sound
// audio clip from assets
Clip *rl.Sound
// internal flag, should be false by default
IsPlaying bool
// should sound be looped. Default is false. If not looped, entity will be destroyed
IsLooping bool
// base is 1.0
Volume float32
Expand All @@ -36,3 +39,16 @@ type SoundEffectsComponentManager = ecs.ComponentManager[SoundEffect]
func NewSoundEffectsComponentManager() SoundEffectsComponentManager {
return ecs.NewComponentManager[SoundEffect](SoundEffectManagerComponentId)
}

type SpatialAudio struct {
// follows raylib rules. Base is 1.0
Volume float32
// follows raylib rules. Base is 0.5, 1.0 is left, 0.0 is right
Pan float32
}

type SpatialAudioComponentManager = ecs.ComponentManager[SpatialAudio]

func NewSpatialAudioComponentManager() SpatialAudioComponentManager {
return ecs.NewComponentManager[SpatialAudio](SpatialAudioManagerComponentId)
}
1 change: 1 addition & 0 deletions examples/new-api/components/ids.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
SpaceshipIntentComponentId
AsteroidSceneManagerComponentId
SoundEffectManagerComponentId
SpatialAudioManagerComponentId
TextureRectComponentId
TextureCircleComponentId
)
14 changes: 13 additions & 1 deletion examples/new-api/entities/satellite.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ Thank you for your support!
package entities

import (
rl "github.com/gen2brain/raylib-go/raylib"
"gomp/examples/new-api/assets"
"gomp/examples/new-api/components"
"gomp/examples/new-api/config"
"gomp/pkg/ecs"
"gomp/stdcomponents"
"gomp/vectors"
"image/color"

rl "github.com/gen2brain/raylib-go/raylib"
)

type CreateSatelliteManagers struct {
Expand All @@ -34,6 +36,7 @@ type CreateSatelliteManagers struct {
RigidBodies *stdcomponents.RigidBodyComponentManager
Velocities *stdcomponents.VelocityComponentManager
Renderables *stdcomponents.RenderableComponentManager
SoundEffects *components.SoundEffectsComponentManager
}

func CreateSatellite(
Expand Down Expand Up @@ -91,5 +94,14 @@ func CreateSatellite(
CameraMask: config.MainCameraLayer | config.MinimapCameraLayer,
})

props.SoundEffects.Create(entity, components.SoundEffect{
Clip: assets.Audio.Get("satellite_main_sound.wav"),
IsLooping: true,
IsPlaying: false,
Volume: 0.05,
Pan: 0.5,
Pitch: 1.0,
})

return entity
}
2 changes: 1 addition & 1 deletion examples/new-api/entities/spaceship.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func CreateSpaceShip(
IsPlaying: false,
IsLooping: true,
Pitch: 1.0,
Volume: 1.0,
Volume: 0.0,
Pan: 0.5,
})

Expand Down
3 changes: 3 additions & 0 deletions examples/new-api/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func (g *Game) Init(engine *core.Engine) {
systems.AssetLib.Init()
systems.Audio.Init()
systems.SpatialAudio.Init()
systems.AudioSettings.Init()
systems.RenderCameras.Init()
systems.Culling.Init()
systems.TexturePositionSmooth.Init()
Expand Down Expand Up @@ -128,6 +129,7 @@ func (g *Game) Render(dt time.Duration) {
systems.YSort.Run()
systems.Audio.Run(dt)
systems.SpatialAudio.Run(dt)
systems.AudioSettings.Run(dt)

scene.Render(dt)

Expand Down Expand Up @@ -160,6 +162,7 @@ func (g *Game) Destroy() {
systems.AssetLib.Destroy()
systems.Audio.Destroy()
systems.SpatialAudio.Destroy()
systems.AudioSettings.Destroy()
systems.RenderCameras.Destroy()
systems.Culling.Destroy()
systems.TexturePositionSmooth.Destroy()
Expand Down
2 changes: 2 additions & 0 deletions examples/new-api/instances/component-list.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type ComponentList struct {
SpaceshipIntent components.SpaceshipIntentComponentManager
AsteroidSceneManager components.AsteroidSceneManagerComponentManager
SoundEffects components.SoundEffectsComponentManager
SpatialAudio components.SpatialAudioComponentManager
TextureRect components.TextureRectComponentManager
PrimitiveCircle components.PrimitiveCircleComponentManager
RenderVisible stdcomponents.RenderVisibleComponentManager
Expand Down Expand Up @@ -114,6 +115,7 @@ func NewComponentList() ComponentList {
SpaceshipIntent: components.NewSpaceshipIntentComponentManager(),
AsteroidSceneManager: components.NewAsteroidSceneManagerComponentManager(),
SoundEffects: components.NewSoundEffectsComponentManager(),
SpatialAudio: components.NewSpatialAudioComponentManager(),
TextureRect: components.NewTextureRectComponentManager(),
PrimitiveCircle: components.NewTextureCircleComponentManager(),
}
Expand Down
2 changes: 2 additions & 0 deletions examples/new-api/instances/system-list.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func NewSystemList() SystemList {
Player: systems.NewPlayerSystem(),
RenderBogdan: systems.NewRenderBogdanSystem(),
Audio: systems.NewAudioSystem(),
AudioSettings: systems.NewAudioSettingsSystem(),
SpatialAudio: systems.NewSpatialAudioSystem(),
DampingSystem: systems.NewDampingSystem(),
AssteroddSystem: systems.NewAssteroddSystem(),
Expand Down Expand Up @@ -90,6 +91,7 @@ type SystemList struct {
RenderBogdan systems.RenderBogdanSystem
Player systems.PlayerSystem
Audio systems.AudioSystem
AudioSettings systems.AudioSettingsSystem
SpatialAudio systems.SpatialAudioSystem
DampingSystem systems.DampingSystem
AssteroddSystem systems.AssteroddSystem
Expand Down
1 change: 1 addition & 0 deletions examples/new-api/systems/asterodd.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func (s *AssteroddSystem) Init() {
BoxColliders: s.BoxColliders,
RigidBodies: s.RigidBodies,
Renderables: s.Renderables,
SoundEffects: s.SoundEffects,
}, 500, 500, 0)

entities.CreateSpaceSpawner(entities.CreateSpaceSpawnerManagers{
Expand Down
90 changes: 71 additions & 19 deletions examples/new-api/systems/audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ package systems

import (
"gomp/examples/new-api/components"
"gomp/pkg/core"
"gomp/pkg/ecs"
"gomp/pkg/worker"
"time"

"github.com/negrel/assert"

rl "github.com/gen2brain/raylib-go/raylib"
)

Expand All @@ -27,59 +31,107 @@ func NewAudioSystem() AudioSystem {
}

type AudioSystem struct {
EntityManager *ecs.EntityManager
SoundEffects *components.SoundEffectsComponentManager
EntityManager *ecs.EntityManager
SoundEffects *components.SoundEffectsComponentManager
accSoundEffectsDelete [][]ecs.Entity
numWorkers int
Engine *core.Engine
}

func (s *AudioSystem) Init() {
s.numWorkers = s.Engine.Pool().NumWorkers()
s.accSoundEffectsDelete = make([][]ecs.Entity, s.numWorkers)
rl.InitAudioDevice()
}

func (s *AudioSystem) Run(dt time.Duration) {
s.SoundEffects.EachEntity()(func(entity ecs.Entity) bool {
for a := range s.accSoundEffectsDelete {
s.accSoundEffectsDelete[a] = s.accSoundEffectsDelete[a][:0]
}

s.SoundEffects.ProcessEntities(func(entity ecs.Entity, workerId worker.WorkerId) {
soundEffect := s.SoundEffects.GetUnsafe(entity)
assert.NotNil(soundEffect)

clip := soundEffect.Clip

// check if clip is valid
if clip == nil || clip.FrameCount == 0 {
return true
// check if clip is loaded
if clip == nil || !rl.IsSoundValid(*clip) {
return
}

if !soundEffect.IsPlaying {
if rl.IsSoundPlaying(*clip) {
rl.StopSound(*clip)
return true
return
} else {
*clip = rl.LoadSoundAlias(*clip)

rl.SetSoundVolume(*clip, soundEffect.Volume)
rl.SetSoundPitch(*clip, soundEffect.Pitch)
rl.SetSoundPan(*clip, soundEffect.Pan)

rl.PlaySound(*clip)
soundEffect.IsPlaying = true
return true
return
}
}

rl.SetSoundVolume(*clip, soundEffect.Volume)
rl.SetSoundPitch(*clip, soundEffect.Pitch)
rl.SetSoundPan(*clip, soundEffect.Pan)

// check if sound is over
if !rl.IsSoundPlaying(*clip) && soundEffect.IsPlaying {
if soundEffect.IsLooping {
rl.PlaySound(*clip)
} else {
// sound is over, remove entity
s.EntityManager.Delete(entity)
s.accSoundEffectsDelete[workerId] = append(s.accSoundEffectsDelete[workerId], entity)

// rl.UnloadSoundAlias(*clip) // TODO: this doesn't work https://github.com/gen2brain/raylib-go/issues/494
}
}

return true
})

for a := range s.accSoundEffectsDelete {
for _, entity := range s.accSoundEffectsDelete[a] {
s.EntityManager.Delete(entity)
}
}
}
func (s *AudioSystem) Destroy() {
rl.CloseAudioDevice()
}

func NewAudioSettingsSystem() AudioSettingsSystem {
return AudioSettingsSystem{}
}

type AudioSettingsSystem struct {
EntityManager *ecs.EntityManager
SoundEffects *components.SoundEffectsComponentManager
SpatialAudio *components.SpatialAudioComponentManager
}

func (s *AudioSettingsSystem) Init() {}

func (s *AudioSettingsSystem) Run(dt time.Duration) {
s.SoundEffects.ProcessEntities(func(entity ecs.Entity, workerId worker.WorkerId) {
soundEffect := s.SoundEffects.GetUnsafe(entity)
assert.NotNil(soundEffect)

clip := soundEffect.Clip

// check if clip is loaded
if clip == nil {
return
}

spatialSettings := s.SpatialAudio.GetUnsafe(entity)

if spatialSettings != nil {
rl.SetSoundVolume(*clip, spatialSettings.Volume*soundEffect.Volume)
rl.SetSoundPan(*clip, spatialSettings.Pan)
} else {
rl.SetSoundVolume(*clip, soundEffect.Volume)
rl.SetSoundPan(*clip, soundEffect.Pan)
}

rl.SetSoundPitch(*clip, soundEffect.Pitch)
})
}
func (s *AudioSettingsSystem) Destroy() {
}
10 changes: 10 additions & 0 deletions examples/new-api/systems/spaceship-intents.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,16 @@ func (s *SpaceshipIntentsSystem) Run(dt time.Duration) {
Volume: 1.0,
Pan: 0.5,
})

s.Positions.Create(
fireSoundEntity,
stdcomponents.Position{
XY: vectors.Vec2{
X: pos.XY.X,
Y: pos.XY.Y,
},
},
)
}
} else {
weapon.CooldownLeft -= dt
Expand Down
Loading
Loading