Project as is
This commit is contained in:
commit
80c9e2d168
55 changed files with 1573 additions and 0 deletions
74
src/com/gnarly/engine/components/Animation.java
Executable file
74
src/com/gnarly/engine/components/Animation.java
Executable file
|
@ -0,0 +1,74 @@
|
|||
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();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue