/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package transientlibs.maps.tiles;
import transientlibs.maps.entities.Bullet;
import transientlibs.maps.entities.Landmark;
import java.util.ArrayList;
//import org.lwjgl.input.Mouse;
import transientlibs.slick2d.util.Log;
import transientlibs.bindedobjects.core.Binding;
import transientlibs.bindedobjects.gamecontent.Terrain;
import transientlibs.maps.entities.Bullet;
import transientlibs.maps.entities.Landmark;
import transientlibs.objects.creatures.items.Inventory;
import transientlibs.maps.units.GenericUnit;
import transientlibs.maps.entities.MapObject;
import transientlibs.maps.tiles.TileEffect;
import transientlibs.maps.interfaces.ITile;
import transientlibs.maps.implementation.BasicMap;
import transientlibs.objects.primitives.Coords;
import transientlibs.preui.gdx.gui.MouseHack;
import transientlibs.preui.objects.gui.elements.InfoGroup;
import static transientlibs.preui.objects.gui.elements.Marker.ScreenHeight;
import transientlibs.processors.misc.Detonator;
/**
*
* @author kibertoad
*/
public class Tile extends MapObject implements ITile {
//boolean wasUsed = false;
//public double distFromFinish;
//public double distFromStart;
public static int fireCode;
public static int fixCode;
public static int dangerCode;
public static int leakCode;
public static int effectXOffset = 10;
public static int effectYOffset = 16;
public static int numberXOffset = 16;
public static int numberYOffset = 14;
public static int itemXOffset = 6;
public static int itemYOffset = 13;
public Terrain terrain;
//Coords mapCoords;
//Coords drawCoords = new Coords ();
public ArrayList<GenericUnit> units = new ArrayList<GenericUnit>();
//public ArrayList<Item> items = new ArrayList<Item>();
public Inventory items = new Inventory();
public ArrayList<Landmark> landmarks = new ArrayList<Landmark>();
public ArrayList<Bullet> missiles = new ArrayList<Bullet>();
public ArrayList<TileEffect> effects = new ArrayList<TileEffect>();
public ArrayList<String> tags = null;
public Coords hoverCoords = new Coords(); //screenCoords are reserved for stupid GDX positioning, hover is real screen coords
//public TransientImage picture;
public Tile(int terrainType, int coordX, int coordY, BasicMap setMap) {
super(setMap);
if (terrainType != -1) {
this.setMOImage(Terrain.terrains.get(terrainType).image);
terrain = Terrain.terrains.get(terrainType);
}
if (getImage() == null) {
this.imageHeight = Terrain.tileHeight;
this.imageWidth = Terrain.tileWidth;
}
infoOnHover = true;
mapCoords = new Coords(coordX, coordY);
//Log.warn("New tile: "+mapCoords.toRoundedString());
//ImageLink = Images.genericTerrain;
noHoverOnTransparent = false;
}
public void addTag(String theOne) {
if (tags == null) {
tags = new ArrayList<String>();
}
if (!(hasTag(theOne))) {
tags.add(theOne);
Log.info("ADDED TAG");
}
}
public boolean hasTag(String theOne) {
if (tags == null) {
return false;
}
Log.info("Check tags: " + tags.size());
for (String s : tags) {
if (s.equals(theOne)) {
return true;
}
}
return false;
}
@Override
public MarkerType markerID() {
return MarkerType.TileMarker;
}
@Override
public String name() {
return (mapCoords.toString() + " (onScreen: " + onScreen + ")");
}
@Override
public void fillInfo(InfoGroup infoTable) {
infoTable.addOption(terrain.name() + " (" + mapCoords.x + ":" + mapCoords.y + ")");
if (!(landmarks.isEmpty())) {
infoTable.addOption(landmarks.get(0).name());
}
//if (region != null) {
// region.fillInfo(infoTable);
//}
if (!items.items.isEmpty()) {
infoTable.addOption(items.items.get(0).item.name());
}
/*
if (!effects.isEmpty()) {
for (TileEffect e : effects) {
if (e.effect.ID == fixCode) {
if (e.unitsLeft.value >= Detonator.instance.getBindedGlobalValue("damagemaxlevel").value)
{
infoTable.addOption("Malfunction: " + effects.get(0).unitsLeft + " damage points");
} else {
infoTable.addOption(e.effect.desc + ": " + effects.get(0).unitsLeft + "/" + Detonator.instance.getBindedGlobalValue("damagemaxlevel").value + " damage points.");
}
} else {
infoTable.addOption(e.effect.desc + ": " + e.unitsLeft + " hazard points left");
}
}
}
*/
}
public void placeLandmark(Landmark landmark) {
landmarks.add(landmark);
}
public void placeUnit(GenericUnit unit) {
units.add(unit);
}
public void changeTerrain(String toTerrain) {
changeTerrain(Binding.getBinding(Binding.terrainBinding, toTerrain));
calcImagePosition();
}
public void changeTerrain(int toTerrain) {
terrain = Terrain.terrains.get(toTerrain);
setBothImages(terrain.image);
recountImageSize();
}
@Override
public void draw() {
//Log.info("Draw "+name());
//this.calcImagePosition();
//Log.info("On map: "+onMap.name());
if (onScreen == true) {
if (terrain.animation == null) {
getImage().draw(screenCoords.getIntX(), screenCoords.getIntY());
} else {
terrain.animation.draw(screenCoords.getIntX(), screenCoords.getIntY());
}
if (landmarks.size() > 0) {
//landmarks.get(0).draw(screenCoords.getIntX(), screenCoords.getIntY());
landmarks.get(0).draw();
}
if (items.items.size() > 0) {
items.items.get(0).item.image.draw(screenCoords.getIntX() + itemXOffset, screenCoords.getIntY() + itemYOffset);
}
if (!effects.isEmpty()) {
for (TileEffect nowEffect : effects) {
if (nowEffect.effect.animation == null) {
nowEffect.effect.image.draw(screenCoords.getIntX() + effectXOffset, screenCoords.getIntY() + effectYOffset);
} else {
nowEffect.effect.animation.draw(screenCoords.getIntX() + effectXOffset, screenCoords.getIntY() + effectYOffset);
}
}
}
}
}
@Override
public void draw(int x, int y) {
}
public void draw(Coords eyeCoords) {
//Log.info("OnScreen: "+onScreen);
//Log.info("Try Draw tile "+eyeCoords.toString());
if ((onScreen == true) && (isDrawn == true)) {
//Log.info("Draw tile "+eyeCoords.toString());
//Log.info("Draw tile "+mapCoords.toString());
//Log.info("DRAW TILE: "+screenCoords.toString());
getImage().draw(screenCoords.getIntX(), screenCoords.getIntY());
//imageLink.draw(screenCoords.x, screenCoords.getIntY(), Detonator.instance.imageScale);
if (landmarks.size() > 0) {
landmarks.get(0).draw(screenCoords.getIntX(), screenCoords.getIntY());
}
// if (units.size() > 0) {
//units.get(0).draw(screenCoords.getIntX(), screenCoords.getIntY());
// units.get(0).draw();
// }
if (items.items.size() > 0) {
items.items.get(0).item.image.draw(screenCoords.getIntX() + itemXOffset, screenCoords.getIntY() + itemYOffset);
}
//if (missiles.size() > 0) {
//missiles.get(0).draw(screenCoords.getIntX() + itemXOffset, screenCoords.getIntY() + itemYOffset);
// missiles.get(0).draw();
// }
if (!effects.isEmpty()) {
for (TileEffect nowEffect : effects) {
if (nowEffect.effect.animation == null) {
nowEffect.effect.image.draw(screenCoords.getIntX() + effectXOffset, screenCoords.getIntY() + effectYOffset);
} else {
nowEffect.effect.animation.draw(screenCoords.getIntX() + effectXOffset, screenCoords.getIntY() + effectYOffset);
}
}
}
}
}
public TileEffect returnEffect(int compareValue) {
for (TileEffect nowEffect : effects) {
if (nowEffect.effect.ID == compareValue) {
return nowEffect;
}
}
return null;
}
public TileEffect returnEffectByCode(String byCode) {
return returnEffect(Binding.readBinding(Binding.effectBinding, byCode));
}
public boolean hasEffect(int compareValue) {
boolean result = false;
//Log.info("tile has "+effects.size()+" effects.");
for (TileEffect nowEffect : effects) {
if (nowEffect.effect.ID == compareValue) {
result = true;
}
}
return result;
}
public boolean hasLandmark(int compareValue) {
boolean result = false;
for (Landmark nowEffect : landmarks) {
if (nowEffect.landmark.ID == compareValue) {
result = true;
}
}
return result;
}
public boolean hasLandmarkByCode(String byCode) {
return hasLandmark(Binding.readBinding(Binding.landmarkBinding, byCode));
}
public boolean hasLandmarkByGroup(String byGroup) {
boolean result = false;
for (Landmark l : landmarks) {
if (l.landmark.group.equals(byGroup)) {
result = true;
}
}
return result;
}
public boolean hasEffectByCode(String byCode) {
return hasEffect(Binding.readBinding(Binding.effectBinding, byCode));
}
public void addEffect(int byID) {
effects.add(new TileEffect(byID));
}
public void addEffect(int byID, int amount) {
TileEffect tryEffect = returnEffect(byID);
if (tryEffect == null) {
effects.add(new TileEffect(byID));
} else {
tryEffect.unitsLeft.value = tryEffect.unitsLeft.value + amount;
}
}
public void removeEffect(int byID, int amount) {
TileEffect tryEffect = returnEffect(byID);
if (tryEffect != null) {
if (tryEffect.unitsLeft.value > amount) {
tryEffect.unitsLeft.value = tryEffect.unitsLeft.value - amount;
} else {
effects.remove(tryEffect);
}
}
}
public void addEffectByCode(String byCode) {
addEffect(Binding.readBinding(Binding.effectBinding, byCode));
//effects.add(Effects.effects.get(Binding.readBinding(Binding.effectBinding, byCode)));
//Log.info("effect added");
}
public void removeEffect(int effectID) {
for (TileEffect nowEffect : effects) {
if (nowEffect.effect.ID == effectID) {
effects.remove(nowEffect);
break;
}
}
}
public void removeEffectByCode(String byCode) {
removeEffect(Binding.readBinding(Binding.effectBinding, byCode));
}
public void drawHover(Coords eyeCoords) {
}
public GenericUnit returnUnit() {
if (units.isEmpty()) {
return null;
} else {
return units.get(0); //replace with check for non-destroyed unit
}
}
@Override
public int getX() {
return mapCoords.getIntX();
}
@Override
public int getY() {
return mapCoords.getIntY();
}
@Override
public Terrain getTerrain() {
return terrain;
}
//walk on tile
@Override
public boolean isMoveable() {
return (terrain.moveCost != -1) && (units.isEmpty());
}
public boolean hasNoUnpassableEffects() {
boolean result = true;
for (TileEffect te : effects) {
if (!(te.effect.isPasseable)) {
result = false;
break;
}
}
return result;
}
public boolean isObstacle() {
return (terrain.moveCost == -1);
}
//walk through tile - can you leave the tile
@Override
public boolean isPassable() {
return ((terrain.moveCost != -1)
&& (hasNoUnpassableEffects()));
}
public boolean isValidDestination(int toX, int toY) {
//Log.info(getX()+":"+getY());
return (isMoveable()) && (toX == getX()) && (toY == getY());
}
@Override
public Coords getCoords() {
return mapCoords;
}
public boolean hasLandmarkByTag(String theTag) {
boolean result = false;
//Log.info("Check landmarks, total: " + landmarks.size());
for (Landmark l : landmarks) {
if (l.landmark.hasTag(theTag)) {
result = true;
}
}
return result;
}
public ArrayList<Landmark> getLandmarksByTag(String theTag) {
ArrayList<Landmark> list = new ArrayList<Landmark>();
for (Landmark l : landmarks) {
if (l.landmark.hasTag(theTag)) {
list.add(l);
}
}
return list;
}
@Override
public boolean isHovered() {
int mouseX = MouseHack.getX();
int mouseY = MouseHack.getY();
//Detonator.INSTANCE.screenCoordsCalculator.cal
hoverCoords = Detonator.INSTANCE.screenCoordsCalculator.returnScreenCoords(this);
mouseY = ScreenHeight - mouseY;
boolean result = ((((mouseX >= hoverCoords.x) && (mouseX < (hoverCoords.x + imageWidth)))
&& ((mouseY >= hoverCoords.y) && (mouseY < (hoverCoords.y + imageHeight)))));
//boolean result = ((((mouseX >= actualX) && (mouseX < (actualX + imageWidth)))
// && ((ScreenHeight - mouseY >= actualY) && (mouseY < (actualY + imageHeight))))
// && ((noHoverOnTransparent == false) || (getImage() == null) || (!isTransparentAreaMouseHovered(mouseX, mouseY))));
//if (result) {
//Log.warn("Map coords: "+mapCoords.getIntX()+"/"+mapCoords.getIntY());
//Log.warn("Mouse coords: " + mouseX + "/" + (mouseY));
//Log.warn("TransientImage size: " + imageWidth + "/" + imageHeight);
//Log.warn("Must be between " + screenCoords.x + "/" + screenCoords.getIntY() + " and " + (screenCoords.x + imageWidth) + '/' + (screenCoords.getIntY() + imageHeight));
//Log.warn("Must be between " + hoverCoords.x + "/" + hoverCoords.y + " and " + (hoverCoords.x + imageWidth) + '/' + (hoverCoords.y + imageHeight));
//Log.warn("Camera position " + onMap.getCameraX() + "/" + onMap.getCameraY() + ".");
//Log.warn("Real Camera position " + realCameraX+ "/" + realCameraY+ ".");
//}
return result;
}
}