Search in sources :

Example 6 with SoundDesc

use of com.bladecoder.engine.model.SoundDesc in project bladecoder-adventure-engine by bladecoder.

the class EditSoundDialog method inputsToModel.

@Override
protected void inputsToModel(boolean create) {
    if (create) {
        e = new SoundDesc();
        // UNDO OP
        Ctx.project.getUndoStack().add(new UndoCreateSound(e));
    } else {
        HashMap<String, SoundDesc> sounds = parent.getSounds();
        sounds.remove(e.getId());
    }
    String checkedId = parent.getSounds() == null ? id.getText() : ElementUtils.getCheckedId(id.getText(), parent.getSounds().keySet().toArray(new String[parent.getSounds().size()]));
    e.setId(checkedId);
    e.setFilename(filename.getText());
    e.setLoop(Boolean.parseBoolean(loop.getText()));
    e.setVolume(Float.parseFloat(volume.getText()));
    e.setPan(Float.parseFloat(pan.getText()));
    e.setPitch(Float.parseFloat(pitch.getText()));
    e.setPreload(Boolean.parseBoolean(preload.getText()));
    parent.getSounds().put(e.getId(), e);
    Ctx.project.setModified();
}
Also used : UndoCreateSound(com.bladecoder.engineeditor.undo.UndoCreateSound) SoundDesc(com.bladecoder.engine.model.SoundDesc)

Example 7 with SoundDesc

use of com.bladecoder.engine.model.SoundDesc in project bladecoder-adventure-engine by bladecoder.

the class WorldSerialization method cacheSounds.

