ludum_dare_38/src/com/gnarly/engine/components/Animation.java
2024-08-07 04:28:53 +00:00

74 lines
1.5 KiB
Java
Executable file

package com.gnarly.engine.components;
import com.gnarly.engine.utils.Library;
public class Animation {
int curFrame, numFrames;
float pastTime, curTime, mspf;
boolean play, loop;
Texture[] frames;
public Animation(String name, String type, int numFrames, int fps, boolean loop) {
this.numFrames = numFrames;
this.loop = loop;
play = true;
mspf = 1000.0f / (float) fps;
pastTime = System.nanoTime() / 1000000.0f;
frames = new Texture[numFrames];
for (int i = 0; i < frames.length; i++)
frames[i] = Library.getTexture(name + " " + (i + 1) + "." + type);
}
public Animation(String name) {
frames = new Texture[1];
frames[0] = Library.getTexture(name);
}
public void update() {
if(frames.length > 1) {
curTime = System.nanoTime() / 1000000.0f;
while(play && curTime - pastTime > mspf) {
if(curFrame != frames.length - 1)
++curFrame;
else if(loop)
curFrame = 0;
else
pause();
pastTime += mspf;
}
}
}
public int getWidth() {
return frames[curFrame].getWidth();
}
public int getHeight() {
return frames[curFrame].getHeight();
}
public void pause() {
play = false;
}
public void play() {
if(!play) {
pastTime = System.nanoTime() / 1000000.0f;
play = true;
}
}
public void reset() {
curFrame = 0;
pastTime = System.nanoTime() / 1000000.0f;
}
public void bind() {
frames[curFrame].bind();
}
public void unbind() {
frames[curFrame].unbind();
}
}