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
Methods
void begin() #
Called before processing of entities begins.
void begin() {}
bool checkProcessing() #
Returns true if the system should be processed, false if not.
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) #
void deleted(Entity e) {
if (_contains(e)) {
_removeFromSystem(e);
}
}
void disabled(Entity e) #
void disabled(Entity e) {
if (_contains(e)) {
_removeFromSystem(e);
}
}
void end() #
Called after the processing of entities ends.
void end() {}
void initialize() #
Override to implement code that gets executed when systems are initialized.
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.
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() #
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) #
Any implementing entity system must implement this method and the logic to process the given entities of the system.
void processEntities(ReadOnlyBag<Entity> entities) => entities.forEach((entity) => processEntity(entity));
void processEntity(entity) #
Process a entity this system is interested in.
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.
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}");
}