Search in sources :

Example 1 with Action

use of com.bladecoder.engine.actions.Action in project bladecoder-adventure-engine by bladecoder.

the class ActionUtils method readJson.

public static Action readJson(Json json, JsonValue jsonData) {
    String className = jsonData.getString("class", null);
    Action action = null;
    if (className != null) {
        jsonData.remove("class");
        try {
            action = ActionFactory.createByClass(className, null);
        } catch (ClassNotFoundException | ReflectionException e1) {
            throw new SerializationException(e1);
        }
        for (int j = 0; j < jsonData.size; j++) {
            JsonValue v = jsonData.get(j);
            try {
                if (v.isNull())
                    ActionUtils.setParam(action, v.name, null);
                else
                    ActionUtils.setParam(action, v.name, v.asString());
            } catch (NoSuchFieldException e) {
                EngineLogger.error("Action field not found - class: " + className + " field: " + v.name);
            } catch (IllegalArgumentException | IllegalAccessException e) {
                EngineLogger.error("Action field error - class: " + className + " field: " + v.name + " value: " + (v == null ? "null" : v.asString()));
            }
        }
    }
    return action;
}
Also used : ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException) Action(com.bladecoder.engine.actions.Action) SerializationException(com.badlogic.gdx.utils.SerializationException) JsonValue(com.badlogic.gdx.utils.JsonValue)

Example 2 with Action

use of com.bladecoder.engine.actions.Action in project bladecoder-adventure-engine by bladecoder.

the class ActionCallbackSerialization method find.

/**
 * Searches for the ActionCallback represented by the id string.
 *
 * @param id
 */
public static ActionCallback find(String id) {
    if (id == null)
        return null;
    Scene s = World.getInstance().getCurrentScene();
    String[] split = id.split(SEPARATION_SYMBOL);
    if (id.startsWith(INK_MANAGER_TAG)) {
        if (split.length == 1)
            return World.getInstance().getInkManager();
        int actionPos = Integer.parseInt(split[1]);
        Action action = World.getInstance().getInkManager().getActions().get(actionPos);
        if (action instanceof ActionCallback)
            return (ActionCallback) action;
    }
    if (split.length < 2)
        return null;
    String actorId;
    String verbId;
    int actionPos = -1;
    if (id.startsWith(UIACTORS_TAG) || id.startsWith(INVENTORY_TAG)) {
        actorId = split[1];
        verbId = split[2];
        if (split.length > 3)
            actionPos = Integer.parseInt(split[3]);
    } else {
        actorId = split[0];
        verbId = split[1];
        if (split.length > 2)
            actionPos = Integer.parseInt(split[2]);
    }
    Verb v = null;
    if (actorId.equals(DEFAULT_VERB_TAG)) {
        v = World.getInstance().getVerbManager().getVerb(verbId, null, null);
    } else {
        InteractiveActor a;
        if (actorId.equals(s.getId())) {
            v = s.getVerbManager().getVerbs().get(verbId);
        } else {
            a = (InteractiveActor) s.getActor(actorId, true);
            if (a == null) {
                EngineLogger.error("ActionCallbackSerialization - Actor not found: " + actorId + " cb: " + id);
                return null;
            }
            v = a.getVerbManager().getVerbs().get(verbId);
        }
    }
    if (v == null) {
        EngineLogger.error("ActionCallbackSerialization - Verb not found: " + verbId + " cb: " + id);
        return null;
    }
    if (actionPos == -1)
        return v;
    Action action = v.getActions().get(actionPos);
    if (action instanceof ActionCallback)
        return (ActionCallback) action;
    EngineLogger.error("ActionCallbackSerialization - CB not found: " + id);
    return null;
}
Also used : Action(com.bladecoder.engine.actions.Action) ActionCallback(com.bladecoder.engine.actions.ActionCallback) Verb(com.bladecoder.engine.model.Verb) InteractiveActor(com.bladecoder.engine.model.InteractiveActor) Scene(com.bladecoder.engine.model.Scene)

Example 3 with Action

use of com.bladecoder.engine.actions.Action in project bladecoder-adventure-engine by bladecoder.

the class InkManager method read.

@Override
public void read(Json json, JsonValue jsonData) {
    wasInCutmode = json.readValue("wasInCutmode", Boolean.class, jsonData);
    sCb = json.readValue("cb", String.class, jsonData);
    // READ ACTIONS
    actions.clear();
    JsonValue actionsValue = jsonData.get("actions");
    for (int i = 0; i < actionsValue.size; i++) {
        JsonValue aValue = actionsValue.get(i);
        Action a = ActionUtils.readJson(json, aValue);
        actions.add(a);
    }
    ip = json.readValue("ip", Integer.class, jsonData);
    actionsValue = jsonData.get("actionsSer");
    int i = 0;
    for (Action a : actions) {
        if (a instanceof Serializable && i < actionsValue.size) {
            if (actionsValue.get(i) == null)
                break;
            ((Serializable) a).read(json, actionsValue.get(i));
            i++;
        }
    }
    // READ STORY
    String storyName = json.readValue("storyName", String.class, jsonData);
    String storyString = json.readValue("story", String.class, jsonData);
    if (storyString != null) {
        try {
            newStory(storyName);
            long initTime = System.currentTimeMillis();
            story.getState().loadJson(storyString);
            EngineLogger.debug("INK SAVED STATE LOADING TIME (ms): " + (System.currentTimeMillis() - initTime));
        } catch (Exception e) {
            EngineLogger.error(e.getMessage(), e);
        }
    }
}
Also used : Action(com.bladecoder.engine.actions.Action) Serializable(com.badlogic.gdx.utils.Json.Serializable) JsonValue(com.badlogic.gdx.utils.JsonValue) IOException(java.io.IOException) ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException)

