-
Notifications
You must be signed in to change notification settings - Fork 0
/
CombatObject.pde
207 lines (194 loc) · 8.07 KB
/
CombatObject.pde
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
class CombatObject extends PhysicsObject {
private boolean active = false;
private String name;
private int timeWhenCreated = millis();
private double startTime;
private int timeWhenStarted;
private double lifetime;
private ArrayList<String> flippedOutputs = new ArrayList<String>();
private PShape texture = createShape();
private PShape textureFlipX = createShape();
private boolean useFlippedTextures = false;
private PVector centreOfTexture = new PVector();
private int scale = 1;
private ArrayList<JSONObject> animations = new ArrayList<JSONObject>();
private ArrayList<Point2D> pointsInShape = new ArrayList<Point2D>();
CombatObject(JSONObject data) {
// Parse data
name = data.getString("type");
position.x = data.getFloat("positionX");
position.y = data.getFloat("positionY");
startTime = data.getFloat("startTime");
if (startTime == 0) {
active = true;
timeWhenStarted = millis();
}
lifetime = data.getFloat("duration");
// Put all flipped outputs into flippedOutputs (if there are any)
if (!data.isNull("flipOutput")) {
JSONArray flipOutput = data.getJSONArray("flipOutput");
for (int i=0; i<flipOutput.size(); i++) {
flippedOutputs.add(flipOutput.getString(i));
}
}
// Get sprite and animations from combat object type
JSONObject combatObjectTypeData = combatObjectJSON.getJSONObject(data.getString("type"));
if (!combatObjectTypeData.isNull("flipTexture")) useFlippedTextures = combatObjectTypeData.getBoolean("flipTexture");
PImage sprite = loadImage(combatObjectTypeData.getString("sprite"));
scale = combatObjectTypeData.getInt("scale");
sprite = nearestNeighbourScale(sprite, scale);
centreOfTexture.x = sprite.width/2;
centreOfTexture.y = sprite.height/2;
JSONArray animationsJSONArray = combatObjectTypeData.getJSONArray("animations");
for (int i=0; i<animationsJSONArray.size(); i++) {
animations.add(animationsJSONArray.getJSONObject(i));
}
// Create textures from sprite
texture.beginShape();
texture.textureMode(NORMAL);
texture.texture(sprite);
texture.vertex(-sprite.width/2, -sprite.height/2, 0, 0); // Top left corner
texture.vertex(sprite.width/2, -sprite.height/2, 1, 0); // Top right corner
texture.vertex(sprite.width/2, sprite.height/2, 1, 1); // Bottom right corner
texture.vertex(-sprite.width/2, sprite.height/2, 0, 1); // Bottom left corner
texture.textureMode(IMAGE);
texture.endShape();
textureFlipX.beginShape();
textureFlipX.textureMode(NORMAL);
textureFlipX.texture(sprite);
textureFlipX.vertex(-sprite.width/2, -sprite.height/2, 1, 0); // Top left corner
textureFlipX.vertex(sprite.width/2, -sprite.height/2, 0, 0); // Top right corner
textureFlipX.vertex(sprite.width/2, sprite.height/2, 0, 1); // Bottom right corner
textureFlipX.vertex(-sprite.width/2, sprite.height/2, 1, 1); // Bottom left corner
textureFlipX.textureMode(IMAGE);
textureFlipX.endShape();
// Create collision shape
if (!combatObjectTypeData.isNull("pointsInShape")) {
JSONArray pointsInShapeData = combatObjectTypeData.getJSONArray("pointsInShape");
for (int i=0; i<pointsInShapeData.size(); i++) {
JSONObject pointData = pointsInShapeData.getJSONObject(i);
pointsInShape.add(new Point2D.Double(pointData.getInt("x") * scale, pointData.getInt("y") * scale));
}
collisionShape = new PolygonCollisionShape(pointsInShape.toArray(new Point2D[0]));
}
ignoreCollisions = true;
}
@Override void update() {
// Start if it has been startTime seconds since the object was created
if (active == false) {
if ((millis() - timeWhenCreated) / 1000.0 > startTime) {
active = true;
timeWhenStarted = millis();
}
} else {
// Apply animations
for (JSONObject animation : animations) {
// Continue if animation hasn't started yet
if (!animation.isNull("delay") && animation.getFloat("delay") > getTimeAlive()) continue;
float magnitude = animation.getFloat("magnitude");
switch (animation.getString("modifies")) {
case "positionY":
boolean flipped = flippedOutputs.contains("positionY");
switch (animation.getString("type")) {
case "linear":
if (!flipped) {
position.y += magnitude;
} else {
position.y -= magnitude;
}
break;
case "sine":
float frequency = animation.getFloat("frequency");
if (!flipped) {
position.y += sin(((millis() - timeWhenStarted) * frequency % 1000) / 1000 * 2 * (float)Math.PI) * magnitude;
} else {
position.y -= sin(((millis() - timeWhenStarted) * frequency % 1000) / 1000 * 2 * (float)Math.PI) * magnitude;
}
break;
}
break;
case "positionX":
flipped = flippedOutputs.contains("positionX");
switch (animation.getString("type")) {
case "linear":
if (!flipped) {
position.x += magnitude;
} else {
position.x -= magnitude;
}
break;
case "sine":
float frequency = animation.getFloat("frequency");
if (!flipped) {
position.x += sin(((millis() - timeWhenStarted) * frequency % 1000) / 1000 * 2 * (float)Math.PI) * magnitude;
} else {
position.x -= sin(((millis() - timeWhenStarted) * frequency % 1000) / 1000 * 2 * (float)Math.PI) * magnitude;
}
break;
}
break;
case "rotation":
flipped = flippedOutputs.contains("rotation");
switch (animation.getString("type")) {
case "linear":
if (!flipped) {
setRotation(rotation + magnitude);
} else {
setRotation(rotation - magnitude);
}
break;
case "sine":
float frequency = animation.getFloat("frequency");
if (!flipped) {
setRotation(sin(((millis() - timeWhenStarted) * frequency % 1000) / 1000 * 2 * (float)Math.PI) * magnitude);
} else {
setRotation(sin(((millis() - timeWhenStarted) * frequency % 1000) / 1000 * 2 * (float)Math.PI + (float)Math.PI) * magnitude + 90);
}
break;
}
break;
}
}
}
// Update collision shape if flipped
if (collisionShape != null && useFlippedTextures && flippedOutputs.contains("positionX")) {
((PolygonCollisionShape)collisionShape).setXFlipped(true);
}
// Translate collision shape to position
if (collisionShape != null) {
collisionShape.setWorldTranslation(new Point2D.Double(position.x, position.y));
}
}
void draw(PGraphics src) {
if (active) {
// Draw to src
src.beginDraw();
src.pushMatrix();
src.translate(position.x + centreOfTexture.x, position.y + centreOfTexture.y);
src.rotate(rotation);
if (useFlippedTextures && flippedOutputs.contains("positionX")) {
src.shape(textureFlipX);
} else {
src.shape(texture);
}
src.popMatrix();
src.endDraw();
// Draw collision overlay
if (collisionShape != null) collisionShape.drawOverlay(new Point2D.Double(position.x, position.y));
}
}
// Returns the length of time in seconds that the object has existed for
double getTimeAlive() {
if (active) {
return (millis() - timeWhenStarted) / 1000.0;
}
return 0;
}
// Returns the lifetime of the object, which is set in the JSON file
double getLifetime() {
return lifetime;
}
String getName() {
return name;
}
}