mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-10 10:34:06 +09:00
on-the-fly sprite assembly WIP
This commit is contained in:
59
src/net/torvald/spriteanimation/Animatable.kt
Normal file
59
src/net/torvald/spriteanimation/Animatable.kt
Normal file
@@ -0,0 +1,59 @@
|
||||
package net.torvald.spriteanimation
|
||||
|
||||
import com.badlogic.gdx.files.FileHandle
|
||||
import com.badlogic.gdx.graphics.Texture
|
||||
import net.torvald.spriteassembler.ADProperties
|
||||
import net.torvald.spriteassembler.AssembleSheetPixmap
|
||||
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 2019-01-18.
|
||||
*/
|
||||
interface HasAssembledSprite {
|
||||
|
||||
var animDesc: FileHandle
|
||||
|
||||
// FIXME sometimes the animmation is invisible (row and nFrames mismatch -- row is changed to 1 but it's drawing 3rd frame?)
|
||||
|
||||
fun reassembleSprite(sprite: SpriteAnimation) {
|
||||
_rebuild(ADProperties(animDesc.read()), sprite)
|
||||
}
|
||||
|
||||
/*fun rebuild(animDesc: String, spriteAnimation: SpriteAnimation) {
|
||||
_rebuild(ADProperties(StringReader(animDesc)), spriteAnimation)
|
||||
}
|
||||
|
||||
fun rebuild(animDesc: FileHandle, spriteAnimation: SpriteAnimation) {
|
||||
_rebuild(ADProperties(animDesc.read()), spriteAnimation)
|
||||
}
|
||||
|
||||
fun rebuild(javaProp: Properties, spriteAnimation: SpriteAnimation) {
|
||||
_rebuild(ADProperties(javaProp), spriteAnimation)
|
||||
}*/
|
||||
|
||||
|
||||
private fun _rebuild(ad: ADProperties, sprite: SpriteAnimation) {
|
||||
// TODO injecting held item/armour pictures? Would it be AssembleSheetPixmap's job?
|
||||
|
||||
val pixmap = AssembleSheetPixmap(ad)
|
||||
val texture = Texture(pixmap)
|
||||
texture.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest)
|
||||
pixmap.dispose()
|
||||
val regionPack = TextureRegionPack(texture, ad.frameWidth, ad.frameHeight)
|
||||
|
||||
val newAnimDelays = FloatArray(ad.animations.size)
|
||||
val newAnimFrames = IntArray(ad.animations.size)
|
||||
|
||||
ad.animations.forEach { t, u ->
|
||||
val index = u.row - 1
|
||||
newAnimDelays[index] = u.delay
|
||||
newAnimFrames[index] = u.frames
|
||||
}
|
||||
|
||||
sprite.setSpriteImage(regionPack)
|
||||
sprite.delays = newAnimDelays
|
||||
sprite.nFrames = newAnimFrames
|
||||
sprite.nRows = newAnimDelays.size
|
||||
}
|
||||
|
||||
}
|
||||
@@ -18,10 +18,10 @@ class SpriteAnimation(val parentActor: ActorWBMovable) {
|
||||
var currentFrame = 0
|
||||
var currentRow = 0
|
||||
|
||||
var nFrames: Int = 1
|
||||
private set
|
||||
var nFrames: IntArray = intArrayOf(1)
|
||||
internal set
|
||||
var nRows: Int = 1
|
||||
private set
|
||||
internal set
|
||||
|
||||
private val currentDelay: Second
|
||||
get() = delays[currentRow]
|
||||
@@ -71,7 +71,15 @@ class SpriteAnimation(val parentActor: ActorWBMovable) {
|
||||
*/
|
||||
fun setRowsAndFrames(nRows: Int, nFrames: Int) {
|
||||
this.nRows = nRows
|
||||
this.nFrames = nFrames
|
||||
this.nFrames = IntArray(nRows) { nFrames }
|
||||
}
|
||||
|
||||
fun setFramesOf(row: Int, frameCount: Int) {
|
||||
nFrames[row] = frameCount
|
||||
}
|
||||
|
||||
fun setFramesCount(framesCount: IntArray) {
|
||||
nFrames = framesCount
|
||||
}
|
||||
|
||||
fun update(delta: Float) {
|
||||
@@ -85,9 +93,9 @@ class SpriteAnimation(val parentActor: ActorWBMovable) {
|
||||
while (this.delta >= currentDelay) {
|
||||
// advance frame
|
||||
if (looping) { // looping, wrap around
|
||||
currentFrame = (currentFrame + 1) % nFrames
|
||||
currentFrame = (currentFrame + 1) % nFrames[currentRow]
|
||||
}
|
||||
else if (currentFrame < nFrames - 1) { // not looping and haven't reached the end
|
||||
else if (currentFrame < nFrames[currentRow] - 1) { // not looping and haven't reached the end
|
||||
currentFrame += 1
|
||||
}
|
||||
|
||||
@@ -110,7 +118,7 @@ class SpriteAnimation(val parentActor: ActorWBMovable) {
|
||||
* *
|
||||
* @param scale
|
||||
*/
|
||||
@JvmOverloads fun render(batch: SpriteBatch, posX: Float, posY: Float, scale: Float = 1f) {
|
||||
fun render(batch: SpriteBatch, posX: Float, posY: Float, scale: Float = 1f) {
|
||||
if (cellWidth == 0 || cellHeight == 0) {
|
||||
throw Error("Sprite width or height is set to zero! ($cellWidth, $cellHeight); master: $parentActor")
|
||||
}
|
||||
@@ -158,7 +166,7 @@ class SpriteAnimation(val parentActor: ActorWBMovable) {
|
||||
currentRow = newRow % nRows
|
||||
|
||||
//if beyond the frame index then reset
|
||||
if (currentFrame > nFrames) {
|
||||
if (currentFrame > nFrames[currentRow]) {
|
||||
reset()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user