Start Page » DragonScript Scripting Language » Abstraction Layers: How you want to build your Game » Behavior Elements » ECBehaviorHearSounds
Behavior element behavior adding hearing sounds support.
Uses a SoundLevelMeter resources to hear what sounds are playing in the audible range of the element.
SoundLevelMeter resources take sound properties of the environment into account like occlusion and how sound propagates through the world geometry. This allows game entities to hear the world similar to how a player hears the world what goes for sound volumes.
The SoundLevelMeter resource can be queried for the speakers currently in audible range and the volume they exhibit. Advanced effects audible to players like frequency band specific gains or special effects are disregarded. Furthermore speakers entering and leaving trigger a notification allowing elements to track elements of interest.
If the ECBehaviorCollider is present in the behavior element the SoundLevelMeter is attached to the collider.
The behavior retrieves the owner of Speakers inside the audible range. If these owners are elements they are stored in a list of elements heared by the owner element. This list is updated when elements enter or leave the audible range. The volume of an element can be queried if required but is not stored.
The owner behavior element as well as other ECBehavior subclasses can add a listener to be notified if elements enter and exit the audible range.
This behavior can be used only once on an element.
Element class properties have the prefix hearSound.
.
Audible distance in meters.
hearSound.audibleDistance
100
1
<float name='hearSound.audibleDistance'>50</float>
Volume threshold required for elements to be considered audible.
hearSound.volumeThreshold
0.01
1
<float name='hearSound.volumeThreshold'>0.02</float>
Position to attach resource to collider.
hearSound.position
or hearSound({id}).position
<vector name='hearSound.position' x='0' y='0' z='0.1'/>
Orientation to attach resource to collider in degrees.
hearSound.orientation
or hearSound({id}).orientation
<vector name='hearSound.orientation' x='30' y='0' z='0'/>
Bone to attach resource to. If empty string attach to collider.
hearSound.bone
or hearSound({id}).bone
<string name='hearSound.bone'>attach</string>
Element become audible.
Element became inaudible.
This behavior requires no other behaviors.
This behavior does support element class to be persistable (setPersistable).
Since DragonScript Module Version 1.0
This example defines an element which can detect hearing sounds.
class MyElement extends BehaviorElementClass public var ECBehaviorComponent component public var ECBehaviorCollider collider public var ECBehaviorHearSounds hearSounds func new() component = ECBehaviorComponent.new(this, null) collider = ECBehaviorCollider.new(this, component) headSounds = ECBehaviorHearSounds.new(this, collider) end end
Using element class supporting adding behaviors the behavior can be added like this:
<?xml version='1.0' encoding='UTF-8'?> <elementClass name='MyClass' class='GenericBehaviorElement'> <behavior type='ECBehaviorComponent'/> <behavior type='ECBehaviorCollider'/> <behavior type='ECBehaviorHearSounds'> <!-- set element properties. omit property prefix if used inside behavior tag --> <float name='.audibleDistance'>50</float> </behavior> </elementClass>