Skip to content

Commit

Permalink
LandingBehaviorWindow parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
TwilCynder committed Dec 26, 2024
1 parent 58a0996 commit dca12ab
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
62 changes: 61 additions & 1 deletion editor/javaEditor/src/gamedata/parsers/AnimationParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import java.awt.Point;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Map;

import KBUtil.DoubleMap;
import KBUtil.StringHelper;
import KBUtil.Vec2;
import gamedata.Animation;
Expand All @@ -12,6 +14,11 @@
import gamedata.Frame;
import gamedata.GameData;
import gamedata.GameplayAnimationBehavior;
import gamedata.GameplayAnimationBehavior.AnimationLandingBehavior;
import gamedata.GameplayAnimationBehavior.LandingBehavior;
import gamedata.GameplayAnimationBehavior.LandingBehaviorType;
import gamedata.GameplayAnimationBehavior.LandingBehaviorWindow;
import gamedata.GameplayAnimationBehavior.NormalLandingBehavior;
import gamedata.Hitbox;
import gamedata.Hurtbox;
import gamedata.RessourcePath;
Expand Down Expand Up @@ -186,6 +193,59 @@ private static void parseFrameMovementAxis(EntityFrame.FrameMovementAxis axis, S
}
}

private static DoubleMap<String, LandingBehaviorType> landingBehaviorTypeCodes = new DoubleMap<>(Map.ofEntries(
Map.entry("l", LandingBehaviorType.NORMAL),
Map.entry("a", LandingBehaviorType.ANIMATION),
Map.entry("n", LandingBehaviorType.NOTHING)
));

/**
* Creates a LandingBehavior from an array of descriptor fields, which must start at the behavor type (3rd field in the line)
* @param fields
* @return
*/
public static LandingBehaviorWindow parseLandingBehaviorWindow(String[] fields) throws RessourceException{
if (fields.length < 3){
throw new RessourceException("Landing behavior window line does not contain enough information (must be at least a type code)");
}

LandingBehaviorType type = landingBehaviorTypeCodes.get(fields[2]);
if (type == null){
throw new RessourceException("Unknown landing behavior type");
}

LandingBehavior behavior = null;
switch (type){
case NORMAL: {
int duration = -1;
if (fields.length > 3){
duration = parseInt(fields[3], "Landing duration");
}
behavior = new NormalLandingBehavior(duration);
}
break;
case ANIMATION: {
expectFields(fields, 4, "Landing behavior window line contains too few ");

int duration = -1;
if (fields.length > 4){
duration = parseInt(fields[4], "Landing duration");
}

behavior = new AnimationLandingBehavior(duration, fields[3]);
}
break;
case NOTHING: {
behavior = new LandingBehavior();
}
break;
}

LandingBehaviorWindow result = new LandingBehaviorWindow(Parser.parseInt(fields[1], "Landing window frame index"), behavior);

return result;
}

public static void parseAnimationDescriptor(GameData gd, String tag, RessourcePath rp, String source_filename, String descriptor_filename, BufferedReader buff_reader) throws RessourceException, IOException {
try (DescriptorReader reader = new DescriptorReader(buff_reader)) {
parseAnimationdescriptor(gd, tag, rp, source_filename, descriptor_filename, reader);
Expand Down Expand Up @@ -360,7 +420,7 @@ private static void parseAnimationDescriptor(Animation anim, String descriptor_f

fields = splitLine(line);
try {
GameplayAnimationBehavior.LandingBehavior.parseDescriptorFields(fields);
parseLandingBehaviorWindow(fields);
} catch (RessourceException ex){
throw new RessourceException(ex.getMessage(), descriptor_filename, line_index, ex.getCause());
}
Expand Down
4 changes: 4 additions & 0 deletions editor/javaEditor/src/gamedata/parsers/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ public static double parseDouble(String str, String numberDescription) throws Re
throw new RessourceException(numberDescription + " is not a valid number", e);
}
}

public static void expectFields(String[] fields, int expected, String message) throws RessourceException{
if (fields.length < expected) throw new RessourceException(message);
}
}

0 comments on commit dca12ab

Please sign in to comment.