package be.nascom.flash.util{ import flash.display.Shape; import flash.events.Event; import flash.events.EventDispatcher; import flash.geom.Point; import flash.geom.Rectangle; public class graphic_player_10_cube_be_nascom_flash_util_UnitGrid extends EventDispatcher{ protected var _active_cells:Array; protected var _grid_rectangle:Rectangle; protected var _rows:uint=0; public function get rows():uint{return _rows;} protected var _cols:uint=0; public function get cols():uint{return _cols;} protected var _unit_size:uint=0; public function get unit_size():uint{return _unit_size;} public function get width():uint{return _unit_size*_cols;} public function get height():uint{return _unit_size*_rows;} protected var _test_shape:Shape; public function get total_cells():uint{ return _cols*_rows; } public function graphic_player_10_cube_be_nascom_flash_util_UnitGrid(){} public function init(rows:uint,cols:uint,unit_size:uint,test_shape:Shape=null):void{ _test_shape=test_shape; _rows=rows; _cols=cols; _unit_size=unit_size; trace("graphic_player_10_cube_be_nascom_flash_util_UnitGrid.init() rows:"+_rows+",cols:"+_cols+",unit_size:"+_unit_size); var tot:uint=total_cells; _active_cells=new Array(); for(var i:uint=0;i=0 || index-1) public function getCellIndexToLeft(cell_index:uint):int{ checkCellIndexBounds(cell_index); if(!hasCellOnLeftSide(cell_index))return -1; return cell_index-1; } public function cellToLeftIsActive(cell_index:uint):Boolean{ if(!hasCellOnLeftSide(cell_index))return false; return Boolean(_active_cells[getCellIndexToLeft(cell_index)]); } //RIGHT SIDE public function hasCellOnRightSide(cell_index:uint):Boolean{ return getColumnIndex(cell_index)<_cols-1 ? true : false; } //use this like you use String.indexOf(), ex) if(getCellToRight(x)>-1) public function getCellIndexToRight(cell_index:uint):int{ checkCellIndexBounds(cell_index); if(!hasCellOnRightSide(cell_index))return -1; return cell_index+1; } public function cellToRightIsActive(cell_index:uint):Boolean{ if(!hasCellOnRightSide(cell_index))return false; return Boolean(_active_cells[getCellIndexToRight(cell_index)]); } //ABOVE public function hasCellAbove(cell_index:uint):Boolean{ return getRowIndex(cell_index) ? true : false; } public function getCellIndexAbove(cell_index:uint):int{ checkCellIndexBounds(cell_index); if(!hasCellAbove(cell_index))return -1; return cell_index-_cols; } public function cellAboveIsActive(cell_index:uint):Boolean{ if(!hasCellAbove(cell_index))return false; return Boolean(_active_cells[getCellIndexAbove(cell_index)]); } //BELOW public function hasCellBelow(cell_index:uint):Boolean{ return getRowIndex(cell_index)<_rows-1 ? true : false; } public function getCellIndexBelow(cell_index:uint):int{ checkCellIndexBounds(cell_index); if(!hasCellBelow(cell_index))return -1; return cell_index+_cols; } /* public function getRectRight(from_rect:Rectangle,minimum_rect:Rectangle,max_rect:Rectangle):Rectangle{ //search for a rect of minimum size, if none found: return null; //else return found Rectangle of maximum size var rect:Rectangle=new Rectangle(); disableCells(rect); return rect; } public function getRectUnder(from_rect:Rectangle,minimum_rect:Rectangle,max_rect:Rectangle):Rectangle{ return null; } public function getRectLeft(from_rect:Rectangle,minimum_rect:Rectangle,max_rect:Rectangle):Rectangle{ return null; } public function getSpaceOnTop(from_rect:Rectangle,minimum_rect:Rectangle,max_rect:Rectangle):Rectangle{ return null; } */ public function getCellIndexContainingPoint(p:Point):int{ if(!_grid_rectangle.containsPoint(p))return -1; var _x:uint=Math.floor(p.x/unit_size); var _y:uint=Math.floor(p.y/unit_size); var index:uint=_y*_cols+_x; return index; } //RIGHT protected function findSeedToTheRight(rect:Rectangle):int{ var seed:int=getCellIndexContainingPoint(new Point(rect.right,rect.top)); trace("graphic_player_10_cube_be_nascom_flash_util_UnitGrid.findSeedToTheRight() seed:"+seed); if(seed==-1)return -1;//failsafe var current_index:uint=this.getCellIndexToRight(seed); if(seed==-1)return -1; if(indexIsActive(current_index))return current_index; //set only after conditions are met var max_iterations:uint=Math.ceil(rect.height/_unit_size); var cur_iteration:uint=0; while(hasCellBelow(current_index) && cur_iterationmax_rect.width)break;//this should be redundant (see main loop) check_cell=down_check_cell=this.getCellIndexToRight(down_check_cell); if(down_check_cell==-1)break; //check new vertical line going down for(j=0;jmax_rect.width)break;//this should be redundant (see main loop) check_cell=down_check_cell=this.getCellIndexToLeft(down_check_cell); if(down_check_cell==-1)break; //check new vertical line going down for(j=0;jmax_rect.width)break;//this should be redundant (see main loop) if(up_check_cell==-1)break; check_cell=up_check_cell; //check new vertical line going up for(j=0;j