﻿using System;
using System.Collections.Generic;

using UnityEngine;
namespace Rayman{
//用于产生平滑地形头像出来的类, 因为用的是开辟战争迷雾的算法.
//所以起的名字如下
public class WarFrogHelper{
	private MapGrid _frogGrid = new MapGrid();

	public WarFrogHelper(int cols, int rows){
		_frogGrid.Init(cols, rows);
		ResetToBlack();
	}

	public MapGrid frogGrid{
		get{ return _frogGrid; }
	}

	//拷贝死亡数据
	public void CopyDeadFrom(MapGrid source){
		_frogGrid.MakeAllWalkable(true);
		_frogGrid.CopyUnWalkableFrom(source);
	}

	public void Dispose(){
		_frogGrid = null;
	}

	//活着的就是要开视野的地方
	public MapGrid OpenAliveGrid(){
		ResetToBlack();

		MapNode node;
		for(int r = 0; r < _frogGrid.numRows; ++r){
			for(int c = 0; c < _frogGrid.numCols; ++c){
				node = _frogGrid.GetNode(r, c);
				if(node.walkable)OpenPoint(c, r);
			}
		}

		return _frogGrid;
	}

	/*视野是正方形*/
	public void Open(int centerRow, int centerCol, int range){
		int boundLeft = 0;
		int boundRight = _frogGrid.numCols - 1;
		int boundUp = _frogGrid.numRows - 1;
		int boundDown = 0;

		int left = centerCol - range;
		if(left < boundLeft)left = boundLeft;

		int right = centerCol + range - 1;
		if(right > boundRight)right = boundRight;

		int up = centerRow + range;
		if(up > boundUp)up = boundUp;

		int down = centerRow - range + 1;
		if(down < boundDown)down = boundDown;

		for(int row = down; row <= up; ++row){
			for(int col = left; col <= right; ++col){
				OpenPoint(col, row);
			}
		}
	}

	public void OpenCircle(int centerRow, int centerCol, int range){
		int boundLeft = 0;
		int boundRight = _frogGrid.numCols - 1;
		int boundUp = _frogGrid.numRows - 1;
		int boundDown = 0;

		int left = centerCol - range;
		if(left < boundLeft)left = boundLeft;

		int right = centerCol + range - 1;
		if(right > boundRight)right = boundRight;

		int up = centerRow + range;
		if(up > boundUp)up = boundUp;

		int down = centerRow - range + 1;
		if(down < boundDown)down = boundDown;

		int line = range * range;
		for(int row = down; row <= up; ++row){
			for(int col = left; col <= right; ++col){
				int dr = row - centerRow;
				dr = dr * dr;

				int dc = col - centerCol;
				dc = dc * dc;

				int dd = dr + dc;

				if(dd > line)continue;

				OpenPoint(col, row);
			}
		}
	}

	public void OpenPoint(int col, int row){
		//Debug.Log(string.Format("{0} - {1}", row, col) );
		// 当前区块的右下
		SetPointCornerValue(col, row, 3, 4);
		// 右边区块的左下
		SetPointCornerValue(col + 1, row, 1, 8);
		// 下面区块的右上
		SetPointCornerValue(col, row - 1, 2, 1);
		// 下面右边区块的左上
		SetPointCornerValue(col + 1, row - 1, 0, 2);
	}

	private void SetPointCornerValue(int col, int row, int corner, int value){
		MapNode node = _frogGrid.GetNode(row, col);
		if(node == null)return;

		int fv = node.GetFrogCornerValue(corner);
		if(fv == value)return;

		node.SetFrogCornerValue(corner, fv + value);
	}

	//所有的雾气全部置为0, 也就是黑色迷雾状态
	private void ResetToBlack(){
		MapNode node;
		int[] defaultFrogValue = new int[4]{ 0, 0, 0, 0 };

		for(int r = 0; r < _frogGrid.numRows; ++r){
			for(int c = 0; c < _frogGrid.numCols; ++c){
				node = _frogGrid.GetNode(r, c);

				node.warFrogTag = new List<int>(defaultFrogValue);
			}
		}
	}
}
}