-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimationData.java
More file actions
133 lines (108 loc) · 4.42 KB
/
AnimationData.java
File metadata and controls
133 lines (108 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package game.slam.util.animation.data;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.UUID;
public class AnimationData {
// Variables
private AnimationBlueprint animationBlueprint;
// Constructor
public AnimationData(String fileName) {
try {
// Grab animation Json file
String resourcePath = "/animations/" + fileName + ".json";
InputStream inputStream = AnimationData.class.getResourceAsStream(resourcePath);
if (inputStream == null) {
throw new IllegalArgumentException("File not found: " + resourcePath);
}
// Create reader
Reader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
// Create Gson instance and teach it how to serialize/deserialize our custom data types
Gson gson = new GsonBuilder()
.registerTypeAdapter(NodeType.class, new NodeType.TypeAdapter())
.registerTypeAdapter(UUID.class, new UUIDTypeAdapter())
.create();
// Create animation blueprint object from Json
this.animationBlueprint = gson.fromJson(reader, AnimationBlueprint.class);
// Close reader
reader.close();
// Parse custom data entries
this.animationBlueprint = this.parseCustomData();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Failed to load animation data from " + fileName, e);
}
}
// Methods
private AnimationBlueprint parseCustomData() {
// Parse and modify animations
List<AnimationBlueprint.Animation> modifiedAnimations = this.animationBlueprint.animations().stream()
.map(this::parseAndModifyAnimation)
.toList();
// Return a new copy of the animation blueprint, with our custom data added
return new AnimationBlueprint(
this.animationBlueprint.blueprint_settings(),
this.animationBlueprint.resources(),
this.animationBlueprint.rig(),
modifiedAnimations
);
}
private AnimationBlueprint.Animation parseAndModifyAnimation(AnimationBlueprint.Animation original) {
// Split name from data
String[] parts = original.name().split("\\|");
// Set actual name
String name = parts[0];
// Get extra data
boolean reversed;
int tickRatio;
// There is extra data
if (parts.length > 1) {
// Get data
int data = Integer.parseInt(parts[1]);
// Negative = reversed
reversed = data < 0;
// Make sure tick ratio is always positive
tickRatio = Math.abs(data);
}
// No extra data, use defaults
else {
reversed = false;
tickRatio = 1;
}
// Construct modified Animation
return new AnimationBlueprint.Animation(
name,
original.storageSafeName(),
original.duration(),
original.loopDelay(),
original.loopMode(),
original.frames(),
original.includedNodes(),
reversed,
tickRatio
);
}
// Getter method to access the parsed data
public AnimationBlueprint getAnimationBlueprint() {
return this.animationBlueprint;
}
// You can add more methods here to provide easy access to specific parts of the data
// For example:
public AnimationBlueprint.BlueprintSettings getBlueprintSettings() {
return this.animationBlueprint.blueprint_settings();
}
public List<AnimationBlueprint.Animation> getAnimations() {
return this.animationBlueprint.animations();
}
public AnimationBlueprint.Animation getAnimationByName(String name) {
return this.animationBlueprint.animations().stream()
.filter(anim -> anim.name().equals(name))
.findFirst()
.orElse(null);
}
// Add more methods as needed for your specific use cases
}