Class: Engine::Components::AudioSource

Inherits:
Engine::Component show all
Defined in:
lib/engine/components/audio_source.rb

Instance Attribute Summary

Attributes inherited from Engine::Component

#game_object

Instance Method Summary collapse

Methods inherited from Engine::Component

#_erase!, #destroy, #destroy!, #destroyed?, destroyed_components, erase_destroyed_components, method_added, #renderer?, #set_game_object, #start, #ui_renderer?

Constructor Details

#initialize(clip, radius: 1000) ⇒ AudioSource

Returns a new instance of AudioSource.



5
6
7
8
9
# File 'lib/engine/components/audio_source.rb', line 5

def initialize(clip, radius: 1000)
  @clip = clip
  @radius = radius
  @source = NativeAudio::AudioSource.new(clip)
end

Instance Method Details

#pauseObject



19
20
21
# File 'lib/engine/components/audio_source.rb', line 19

def pause
  @source.pause
end

#playObject



11
12
13
# File 'lib/engine/components/audio_source.rb', line 11

def play
  @source.play
end

#resumeObject



23
24
25
# File 'lib/engine/components/audio_source.rb', line 23

def resume
  @source.resume
end

#stopObject



15
16
17
# File 'lib/engine/components/audio_source.rb', line 15

def stop
  @source.stop
end

#update(delta_time) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/engine/components/audio_source.rb', line 31

def update(delta_time)
  camera = Engine::Camera.instance
  local_pos = camera.game_object.world_to_local_coordinate(game_object.pos)
  angle = Math.atan2(local_pos[0], -local_pos[2]) * 180 / Math::PI
  angle = (angle + 360) % 360
  distance = (local_pos.magnitude * 255 / @radius).clamp(0, 255)
  
  @source.set_pos(angle, distance)
end

#volume=(volume) ⇒ Object



27
28
29
# File 'lib/engine/components/audio_source.rb', line 27

def volume=(volume)
  @source.set_volume(volume)
end