-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor animation setting of entity (=introduce AnimationService)
- Loading branch information
1 parent
30ec739
commit 729019a
Showing
12 changed files
with
130 additions
and
115 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
core/src/main/kotlin/com/quillraven/github/quillyjumper/AnimationService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.quillraven.github.quillyjumper | ||
|
||
import com.badlogic.gdx.graphics.g2d.Animation.PlayMode | ||
import com.badlogic.gdx.graphics.g2d.Sprite | ||
import com.badlogic.gdx.graphics.g2d.TextureAtlas | ||
import com.badlogic.gdx.graphics.g2d.TextureRegion | ||
import com.github.quillraven.fleks.ComponentType | ||
import com.github.quillraven.fleks.Entity | ||
import com.github.quillraven.fleks.World | ||
import com.quillraven.github.quillyjumper.component.* | ||
import ktx.app.gdxError | ||
import ktx.log.logger | ||
|
||
class AnimationService(private val objectAtlas: TextureAtlas) { | ||
private val animationCache = mutableMapOf<String, GdxAnimation>() | ||
|
||
fun World.entityAnimation( | ||
entity: Entity, | ||
type: AnimationType, | ||
playMode: PlayMode, | ||
cmpType: ComponentType<Animation>, | ||
) { | ||
val (gameObject) = entity[Tiled] | ||
val gdxAnimation = gdxAnimation(gameObject, type) | ||
|
||
var aniCmp = entity.getOrNull(cmpType) | ||
if (aniCmp == null) { | ||
aniCmp = Animation(gdxAnimation, playMode, type = cmpType) | ||
entity.configure { it += aniCmp } | ||
} else { | ||
aniCmp.gdxAnimation = gdxAnimation | ||
aniCmp.playMode = playMode | ||
} | ||
|
||
val (sprite) = entity[Graphic] | ||
sprite.updateRegion(gdxAnimation.getKeyFrame(0f)) | ||
} | ||
|
||
fun gdxAnimation( | ||
gameObject: GameObject, | ||
type: AnimationType | ||
): GdxAnimation { | ||
val animationAtlasKey = "${gameObject.atlasKey}/${type.atlasKey}" | ||
val gdxAnimation = animationCache.getOrPut(animationAtlasKey) { | ||
val regions = objectAtlas.findRegions(animationAtlasKey) | ||
if (regions.isEmpty) { | ||
gdxError("There are no regions for the animation $animationAtlasKey") | ||
} | ||
GdxAnimation(Animation.DEFAULT_FRAME_DURATION, regions) | ||
} | ||
|
||
if (animationCache.size > 100) { | ||
log.info { "Animation cache is larger than 100" } | ||
} | ||
return gdxAnimation | ||
} | ||
|
||
companion object { | ||
private val log = logger<AnimationService>() | ||
|
||
fun Sprite.updateRegion(region: TextureRegion) { | ||
val flipX = isFlipX | ||
val flipY = isFlipY | ||
setRegion(region) | ||
setFlip(flipX, flipY) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 0 additions & 13 deletions
13
core/src/main/kotlin/com/quillraven/github/quillyjumper/util/fleksUtils.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.