AnimationCache { public Animation<TextureRegion> getAnimation(Object animationType) { Animation<TextureRegion> animation = animations.get(animationType); if (animation == null || hasDirectionChanged(animationType)) { TextureRegion[] regions = getRegionsForType(animationType); AnimationFrames frames = config.getFrames(animationType); animation = new Animation<TextureRegion>(frames.getDuration(), regions); animations.put(animationType, animation); directionMap.put(animationType, frames.getDirection()); } updateAnimation(animationType, animation); return animation; } AnimationCache(AnimationSpriteSheet spriteSheet, AnimationConfig config); Animation<TextureRegion> getAnimation(Object animationType);  }
@Test public void testGetAnimation_ShouldUpdateDirectionDynamically() { Animation<TextureRegion> animation = cache.getAnimation(ANIMATION_TYPE_A); TextureRegion[] regionsHorizontal = sheet.getFrames(0, 0, 5, 0); TextureRegion[] regionsVertical = sheet.getFrames(0, 0, 0, 5); TextureRegion region_0_0 = regionsHorizontal[0]; TextureRegion region_1_0 = regionsHorizontal[1]; TextureRegion region_0_1 = regionsVertical[1]; assertThat(animation.getKeyFrame(DURATION_A * 0)).isEqualTo(region_0_0); assertThat(animation.getKeyFrame(DURATION_A * 1)).isEqualTo(region_1_0); framesA.setDirection(AnimationFrames.Direction.VERTICAL); animation = cache.getAnimation(ANIMATION_TYPE_A); assertThat(animation.getKeyFrame(DURATION_A * 0)).isEqualTo(region_0_0); assertThat(animation.getKeyFrame(DURATION_A * 1)).isEqualTo(region_0_1); }
@Test public void testGetAnimation_ShouldHaveEachDifferentKeyFrames_ForAnimationTypeA() { Animation<TextureRegion> animation = cache.getAnimation(ANIMATION_TYPE_A); TextureRegion frame = animation.getKeyFrame(0f); for (float time = 1f; time < 10f; time += 1f) { TextureRegion temporary = animation.getKeyFrame(time); assertThat(temporary).isNotEqualTo(frame); frame = temporary; } }
@Test public void testGetAnimation_ShouldHaveEachDifferentKeyFrames_ForAnimationTypeB() { Animation<TextureRegion> animation = cache.getAnimation(ANIMATION_TYPE_B); TextureRegion frame = animation.getKeyFrame(0f); for (float time = 2f; time < 10f; time += 2f) { TextureRegion temporary = animation.getKeyFrame(time); assertThat(temporary).isNotEqualTo(frame); frame = temporary; } }
@Test public void testGetAnimation_ShouldSupportIndividualPlayModes_IncludingCaching() { Animation<TextureRegion> animationA = cache.getAnimation(ANIMATION_TYPE_A); Animation<TextureRegion> animationB = cache.getAnimation(ANIMATION_TYPE_B); boolean reversed = false; for (float time = 0f; time < 10f; time += 1f) { TextureRegion regionA = animationA.getKeyFrame(time); TextureRegion regionB = animationB.getKeyFrame(time * 2); if (time != 0 && time % FRAME_COUNT == 0) { reversed = true; } if (reversed) { assertThat(regionA).isNotEqualTo(regionB); } else { assertThat(regionA).isEqualTo(regionB); } } }
@Test public void testGetAnimation_ShouldUpdatePlayModeDynamically() { Animation<TextureRegion> animationA = cache.getAnimation(ANIMATION_TYPE_A); assertThat(animationA.getPlayMode()).isEqualTo(PLAY_MODE_A); framesA.setPlayMode(PLAY_MODE_B); animationA = cache.getAnimation(ANIMATION_TYPE_A); assertThat(animationA.getPlayMode()).isEqualTo(PLAY_MODE_B); }
@Test public void testGetAnimation_ShouldUpdateDurationDynamically() { Animation<TextureRegion> animationA = cache.getAnimation(ANIMATION_TYPE_A); assertThat(animationA.getFrameDuration()).isEqualTo(DURATION_A); framesA.setDuration(DURATION_B); animationA = cache.getAnimation(ANIMATION_TYPE_A); assertThat(animationA.getFrameDuration()).isEqualTo(DURATION_B); }