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;
}
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;
}
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);
}
}
}
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);
}
}
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++;
}
}
}
}
Aggregations