package server.sprite;
import java.io.IOException;
import java.nio.FloatBuffer;
import org.lwjgl.BufferUtils;
import client.game.Game;
import client.ogl.Texture;
import client.ogl.TextureLoader;
import server.player.Player;
import general.datastructures.Rectangle;
import general.datastructures.Vector2f;
import general.interfaces.IUpdateable;
/**
* k�nftig kein eigener thread mehr, nur noch in einem einheitenthread, der alle einheiten beinhaltet
* @author Tim
*
*/
public abstract class Sprite extends Thread implements IUpdateable {
Game g;
private int playerID;
private Texture texture;
protected Vector2f wc_position;
private float rotation;
private boolean toBeKilled=false;
private FloatBuffer verts_buf = BufferUtils.createFloatBuffer(2*4);
private FloatBuffer tex_buf = BufferUtils.createFloatBuffer(2*4);
public Sprite(Game g, String name, String ref, Player owner)
{
//TODO for multithreading
super(owner.getThreadGroup(),name);
try
{
this.g = g;
this.texture = TextureLoader.getTexture(ref);
}
catch(IOException ioe)
{
ioe.printStackTrace();
System.out.println("Error, Could not create Sprite");
return;
}
verts_buf.put(new float[]{
0.0f, 0.0f,
texture.getImageHeight(), 0.0f,
texture.getImageWidth(), texture.getImageHeight(),
0.0f, texture.getImageWidth()
});
tex_buf.put(new float[]{
0.0f,0.0f,
1.0f,0.0f,
1.0f,1.0f,
0.0f,1.0f
});
}
public int getWidth()
{
return texture.getImageWidth();
}
public int getHeight()
{
return texture.getImageHeight();
}
public Vector2f getPosition()
{
return this.wc_position;
}
public Rectangle getCollisionRectangle()
{
return new Rectangle(this.wc_position,this.getWidth(),this.getHeight());
}
public float getRotation()
{
return this.rotation;
}
public Texture getTexture()
{
return this.texture;
}
public FloatBuffer getVertexBuffer() {
this.verts_buf.rewind();
return verts_buf;
}
public FloatBuffer getTextureBuffer() {
this.tex_buf.rewind();
return tex_buf;
}
public void kill()
{
this.toBeKilled = true;
}
@Override
public abstract void update(long gametime);
public void run()
{
long gametime = Game.getTime();
long oldtime = Game.getTime();
while(!toBeKilled)
{
gametime = Game.getTime() - oldtime;
oldtime = Game.getTime();
this.update(gametime);
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public String toString()
{
return ("Sprite " + this.getName() + " Pos: " +wc_position.toString());
}
}