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?

Methods included from Serializable

allowed_class?, get_class, included, register_class, #uuid

Instance Method Details

#awakeObject



7
8
9
10
11
# File 'lib/engine/components/audio_source.rb', line 7

def awake
  @radius ||= 1000
  @clip = NativeAudio::Clip.new(@clip_path)
  @source = NativeAudio::AudioSource.new(@clip)
end

#pauseObject



21
22
23
# File 'lib/engine/components/audio_source.rb', line 21

def pause
  @source.pause
end

#playObject



13
14
15
# File 'lib/engine/components/audio_source.rb', line 13

def play
  @source.play
end

#resumeObject



25
26
27
# File 'lib/engine/components/audio_source.rb', line 25

def resume
  @source.resume
end

#stopObject



17
18
19
# File 'lib/engine/components/audio_source.rb', line 17

def stop
  @source.stop
end

#update(delta_time) ⇒ Object



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

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



29
30
31
# File 'lib/engine/components/audio_source.rb', line 29

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