package fm.ak.client;
import fm.ak.otse.twod.Mousable;
import fm.ak.client.object.Player;
import fm.ak.client.object.Entity;
import fm.ak.client.object.Region;
import fm.ak.client.object.Tile;
import fm.ak.otse.net.Net;
import fm.ak.otse.net.Callback;
import fm.ak.otse.font.Text;
import fm.ak.otse.twod.Component;
import fm.ak.otse.Mouse.MouseState;
import fm.ak.otse.Renderable;
import java.io.IOException;
/**
* the game map
*/
public final class Viewport extends Renderable implements Mousable {
/**
* the {@link Component} that highlights the active tile
*
* @see Viewport#marked
*/
private static Component marker;
/**
* Center points of the screen.
*/
public static int centerX, centerY;
/**
* the {@link tile} that is marked by the {@link Viewport#marker}
*
* @see Viewport#marker
*/
private static Tile marked;
/**
* Text that shows up when inside a building.
*/
public static Text insideOutsideLabel = null;
/**
* the name of a building. If not null will be subject to positional
* updates.
*/
public static Text buildingLabel = null;
/**
* the animating in-building darkness multiplier
*/
public static float scaleshade = 1;
public Viewport() throws IOException {
marker = new Component("$mark", 0, 0, 40, 40);
marker.setSheetSprite(Main.main.sheet1, "marker");
//TextInstance.create(Main.SegoeUILight14, "WGHAT MAKES YOU THINK ANYTHING AT ASO LDSADAS ASD ASD", 500, 50, 0, 450, TextInstance.NORMAL_ANGLE, TextInstance.EffectEnum.FADE_IN, 0, 0, 0);
marker.index = 2;
}
@Override
public POST reindeer() {
render();
return POST.RETAIN;
}
// TODO: render to a render buffer? Look into this.
@Override
final protected void render() {
//System.out.println("render Viewport");
justify();
Region r;
if ( (r = Local.Regions.get(Local.Self.tile.rid)) != null && r.type == 1 ) {
if (scaleshade > 0.50f) {
scaleshade -= 0.01f;
}
} else {
scaleshade = 1;
}
// reposition the building label
if ( buildingLabel != null ) {
if ( ! buildingLabel.isActive() ) {
buildingLabel = null;
} else {
r = Local.Regions.get(Local.Self.tile.rid);
if ( r != null ) {
buildingLabel.visibile = true;
buildingLabel.setX(r.getOnscreenX());
buildingLabel.setY(r.getOnscreenY() - buildingLabel.getFont().getMetrics().Height);
}
}
}
for ( Tile t : Local.TilesByPos.values() ) {
t.render();
}
for ( Player p : Local.Players.values() ) {
p.reindeer();
}
}
public final void handleMouseFocus() {
if ( Main.main.getMouse().getLeftMouse() == MouseState.PRESSED ) {
onMousePress();
} else if ( Main.main.getMouse().getLeftMouse() == MouseState.RELEASED ) {
onMouseRelease();
}
onMouseOver();
}
public final void onMouseOver() {
int x = Main.main.getMouse().getX();
int y = Main.main.getMouse().getY();
String xy = ((int) ((double) ((double) (x - centerX) / 40) +
Local.Self.mx)) + "," + ((int) ((double) ((double)
(y - centerY) / 40) + Local.Self.my));
//String xy = ((Main.mousex - centerx) / 40) + Local.Self.mx + "," + ((Main.mousey - centery) / 40) + Local.Self.my;
if (Local.TilesByPos.containsKey(xy)) {
Tile t = marked = Local.TilesByPos.get(xy);
marker.position(t.onscreenx(), t.onscreeny(), marker.w(), marker.h());
} else {
marked = null;
marker.position(-40, -40, marker.w(), marker.h());
}
}
public final void onMousePress() {
//ystem.out.println("Press on Viewport"); // oh shut up
}
public final void onMouseRelease() {
if ( getMarked() == null ) {
return;
}
Entity e;
Region r;
for (int d = 0; d < 4; d++) {
if ( getMarked().at(d) == Local.Self.tile ) {
if ((r = Local.Regions.get(getMarked().rid)) != null && r.type == 1 && r.id != Local.Self.tile.rid
&& !((e = Local.Entities.get(getMarked().id)) != null && e.getType() == Entity.Type.DOOR && e.parama == d)
|| (r = Local.Regions.get(Local.Self.tile.rid)) != null && r.type == 1 && r.id != getMarked().rid
&& !((e = Local.Entities.get(Local.Self.tile.id)) != null && e.getType() == Entity.Type.DOOR && e.parama == Main.opposite[d])) {
System.out.println("stubmling into region " + r.name);
return; // We tried going into or out of a building without there being a door.
} else if ((r = Local.Regions.get(getMarked().rid)) != null && r.type == 1 && (r = Local.Regions.get(Local.Self.tile.rid)) == null) {
// We went into a building.
r = Local.Regions.get(getMarked().rid);
//InsideOutsideLabel = TextInstance.create(Main.SegoeUILight14, "The world outside blackens.", 0, 0, 0, TextInstance.NO_EFFECTS, 1, 1, 1);
//InsideOutsideLabel.setVisibility(false);
buildingLabel = Text.create(StaticFont.SegoeUILight14, r.name, 0, 0, 0, 0, Text.NORMAL_ANGLE, Text.Fades.NONE, 1, 1, 1, 1);
buildingLabel.index = 3;
buildingLabel.visibile = false;
} else if (Local.Regions.get(getMarked().rid) == null && (r = Local.Regions.get(Local.Self.tile.rid)) != null && r.type == 1) {
// We went out of a building.
buildingLabel.markDispose(); // TODO: We shouldn't remove it until we have moved. Resolve!
// TODO: Today it crashed here :C
}
Callback.Try(Net.Do.MOVE, "move:" + Main.opposite[d], false);
break;
}
}
}
/**
* change the 'center point'. A weird function
*
* @deprecated
*/
public static void justify() {
centerX = ((Main.width / 2) - 20);
centerY = ((Main.height / 2) - 20);
}
/**
* Gets the {@link Viewport#marker} {@link Component}.
*
* @see Viewport#marker
*/
public static Component getMarker() {
return marker;
}
/**
* Gets the {@link tile} the {@link Viewport#marker} is highlighting.
*/
public static Tile getMarked() {
return marked;
}
}