Skip to content

Commit

Permalink
Made Independent Json Variable Tracking System
Browse files Browse the repository at this point in the history
Humu
  • Loading branch information
Iteranya committed Nov 5, 2024
1 parent 5b97c99 commit 27ca00f
Show file tree
Hide file tree
Showing 3 changed files with 358 additions and 228 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ public VisualNovelEngine(List<Map<String, Object>> gameData,String scriptName) {
}

private void initializeVariable() {
if(this.gameData.get(0).get("type")!="variable"){
if(!"variable".equals(this.gameData.get(this.gameData.size() - 1).get("type"))){
System.out.println(this.gameData.get(this.gameData.size() - 1).get("type"));
this.variables = new HashMap<>();
this.variables.put("type", "variable");
this.gameData.add(0, variables);
this.gameData.add(variables);
System.out.println("Initialize Variable");
}else{
this.variables = this.gameData.get(0);
this.variables = this.gameData.get(this.gameData.size() - 1);
}
}

Expand Down Expand Up @@ -161,8 +163,11 @@ private void processJump(Map<String, Object> action) {

@SuppressWarnings("unchecked")
private void processConditional(Map<String, Object> condition) {
System.out.println("trying to get:"+condition.get("var"));
Object var = this.variables.get(condition.get("var"));
Object value = condition.get("value");
System.out.println(var);
System.out.println(value);
long end = (long) condition.get("end");

if (value instanceof Map) {
Expand All @@ -171,27 +176,33 @@ private void processConditional(Map<String, Object> condition) {

String conditionType = (String) condition.get("condition");
boolean result = false;
if(var!=null){
switch (conditionType) {
case "equal":
result = var.equals(value);
break;
case "not_equal":
result = !var.equals(value);
break;
case "less_than":
if (var instanceof Number && value instanceof Number) {
result = ((Number) var).doubleValue() < ((Number) value).doubleValue();
}
break;
case "greater_than":
if (var instanceof Number && value instanceof Number) {
result = ((Number) var).doubleValue() > ((Number) value).doubleValue();
}
break;
}

switch (conditionType) {
case "equal":
result = var.equals(value);
break;
case "not_equal":
result = !var.equals(value);
break;
case "less_than":
if (var instanceof Number && value instanceof Number) {
result = ((Number) var).doubleValue() < ((Number) value).doubleValue();
}
break;
case "greater_than":
if (var instanceof Number && value instanceof Number) {
result = ((Number) var).doubleValue() > ((Number) value).doubleValue();
}
break;
this.currentState = result ? this.currentState + 1 : end;
}
else{
this.currentState = end;
}


this.currentState = result ? this.currentState + 1 : end;
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -275,6 +286,7 @@ private void processFinishing() {
public void runEngine() {
while (isEngineRunning) { // Infinite loop
// Check if engine is running
System.out.println(this.currentState);
Map<String, Object> action = getDictById(this.currentState);
if(action == null){
shutdown = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,24 @@ public static List<Map<String, Object>> loadScript(String filePath) {
}

public static List<Map<String, Object>> loadDemo() throws IOException {
ResourceManager resourceManager = Minecraft.getInstance().getResourceManager();
ResourceLocation resourceLocation = new ResourceLocation("mobtalkerredux", "demo.json");
String configPath = FMLPaths.CONFIGDIR.get() + "\\" + MobTalkerRedux.MODID + "\\" + "demo.json";
File configFile = new File(configPath);

try (InputStream inputStream = resourceManager.getResource(resourceLocation).get().open()) {
return loadJson(inputStream);
} catch (FileNotFoundException e) {
throw new FileNotFoundException("Resource file demo.json could not be found.");
if (configFile.exists()) {
// Load from the config directory if it exists
System.out.println("Loading Save File");
return loadJson(configPath);
} else {
System.out.println("Making New Save File");
// Otherwise, try loading from the resource manager
ResourceManager resourceManager = Minecraft.getInstance().getResourceManager();
ResourceLocation resourceLocation = new ResourceLocation("mobtalkerredux", "demo.json");

try (InputStream inputStream = resourceManager.getResource(resourceLocation).get().open()) {
return loadJson(inputStream);
} catch (FileNotFoundException e) {
throw new FileNotFoundException("Resource file demo.json could not be found.");
}
}
}

Expand Down
Loading

0 comments on commit 27ca00f

Please sign in to comment.