Dart Documentationsystem_simple_audioSystem_Audio

System_Audio class

class System_Audio extends EntityProcessingSystem {
 ComponentMapper<Transform> _transformMapper;
 ComponentMapper<AudioDef> _objDefMapper;
 ComponentMapper<_AudioCache> _objCacheMapper;

 var _listener;
 var _positional = false;
 final AudioManager _audioManager;

 ClipProvider _clipProvider;

 /// [clipProvider] is the function to find AudioClip from name,
 /// default implementation is [clipProvider0]
 System_Audio(this._audioManager, {clipProvider, positional : false}):
   super(Aspect.getAspectForAllOf([AudioDef])),
   _positional = positional {
   _clipProvider = (clipProvider == null) ? this.clipProvider0 : clipProvider;
 }

 void initialize(){
   _transformMapper = new ComponentMapper<Transform>(Transform, world);
   _objDefMapper = new ComponentMapper<AudioDef>(AudioDef, world);
   _objCacheMapper = new ComponentMapper<_AudioCache>(_AudioCache, world);
 }

 /// a default implementation for clipProvider, that retreive clip from [AudioManager]
 ///   (x) = > _audioManager.findClip(x);
 AudioClip clipProvider0(x) => _audioManager.findClip(x);

 void processEntity(entity) {
   var cache = _objCacheMapper.getSafe(entity);
   if (cache != null) {
     var obj = cache.source;
     _applyTransform(obj, entity);
     var def = _objDefMapper.get(entity);
     def.l.iterateAndUpdate((x) {
       var clip = _clipProvider(x);
       if (clip == null) {
         //TODO log("can't play sound '${x}' : notfound in audioManager nor assetManager");
       } else {
         //TODO log("play ${x} : ${_assetManager.getAssetAtPath(x).url} : ${clip}");
         if (obj != _listener) {
           obj.playOnce(clip);
         } else {
           _audioManager.music.stop();
           _audioManager.music.clip = clip;
           _audioManager.music.play(loop : false);
         }
       }
       return null;
     });
   }
 }

 void inserted(Entity entity){
   var objDef = _objDefMapper.get(entity);
   var obj = _audioManager.makeSource(entity.id.toString());
   entity.addComponent(new _AudioCache(obj));
   entity.changedInWorld();
   _applyTransform(obj, entity);
   if (objDef.isAudioListener) {
     //log("set audiolistener");
     _listener = obj;
   }
 }

 void removed(Entity entity){
   var cache = _objCacheMapper.getSafe(entity);
   if (cache != null) {
     cache.source.stop();
     if (cache.source == _listener) _listener = null;
   }
   entity.removeComponent(_AudioCache);
   //log("removed audio ${entity}");
 }

 void _applyTransform(obj, Entity entity) {
   if (! _positional) return;
   var tf = _transformMapper.getSafe(entity);
   if (obj != null && tf != null) {
     obj.positional = true;
     obj.setPosition(tf.position3d.x, tf.position3d.y, tf.position3d.z);
     if (obj == _listener) {
       _audioManager.setPosition(tf.position3d.x, tf.position3d.y, tf.position3d.z);
     }
   }
 }

}

Extends

EntitySystem > EntityProcessingSystem > System_Audio

Constructors

new System_Audio(AudioManager _audioManager, {clipProvider, positional: false}) #

clipProvider is the function to find AudioClip from name, default implementation is clipProvider0

System_Audio(this._audioManager, {clipProvider, positional : false}):
 super(Aspect.getAspectForAllOf([AudioDef])),
 _positional = positional {
 _clipProvider = (clipProvider == null) ? this.clipProvider0 : clipProvider;
}

Properties

final passive #

inherited from EntitySystem
get passive => _passive;

World world #

inherited from EntitySystem
World world

Methods

void added(Entity e) #

inherited from EntitySystem
void added(Entity e) => _check(e);

void begin() #

inherited from EntitySystem

Called before processing of entities begins.

void begin() {}

void changed(Entity e) #

inherited from EntitySystem
void changed(Entity e) => _check(e);

bool checkProcessing() #

inherited from EntityProcessingSystem

Returns true if the system should be processed, false if not.

docs inherited from EntitySystem
bool checkProcessing() => true;

AudioClip clipProvider0(x) #

a default implementation for clipProvider, that retreive clip from AudioManager (x) = > _audioManager.findClip(x);

AudioClip clipProvider0(x) => _audioManager.findClip(x);

void deleted(Entity e) #

inherited from EntitySystem
void deleted(Entity e) {
 if (_contains(e)) {
   _removeFromSystem(e);
 }
}

void disabled(Entity e) #

inherited from EntitySystem
void disabled(Entity e) {
 if (_contains(e)) {
   _removeFromSystem(e);
 }
}

void enabled(Entity e) #

inherited from EntitySystem
void enabled(Entity e) => _check(e);

void end() #

inherited from EntitySystem

Called after the processing of entities ends.

void end() {}

void initialize() #

Override to implement code that gets executed when systems are initialized.

docs inherited from EntitySystem
void initialize(){
 _transformMapper = new ComponentMapper<Transform>(Transform, world);
 _objDefMapper = new ComponentMapper<AudioDef>(AudioDef, world);
 _objCacheMapper = new ComponentMapper<_AudioCache>(_AudioCache, world);
}

void inserted(Entity entity) #

Called if the system has received an entity it is interested in, e.g. created or a component was added to it.

docs inherited from EntitySystem
void inserted(Entity entity){
 var objDef = _objDefMapper.get(entity);
 var obj = _audioManager.makeSource(entity.id.toString());
 entity.addComponent(new _AudioCache(obj));
 entity.changedInWorld();
 _applyTransform(obj, entity);
 if (objDef.isAudioListener) {
   //log("set audiolistener");
   _listener = obj;
 }
}

void process() #

inherited from EntitySystem

This is the only method that is supposed to be called from outside the library,

void process() {
 if(checkProcessing()) {
   begin();
   processEntities(_actives.readOnly);
   end();
 }
}

void processEntities(ReadOnlyBag<Entity> entities) #

inherited from EntityProcessingSystem

Any implementing entity system must implement this method and the logic to process the given entities of the system.

docs inherited from EntitySystem
void processEntities(ReadOnlyBag<Entity> entities) => entities.forEach((entity) => processEntity(entity));

void processEntity(entity) #

Process a entity this system is interested in.

docs inherited from EntityProcessingSystem
void processEntity(entity) {
 var cache = _objCacheMapper.getSafe(entity);
 if (cache != null) {
   var obj = cache.source;
   _applyTransform(obj, entity);
   var def = _objDefMapper.get(entity);
   def.l.iterateAndUpdate((x) {
     var clip = _clipProvider(x);
     if (clip == null) {
       //TODO log("can't play sound '${x}' : notfound in audioManager nor assetManager");
     } else {
       //TODO log("play ${x} : ${_assetManager.getAssetAtPath(x).url} : ${clip}");
       if (obj != _listener) {
         obj.playOnce(clip);
       } else {
         _audioManager.music.stop();
         _audioManager.music.clip = clip;
         _audioManager.music.play(loop : false);
       }
     }
     return null;
   });
 }
}

void removed(Entity entity) #

Called if an entity was removed from this system, e.g. deleted or had one of it's components removed.

docs inherited from EntitySystem
void removed(Entity entity){
 var cache = _objCacheMapper.getSafe(entity);
 if (cache != null) {
   cache.source.stop();
   if (cache.source == _listener) _listener = null;
 }
 entity.removeComponent(_AudioCache);
 //log("removed audio ${entity}");
}