/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package transientlibs.maps.entities;
import transientlibs.maps.units.GenericUnit;
import transientlibs.maps.implementation.TilelessMap;
import transientlibs.maps.tiles.Tile;
import transientlibs.bindedobjects.gamecontent.Terrain;
import transientlibs.objects.primitives.Coords;
import transientlibs.preui.objects.gui.elements.Marker;
import transientlibs.processors.misc.Detonator;
import java.util.ArrayList;
import transientlibs.slick2d.util.Log;
import transientlibs.objects.primitives.Int;
import transientlibs.maps.utils.coordcalculators.AbstractScreenCoordsCalculator;
import transientlibs.preui.objects.gui.interfaces.IAnimation;
import transientlibs.preui.objects.gui.interfaces.IImage;
/**
*
* @author kibertoad
*/
public abstract class MapObject extends Marker {
public Coords mapCoords = new Coords(0, 0);
//public TransientImage picture;
public Int side = new Int(-1);
public TilelessMap onMap;
public boolean onScreen = false;
public GenericUnit collidedUnit;
public int sizeX; //absolute in pixels
public int sizeY;
public float mapSizeX; //relative to tile, where 1 = full tile
public float mapSizeY; //relative to tile, where 1 = full tile
public static int screenOffsetX = 0;
public static int screenOffsetY = 0;
public IAnimation currentAnimation = null;
public void setAnimation(IAnimation setAnim) {
currentAnimation = setAnim;
}
public int getCurrentTileMovementCost() {
return (int) Detonator.INSTANCE.tileMovementCalculator.getCost(mapCoords.getIntX(), mapCoords.getIntY());
}
public MapObject(TilelessMap setMap) {
super(0, 0);
onMap = setMap;
}
public final void setMOImage(IImage setOriginalImage) {
setBothImages(setOriginalImage);
originalImage = setOriginalImage;
//setHoveredImage (image);
recountImageSize();
//Log.info("setmap is null"+setMap.toString());
//Log.info("tileless is null"+transientlibs.preui.grid.TilelessMap.map.toString());
if (onMap != null) {
if (getWidth() == 0) {
mapSizeX = 0.1f;
mapSizeY = 0.1f;
} else {
//Log.info("getWidth: " + getWidth());
//TilelessMap hack
if ((onMap.equals(transientlibs.maps.implementation.ExtendedTilelessMap.map))
|| (Terrain.tileWidth == 0)) {
mapSizeX = 0.1f;
mapSizeY = 0.1f;
} else {
//mapSizeX = getImage().getWidth() / Terrain.tileWidth;
//mapSizeY = getImage().getHeight() / Terrain.tileHeight;
mapSizeX = getWidth() / Terrain.tileWidth;
mapSizeY = getHeight() / Terrain.tileHeight;
}
if (mapSizeX < 0.1) {
mapSizeX = (float) 0.1;
}
if (mapSizeY < 0.1) {
mapSizeY = (float) 0.1;
}
}
}
}
public MapObject(TilelessMap setMap, IImage setOriginalImage) {
super(0, 0);
onMap = setMap;
setMOImage(setOriginalImage);
//Log.warn("Tile Original is null: "+(setOriginalImage == null));
}
public static ArrayList<MapObject> killList = new ArrayList<MapObject>();
public Tile currentTile() {
return onMap.getTile(mapCoords.getIntX(), mapCoords.getIntY());
}
public Coords deltaCoords(float deltaX, float deltaY) {
Coords result = new Coords(deltaX, deltaY);
result.x += mapCoords.x;
result.y += mapCoords.y;
return result;
}
public Coords deltaCoords(Coords delta) {
Coords result = new Coords(delta.getIntX(), delta.getIntY());
result.x += mapCoords.x;
result.y += mapCoords.y;
return result;
}
public Tile deltaTile(float deltaX, float deltaY) {
float checkX = mapCoords.x + deltaX;
float checkY = mapCoords.y + deltaY;
Tile tile;
try {
tile = onMap.getTile((int) (checkX), (int) (checkY));
} catch (ArrayIndexOutOfBoundsException ex) {
Log.info("null");
tile = null;
}
return tile;
}
public void becomeDestroyed() {
killList.remove(this);
}
public static void processKillList() {
while (!(killList.isEmpty())) {
//Log.info("KillList entries: "+killList.size());
//Log.info("Now kill "+killList.get(0).toString());
killList.get(0).becomeDestroyed();
}
}
public Tile tileBelow() {
return onMap.getTile(mapCoords.getIntX(), mapCoords.getIntY() + 1);
}
public Tile tileAbove() {
return onMap.getTile(mapCoords.getIntX(), mapCoords.getIntY() - 1);
}
public Tile tileNext() {
return onMap.getTile(mapCoords.getIntX() + 1, mapCoords.getIntY());
}
public void processUnitCollision() {
}
public int imageIntEndX() {
return (int) screenCoords.x + imageWidth;
}
public int imageIntEndY() {
return (int) screenCoords.y + imageHeight;
}
public float imageEndX() {
return screenCoords.x + imageWidth;
}
public float imageEndY() {
return screenCoords.y + imageHeight;
}
public boolean isVisiblyCollided(MapObject secondObject) {
return (//OK
(screenCoords.x <= secondObject.screenCoords.x)
&& (imageEndX() >= secondObject.screenCoords.x)
&& (screenCoords.y <= secondObject.screenCoords.y)
&& (imageEndY() >= secondObject.screenCoords.y))
|| ((screenCoords.x >= secondObject.screenCoords.x)
&& (imageEndX() <= secondObject.imageEndX())
&& (screenCoords.y >= secondObject.screenCoords.y)
&& (imageEndY() <= secondObject.imageEndY()))
|| //untested
((screenCoords.x >= secondObject.screenCoords.x)
&& (imageEndX() <= secondObject.imageEndX())
&& (screenCoords.y >= secondObject.screenCoords.y)
&& (screenCoords.y <= secondObject.imageEndY()));
}
public boolean terrainCollision(float deltaX, float deltaY) {
return Detonator.INSTANCE.tileMovementCalculator.terrainCollision(deltaX, deltaY, this);
}
public void calcImagePosition(AbstractScreenCoordsCalculator calculator) {
calculator.calculateScreenCoords(this);
}
public void calcImagePosition() {
if (Detonator.INSTANCE.screenCoordsCalculator != null) {
//Log.warn("DODO");
Detonator.INSTANCE.screenCoordsCalculator.calculateScreenCoords(this);
}
}
public boolean canMove(float xDelta, float yDelta) {
int tryX = (int) (mapCoords.getIntX() + xDelta);
int tryY = (int) (mapCoords.getIntY() + yDelta);
if (onMap.withinBorders(tryX, tryY)) {
//Log.info("Try: "+tryX+"/"+tryY);
if (!(onMap.getTile(tryX, tryY).units.isEmpty())) {
collidedUnit = onMap.getTile(tryX, tryY).units.get(0);
return false;
}
}
return (!(Detonator.INSTANCE.tileMovementCalculator.isBlocked(tryX, tryY)));
}
public boolean canMoveLegacy(float xDelta, float yDelta) {
int tryX = (int) (mapCoords.getIntX() + xDelta);
int tryY = (int) (mapCoords.getIntY() + yDelta);
Tile tryTile;
if ((tryX > 0) && (tryX <= onMap.sizeX)
&& (tryY > 0) && (tryY <= onMap.sizeY)) {
tryTile = onMap.getTile(tryX, tryY);
} else {
tryTile = null;
}
//Log.info("TryY "+tryY);
//Log.info("Map sizeY "+onMap.sizeY);
if (onMap.returnTileUnit(tryX, tryY, 0) == null) {
collidedUnit = null;
} else {
collidedUnit = tryTile.units.get(0);
}
return ((tryTile != null)
&& (collidedUnit == null)
&& (tryTile.isMoveable()));
}
public void performTeleport(int toX, int toY) {
mapCoords.x = toX;
mapCoords.y = toY;
}
public void performMove(int xDelta, int yDelta) {
Log.info("perform move");
mapCoords.x = mapCoords.x + xDelta;
mapCoords.y = mapCoords.y + yDelta;
}
public void performTeleport(float toX, float toY) {
mapCoords.x = toX;
mapCoords.y = toY;
}
public void performMove(float xDelta, float yDelta) {
mapCoords.x = mapCoords.x + xDelta;
mapCoords.y = mapCoords.y + yDelta;
}
public boolean move(int inDirection) {
collidedUnit = null;
boolean result = true;
//Log.info("move in "+inDirection+"direction");
switch (inDirection) {
case 4: {
if (canMove(-1, 0)) {
performMove(-1, 0);
} else {
result = false;
}
break;
}
case 6: {
if (canMove(1, 0)) {
performMove(1, 0);
} else {
result = false;
}
break;
}
case 8: {
if (canMove(0, -1)) {
performMove(0, -1);
} else {
result = false;
}
break;
}
case 2: {
if (canMove(0, 1)) {
performMove(0, 1);
} else {
result = false;
}
break;
}
case 7: {
if (canMove(-1, -1)) {
performMove(-1, -1);
} else {
result = false;
}
break;
}
case 1: {
if (canMove(-1, 1)) {
performMove(-1, 1);
} else {
result = false;
}
break;
}
case 9: {
if (canMove(1, -1)) {
performMove(1, -1);
} else {
result = false;
}
break;
}
case 3: {
if (canMove(1, 1)) {
performMove(1, 1);
} else {
result = false;
}
break;
}
default: {
Log.info("Unknown direction: " + inDirection);
}
}
if (!(collidedUnit == null)) {
processUnitCollision();
}
// Log.info("finished move");
return result;
}
@Override
public void draw(int onX, int onY) {
//getImage().draw(onX+onMap.drawOffset.getIntX(), onY+onMap.drawOffset.getIntY());
getImage().draw(onX, onY);
//imageLink.draw(onX, onY, Detonator.instance.imageScale);
}
public int getCameraX() {
return onMap.eye.eyePosition.getIntX();
}
public int getCameraY() {
return onMap.eye.eyePosition.getIntY();
}
}