Skip to content

Commit

Permalink
Made some form of var management system
Browse files Browse the repository at this point in the history
  • Loading branch information
Iteranya committed Nov 5, 2024
1 parent 6fa01b8 commit 5b97c99
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,19 @@ public class VisualNovelEngine {
public VisualNovelEngine(List<Map<String, Object>> gameData,String scriptName) {
this.gameData = gameData;
this.currentState = 0;
this.variables = new HashMap<>();
this.state = new DialogueState(null,null,null);
this.scriptName = scriptName;
initializeVariable();
}

private void initializeVariable() {
if(this.gameData.get(0).get("type")!="variable"){
this.variables = new HashMap<>();
this.variables.put("type", "variable");
this.gameData.add(0, variables);
}else{
this.variables = this.gameData.get(0);
}
}

private Long findLabelId(String var) {
Expand Down Expand Up @@ -61,8 +71,6 @@ private void updateSprite(Map<String, Object> sprite) {
));

if(Objects.equals((String) sprite.get("action"), "show")){
// System.out.println("New Sprite: "+newSprite.getSprite());
// System.out.println("Old Sprite: "+sprite.get("action"));
if(sprite.get("wRatio")!=null){
newSprite.setPositioning(
((Long) sprite.get("wRatio")).intValue(),
Expand All @@ -74,8 +82,6 @@ private void updateSprite(Map<String, Object> sprite) {
);
}
for (SpriteState oldSprite: this.state.getSprites()) {
// System.out.println("New Sprite: "+oldSprite.getSprite());
// System.out.println("Old Sprite: "+newSprite.getSprite());

if(Objects.equals(oldSprite.getSprite(), newSprite.getSprite())){
removeSpriteByFolder(this.state.getSprites(), newSprite.getSprite());
Expand All @@ -88,7 +94,6 @@ private void updateSprite(Map<String, Object> sprite) {
this.currentState++;
}
public void removeSpriteByFolder(List<SpriteState> sprites, String folderName) {
//System.out.println("Remove: "+folderName);
sprites.removeIf(sprite -> sprite.getSprite().equals(folderName));
}
private void updateDialogue(String label, String content) {
Expand All @@ -114,6 +119,7 @@ private Object processCommand(Map<String, Object> value) {
if ("get_gamemode".equals(action)) {
return "Survival";
} else if ("custom_command".equals(action)) {
// NOT IMPLEMENTED YET
return "Nothing for now";
}
return "Nothing for now";
Expand Down Expand Up @@ -144,13 +150,13 @@ private void modifyVariable(String variable, String operation, Object value) {
}

private void giveItem(String item, long amount) {

// Not Implemented Yet
this.currentState++;
}

private void processJump(Map<String, Object> action) {
this.currentState = findLabelId((String) action.get("label"));
this.currentState++; //TODO: Figure out if this is necessary
this.currentState++; //TO-DO: Figure out if this is necessary (Update: Yes It Is)
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -262,7 +268,7 @@ private boolean processAction(Map<String, Object> action) {

private void processFinishing() {
isEngineRunning=false;
ScriptLoader.saveState(variables,scriptName);
ScriptLoader.saveState(gameData,scriptName);
shutdown = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import java.io.*;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -74,24 +73,12 @@ public static List<Map<String, Object>> loadDemo() throws IOException {
}
}

public static void saveState(Map<String, Object> variables, String filePath) {
try {
filePath = FMLPaths.CONFIGDIR.get() + "\\"+ MobTalkerRedux.MODID +"\\" + filePath;
// Load existing JSON content
List<Map<String, Object>> list = loadJson(filePath);

// Append the 'variables' map to the list
list.add(variables);

// Convert the updated list back to a JSON string
String jsonContent = gson.toJson(list);

// Save the JSON string back into the file
try (Writer writer = new OutputStreamWriter(new FileOutputStream(filePath), StandardCharsets.UTF_8)) {
writer.write(jsonContent);
}
public static void saveState(List<Map<String, Object>> gamestate, String filePath) {
filePath = FMLPaths.CONFIGDIR.get() + "\\" + MobTalkerRedux.MODID + "\\" + filePath;
try (Writer writer = new FileWriter(filePath)) {
gson.toJson(gamestate, writer);
} catch (IOException e) {
throw new RuntimeException("Failed to save state to file at " + filePath, e);
throw new RuntimeException("Failed to save gamestate to " + filePath, e);
}
}
}

0 comments on commit 5b97c99

Please sign in to comment.