cocos creator+typescript 简易消消乐记录

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);
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
CocosCreator是一种基于 TypeScript的游戏开发引擎,而protobuf是一种数据序列化格式。要在CocosCreator中使用protobuf实现登录功能,首先需要进行以下几个步骤: 1. 下载protobuf库:在项目的assets目录中创建一个新的文件夹,例如"protobuf",然后从protobuf官方网站上下载protobuf的JavaScript库文件,并将其解压到该文件夹中。 2. 创建.proto文件:在项目的assets目录中创建一个新的文件,例如"login.proto",并在其中定义登录功能所需的消息结构。例如,可以定义一个"LoginRequest"消息,包含用户名和密码字段,并定义一个"LoginResponse"消息,表示登录结果。 3. 生成JavaScript代码:打开终端,进入到protobuf库所在的文件夹,执行以下命令来生成JavaScript代码文件: protoc --js_out=import_style=commonjs,binary:生成代码路径 -I=proto文件所在路径 proto文件 这将根据.proto文件生成对应的JavaScript代码文件,用于在CocosCreator中进行序列化和反序列化。 4. 在CocosCreator中使用protobuf:将生成的JavaScript代码文件拷贝到CocosCreator项目的assets目录中,然后在适当的地方引入protobuf库。 5. 编写登录功能代码:在需要实现登录功能的脚本文件中,使用引入的protobuf库来序列化登录请求数据,并将其发送到服务器,并处理服务器返回的响应数据。 总的来说,使用CocosCreator和protobuf实现登录功能需要先创建.proto文件,然后通过protobuf库生成对应的JavaScript代码文件,并将其引入项目中,在代码中使用protobuf库进行消息的序列化和反序列化,以实现与服务器的通信。这样就可以在CocosCreator中使用protobuf来实现登录功能了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值