lianliankan
...
@author lizhi http://game-develop.net/
♥0 |
Line 150 |
Modified 2013-04-27 14:10:43 |
MIT License
archived:2017-03-20 15:33:35
ActionScript3 source code
/**
* Copyright lizhi ( http://wonderfl.net/user/lizhi )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/txyi
*/
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.xml.XMLNode;
/**
* ...
* @author lizhi http://game-develop.net/
*/
public class LianLianKan extends Sprite
{
//-1代表无效 0代表没有 >0代表有
private var data:Vector.<int> = new Vector.<int>();
private var w:int = 10;
private var h:int = 10;
private var cw:int = 20;
private var ch:int = 20;
private var colors:Vector.<int> = new Vector.<int>;
private var selected:int = -1;
private var dirs:Vector.<int> = Vector.<int>([1, -1, w, -w]);
private var pathNode:Node;
public function LianLianKan()
{
data.length = w * h;
for (var i:int = 0; i < data.length;i++ ) {
data[i] = int(Math.random()*3) + 1;
}
colors[0] = 0xffffff;
colors[1] = 0xff;
colors[2] = 0xff00;
colors[3] = 0xff0000;
addEventListener(Event.ENTER_FRAME, enterFrame);
stage.addEventListener(MouseEvent.CLICK, stage_click);
}
private function stage_click(e:MouseEvent):void
{
pathNode = null;
var x:int = mouseX / cw;
var y:int = mouseY / ch;
if (x < 0 || y < 0 || x >= w || y >= h) {
}else {
var i:int = x + y * w;
var value2:int = getValue(i);
if (selected > -1) {
var value:int = data[selected];
if (value2 == value) {
var node:Node = new Node;
node.index = selected;
find(node);
var flag:Boolean = false;
var nodes:Vector.<Node> = new Vector.<Node>;
nodes.push(node);
var finded:Array = new Array;
while (nodes.length>0) {
var fnode:Node = nodes.pop();
if (fnode.index == i&&fnode.index!=selected) {
if(fnode.curve<3){
flag = true;
data[selected] = 0;
data[i] = 0;
finded.push(fnode);
}
}
for each(var cnode:Node in fnode.children) {
nodes.push(cnode);
}
}
if (flag) {
finded.sortOn("depth", Array.NUMERIC);
pathNode = finded[0];
selected = -1;
}else {
selected = i;
}
}else {
selected = x + w * y;
}
}else if (value2>0) {
selected = x + w * y;
}
}
}
private function find(node:Node):void {
for each(var dir:int in dirs) {
var to:int = node.index + dir;
var v:int = getValue(to);
if(node.parent==null||node.parent.index!=to){
if (v>-1) {
var c:Node = new Node;
c.parent = node;
c.curve = c.parent.curve;
c.depth = c.parent.depth + 1;
c.index = to;
if (c.parent.parent&&((c.parent.parent.index-c.parent.index)!=(c.parent.index-c.index))) {
c.curve++;
}
node.children.push(c);
}
if (v == 0 && c.curve < 3) {
find(c);
}
}
}
}
private function enterFrame(e:Event):void
{
graphics.clear();
for (var i:int = 0; i < w * h; i++) {
var x:int = i % w;
var y:int = i / w;
var v:int = getValue(i);
var color:int = colors[v];
graphics.beginFill(color);
graphics.drawRect(x * cw, y * ch, cw, ch);
graphics.endFill();
}
if (selected>-1) {
x = selected % w;
y = selected / w;
graphics.lineStyle(2, 0xffff00);
graphics.drawRect(x * cw, y * ch, cw, ch);
}
if (pathNode) {
graphics.lineStyle(2, 0xffff);
x = pathNode.index % w;
y = pathNode.index / w;
graphics.moveTo((x + .5) * cw, (y + .5) * ch);
var pNode:Node = pathNode.parent;
while (pNode) {
x = pNode.index % w;
y = pNode.index / w;
graphics.lineTo((x + .5) * cw, (y + .5) * ch);
pNode = pNode.parent;
}
}
}
private function getValue(index:int):int {
var x:int = index % w;
var y:int = index / w;
if (x<0||x>=w||y<0||y>=h) {
return -1;
}
return data[index];
}
}
}
class Node {
public var parent:Node;
public var index:int;
public var curve:int=0;
public var children:Vector.<Node> = new Vector.<Node>;
public var depth:int = 0;
}