/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package transientlibs.bindedobjects.gamecontent;
import java.util.ArrayList;
import java.util.Iterator;
import transientlibs.slick2d.util.Log;
import transientlibs.bindedobjects.core.Binding;
import transientlibs.bindedobjects.core.datasets.LesserBindedValue;
import static transientlibs.bindedobjects.gamecontent.Terrain.lastTerrain;
import transientlibs.preui.objects.gui.interfaces.IAnimation;
import transientlibs.processors.misc.Detonator;
import transientlibs.preui.objects.gui.interfaces.IImage;
import transientlibs.tex.StringAnalyst;
/**
*
* @author kibertoad
*/
public class Terrain extends LesserBindedValue {
public static int GENERIC_TERRAIN = 0;
//public int ID;
//public String name;
public IImage image = null;
public IAnimation animation = null;
public char charCode;
public float moveCost = (float) 1.0;
public boolean canStandOn = false;
public boolean blocksShooting = false;
public static int tileWidth = -1;
public static int tileHeight;
public static float tileStepVectorSize; //What fraction of tile must be passed for that to be movement by 2/1
//public static HashMap terrainList = new HashMap();
//public static TIntObjectHashMap terrainList = new TIntObjectHashMap();
//public static TObjectIntHashMap terrainCodes = new TObjectIntHashMap();
public static ArrayList<Terrain> terrains = new ArrayList<Terrain>();
public static Terrain lastTerrain;
public static void loadTerrain() {
if (StringAnalyst.isFileExist("terrain.dat")) {
StringAnalyst reader = new StringAnalyst();
reader.openFile("terrain.dat");
while (!reader.eof) {
reader.readRow();
analyzeStringForTerrain(reader);
}
if (tileWidth == -1) {
if (terrains.size() > 0) {
Log.warn("TERRAIN DAWG");
//tileWidth = terrains.get(0).image.image.getWidth();
//tileHeight = terrains.get(0).image.image.getHeight();
tileWidth = terrains.get(0).getWidth();
tileHeight = terrains.get(0).getHeight();
}
}
reader.closeFile();
}
}
public boolean isTerrainType(String byCode) {
return (ID == Binding.readBinding(Binding.terrainBinding, byCode));
}
public static void analyzeStringForTerrain(StringAnalyst analyst) {
analyzeStringForTerrain("-NO-", analyst);
}
public static void analyzeStringForTerrain(String getString, StringAnalyst analyst) {
if (!"-NO-".equals(getString)) {
analyst.fullStr = getString;
}
analyst.checkForAliases();
analyst.getNextString();
String nowStr = analyst.lastStr;
boolean withResult = false;
if (nowStr.equals("terrain")) {
terrains.add(new Terrain());
lastTerrain = terrains.get(terrains.size() - 1);
lastTerrain.ID = analyst.getBinding(Binding.terrainBinding);
lastTerrain.LName.value = "Tile";
withResult = true;
}
if (nowStr.equals("symbol")) {
lastTerrain.charCode = analyst.getNextChar();
withResult = true;
}
if (nowStr.equals("name")) {
lastTerrain.LName.value = analyst.restOfRow();
withResult = true;
}
if (nowStr.equals("tag")) {
lastTerrain.tags.add(analyst.getNextString());
withResult = true;
}
if (nowStr.equals("group")) {
lastTerrain.group = analyst.getNextString();
withResult = true;
}
if (nowStr.equals("movecost")) {
lastTerrain.moveCost = analyst.getNextFloat();
withResult = true;
}
if (nowStr.equals("standable")) {
lastTerrain.canStandOn = true;
withResult = true;
}
if (nowStr.equals("shootingblock")) {
lastTerrain.blocksShooting = true;
withResult = true;
}
if (nowStr.equals("image")) {
Log.info(nowStr);
/*
if (Images.useGDXImages == false) {
lastTerrain.image = Images.imageList.get(Binding.readBinding(Binding.imageBinding, analyst.getNextString()));
} else {
lastTerrain.image = null;
}
*/
lastTerrain.image = Detonator.INSTANCE.imageProvider.getImage(analyst.getNextString());
withResult = true;
}
if (nowStr.equals("animation")) {
Log.info(nowStr);
//lastTerrain.animation = Animations.getAnimationByID(Binding.readBinding(Binding.animationBinding, analyst.getNextString())).animation;
Log.notImplemented();
withResult = true;
}
if ((withResult == false) && (nowStr.length() > 2) && (!nowStr.startsWith("//"))
&& (!nowStr.startsWith("#"))) {
Log.error("Unknown string: " + nowStr);
}
}
@Override
public String name() {
return LName.value;
}
public static char getWallSymbol() {
for (Terrain t : terrains) {
if (t.hasTag("wall")) {
return t.charCode;
}
}
return '#';
}
public static char getFloorSymbol() {
for (Terrain t : terrains) {
if (t.hasTag("floor")) {
return t.charCode;
}
}
return '.';
}
public static int charToTerrain(char fromChar) {
Iterator<Terrain> x = terrains.iterator();
Terrain nowTerrain = x.next();
int counter = 0;
while ((x.hasNext()) && (nowTerrain.charCode != fromChar)) {
//Log.info("check "+fromChar +" vs " + nowTerrain.charCode);
nowTerrain = x.next();
counter++;
}
if (nowTerrain.charCode != fromChar) {
Log.error("unknown terrain " + fromChar);
}
//Log.info("now terrain: "+counter);
return counter;
}
public int getWidth() {
if (image != null) {
return image.getHeight();
} else {
return 0;
}
}
public int getHeight() {
if (image != null) {
return image.getWidth();
} else {
return 0;
}
}
}