| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- // Learn cc.Class:
- // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
- // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
- // Learn Attribute:
- // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
- // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
- // Learn life-cycle callbacks:
- // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
- // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
- cc.Class({
- extends: cc.Component,
- properties: {
- allowDiagonals: {
- default: false,
- serializable: false,
- },
- diagonalCost: {
- default: false,
- serializable: false,
- },
- dotTiebreaker: {
- default: false,
- serializable: false,
- },
- badSorting: {
- default: true,
- serializable: false,
- },
- },
- setAllowDiagonals(event) {
- // cc.log('event',event);
- this.allowDiagonals = event.isChecked;
- this.draw();
- },
- setDiagonalCost(event) {
- // cc.log('event',event);
- this.diagonalCost = event.isChecked;
- this.draw();
- },
- setDotTiebreaker(event) {
- // cc.log('event',event);
- this.dotTiebreaker = event.isChecked;
- this.draw();
- },
- setBadSorting(event) {
- // cc.log('event',event);
- this.badSorting = event.isChecked;
- this.draw();
- },
- start() {
- // AStar.Init();
- this.draw();
- },
- clearMap() {
- let map = AStar.getMap();
- for (var i = 0; i < 32; i++) {
- for (var j = 0; j < 32; j++) {
- map[i][j].solid = false;
- }
- }
- this.draw();
- },
- randomMap() {
- let map = AStar.getMap();
- for (var i = 0; i < 32; i++) {
- for (var j = 0; j < 32; j++) {
- map[i][j].solid = Math.random() > 0.8;
- }
- }
- this.draw();
- },
- draw() {
- // console.log('this.allowDiagonals=', this.allowDiagonals,
- // 'this.diagonalCost=', this.diagonalCost,
- // 'this.dotTiebreaker=', this.dotTiebreaker,
- // 'this.badSorting=', this.badSorting)
- AStar.setInitInfo(this.allowDiagonals, this.diagonalCost, this.dotTiebreaker, this.badSorting);
- var ctx = this.getComponent(cc.Graphics);
- let map = AStar.getMap();
- var start = { x: 2, y: 2 };
- AStar.setStart(start);
- var end = { x: 31, y: 31 };
- ctx.fillColor = new cc.Color().fromHEX('#EEEEEE');
- ctx.strokeColor = new cc.Color().fromHEX('#888888');
- ctx.rect(0, 0, 500, 500);
- ctx.stroke();
- ctx.fill();
- //设置AStar类里面的graphics
- AStar.setGraphics(ctx);
- var r = AStar.pathFind(start.x, start.y, end.x, end.y);
- // cc.log('得到的路径 = ', r);
- ctx.strokeColor = new cc.Color().fromHEX('#888888');
- ctx.rect(0.5, 0.5, 500, 500);
- ctx.stroke();
- ctx.fillColor = new cc.Color().fromHEX('#333333');
- ctx.strokeColor = new cc.Color().fromHEX('#333333');
- for (let i = 0; i < 32; i++) {
- for (let j = 0; j < 32; j++) {
- if (map[i][j].solid) {
- ctx.rect(i * 10, j * 10, 10, 10);
- ctx.stroke();
- ctx.fill();
- }
- else {
- ctx.rect(i * 10 - 0.5, j * 10 - 0.5, 10, 10);
- ctx.stroke();
- }
- }
- }
- // return;
- ctx.fillColor = new cc.Color().fromHEX('#FF7777');
- ctx.rect(start.x * 10, start.y * 10, 10, 10);
- ctx.stroke();
- ctx.fill();
- ctx.fillColor = new cc.Color().fromHEX('#7777FF');
- ctx.rect(end.x * 10, end.y * 10, 10, 10);
- ctx.stroke();
- ctx.fill();
- ctx.strokeColor = new cc.Color().fromHEX('#FF0000');
- for (let i = 0; i < r.length; i++) {
- if (i == 0) ctx.moveTo(r[i].x * 10 + 5.5, r[i].y * 10 + 5.5);
- else ctx.lineTo(r[i].x * 10 + 5.5, r[i].y * 10 + 5.5);
- }
- ctx.stroke();
- }
- });
|