Skip to content

Commit 9721bd2

Browse files
committed
Fix random animation offset
1 parent 7ccc8c0 commit 9721bd2

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

Diff for: 2d/src/main/java/de/bitbrain/braingdx/graphics/animation/AnimationFrames.java

+12-7
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ public enum Direction {
1616
private float duration;
1717
private int resetIndex;
1818
private float offset;
19+
private boolean randomOffset;
1920

20-
private AnimationFrames(int originX, int originY, Direction direction, Animation.PlayMode playMode, int frames, float duration, int resetIndex, float offset) {
21+
private AnimationFrames(int originX, int originY, Direction direction, Animation.PlayMode playMode, int frames, float duration, int resetIndex, float offset, boolean randomOffset) {
2122
this.originX = originX;
2223
this.originY = originY;
2324
this.direction = direction;
@@ -26,12 +27,17 @@ private AnimationFrames(int originX, int originY, Direction direction, Animation
2627
this.duration = duration;
2728
this.resetIndex = resetIndex;
2829
this.offset = offset;
30+
this.randomOffset = randomOffset;
2931
}
3032

3133
public float getOffset() {
3234
return offset;
3335
}
3436

37+
public boolean isRandomOffset() {
38+
return randomOffset;
39+
}
40+
3541
public void setOriginX(int originX) {
3642
this.originX = originX;
3743
}
@@ -101,6 +107,7 @@ public static class AnimationFramesBuilder {
101107
private float duration = 1f;
102108
private int resetIndex = 0;
103109
private float offset = 0;
110+
private boolean randomOffset;
104111

105112
private AnimationFramesBuilder() {
106113

@@ -143,11 +150,8 @@ public AnimationFramesBuilder offset(float offsetInMilliseconds) {
143150
}
144151

145152
public AnimationFramesBuilder randomOffset() {
146-
if (frames > 0) {
147-
return this.offset((float) (Math.random() * (this.duration * this.frames)));
148-
} else {
149-
return this.offset((float) (Math.random() * (this.duration * 8f)));
150-
}
153+
randomOffset = true;
154+
return this;
151155
}
152156

153157
public AnimationFrames build() {
@@ -159,7 +163,8 @@ public AnimationFrames build() {
159163
frames,
160164
duration,
161165
resetIndex,
162-
offset
166+
offset,
167+
randomOffset
163168
);
164169
}
165170

Diff for: 2d/src/main/java/de/bitbrain/braingdx/graphics/animation/AnimationRenderer.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,20 @@ private TextureRegion retrieveRegionFor(GameObject object, float delta) {
115115
Animation<TextureRegion> animation = animationCache.getAnimation(currentAnimationType);
116116
AnimationFrames frames = config.getFrames(currentAnimationType);
117117
if (state.stateTime == 0f) {
118-
state.stateTime = frames.getOffset();
118+
if (frames.isRandomOffset()) {
119+
state.stateTime = (float) (Math.random() * (frames.getDuration() * frames.getFrames()));
120+
} else {
121+
state.stateTime = frames.getOffset();
122+
}
119123
}
120124
state.stateTime += delta;
121125
boolean animationEnabled = animationEnabler.isEnabledFor(object);
122126
if (!animationEnabled) {
123-
state.stateTime = frames.getOffset();
127+
if (frames.isRandomOffset()) {
128+
state.stateTime = (float) (Math.random() * (frames.getDuration() * frames.getFrames()));
129+
} else {
130+
state.stateTime = frames.getOffset();
131+
}
124132
}
125133
return animation.getKeyFrame(animationEnabled ? state.stateTime : frames.getDuration() * frames.getResetIndex());
126134
}

Diff for: CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Version 0.6.28
2+
3+
* fix random animation offset
4+
15
# Version 0.6.27
26

37
* improve animation rendering and add offset option

0 commit comments

Comments
 (0)