From 9dfd4cbfce88951f196cfbb86154d7d7b2320cf3 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sat, 21 Nov 2015 17:31:08 +0100 Subject: Add ProbabilisticAnimal type and adapt assets retrieval accordingly --- src/ch/epfl/maze/graphics/Animation.java | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'src/ch/epfl/maze/graphics') diff --git a/src/ch/epfl/maze/graphics/Animation.java b/src/ch/epfl/maze/graphics/Animation.java index 47e0fc8..cd5fa38 100644 --- a/src/ch/epfl/maze/graphics/Animation.java +++ b/src/ch/epfl/maze/graphics/Animation.java @@ -19,6 +19,8 @@ import java.util.TreeMap; /** * Handles the animation of a {@code Simulation} by extrapolating the positions * of animals. + * + * @author Pacien TRAN-GIRARD */ public final class Animation { @@ -226,7 +228,9 @@ public final class Animation { private BufferedImage loadImage(Animal animal) { // path = "img/superclass/class.png" - String folder = animal.getClass().getSuperclass().getSimpleName(); + String superClassName = animal.getClass().getSuperclass().getSimpleName(); + String[] superClassComponents = this.splitCamelCase(superClassName); + String folder = superClassComponents[superClassComponents.length - 1]; String file = animal.getClass().getSimpleName(); String path = "img/" + folder + File.separator + file + ".png"; @@ -243,4 +247,23 @@ public final class Animation { return img; } + + /** + * Splits a camel case string + * http://stackoverflow.com/a/2560017/1348634 + * + * @param s A string + * @return An array of words + */ + private static String[] splitCamelCase(String s) { + return s.replaceAll( + String.format("%s|%s|%s", + "(?<=[A-Z])(?=[A-Z][a-z])", + "(?<=[^A-Z])(?=[A-Z])", + "(?<=[A-Za-z])(?=[^A-Za-z])" + ), + " " + ).split(" "); + } + } -- cgit v1.2.3