private void cacheSounds() {
    for (Scene s : w.getScenes().values()) {
        HashMap<String, Verb> verbs = s.getVerbManager().getVerbs();
        // Search SoundAction and PlaySoundAction
        for (Verb v : verbs.values()) {
            ArrayList<Action> actions = v.getActions();
            for (int i = 0; i < actions.size(); i++) {
                Action act = actions.get(i);
                try {
                    if (act instanceof SoundAction) {
                        String actor = ActionUtils.getStringValue(act, "actor");
                        String play = ActionUtils.getStringValue(act, "play");
                        if (play != null) {
                            if (actor.equals("$PLAYER"))
                                actor = s.getPlayer().getId();
                            SoundDesc sd = w.getSounds().get(actor + "_" + play);
                            if (sd == null) {
                                EngineLogger.error("Reference to sound not found: " + s.getId() + "." + actor + "." + play);
                                continue;
                            }
                            s.getSoundManager().addSoundToLoad(sd);
                            HashMap<String, String> params = new HashMap<>();
                            params.put("sound", sd.getId());
                            try {
                                Action a2 = ActionFactory.create(PlaySoundAction.class.getName(), params);
                                actions.set(i, a2);
                                a2.init(w);
                            } catch (ClassNotFoundException | ReflectionException e) {
                                e.printStackTrace();
                            }
                            EngineLogger.debug("Converting SoundAction:" + s.getId() + "." + v.getId());
                        } else {
                            EngineLogger.debug("WARNING: Cannot convert SoundAction:" + s.getId() + "." + v.getId());
                        }
                    } else if (act instanceof PlaySoundAction) {
                        String sound = ActionUtils.getStringValue(act, "sound");
                        SoundDesc sd = w.getSounds().get(sound);
                        if (sd != null && sd.isPreload())
                            s.getSoundManager().addSoundToLoad(sd);
                    }
                } catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) {
                }
            }
        }
        for (BaseActor a : s.getActors().values()) {
            if (a instanceof InteractiveActor) {
                HashMap<String, Verb> actorVerbs = ((InteractiveActor) a).getVerbManager().getVerbs();
                for (Verb v : actorVerbs.values()) {
                    ArrayList<Action> actions = v.getActions();
                    for (int i = 0; i < actions.size(); i++) {
                        Action act = actions.get(i);
                        try {
                            if (act instanceof SoundAction) {
                                String actor = ActionUtils.getStringValue(act, "actor");
                                String play = ActionUtils.getStringValue(act, "play");
                                if (play != null) {
                                    if ("$PLAYER".equals(actor))
                                        actor = s.getPlayer().getId();
                                    SoundDesc sd = w.getSounds().get(actor + "_" + play);
                                    if (sd == null) {
                                        EngineLogger.error("Reference to sound not found: " + s.getId() + "." + actor + "." + play);
                                        continue;
                                    }
                                    s.getSoundManager().addSoundToLoad(sd);
                                    HashMap<String, String> params = new HashMap<>();
                                    params.put("sound", sd.getId());
                                    try {
                                        Action a2 = ActionFactory.create(PlaySoundAction.class.getName(), params);
                                        actions.set(i, a2);
                                        a2.init(w);
                                    } catch (ClassNotFoundException | ReflectionException e) {
                                        e.printStackTrace();
                                    }
                                    EngineLogger.debug("Converting SoundAction in:" + s.getId() + "." + a.getId() + "." + v.getId());
                                } else {
                                    EngineLogger.debug("WARNING: Cannot convert SoundAction:" + s.getId() + "." + a.getId() + "." + v.getId());
                                }
                            } else if (act instanceof PlaySoundAction) {
                                String sound = ActionUtils.getStringValue(act, "sound");
                                SoundDesc sd = w.getSounds().get(sound);
                                if (sd != null && sd.isPreload())
                                    s.getSoundManager().addSoundToLoad(sd);
                            }
                        } catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) {
                        }
                    }
                }
            }
            if (a instanceof SpriteActor && ((SpriteActor) a).getRenderer() instanceof AnimationRenderer) {
                HashMap<String, AnimationDesc> anims = ((AnimationRenderer) ((SpriteActor) a).getRenderer()).getAnimations();
                for (AnimationDesc ad : anims.values()) {
                    if (ad.sound != null) {
                        String sid = ad.sound;
                        SoundDesc sd = w.getSounds().get(sid);
                        if (sd == null)
                            sid = a.getId() + "_" + sid;
                        sd = w.getSounds().get(sid);
                        if (sd != null) {
                            if (sd.isPreload())
                                s.getSoundManager().addSoundToLoad(sd);
                        } else
                            EngineLogger.error(a.getId() + ": SOUND not found: " + ad.sound + " in animation: " + ad.id);
                    }
                }
            }
        }
    }
}
Also used : SoundAction(com.bladecoder.engine.actions.SoundAction) PlaySoundAction(com.bladecoder.engine.actions.PlaySoundAction) Action(com.bladecoder.engine.actions.Action) SoundDesc(com.bladecoder.engine.model.SoundDesc) HashMap(java.util.HashMap) InteractiveActor(com.bladecoder.engine.model.InteractiveActor) SoundAction(com.bladecoder.engine.actions.SoundAction) PlaySoundAction(com.bladecoder.engine.actions.PlaySoundAction) AnimationRenderer(com.bladecoder.engine.model.AnimationRenderer) Verb(com.bladecoder.engine.model.Verb) SpriteActor(com.bladecoder.engine.model.SpriteActor) ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException) Scene(com.bladecoder.engine.model.Scene) PlaySoundAction(com.bladecoder.engine.actions.PlaySoundAction) BaseActor(com.bladecoder.engine.model.BaseActor) AnimationDesc(com.bladecoder.engine.anim.AnimationDesc)

Aggregations

SoundDesc (com.bladecoder.engine.model.SoundDesc)7 Scene (com.bladecoder.engine.model.Scene)2 JsonValue (com.badlogic.gdx.utils.JsonValue)1 ReflectionException (com.badlogic.gdx.utils.reflect.ReflectionException)1 Action (com.bladecoder.engine.actions.Action)1 PlaySoundAction (com.bladecoder.engine.actions.PlaySoundAction)1 SoundAction (com.bladecoder.engine.actions.SoundAction)1 AnimationDesc (com.bladecoder.engine.anim.AnimationDesc)1 AnimationRenderer (com.bladecoder.engine.model.AnimationRenderer)1 BaseActor (com.bladecoder.engine.model.BaseActor)1 CharacterActor (com.bladecoder.engine.model.CharacterActor)1 InteractiveActor (com.bladecoder.engine.model.InteractiveActor)1 Inventory (com.bladecoder.engine.model.Inventory)1 SpriteActor (com.bladecoder.engine.model.SpriteActor)1 Verb (com.bladecoder.engine.model.Verb)1 UndoCreateSound (com.bladecoder.engineeditor.undo.UndoCreateSound)1 UndoDeleteSound (com.bladecoder.engineeditor.undo.UndoDeleteSound)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1