package catchemrpg.threads;
import _api.alienfactory.javamappy.Map;
import _api.alienfactory.javamappy.loader.MapLoader;
import _api.alienfactory.javamappy.viewer.LayerViewer;
import _api.alienfactory.javamappy.viewer.MapViewer;
import _api.alienfactory.javamappy.viewer.render.J2SE14Renderer;
import _api.alienfactory.javamappy.viewer.render.Renderer;
import catchemrpg.drawers.GameMap;
import catchemrpg.base.*;
import catchemrpg.gameobjects.MapMeta;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
/**
*
* @author Toby Pinder (Gigitrix)
*/
public class MapThread
{
/**
* map maximum in the Y direction.
*/
public static int mapMaxY;
/**
* map maximum in the X direction
*/
public static int mapMaxX;
/**
* NPC layer of the map. This is for characters who talk, battle etc.
*/
public static LayerViewer currentMapMainNPCs;
/**
* Main (background) layer of the map. This is for the actual gameworld
* graphics.
*/
public static LayerViewer currentMapMain;
/**
* World Metadata
*/
public static MapMeta world;
private static Renderer r;
private static Map map;
private InputStream mapStream;
/**
* Viewer object for the currently displayed map.
*/
public static MapViewer mapViewer;
/**
* PLAYER'S UP IMAGE
*/
public static BufferedImage playerUpImage;
/**
* PLAYER'S RIGHT IMAGE
*/
public static BufferedImage playerRightImage;
/**
* PLAYER'S DOWN IMAGE
*/
public static BufferedImage playerDownImage;
/**
* PLAYER'S LEFT IMAGE
*/
public static BufferedImage playerLeftImage;
/**
* PLAYER'S CURRENT IMAGE
*/
public static BufferedImage charImg;
/**
* An int that delays movement from one tile to another.
*/
public static int DelayAfterMove = 0;
/**
* Delay in frames after moving before allowed to move again.
*/
public static final int DELAYAFTERMOVEMAX = 4;
/**
* Array of speech and other event messages. Shoulld be 25 instances.
*/
public static String[] speechArray = new String[]{"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""};
/**
* Initialise the screen.
*/
public void initialise()
{
//set up the map
world = new MapMeta(); //create blank map object.
try
{
mapStream = new FileInputStream(FileManagement.ASSETS_FOLDER + MapMeta.filePath+".fmp");
} catch (FileNotFoundException fnfe)
{
System.err.println("SUPER ERROR: MAP CANNOT LOAD.");
fnfe.printStackTrace();
}
map = MapLoader.loadMap(mapStream);
r = new J2SE14Renderer(map);
mapViewer = new MapViewer(map, r, BaseVars.MAPHEIGHT, BaseVars.MAPWIDTH);
//NAME THE LAYERS
currentMapMain = mapViewer.getLayerViewers()[0];
currentMapMainNPCs = mapViewer.getLayerViewers()[1];
//SET INITIAL POSITIONS
System.out.println("X:"+MapMeta.xPos);
System.out.println("Y:"+MapMeta.yPos);
currentMapMain.setBlockX(MapMeta.xPos-BaseVars.MAPTILEOFFSETX);
currentMapMain.setBlockY(MapMeta.yPos-BaseVars.MAPTILEOFFSETY);
currentMapMainNPCs.setBlockX(MapMeta.xPos-BaseVars.MAPTILEOFFSETX);
currentMapMainNPCs.setBlockY(MapMeta.yPos-BaseVars.MAPTILEOFFSETY);
mapMaxX = currentMapMain.getLayer().getWidthInPixels() - BaseVars.WINDOWWIDTH;
mapMaxY = currentMapMain.getLayer().getHeightInPixels() - BaseVars.WINDOWHEIGHT;
//free up resources?
map = null;
r = null;
System.gc();
try
{
mapStream.close();
} catch (IOException ex)
{
System.err.println("UNABLE TO CLOSE MAP INPUT STREAM");
}
//load char images
playerUpImage = loadCharImage("up.gif");
playerRightImage = loadCharImage("right.gif");
playerDownImage = loadCharImage("down.gif");
playerLeftImage = loadCharImage("left.gif");
//initialise the character in the down position.
MapThread.charImg = MapThread.playerDownImage;
}
/**
* Renders the buffered <code>window</code> to the screen.
* @param window The graphics to draw on
* @return window The graphics with the new screen on.
*/
public Graphics2D renderToScreen(Graphics2D window)
{
if (window != null)
{
window = GameMap.draw(GameWindow.backBuffer);
}
return window;
}
/**
* Helper function for creating the images used in the program for the player char. Will be extended for normal image loading?
*/
private BufferedImage loadCharImage(String imgPath)
{
BufferedImage img = null;
try
{
InputStream inputStream = new FileInputStream(FileManagement.ASSETS_FOLDER + "/catchemrpg/assets/chars/" + imgPath);
try
{
img = ImageIO.read(inputStream);
} catch (IOException e)
{
e.printStackTrace();
System.err.println("FILE NOT READ");
}
} catch (FileNotFoundException fnfe)
{
System.err.println("FILE NOT READ");
fnfe.printStackTrace();
}
return img;
}
}