/**
* Create a game scene in main
* Create a game scene
*/
Private Creategamescene () {
MtwGame.Instance.init (This.stage);
}
=========================== Scene Class ============================
Class Mtwgame {
Public Constructor () {
}
private static _instance:mtwgame;
public static Get Instance (): mtwgame {
if (this._instance = = NULL | | this._instance = = undefined) {
This._instance = new Mtwgame ();
}
return this._instance;
}
Public Stage:egret. Stage;
x↓; y→
Public maptitle:array<array<number>>;
Obstacles
Public mapobstacle:array<array<number>> = [
[1, 3], [2, 3], [3, 3], [4, 3], [5, 3], [6, 3],
[2, 5], [3, 5], [4, 5], [5, 5], [6, 5],
];
Public path:array<array<number>>;
Public width:number = 20;
Public strokewid:number = 3;
Public Color1:number = 0x00000;//line and blocked color
Public Color2:number = Color of 0xe60b0b;//path
Public color3:number = 0x00000;
Public Showtxt2:eui. Label;
Pathrect = [];//for storing path blocks for easy management
Map:mapclass;
Point = {};//is used to store block points for easy management
Initializing the Stage
Public Init (Stage:egret. Stage): void {
This.stage = stage;
This.initmap (8, 8);//The length and width of the input map (number of squares)
This.initmenu ();
This.initblock ();
}
/** Click Add or Remove blocking points */
Protected Addobstacle (Evt:egret. TouchEvent): void {
Let x = Math.floor (Math.floor (evt.localy)/(This.strokewid + this.width));
Let y = Math.floor (Math.floor (EVT.LOCALX)/(This.strokewid + this.width));
Let name = x + "_" + y;
if (This.point[name]) {
This.stage.removeChild (This.point[name]);
Delete This.point[name];
This.map.update (x, y, false);
} else {
let xx = This.strokewid * (y + 1) + this.width * y;
Let yy = This.strokewid * (x + 1) + this.width * x;
This.setrect (xx, yy, This.color1, 0.8, name);
This.map.update (x, y, True);
}
}
/** Initialization Menu */
Protected Initmenu (): void {
Let Shp:egret. Shape = New Egret. Shape ();
Shp.graphics.beginFill (0XFFFFFF, 1);
Shp.graphics.drawRect (0, 0, this.stage.stageWidth, this.stage.stageHeight);
Shp.graphics.endFill ();
This.stage.addChild (SHP);
Let Shp1:eui. Rect = new Eui. Rect ();
Shp1.fillalpha = 0.2;
Shp1.width = Shp1.height = This.stage.stageWidth;
This.stage.addChild (SHP1);
Shp1.addeventlistener (Egret. Touchevent.touch_end, this.addobstacle, this);
THIS.SHOWTXT2 = new Eui. Label ();
This.stage.addChild (THIS.SHOWTXT2);
This.showTxt2.text = "Regenerate path";
This.showTxt2.size = 32;
this.showTxt2.textAlign = "center";
this.showTxt2.fontFamily = "Simhei";
This.showTxt2.textColor = 0xf3c382;
This.showTxt2.strokeColor = 0x0;
This.showTxt2.stroke = 1;
This.showTxt2.width = Math.floor (THIS.STAGE.STAGEWIDTH/3 * 2);
this.showtxt2.x = this.stage.stagewidth-this.showtxt2.width >> 1;
THIS.SHOWTXT2.Y = 680;
This.showTxt2.addEventListener (Egret. Touchevent.touch_end, This.touchhandler, this);
}
/** re-seeking the road * *
Protected Touchhandler (Evt:egret. TouchEvent): void {
if (This.tt > 0)
Clearinterval (THIS.TT);
This.findpath ();
}
/** begins to find the way * *
Protected Findpath (): void {
Let returns = This.map.getPath ();//input start and end point (2,2), (5,5)
This.path = returns;
Show path
This.initpath ();
}
/** Render Map */
Protected InitBlock (): void {
Heng
for (Let i = 0; i < this.mapTitle.length + 1; i++) {
Let y = i * (this.width + this.strokewid);
This.setline (0, y, this.stage.stageWidth, y);
}
Vertical
for (Let i = 0; i < this.maptitle[0].length + 1; i++) {
Let x = i * (this.width + this.strokewid);
Let height = this.width * this.maptitle[0].length + this.strokewid * (this.maptitle[0].length + 1);
This.setline (x, 0, x, height);
}
Blocking point
for (Let i = 0; i < this.mapTitle.length; i++) {
for (Let j = 0; J < This.maptitle[i].length; J + +) {
if (this.maptitle[i][j] = = 1) {
This.setrect (This.strokewid * (j + 1) + This.width * J, This.strokewid * (i + 1) + This.width * I, This.color1, 0.8, i + " _ "+ j);
}
}
}
}
tt
/**
* Display the path on the map
*/
Protected Initpath (): void {
if (This.pathRect.length > 0) {
for (Let i = 0; i < this.pathRect.length; i++) {
This.stage.removeChild (This.pathrect[i]);
}
This.pathrect = [];
}
if (this.path && this.path.length > 0) {
Let i = 0;
This.tt = SetInterval (() = {
if (!this.path[i]) {
Clearinterval (THIS.TT);
Return
}
Let x = This.strokewid * (this.path[i][1] + 1) + this.width * this.path[i][1];
Let y = This.strokewid * (this.path[i][0] + 1) + this.width * This.path[i][0];
This.setrect (x, Y, This.color2, 0.3, This.path[i][0] + "_" + this.path[i][1], true);
i++;
}, 50);
}
}
/**
* Initialize Map
* */
Protected Initmap (W:number, h:number): void {
if (W < 2 | | H < 2) console.error ("Map lattice length and width number is incorrect");
This.maptitle = [];
for (Let i = 0; i < H; i++) {
This.mapTitle.push ([]);
for (Let j = 0; J < W; J + +) {
if (i = = 0 | | j = 0 | | i = = H-1 | | j = w-1) {
THIS.MAPTITLE[I][J] = 1;
} else {
THIS.MAPTITLE[I][J] = 0;
}
}
}
This.width = (This.stage.stageWidth-(this.maptitle[0].length + 1) * This.strokewid)/this.maptitle[0].length;
This.map = new Mapclass (this.maptitle);
This.initobstacle ();
For (Let I in This.mapobstacle) {
This.map.update (This.mapobstacle[i][0], this.mapobstacle[i][1], True)
}
}
/**
* Show obstacles on the map
* */
Protected initobstacle (): void {
for (Let i = 0; i < this.mapObstacle.length; i++) {
if (This.mapobstacle[i][0] < this.maptitle.length-1 && This.mapobstacle[i][0] > 0
&& This.mapobstacle[i][1] < this.maptitle[0].length-1 && This.mapobstacle[i][1] > 0)
THIS.MAPTITLE[THIS.MAPOBSTACLE[I][0]][THIS.MAPOBSTACLE[I][1]] = 1;
}
}
Crossed
Public Setline (X:number, Y:number, X1:number, Y1:number): Egret. Shape {
var shp:egret. Shape = New Egret. Shape ();
shp.touchenabled = false;
Shp.graphics.lineStyle (This.strokewid, This.color1);
Shp.graphics.moveTo (x, y);
Shp.graphics.lineTo (x1, y1);
Shp.graphics.endFill ();
This.stage.addChild (SHP);
return shp;
}
Dicing Block
Protected SetRect (X:number, Y:number, col:number, Alpha:number = 1, name:string, Bool:boolean = False): Egret. Shape {
var shp:egret. Shape = New Egret. Shape ();
shp.touchenabled = false;
Shp.graphics.beginFill (col, Alpha);
Shp.graphics.drawRect (x, Y, This.width, this.width);
Shp.graphics.endFill ();
This.stage.addChild (SHP);
if (bool) {
This.pathRect.push (shp);//record path
} else {
This.point[name] = shp;//record blocking point
}
return shp;
}
}
================================================-pathfinding Class (core code) ===============================================
Class Mapclass {
Public Closearr: {[pos:string]: number};//already used grid
Public Openarr: {[pos:string]: array<number>};//can walk the lattice
Public ARR: {[pos:string]: number};//all lattices
Public patharr:array<array<number>>;//currently can only connect 1-way singularities.
openobj;//records all used lattice information for finding the way after the end
Public constructor (maparr:array<array<number>>) {
This. ARR = {};
for (Let i = 0; i < maparr.length; i++) {
for (Let j = 0; J < Maparr.length; J + +) {
This. Arr[i + "_" + j] = Maparr[i][j];
}
}
}
count:number;//Iteration Count
Allcount:number;
/**
* Get the shortest path;
* X1,y1 is the starting point; X2,y2 is the end
*/
Public GetPath (): any {
This.closearr = {};
This.openarr = {};
This.count = 0;
This.patharr = [];
For (let key in). ARR) {
if (this. Arr[key] = = 0)
This.openarr[key] = [Number (Key.split ("_") [0]), Number (Key.split ("_") [1])]
}
This.allcount = Object.keys (this.openarr). length;
if (This.returnonestrockes ())
return This.patharr;
else{
Console.log ("Cannot finish one stroke");
}
}
Public jiarr:array<array<number>>;//currently can only connect 1-way singularities.
Public ouarr:array<array<number>>;//record even points
/**
* Judging the singularity inside the This.openarr
* Return to the end of a step
*/
Public Returnonestrockes (): boolean {
if (Object.keys (this.openarr). length = = 1) {
For (let key in This.openarr) {
This.pathArr.push ([this.openarr[key][0],this.openarr[key][1]]);
}
return true;
}
this.count++;
if (This.count > This.allcount)//Every lattice goes through, it means no strokes
return False
This.jiarr = [];
This.ouarr = [];
For (let key in This.openarr) {
Let four = This.getfour (This.openarr[key][0], this.openarr[key][1]);
if (four.length% 2 = = 1) {//Record singularity
if (four.length = = 1) {
This.jiArr.push ([this.openarr[key][0], this.openarr[key][1]])
}
}
if (four.length% 2 = = 0) {//record even point
This.ouArr.push ([this.openarr[key][0], this.openarr[key][1]])
}
if (Four.length = = 0 && object.keys (this.openarr). length > 1)//1. When there are multiple points, there is no road point
return false;
if (This.jiArr.length > 2)//2. The singularity of the road has a maximum of two
return False
}
if (this.jiArr.length% 2 = 0 && this.jiArr.length! = 1)//3. Even when odd points are odd, there must be a singular point
return false;
if (Object.keys (this.openarr). length > 0 && this.jiArr.length > 0) {//Presence singularity
Let xy:array<number> = [this.jiarr[0][0], this.jiarr[0][1]];
if (this.pathArr.length! = 0) {//start pathfinding with first singularity as starting point
Let lastp:array<number> = [this.patharr[this.patharr.length-1][0], this.patharr[this.patharr.length-1][1]];
Let four = This.getfour (Lastp[0], lastp[1]);
if (four.length = = 1) {
xy = four[0];
} else {
Let ouxy:array<number> = [];
Let jistr:array<string> = This.tostringarr (This.jiarr);
Let oustr:array<string> = This.tostringarr (This.ouarr);
For (Let I, four) {
Let fours:string = Four[i].join ("#");
if (Jistr.indexof (Fours) >-1) {
XY = [four[i][0], four[i][1]];
}
if (Oustr.indexof (Fours) >-1 && Ouxy = = []) {
Ouxy = [four[i][0], four[i][1]];
}
}
if (!xy) xy = Ouxy;
}
}
This.pathArr.push ([xy[0], xy[1]]);
Delete This.openarr[xy[0] + "_" + xy[1];
This.closearr[xy[0] + "_" + xy[1]] = 1;
return This.returnonestrockes ();
} else {//all for even points
Let xy:array<number> = [this.ouarr[0][0], this.ouarr[0][1]];
if (this.pathArr.length! = 0) {//Start pathfinding, start with first even point
Let lastp:array<number> = [this.patharr[this.patharr.length-1][0], this.patharr[this.patharr.length-1][1]];
XY = This.getfour (lastp[0], lastp[1]) [0];
}
This.pathArr.push ([xy[0], xy[1]]);
Delete This.openarr[xy[0] + "_" + xy[1];
This.closearr[xy[0] + "_" + xy[1]] = 1;
return This.returnonestrockes ();
}
}
Public Tostringarr (arr:array<array<number>>): array<string>{
Let strarr:array<string> = [];
For (Let I in arr) {
Strarr.push (Arr[i].join ("#"))
}
return strarr;
}
Get four-direction lattice [x, Y,]
Public Getfour (X1:number, Y1:number): array<array<number>> {
Let sige:array<array<number>> = [];
Let str1:string = x1 + 1 + "_" + y1;
Let str2:string = X1-1 + "_" + y1;
Let str3:string = x1 + "_" + (y1 + 1);
Let str4:string = x1 + "_" + (y1-1);
if (this. ARR[STR1] = = 0 &&!this.closearr[str1])
Sige.push ([x1 + 1, y1]);
if (this. ARR[STR2] = = 0 &&!this.closearr[str2])
Sige.push ([X1-1, Y1]);
if (this. ARR[STR3] = = 0 &&!THIS.CLOSEARR[STR3])
Sige.push ([x1, y1 + 1]);
if (this. ARR[STR4] = = 0 &&!THIS.CLOSEARR[STR4])
Sige.push ([x1, y1-1]);
return siGe;
}
/** Update the block of the map */
Public Update (X1:number, Y1:number, Bool:boolean): void {
This. Arr[x1 + "_" + y1] = bool? 1:0;
}
Public Dispose (): void {
This.closearr = null;
This.openarr = null;
This. ARR = null;
This.openobj = null;
}
}
Egret Simple one-stroke algorithm, in wing can run directly (later play this kind of game has a cheat device)