package box.domain;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import javax.swing.*;
@SuppressWarnings("serial")
public class Myframe extends JFrame implements ActionListener, MouseListener, KeyListener {
	private static final long serialVersionUID = 1L;// 定义程序序列化ID,版本兼容
	
	private int grade = 0;// 定义关卡数
	// row,column表示人物坐标；leftX,leftY记载左上角图片位置
	private int row = 7, column = 7, leftX = 0, leftY = 0;
	private int mapRow = 0, mapColumn = 0;// 地图的行列数
	private int width = 0, height = 0;// 屏幕大小
	private boolean acceptKey = true;
	private Image img[] = null;// 图片数组
	private byte[][] map = null;// 地图数组
	private ArrayList<Map> list = new ArrayList<Map>();// 动态数组，存放map数据，用于撤回操作
	Sound sound;
	final byte WALL = 1, BOX = 2, BOXONEND = 3, END = 4, MANDOWN = 5, MANLEFT = 6, MANRIGHT = 7, MANUP = 8, GRASS = 9,
			MANDOWNONEND = 10, MANLEFTONEND = 11, MANRIGHTONEND = 12, MANUPONEND = 13;

	// 构造方法
	public Myframe() { 
		
		setTitle("推箱子");
		// 1.窗体设置
		setSize(600, 600);// 窗体大小 
		setVisible(true);// 窗体可见 
		setResizable(true);//窗体可被用户改变 
		setLocation(680, 200);// 窗体位置 
		setBackground(Color.GRAY);// 窗体背景颜色
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 窗体结束程序
		//2.cont容器设置
		Container cont = getContentPane();
		cont.setLayout(null);//清空默认的流式布局管理器
		cont.setBackground(Color.white);//容器背景颜色
		getPic();// 获取图片
		width = this.getWidth();
		height = this.getHeight();// 获取屏幕宽、高
		this.setFocusable(true);// 设置组件可以被选中
		initMap();// 调用初始化地图功能 
		this.addKeyListener(this);// 按键监听
		this.addMouseListener(this);// 鼠标监听

	}

	// 初始化地图
	public void initMap() {
		map = getMap(grade);// 获取每个地图的关卡数
		list.clear();// 清除列表
		byte[][] temp = map;

		// 创建地图
		for (int i = 0; i < temp.length; i++) {
			for (int j = 0; j < temp[0].length; j++) {
				System.out.print(temp[i][j] + " ");
			}
			System.out.println();
		}
		getMapSizeAndPosition();
		getManPosition();
	}

	// 获取人物当前位置
	public void getManPosition() {

		for (int i = 0; i < map.length; i++) {
			for (int j = 0; j < map[0].length; j++) {
				if (map[i][j] == MANDOWN || map[i][j] == MANUP || map[i][j] == MANLEFT || map[i][j] == MANRIGHT) {
					row = i;
					column = j;
					break;
				}
			}
		}
	}

	// 获取游戏区域大小及显示游戏的左上角位置
	public void getMapSizeAndPosition() {
		mapRow = map.length;
		mapColumn = map[0].length;
		leftX = (width - map[0].length * 30) / 2;
		leftY = (height - map.length * 30) / 2;
		System.out.println(leftX);
		System.out.println(leftY);
		System.out.println(mapRow);
		System.out.println(mapColumn);
	}

	// 获取图片
	public void getPic() {
		img = new Image[14];
		for (int i = 0; i <= 13; i++) {
			img[i] = Toolkit.getDefaultToolkit().getImage("Utils/image/img" + i + ".png");
		}
	}

	// 人走过变草地
	public byte grassOrEnd(byte man) {
		byte result = GRASS;
		if (man == MANLEFTONEND || man == MANRIGHTONEND || man == MANUPONEND || man == MANDOWNONEND) {
			result = END;
		} 

		return result;
	}
	//人物前进
	private void moveUp() {
		if (map[row - 1][column] == WALL)
			return;
		byte tempBox;
		byte tempMan;

		if (map[row - 1][column] == BOX || map[row - 1][column] == BOXONEND) { // 如果向上一格是箱子
			if (map[row - 2][column] == GRASS || map[row - 2][column] == END) { // 如果向上第二格是过道或者终点
				Map currentMap = new Map(row, column, map);
				list.add(currentMap);// 用于撤回操作
				tempBox = map[row - 2][column] == END ? BOXONEND : BOX;
				tempMan = map[row - 1][column] == BOXONEND ? MANUPONEND : MANUP;
				map[row][column] = grassOrEnd(map[row][column]);
				map[row - 2][column] = tempBox;
				map[row - 1][column] = tempMan;
				row--;
			}
		} else {// 如果向上一格是过道或者终点
			Map currentMap = new Map(row, column, map);
			list.add(currentMap);// 用于撤回操作
			tempMan = map[row - 1][column] == GRASS ? MANUP : MANUPONEND;
			map[row][column] = grassOrEnd(map[row][column]);
			map[row - 1][column] = tempMan;
			row--;
		}
	}