CreateMap.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { _decorator, assetManager, Component, Node, Prefab, instantiate, Vec3, Material, Color, MeshRenderer, color, find, tween } from 'cc';
  2. import { config,IdToName } from './CubeConfig';
  3. import { CubeInfo } from './CubeInfo';
  4. import { PlayerCtl } from './PlayerCtl';
  5. const { ccclass, property } = _decorator;
  6. @ccclass('CreateMap')
  7. export class CreateMap extends Component {
  8. private resourcesLoadedCount: number = 0;
  9. @property(Prefab)
  10. private cubePrefab: Prefab=null;
  11. private map: Node[][][] = [];
  12. private MaxK = 3; // 最大纵深
  13. private MaxI = 15; // 最大行
  14. private MaxJ = 7; // 最大列
  15. start() {
  16. // 读取Json配置表
  17. assetManager.loadBundle('CubePrefab', (err, bundle) => {
  18. bundle.load('Cube', Prefab, (err, prefab) => {
  19. if (err) {
  20. console.error(err);
  21. return;
  22. }
  23. this.cubePrefab = prefab;
  24. this.checkAllResourcesLoaded();
  25. });
  26. });
  27. }
  28. update(deltaTime: number) { }
  29. //检查资源加载
  30. private checkAllResourcesLoaded(): void {
  31. this.resourcesLoadedCount++;
  32. if (this.resourcesLoadedCount === 1) {
  33. this.initMap();
  34. }
  35. }
  36. // 获取一个随机数
  37. getRandomInt(min: number, max: number): number {
  38. min = Math.ceil(min);
  39. max = Math.floor(max);
  40. return Math.floor(Math.random() * (max - min + 1)) + min;
  41. }
  42. // 根据配置表信息初始化地图
  43. private initMap(): void {
  44. this.map = new Array(this.MaxK).fill(0).map(() => new Array(this.MaxI).fill(0).map(() => new Array(this.MaxJ)));
  45. for (let k = 0; k < this.MaxK; k++) {
  46. for (let i = 0; i < this.MaxI; i++) {
  47. for (let j = 0; j < this.MaxJ; j++) {
  48. let value = this.getRandomInt(0, 3);
  49. if (this.cubePrefab !== null) {
  50. let cube = instantiate(this.cubePrefab);
  51. cube.setPosition(new Vec3((i - 7), 16 - k, (3.05 - j)));
  52. cube.addComponent(CubeInfo);
  53. cube.getComponent(CubeInfo).setPos(k, i, j);
  54. cube.getComponent(CubeInfo).setId(value);
  55. cube.getComponent(CubeInfo).setColor(config[IdToName[value]].color);
  56. if (k != 0) {
  57. cube.getComponentInChildren(MeshRenderer).material.setProperty("albedo", Color.GRAY);
  58. cube.getComponent(CubeInfo).setLock(true);
  59. }
  60. else
  61. {
  62. cube.getComponentInChildren(MeshRenderer).material.setProperty("albedo", config[IdToName[value]].color);
  63. cube.getComponent(CubeInfo).setLock(false);
  64. }
  65. this.node.addChild(cube);
  66. this.map[k][i][j] = cube;
  67. }
  68. }
  69. }
  70. }
  71. }
  72. public GetMap(): Node[][][] {
  73. return this.map;
  74. }
  75. public GetMaxKIJ(): Vec3 {
  76. return new Vec3(this.MaxK, this.MaxI, this.MaxJ);
  77. }
  78. //更新地图
  79. public setMap(newMap: Node[][], index: number): void {
  80. this.map[index] = newMap;
  81. for (let i = 0; i < this.MaxI; i++) {
  82. for (let j = 0; j < this.MaxJ; j++) {
  83. if (newMap[i][j]) {
  84. // newMap[i][j].setPosition(new Vec3((i - 7), 16 - index, (3.05 - j)));
  85. // 移动一个方块到新位置
  86. tween(newMap[i][j])
  87. .to(0.5, { position: new Vec3((i - 7), 16 - index, (3.05 - j)) }, { easing: 'quadInOut' })
  88. .start();
  89. }
  90. // else {
  91. // // 如果新地图中某个位置没有节点,确保原来的节点也被销毁或者移除
  92. // if (this.map[index][i][j]) {
  93. // this.map[index][i][j].destroy();
  94. // }
  95. // }
  96. }
  97. }
  98. }
  99. //解锁新层后改变颜色,颜色也需伴随移动改变
  100. public changeColor(index: number) {
  101. for (let k = 0; k < index; k++) {
  102. if (k < this.MaxK && k !== 0) {
  103. for (let i = 0; i < this.MaxI; i++) {
  104. for (let j = 0; j < this.MaxJ; j++) {
  105. if (this.map[k][i][j])
  106. if (this.map[k - 1][i][j] === null) {
  107. this.map[k][i][j].getComponentInChildren(MeshRenderer).material.setProperty("albedo", this.map[k][i][j].getComponent(CubeInfo).getColor());
  108. this.map[k][i][j].getComponent(CubeInfo).setLock(false);
  109. }
  110. else {
  111. this.map[k][i][j].getComponentInChildren(MeshRenderer).material.setProperty("albedo", Color.GRAY);
  112. this.map[k][i][j].getComponent(CubeInfo).setLock(true);
  113. }
  114. }
  115. }
  116. }
  117. }
  118. }
  119. }