Example 4 with Action

use of com.bladecoder.engine.actions.Action in project bladecoder-adventure-engine by bladecoder.

the class InkManager method processTextLine.

private void processTextLine(HashMap<String, String> params, String line) {
    // ej. "Johnny: Hello punks!"
    if (!params.containsKey("actor")) {
        int idx = line.indexOf(COMMAND_MARK);
        if (idx != -1) {
            params.put("actor", line.substring(0, idx).trim());
            line = line.substring(idx + 1).trim();
        }
    }
    if (!params.containsKey("actor") && World.getInstance().getCurrentScene().getPlayer() != null) {
        if (!params.containsKey("type")) {
            params.put("type", Type.SUBTITLE.toString());
        }
    } else if (params.containsKey("actor") && !params.containsKey("type")) {
        params.put("type", Type.TALK.toString());
    } else if (!params.containsKey("type")) {
        params.put("type", Type.SUBTITLE.toString());
    }
    params.put("text", translateLine(line));
    try {
        if (!params.containsKey("actor")) {
            Action action = ActionFactory.createByClass("com.bladecoder.engine.actions.TextAction", params);
            actions.add(action);
        } else {
            Action action = ActionFactory.createByClass("com.bladecoder.engine.actions.SayAction", params);
            actions.add(action);
        }
    } catch (ClassNotFoundException | ReflectionException e) {
        EngineLogger.error(e.getMessage(), e);
    }
}
Also used : ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException) Action(com.bladecoder.engine.actions.Action)

Example 5 with Action

use of com.bladecoder.engine.actions.Action in project bladecoder-adventure-engine by bladecoder.

the class Verb method read.

@Override
public void read(Json json, JsonValue jsonData) {
    if (SerializationHelper.getInstance().getMode() == Mode.MODEL) {
        id = json.readValue("id", String.class, jsonData);
        target = json.readValue("target", String.class, (String) null, jsonData);
        state = json.readValue("state", String.class, (String) null, jsonData);
        icon = json.readValue("icon", String.class, (String) null, jsonData);
        actions.clear();
        JsonValue actionsValue = jsonData.get("actions");
        for (int i = 0; i < actionsValue.size; i++) {
            JsonValue aValue = actionsValue.get(i);
            String clazz = aValue.getString("class");
            try {
                Action a = ActionUtils.readJson(json, aValue);
                actions.add(a);
            } catch (SerializationException e) {
                EngineLogger.error("Error loading action: " + clazz + " " + aValue.toString());
                throw e;
            }
        }
    } else {
        // MUTABLE
        currentTarget = json.readValue("currentTarget", String.class, (String) null, jsonData);
        ip = json.readValue("ip", Integer.class, jsonData);
        String sCb = json.readValue("cb", String.class, jsonData);
        cb = ActionCallbackSerialization.find(sCb);
        JsonValue actionsValue = jsonData.get("actions");
        int i = 0;
        for (Action a : actions) {
            if (a instanceof Serializable && i < actionsValue.size) {
                if (actionsValue.get(i) == null)
                    break;
                ((Serializable) a).read(json, actionsValue.get(i));
                i++;
            }
        }
    }
}
Also used : Action(com.bladecoder.engine.actions.Action) Serializable(com.badlogic.gdx.utils.Json.Serializable) SerializationException(com.badlogic.gdx.utils.SerializationException) JsonValue(com.badlogic.gdx.utils.JsonValue)

Aggregations

Action (com.bladecoder.engine.actions.Action)31 DisableActionAction (com.bladecoder.engine.actions.DisableActionAction)12 EndAction (com.bladecoder.engine.actions.EndAction)11 AbstractControlAction (com.bladecoder.engine.actions.AbstractControlAction)10 AbstractIfAction (com.bladecoder.engine.actions.AbstractIfAction)10 CommentAction (com.bladecoder.engine.actions.CommentAction)10 UndoDeleteAction (com.bladecoder.engineeditor.undo.UndoDeleteAction)10 Verb (com.bladecoder.engine.model.Verb)8 InteractiveActor (com.bladecoder.engine.model.InteractiveActor)6 Scene (com.bladecoder.engine.model.Scene)6 ReflectionException (com.badlogic.gdx.utils.reflect.ReflectionException)5 SetCutmodeAction (com.bladecoder.engine.actions.SetCutmodeAction)5 BaseActor (com.bladecoder.engine.model.BaseActor)5 Array (com.badlogic.gdx.utils.Array)4 Serializable (com.badlogic.gdx.utils.Json.Serializable)4 JsonValue (com.badlogic.gdx.utils.JsonValue)4 LookAtAction (com.bladecoder.engine.actions.LookAtAction)4 SayAction (com.bladecoder.engine.actions.SayAction)4 IOException (java.io.IOException)4 CharacterActor (com.bladecoder.engine.model.CharacterActor)3