package core;
import icon.BaseIcon;
import icon.HouseIcon;
import icon.ScvIcon;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.DirectColorModel;
import java.awt.image.MemoryImageSource;
import java.awt.image.PixelGrabber;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.ImageIcon;
import tile.Barrack;
import tile.Headquarter;
import tile.House;
import tile.Marine;
import tile.Mine;
import tile.Scv;
import tile.Sprite;
import tile.Supply;
import tile.Tank;
import tile.Tile;
import tile.Sprite.Animation;
public class ResourceManager {
private GridMap gridMap;
public static ResourceManager resourceManager;
private static Map<String,Image> IMAGE_CACHE = new HashMap<String, Image>();
public static Image loadImage(String fileName) {
if(!IMAGE_CACHE.containsKey(fileName)){
IMAGE_CACHE.put(fileName, new ImageIcon(ResourceManager.class.getResource("/images/"+fileName)).getImage());
}
return IMAGE_CACHE.get(fileName);
}
public static GridMapRender load(int type,List<Integer> types){
resourceManager = new ResourceManager(type,types);
return resourceManager.gridMap.getTileMapRender();
}
public GridMap getGridMap() {
return gridMap;
}
/**
* 鍒濆鍖栫缉鐣ュ浘
* @return
*/
private Image initMapBg(){
BufferedImage buffer = new BufferedImage(128,128,BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = (Graphics2D) buffer.getGraphics();
for (int y = 0; y < gridMap.getHeight(); ++y) {
for (int x = 0; x < gridMap.getWidth(); ++x) {
AffineTransform affineTransform = new AffineTransform();
affineTransform.setToTranslation(x*10, y*5);
affineTransform.scale(0.2, 0.2);
g2.drawImage(Constant.IMAGE_BG, affineTransform, null);
}
}
return buffer;
}
private ResourceManager(int type,List<Integer> types) {
Constant.load();
gridMap = loadMap(type,types);
Constant.IMAGE_MAP_BG = initMapBg();
Constant.progress = Constant.TOTAL;
}
private GridMap loadMap(int type,List<Integer> types) {
List<String> list = new ArrayList<String>();
int width = readMap(list, "startmap1.map");
GridMap map = new GridMap(width, list.size());
GridMapRender mapRender = new GridMapRender(map);
mapRender.type = type;
map.setTileMapRender(mapRender);
map.setIconMap(Constant.ICON_MAP);
for (int y = 0; y < list.size(); ++y) {
// 璇诲彇姣忎竴琛�
String s = list.get(y);
for (int x = 0; x < s.length(); ++x) {
int code = (int)s.charAt(x)-40;
if(code<0||code>Constant.TILE_TABLE.length)
continue;
if(code<80&&!types.contains((code/20)))
continue;
Tile tile = Constant.TILE_TABLE[code].clone(x, y,map);
tile.setHealth(1.0f);
map.add(tile);
//纭畾褰撳墠瑙嗚鑼冨洿
if(tile.getType()==type&&tile instanceof Headquarter){
//鑾峰彇褰撳墠鍩哄湴鍧愭爣
int hqX = GridMapRender.tileXToPx(tile.getTileX());
int hqY = GridMapRender.tileYToPx(tile.getTileY());
Point size = tile.getSize();
//鑾峰彇灞忓箷涓績鍧愭爣
int centerX = (800-GridMapRender.tileXToPx(size.x))/2;
int centerY = (600-GridMapRender.tileYToPx(size.y))/2;
mapRender.setOffset(Math.max(hqX-centerX, 0),Math.max(hqY-centerY, 0));
}
}
}
return map;
}
private static int readMap(List list, String fileName) {
BufferedReader br = null;
int width = 0;
try {
br = new BufferedReader(new InputStreamReader(ResourceManager.class.getResourceAsStream("/map/" + fileName)));
String line = null;
while ((line = br.readLine()) != null) {
if (line.startsWith("#"))
continue;
list.add(line);
width = Math.max(width, line.length());
}
return width;
} catch (Exception e) {
e.printStackTrace();
return -1;
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static class Constant {
public static String IP;
public static Color GREEN;
public static Color RED=Color.red;
//鑳屾櫙
public static Image IMAGE_BG;
//鎺у埗鍙�
public static Image IMAGE_CONTROL;
//鍏夌収鏁堟灉
public static Image SCV_SPARK;
//鐭夸骇
public static Image ICON_MINE;
//浜哄彛
public static Image ICON_MAN;
//鐭夸骇閿欒淇℃伅
public static String MINE_ERROR="娌¤祫婧愬挴锛岃繕鏄鍘婚噰鐐圭熆鍚�";
//浜哄彛閿欒淇℃伅
public static String MAN_ERROR="澶尋鍟︼紝蹇�鐐规埧瀛愬嚭鏉ュ惂";
public static String BUILD_ERROR="鍦板熀涓嶇ǔ锛屼笉鑳戒慨鍦ㄨ繖閲�";
public static Image IMAGE_MAP_BG;
//ICONS
public static final BaseIcon[][] SCV_ICONS = new BaseIcon[2][3];
public static final BaseIcon[][] HQ_ICONS = new BaseIcon[2][3];
public static final BaseIcon[][] BACK_ICONS = new BaseIcon[2][3];
//ICON璧勬簮琛�
public static final Map<Integer,BaseIcon[][]> ICON_TABLE = new HashMap<Integer, BaseIcon[][]>();
//Tile璧勬簮琛�
public static final Tile[] TILE_TABLE = new Tile[82];
private static final int TYPE = 4;
public static final int TYPE_SIZE = 20;
//杩涘害鎺у埗
private static float progress;
private static final float TOTAL=19;
static void load(){
IMAGE_BG = loadImage("bg1.gif");
IMAGE_CONTROL = loadImage("panel/main.gif");
SCV_SPARK = loadImage("unit/0_scv_spark.gif");
ICON_MINE = loadImage("panel/mine.gif");
ICON_MAN = loadImage("panel/man.gif");
GREEN = new Color(16, 252, 24);
initTile();
}
private static final Map<String, BaseIcon> ICON_MAP = new HashMap<String, BaseIcon>();
private static int count = 0;
private static BaseIcon createIcon(BaseIcon icon){
ICON_MAP.put("icon"+(++count), icon);
return icon;
}
private static void initTile(){
//鍔犺浇鍥剧墖
Image scv = loadImage("unit/0_scv_red.gif");
Image marine = loadImage("unit/0_marine_red.gif");
Image marineFight = loadImage("unit/0_fight_marine_red.png");
Image tank = loadImage("unit/0_tank.gif");
Image supply = loadImage("build/0_supply_red.gif");
Image barrack = loadImage("build/0_barrack_red.gif");
Image base = loadImage("build/0_hq_red.gif");
Image mine = loadImage("block/mine.gif");
//杞崲鎴恇uffer
BufferedImage scv_buffer=imageToBuffer(scv);
BufferedImage marine_buffer=imageToBuffer(marine);
BufferedImage marineFight_buffer=imageToBuffer(marineFight);
BufferedImage supply_buffer=imageToBuffer(supply);
BufferedImage barrack_buffer=imageToBuffer(barrack);
BufferedImage base_buffer=imageToBuffer(base);
BufferedImage tank_buffer=imageToBuffer(tank);
//鍒濆鍖栫熆浜ц祫婧�
TILE_TABLE[80] = new Mine(mine,80);
//ICON
HQ_ICONS[0][0] = createIcon(new HouseIcon(loadImage("ico/0_scv.gif")));
SCV_ICONS[0][0] = createIcon(new ScvIcon(loadImage("ico/0_supply.gif")));
SCV_ICONS[0][1] = createIcon(new ScvIcon(loadImage("ico/0_barrack.gif")));
BACK_ICONS[0][0] = createIcon(new HouseIcon(loadImage("ico/0_marine.gif")));
//BACK_ICONS[0][1] = new HouseIcon(loadImage("ico/0_tanke.gif"));
//鍒濆鍖栨瘮杈冨鏉傜殑Tile
for(int i=0;i<TYPE;++i){
//鍐滄皯