package com.lucasdnd.ags.map;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.geom.Polygon;
import org.newdawn.slick.geom.Shape;
import org.newdawn.slick.state.StateBasedGame;
import com.lucasdnd.ags.gameplay.Asset;
import com.lucasdnd.ags.system.GameSystem;
/**
* The GameTile represents the polygon around each Tile. It is used to catch the
* player's mouse over a tile and display a polygon around it.
*
* @author tulio
*
*/
public class GameTile {
protected Shape polygon;
protected float points[];
protected float xOffset;
protected float yOffset;
private boolean mouseOver;
private boolean highlighted;
/**
* Creates the size and position of every Game Tile, but does not create the
* polygon yet (they're created inside the update method).
*
* @param x0
* @param y0
* @param x1
* @param y1
* @param x2
* @param y2
* @param x3
* @param y3
*/
protected GameTile(float x0, float y0, float x1, float y1, float x2,
float y2, float x3, float y3) {
// Creates the array of points that form the Polygon
points = new float[8];
points[0] = x0;
points[1] = y0;
points[2] = x1;
points[3] = y1;
points[4] = x2;
points[5] = y2;
points[6] = x3;
points[7] = y3;
// At creation, we assume nobody is mouse overing it
mouseOver = false;
}
/**
* Creates a polygon at each step and checks if the user has the mouse over
* it (using polygon.contains). If it does, set that polygon to
* "mouseOver = true" which, in turn, will set "highlighted = true".
*
* @param container
* @param game
* @param delta
* @param mouseX
* @param mouseY
*/
public void update(int delta, int mouseX, int mouseY) {
// Creates the Polygon to check for the Mouse Over check
polygon = new Polygon(new float[] { points[0] + xOffset,
points[1] + yOffset, points[2] + xOffset, points[3] + yOffset,
points[4] + xOffset, points[5] + yOffset, points[6] + xOffset,
points[7] + yOffset });
// Checks if the user has the mouse inside the Polygon (Tile)
if (polygon.contains((float) mouseX / GameSystem.globalScale,
(float) mouseY / GameSystem.globalScale)) {
mouseOver = true;
highlighted = true;
} else {
mouseOver = false;
}
}
/**
* Checks if that GameTile has "highlighted = true". If it does, renders the
* polygon and fills it with a color, to indicate which tile the user is
* mousing over.
*
* @param container
* @param game
* @param g
*/
public void render(GameContainer container, StateBasedGame game,
Graphics g, float cameraSpeed, Asset placingAsset) {
// Only render the Polygon that is set to highlighted
if (highlighted) {
// Round the offsets and create the Polygons
float preciseXOffset = OffsetUtil.getPreciseOffset(xOffset);
float preciseYOffset = OffsetUtil.getPreciseOffset(yOffset);
polygon = new Polygon(new float[] { points[0] + preciseXOffset,
points[1] + preciseYOffset, points[2] + preciseXOffset,
points[3] + preciseYOffset, points[4] + preciseXOffset,
points[5] + preciseYOffset, points[6] + preciseXOffset,
points[7] + preciseYOffset });
// We save the previous graphics color
Color previousColor = g.getColor();
// Now, if the player is trying to place an Asset on the Map, we
// check if the placement is valid or not
if (placingAsset != null) {
if (placingAsset.isValidPlacement()) {
// If it's valid, use a light green color
g.setColor(new Color(0, 255, 0, 0.5f));
} else {
// If it's not valid, use a light red color
g.setColor(new Color(255, 0, 0, 0.5f));
}
} else {
/*
* In this case, the player is not trying to place an asset on
* the map. So we're all good. We're going to set the default
* light blue color
*/
g.setColor(new Color(0, 0, 255, 0.5f));
}
// Draws the fill of the Polygon using the right color
g.fill(polygon);
// Revert back to the previous color
g.setColor(previousColor);
}
}
public boolean isMouseOver() {
return mouseOver;
}
public void setMouseOver(boolean mouseOver) {
this.mouseOver = mouseOver;
}
public float getxOffset() {
return xOffset;
}
public void setxOffset(float xOffset) {
this.xOffset = xOffset;
}
public float getyOffset() {
return yOffset;
}
public void setyOffset(float yOffset) {
this.yOffset = yOffset;
}
public boolean isHighlighted() {
return highlighted;
}
public void setHighlighted(boolean highlighted) {
this.highlighted = highlighted;
}
}