package fm.ak.client.object;
import fm.ak.client.twod.Datagram;
import fm.ak.client.Local;
import fm.ak.client.Main;
import fm.ak.otse.net.Patchable;
import fm.ak.client.Viewport;
import fm.ak.otse.twod.Sheet;
import fm.ak.otse.twod.Sprite;
import static org.lwjgl.opengl.GL11.*;
/**
* reflects most of mysql://akfm
*/
public class Tile implements Patchable {
public int id;
public int x;
public int y;
private Sprite timage;
private Sprite tdecal;
public static Sheet sheet;
public int rid;
private final int timagerot;
private final int tdecalrot;
private Tile(int tid, String timage, int timagerot, String tdecal, int tdecalrot, int tx, int ty, int trid) {
id = tid;
x = tx;
y = ty;
rid = trid;
//System.out.println("timagerot: " + timagerot);
this.timagerot = timagerot;
this.tdecalrot = tdecalrot;
if ( timage.length() > 0 ) {
this.timage = Tile.sheet.getSprites().get(timage);
if ( this.timage == null ) {
System.err.println("can't match timage " + timage + " with sprite");
//Main.clear.message("can't match timage " + timage + " with sprite");
//Main.clear.display();
}
} else {
// do error handling for tile without timage (rare)
}
//if ( tdecal.length() > 0 ) {
this.tdecal = Tile.sheet.getSprites().get(tdecal);
/*if ( this.tdecal == null ) {
Local.fatal = true;
String e = "can't match tdecal " + tdecal + " with sprite";
System.err.println(e);
synchronized(Component.Components) {
Main.clear.message("can't match timage " + timage + " with sprite");
if ( Local.Stages.FIRSTSCREEN != Local.getStage() ) {
System.err.println("loading clear because not in FIRSTSCREEN");
Main.clear.load();
Main.clear.display();
}
}
}*/
//}
}
/**
* utility safety method for creating a tile
*/
public static Tile create(int tid, String timage, int timagerot, String tdecal, int tdecalrot, int tx, int ty, int trid) {
Tile T = new Tile(tid, timage, timagerot, tdecal, tdecalrot, tx, ty, trid);
T.patchifyOrLocalize();
return T;
}
@Override
public void patch(Object o) {
System.out.println("Patching Tile");
Tile Patch = (Tile) o;
// patch it
}
@Override
public void patchifyOrLocalize() {
if (Local.Tiles.containsKey(id)) {
Local.queuePatch(this, Local.Tiles.get(id));
} else {
Local.TilesByPos.put(x + "," + y, this);
Local.Tiles.put(id, this);
}
Datagram.create("puzzle");
}
/**
* gets a tile next to this one in the specified direction
*/
public Tile at(int Direction) {
String xy;
switch (Direction) {
case 0:
xy = x + "," + (y - 1);
break;
case 1:
xy = (x + 1) + "," + y;
break;
case 2:
xy = x + "," + (y + 1);
break;
case 3:
xy = (x - 1) + "," + y;
break;
default:
return null;
}
if ( Local.TilesByPos.containsKey(xy) ) {
return Local.TilesByPos.get(xy);
}
return null;
}
public void render() {
// bound check?
if ( ! ( x >= (Local.Self.mx - 1 - ((Main.width / 40) / 2) )
&& x <= (Local.Self.mx + 1 + ((Main.width / 40) / 2) )
&& y >= (Local.Self.my - 1 - ((Main.height / 40) / 2) )
&& y <= (Local.Self.my + 1 + ((Main.height / 40) / 2) ))) {
return;
}
glPushMatrix();
glTranslatef(onscreenx(), Math.abs((onscreeny() - Main.height)), 0);
//glTranslatef(1*40, 0, 0);
glRotatef(180, 1, 0, 0);
// darken out
Region r = Local.Regions.get(Local.Self.tile.rid);
float Shade = Viewport.scaleshade;// / 2;
if ( r != null && r.type == 1 && rid != Local.Self.tile.rid ) {
glColor4f(Shade, Shade, Shade, 1);
}
// draw timage and tdecal
for ( int i = 0; i < 2; i++ ) {
sheet.getTexture().bind();
Sprite sprite = null;
int rot = 0;
float tr[][] = {{-1,-1}, {1,0}, {1,-1}, {0,-1}};
float ar[] = {-1, 90, 180, 270};
if ( i == 0 ) { // image
sprite = timage;
rot = timagerot;
}
else if ( i == 1 ) { // decal
sprite = tdecal;
rot = tdecalrot;
}
if ( sprite == null )
break;
if ( rot > 0 ) {
glPushMatrix();
glTranslatef(tr[rot][0] * 40, -tr[rot][1] * 40, 0);
glRotatef(ar[rot], 0, 0, 1);
}
float TW = Tile.sheet.getTexture().getTextureWidth();
float TH = Tile.sheet.getTexture().getTextureHeight();
float tx = (float) sprite.x() / TW;
float ty = (float) sprite.y() / TH;
float tw = ((float) 40 / TW) + tx;
float th = ((float) 40 / TH) + ty;
glBegin(GL_QUADS);
glTexCoord2f(tx, ty);
glVertex2f(0, 0);
glTexCoord2f(tw, ty);
glVertex2f(40, 0);
glTexCoord2f(tw, th);
glVertex2f(40, 40);
glTexCoord2f(tx, th);
glVertex2f(0, 40);
glEnd();
if ( rot > 0 )
glPopMatrix();
}
glColor4f(1, 1, 1, 1);
glPopMatrix();
}
/**
* @deprecated This is ugly
*/
public int onscreenx() {
return (x - Local.Self.mx) * 40 + Viewport.centerX;
}
/**
* @deprecated Real fugly.
*/
public int onscreeny() {
return (y - Local.Self.my) * 40 + Viewport.centerY;
}
}