export default class GameUtiles {
/**
* 连线数组
*/
public static drawDate: cc.Vec2[] = [];
private static guaidian: cc.Vec2[] = [];
/**
* 是否有阻碍物
* @param array
* @param x
* @param y
* @returns
*/
public static isBlocked(array: number[][], x: number, y: number): boolean {
if (!array[x]) {
return false;
}
if (!array[x][y]) {
return false;
}
if (array[x][y] == 0) {
return false;
}
return true;
}
/**
* 水平检测
* @param array
* @param x1
* @param y1
* @param x2
* @param y2
* @returns
*/
public static horizon(
array: number[][],
x1: number,
y1: number,
x2: number,
y2: number
): boolean {
if (x1 == x2 && y1 == y2) {
return false;
}
if (x1 != x2) {
return false;
}
let start_x = Math.min(y1, y2);
let end_x = Math.max(y1, y2);
//判断直线中间是否有障碍物
for (let i = start_x + 1; i < end_x; i++) {
if (this.isBlocked(array, x1, i)) {
return false;
}
}
return true;
}
/**
* 垂直检测
* @param array
* @param x1
* @param y1
* @param x2
* @param y2
* @returns
*/
public static vertical(
array: number[][],
x1: number,
y1: number,
x2: number,
y2: number
): boolean {
if (x1 == x2 && y1 == y2) {
return false;
}
if (y1 != y2) {
return false;
}
let start_x = Math.min(x1, x2);
let end_x = Math.max(x1, x2);
//判断直线中间是否有障碍物
for (let i = start_x + 1; i < end_x; i++) {
if (this.isBlocked(array, i, y1)) {
return false;
}
}
return true;
}
/**
* 一个拐点
* @param array
* @param x1
* @param y1
* @param x2
* @param y2
* @returns
*/
public static turnOnce(
array: number[][],
x1: number,
y1: number,
x2: number,
y2: number
): boolean {
if (x1 == x2 && y1 == y2) {
return false;
}
let c_x = x2,
c_y = y1;
if (!this.isBlocked(array, c_x, c_y)) {
if (
this.vertical(array, x1, y1, c_x, c_y) &&
this.horizon(array, c_x, c_y, x2, y2)
) {
GameUtiles.guaidian[0] = new cc.Vec2(c_x, c_y);
return true;
}
}
let d_x = x1,
d_y = y2;
if (!this.isBlocked(array, d_x, d_y)) {
if (
this.horizon(array, x1, y1, d_x, d_y) &&
this.vertical(array, d_x, d_y, x2, y2)
) {
GameUtiles.guaidian[0] = new cc.Vec2(d_x, d_y);
return true;
}
}
return false;
}
/**
* 两个拐点
* @param array
* @param x1
* @param y1
* @param x2
* @param y2
* @returns
*/
public static turnTwice(
array: number[][],
x1: number,
y1: number,
x2: number,
y2: number
) {
if (x1 == x2 && y1 == y2) {
return false;
}
let max_x = array.length;
let max_y = array[0].length;
for (let i = -1; i <= max_x; i++) {
for (let j = -1; j <= max_y; j++) {
if (i != x1 && i != x2 && j != y1 && j != y2 && i >= 0 && j >= 0) {
continue;
}
if ((i == x1 && j == y1) || (i == x2 && j == y2)) {
continue;
}
if (this.isBlocked(array, i, j)) {
continue;
}
if (
this.turnOnce(array, x1, y1, i, j) &&
(this.horizon(array, i, j, x2, y2) ||
this.vertical(array, i, j, x2, y2))
) {
GameUtiles.guaidian[1] = new cc.Vec2(i, j);
return true;
}
if (
(this.horizon(array, x1, y1, i, j) ||
this.vertical(array, x1, y1, i, j)) &&
this.turnOnce(array, i, j, x2, y2)
) {
let g1 = GameUtiles.guaidian[0];
GameUtiles.guaidian[0] = new cc.Vec2(i, j);
GameUtiles.guaidian[1] = g1;
return true;
}
}
}
return false;
}
/**
* 是否可以移除
* @param array
* @param x1
* @param y1
* @param x2
* @param y2
* @returns
*/
public static canBeRemoved(
array: number[][],
x1: number,
y1: number,
x2: number,
y2: number
): boolean {
GameUtiles.drawDate = [];
GameUtiles.guaidian = [];
if (
this.horizon(array, x1, y1, x2, y2) ||
this.vertical(array, x1, y1, x2, y2) ||
this.turnOnce(array, x1, y1, x2, y2) ||
this.turnTwice(array, x1, y1, x2, y2)
) {
array[x1][y1] = 0;
array[x2][y2] = 0;
let id = 0;
GameUtiles.drawDate[id] = new cc.Vec2(x1, y1);
let l = GameUtiles.guaidian.length;
for (let i = 0; i < l; i++) {
id++;
GameUtiles.drawDate[id] = GameUtiles.guaidian[i];
}
id++;
GameUtiles.drawDate[id] = new cc.Vec2(x2, y2);
return true;
}
return false;
}
//获得节点的世界坐标
public static getWorldPos(node: cc.Node): cc.Vec2 {
return node.convertToWorldSpaceAR(cc.Vec2.ZERO);
}
}
cocos creator+typescript 简易消消乐记录
最新推荐文章于 2024-05-15 01:28:34 发布