Untitled
unknown
plain_text
4 years ago
13 kB
8
Indexable
import Board from "./board";
import gameplayController from "./gameplayController";
import lineData from "./lineData";
const { ccclass, property } = cc._decorator;
@ccclass
export default class boardController extends cc.Component {
@property(cc.Node) nBoard: cc.Node = null;
@property(cc.Prefab) prfBox: cc.Prefab = null;
@property(cc.Prefab) prfLine: cc.Prefab = null;
static inst: boardController = null;
graphics: cc.Graphics = null;
_configMatrix: number[][] = [];
//listBoard: Board[][] = [];
posList: cc.Vec3[] = [];
//cantFill: boolean = false;
_maxCol: number = 9;
_maxRow: number = 9;
touchColor: number = null;
listLines = [];
gridLines = [];
isColor: boolean = false;
//count: number = 0;
onLoad() {
boardController.inst = this;
}
init(_listMatrix: number[][]) {
this._configMatrix = _listMatrix;
console.log(this._configMatrix);
this._maxRow = this._configMatrix.length;
this._maxCol = this._configMatrix[0].length;
this.loadBoard();
//console.log(this.posList);
}
getBoardSize() {
}
loadBoard() {
this.nBoard.removeAllChildren();
this.posList = [];
for (let i = 0; i < this._maxRow; i++) {
for (let j = 0; j < this._maxCol; j++) {
var box = cc.instantiate(this.prfBox);
box.width = this.nBoard.width / this._maxCol;
box.height = this.nBoard.height / this._maxRow;
let compScript: Board = null;
compScript = box.getComponent(Board);
compScript.init(i, j, this._configMatrix[i][j]);
compScript.setBoxSize(box.width, box.height)
box.x = box.width * (j + 1) - this.nBoard.width / 2 - box.width / 2;
box.y = -box.height * (i + 1) + this.nBoard.height / 2 + box.height / 2;
//this.fillColor(this._configMatrix[i][j], box);
if (this._configMatrix[i][j] == 0) {
box.getChildByName(`box0`).getChildByName('New Label').getComponent(cc.Label).string = this._configMatrix[i][j].toString();
//this.isColor = false;
} else {
box.getChildByName(`box0`).getChildByName('New Label').getComponent(cc.Label).string = this._configMatrix[i][j].toString() + 'g';
//this.isColor = true;
}
// ${this._configMatrix[i][j]}
//console.log(box.getChildByName(`box${this._configMatrix[i][j]}`).getChildByName('New Label').getComponent(cc.Label));
box.name = 'box' + i + j;
this.posList.push(box.position);
this.handleEventTouch(box);
this.nBoard.addChild(box);
}
}
//console.log(this.posList);
//console.log(box);
//console.log('á;dkjhas;dlasd',this.posList);
}
fillColor(colorBox: number, box: any) {
for (let index = 0; index < 11; index++) {
// if (box.getChildByName(`box${index}`).active != box.getChildByName(`box0`).active) {
// }
box.getChildByName(`box${index}`).active = false;
}
box.getChildByName(`box${colorBox}`).active = true;
//box.getChildByName(`box${colorBox}`).getComponent(cc.Label).string = `${colorBox}`;
//this.cantFill = true;
}
fillNumber(colorBox: number, box: any) {
box.getChildByName('box0').getChildByName('New Label').getComponent(cc.Label).string = colorBox;
//console.log('color box ', colorBox);
}
handleEventTouch(target) {
target.on(cc.Node.EventType.TOUCH_START, this.handleTouchStart, this);
target.on(cc.Node.EventType.TOUCH_MOVE, this.handleTouchMove, this);
target.on(cc.Node.EventType.TOUCH_END, this.handleTouchEnd, this);
//console.log(this.posList);
}
handleTouchStart(event) {
const target = event.target;
let endTarget;
// for (let colorBox = 1; colorBox < 11; colorBox++) {
// if (target.getChildByName(`box${colorBox}`).active == true) {
// this.touchColor = colorBox;
// this.scaleUp(target);
// }
// }
let color = target.getChildByName(`box0`).getChildByName('New Label').getComponent(cc.Label).string.slice(0, 1);
//console.log('color ', this.isColor);
//const lineDraw = new lineData;
if (color != '0') {
this.touchColor = color;
// lineDraw.lines.push(target);
// lineDraw.color = this.touchColor;
// //this.scaleUp(target);
// this.listLines.push(lineDraw);
// console.log(this.listLines);
console.log(this.touchColor);
}
for (let i = 0; i < this._maxRow; i++) {
for (let j = 0; j < this._maxCol; j++) {
if (this._configMatrix[i][j] == this.touchColor) {
if (this.nBoard.getChildByName(`box${i}${j}`) == target) {
//console.log('trung');
//return;
} else {
endTarget = this.nBoard.getChildByName(`box${i}${j}`);
//console.log('ko trung');
}
}
}
}
// for (let colorBox = 1; colorBox < 11; colorBox++) {
// this.scaleUp(endTarget)
// }
console.log(target);
}
handleTouchMove(event) {
const target = event.target;
//console.log(this.posList);
let worldPos = event.getLocation();
let localPos = this.node.parent.convertToNodeSpaceAR(worldPos);
let detectRange = 0.5 * this.nBoard.width / this._maxCol;
//console.log(detectRange);
//console.log(this.posList.length);
// if (target.getChildByName(`box0`).active == false) {
// for (let i = 0; i < this._maxCol * this._maxRow; i++) {
// const element = this.posList[i];
// //console.log('element ', element.x, element.y);
// var cotdoc = (i - i % this._maxCol) / this._maxCol;
// var hangngang = i % this._maxRow;
// if (cc.Vec3.distance(localPos, element) < detectRange) {
// //console.log('cc.Vec3.distance(localPos, element) ', cc.Vec3.distance(localPos, element));
// //console.log(hangngang, cotdoc);
// // console.log(this.nBoard);
// //console.log(this.nBoard.getChildByName(`box${cotdoc}${hangngang}`).getChildByName('box0').active);
// //console.log(this._configMatrix[cotdoc][hangngang]);
// if (this._configMatrix[cotdoc][hangngang] != 0) {
// this.fillColor(this._configMatrix[cotdoc][hangngang], this.nBoard.getChildByName(`box${cotdoc}${hangngang}`));
// } else {
// this.fillColor(this.touchColor, this.nBoard.getChildByName(`box${cotdoc}${hangngang}`));
// count++;
// }
// //this.fillColor(this.touchColor, this.nBoard.getChildByName(`box${cotdoc}${hangngang}`));
// }
// }
// }
const lineDraw = new lineData;
let color = target.getChildByName(`box0`).getChildByName('New Label').getComponent(cc.Label).string;
if (color != '0') {
for (let i = 0; i < this._maxCol * this._maxRow; i++) {
const element = this.posList[i];
//console.log('element ', element.x, element.y);
var cotdoc = (i - i % this._maxCol) / this._maxCol;
var hangngang = i % this._maxRow;
if (cc.Vec3.distance(localPos, element) < detectRange) {
//console.log('cc.Vec3.distance(localPos, element) ', cc.Vec3.distance(localPos, element));
//console.log(hangngang, cotdoc);
// console.log(this.nBoard);
//console.log(this.nBoard.getChildByName(`box${cotdoc}${hangngang}`).getChildByName('box0').active);
//console.log(this._configMatrix[cotdoc][hangngang]);
if (this._configMatrix[cotdoc][hangngang] != 0) {
color = this._configMatrix[cotdoc][hangngang];
if (this.nBoard.getChildByName(`box${cotdoc}${hangngang}`).getChildByName('box0').getChildByName('New Label').getComponent(cc.Label).string == `${color}g`) {
console.log('ggggg');
} else {
this.fillNumber(color, this.nBoard.getChildByName(`box${cotdoc}${hangngang}`));
}
//console.log('abc ', color);
} else {
color = this.touchColor;
//this._configMatrix[cotdoc][hangngang] = parseInt(color);
//console.log('lineDraw.lines.length ',lineDraw.lines.length);
//const lineDraw = new lineData;
if (this.nBoard.getChildByName(`box${cotdoc}${hangngang}`).getChildByName('box0').getChildByName('New Label').getComponent(cc.Label).string == '0') {
//console.log('asdasasd', this.nBoard.getChildByName(`box${cotdoc}${hangngang}`));
lineDraw.lines.push(this.nBoard.getChildByName(`box${cotdoc}${hangngang}`));
console.log(lineDraw.lines);
} else {
//console.log('falsefalse');
}
lineDraw.color = color;
this.fillNumber(color, this.nBoard.getChildByName(`box${cotdoc}${hangngang}`));
//this.listLines.push(lineDraw);
//console.log('xyz ', color);
}
//console.log(lineDraw.lines);
}
// console.log(this._configMatrix);
}
}
//this.drawLine(target);
setTimeout(() => {
gameplayController.inst.checkWin();
}, 1000);
}
handleTouchEnd(event) {
const target = event.target;
//gameplayController.inst.checkWin();
}
scaleUp(box: any) {
cc.tween(box)
.to(0.1, { scale: 1.1 })
.call(() => {
//console.log('uppppp');
})
.to(0.1, { scale: 0.9 })
.to(0.1, { scale: 1 })
.start();
}
isFullMap() {
let count = 0;
for (let i = 0; i < this._maxRow; i++) {
for (let j = 0; j < this._maxCol; j++) {
if (this.nBoard.getChildByName(`box${i}${j}`).getChildByName(`box0`).active == false)
count++;
}
}
if (count == (this._maxCol) * (this._maxRow)) {
console.log('count ', count);
return true;
} else return false;
}
// drawLine(target) {
// this.lines.length = 0;
// for (let i = 0; i < this._maxCol + 1; i++) {
// let linesSprt = cc.instantiate(this.prfLine);
// linesSprt.width = this.nBoard.width / this._maxCol;
// linesSprt.height = this.nBoard.height / this._maxRow / 3;
// this.nBoard.addChild(linesSprt);
// this.lines[i] = linesSprt;
// // this.graphics = linesSprt.getComponent(cc.Graphics);
// // this.graphics.lineWidth = linesSprt.height;
// // this.graphics.lineCap = this.graphics.lineJoin = 1;
// // this.graphics.clear();
// let worldPos = target.getPosition();
// let localPos = this.node.parent.convertToNodeSpaceAR(worldPos);
// // this.graphics.moveTo(localPos.x, localPos.y);
// let detectRange = 0.5 * this.nBoard.width / this._maxCol;
// for (let i = 0; i < this._maxCol * this._maxRow; i++) {
// const element = this.posList[i];
// var cotdoc = (i - i % this._maxCol) / this._maxCol;
// var hangngang = i % this._maxRow;
// if (cc.Vec3.distance(localPos, element) < detectRange) {
// if (this._configMatrix[cotdoc][hangngang] != 0) {
// // this.graphics.lineTo(element.x, element.y);
// } else {
// }
// }
// //this.graphics.lineTo(element.x, element.y);
// }
// }
// }
drawNum() {
}
}
Editor is loading...