CreateMap.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import { _decorator, assetManager, Component, Node, Prefab, instantiate, Vec3, Material, Color, MeshRenderer, color, find, tween, director } from 'cc';
  2. import { CubeInfo } from './CubeInfo';
  3. import { PlayerCtl } from './PlayerCtl';
  4. import { CubePool } from './CubePool';
  5. import { GameState, PlayState, StateManager } from './StateManager';
  6. import { HardColorStrategy, NormalColorStrategy, StrategyManager } from './StrategyManager';
  7. import { LoadRes } from '../Config/LoadRes';
  8. import { Singleton } from '../Tools/Singleton';
  9. import ch_util from '../../ch/ch_util';
  10. const { ccclass, property } = _decorator;
  11. export class CreateMap {
  12. private resourcesLoadedCount: number = 0;
  13. public container: Node = null;
  14. private mainCanvas: Node = null;
  15. private settingsPanel:Node=null;
  16. private map: Node[][][] = [];
  17. private MaxDeep = 1000;
  18. public MaxK = 4; // 最大纵深
  19. private MaxI = 18; // 最大行
  20. private MaxJ = 10; // 最大列
  21. public MaxColorNum = 0;
  22. private cubePool: CubePool;
  23. public static getInstance(): CreateMap {
  24. return Singleton.getInstance(CreateMap);
  25. }
  26. onLoad() {
  27. this.initScene();
  28. this.cubePool = new CubePool(LoadRes.getInstance().cubePrefab, LoadRes.getInstance().materials, 720);
  29. this.initMap();
  30. StateManager.getInstance().setState(new PlayState());
  31. }
  32. start() {
  33. }
  34. initScene() {
  35. this.container = instantiate(LoadRes.getInstance().container);
  36. this.container.parent = director.getScene();
  37. this.mainCanvas = instantiate(LoadRes.getInstance().mainCanvas);
  38. this.mainCanvas.parent = director.getScene();
  39. // this.settingsPanel=instantiate(LoadRes.getInstance().settingsPanel);
  40. // this.settingsPanel.parent=this.mainCanvas;
  41. // this.settingsPanel.active=false;
  42. }
  43. update(deltaTime: number) {
  44. }
  45. // // 获取一个随机数
  46. // getRandomInt(min: number, max: number): number {
  47. // min = Math.ceil(min);
  48. // max = Math.floor(max);
  49. // return Math.floor(Math.random() * (max - min + 1)) + min;
  50. // }
  51. private setupCube(cube: Node, k: number, i: number, j: number, value: number): void {
  52. const position = new Vec3((i - 8.5) * 1.3, 9 - (k * 1.2), (4.5 - j) * 1.3);
  53. cube.setPosition(position);
  54. // cube.addComponent(CubeInfo);
  55. cube.getComponent(CubeInfo).setPos(k, i, j);
  56. cube.getComponent(CubeInfo).setId(value);
  57. cube.getComponent(CubeInfo).setMaterial(LoadRes.getInstance().materials[value]);
  58. cube.getComponent(CubeInfo).setOriginalMaterial(LoadRes.getInstance().materials[value]);
  59. this.container.addChild(cube);
  60. this.map[k][i][j] = cube;
  61. if (k !== 0) {
  62. cube.getComponent(CubeInfo).setLock(true);
  63. } else {
  64. cube.getComponentInChildren(MeshRenderer).setMaterialInstance(LoadRes.getInstance().materials[value], 0);
  65. cube.getComponent(CubeInfo).setOriginalMaterial(LoadRes.getInstance().materials[value]);
  66. cube.getComponent(CubeInfo).setLock(false);
  67. }
  68. }
  69. // 根据配置表信息初始化地图
  70. /*
  71. 分批实例化,默认只有四层,每开新一层就再实例化一层
  72. */
  73. private initMap() {
  74. this.map = new Array(this.MaxDeep).fill(0).map(() => new Array(this.MaxI).fill(0).map(() => new Array(this.MaxJ)));
  75. for (let k = 0; k < this.MaxK; k++) {
  76. for (let i = 0; i < this.MaxI; i++) {
  77. for (let j = 0; j < this.MaxJ; j++) {
  78. let value = ch_util.getRandomInt(0, this.MaxColorNum);
  79. if (LoadRes.getInstance().cubePrefab !== null) {
  80. let cube = this.cubePool.getCube();
  81. this.setupCube(cube, k, i, j, value);
  82. }
  83. }
  84. }
  85. if (k === 0) {
  86. console.log(StrategyManager.getInstance().getColorStrategy().strategyType);
  87. StrategyManager.getInstance().setColorStrategy(new NormalColorStrategy());
  88. }
  89. }
  90. }
  91. public initNewLayer(index: number, move: number): void {
  92. console.log("新增实例化层");
  93. StrategyManager.getInstance().setColorStrategy(new HardColorStrategy());
  94. this.MaxK++;
  95. for (let i = 0; i < this.MaxI; i++) {
  96. for (let j = 0; j < this.MaxJ; j++) {
  97. let value = ch_util.getRandomInt(0, this.MaxColorNum);
  98. if (LoadRes.getInstance().cubePrefab !== null) {
  99. let cube = this.cubePool.getCube();
  100. this.setupCube(cube, index, i, j, value);
  101. }
  102. }
  103. }
  104. }
  105. // private initMap(): void {
  106. // this.map = new Array(this.MaxDeep).fill(0).map(() => new Array(this.MaxI).fill(0).map(() => new Array(this.MaxJ)));
  107. // for (let k = 0; k < this.MaxK; k++) {
  108. // for (let i = 0; i < this.MaxI; i++) {
  109. // for (let j = 0; j < this.MaxJ; j++) {
  110. // let value = this.getRandomInt(0, 3);
  111. // if (LoadRes.getInstance().cubePrefab !== null) {
  112. // let cube = this.cubePool.getCube();
  113. // cube.setPosition(new Vec3((i - 8.5) * 1.3, 9 - (k * 1.2), (4.5 - j) * 1.3));
  114. // cube.addComponent(CubeInfo);
  115. // cube.getComponent(CubeInfo).setPos(k, i, j);
  116. // cube.getComponent(CubeInfo).setId(value);
  117. // // cube.getComponent(CubeInfo).setColor(config[IdToName[value]].color);
  118. // cube.getComponent(CubeInfo).setMaterial(LoadRes.getInstance().materials[value]);
  119. // if (k !== 0) {
  120. // cube.getComponent(CubeInfo).setLock(true);
  121. // }
  122. // else {
  123. // cube.getComponentInChildren(MeshRenderer).setMaterialInstance(LoadRes.getInstance().materials[value], 0);
  124. // //material.setProperty("albedo", config[IdToName[value]].color);
  125. // cube.getComponent(CubeInfo).setLock(false);
  126. // }
  127. // this.container.addChild(cube);
  128. // this.map[k][i][j] = cube;
  129. // }
  130. // }
  131. // }
  132. // }
  133. // }
  134. public GetMap(): Node[][][] {
  135. return this.map;
  136. }
  137. public GetMaxKIJ(): Vec3 {
  138. return new Vec3(this.MaxK, this.MaxI, this.MaxJ);
  139. }
  140. public setMap(newMap: Node[][], index: number, move: number): void {
  141. this.map[index] = newMap;
  142. console.log("正在移动第" + index + "层");
  143. let hasMovingCube = false;
  144. let pendingTweens = 0;
  145. for (let i = 0; i < this.MaxI; i++) {
  146. for (let j = 0; j < this.MaxJ; j++) {
  147. const cube = newMap[i][j];
  148. if (cube) {
  149. const targetPosition = new Vec3((i - 8.5) * 1.3, 9 - (index - move) * 1.2, (4.5 - j) * 1.3);
  150. if (!cube.position.equals(targetPosition)) {
  151. hasMovingCube = true;
  152. pendingTweens++;
  153. this.moveCube(cube, targetPosition, pendingTweens);
  154. }
  155. }
  156. }
  157. }
  158. if (!hasMovingCube) {
  159. PlayerCtl.instance.touchLock = false;
  160. }
  161. }
  162. moveCube(cube, targetPosition, pendingTweens) {
  163. tween(cube)
  164. .to(0.4, { position: targetPosition }, { easing: 'smooth' })
  165. .call(() => {
  166. pendingTweens--;
  167. if (pendingTweens === 0) {
  168. PlayerCtl.instance.touchLock = false;
  169. }
  170. })
  171. .start();
  172. }
  173. //解锁新层后改变颜色,颜色也需伴随移动改变
  174. public changeColor(surface: number, index: number) {
  175. for (let k = surface; k < index; k++) {
  176. if (k < this.MaxK && k !== 0) {
  177. for (let i = 0; i < this.MaxI; i++) {
  178. for (let j = 0; j < this.MaxJ; j++) {
  179. if (this.map[k][i][j]) {
  180. this.map[k][i][j].getComponentInChildren(MeshRenderer).setMaterialInstance(this.map[k][i][j].getComponent(CubeInfo).getMaterial(), 0);
  181. if (this.map[k - 1][i][j] === null) {
  182. this.map[k][i][j].getComponent(CubeInfo).setLock(false);
  183. }
  184. else {
  185. // this.map[k][i][j].getComponentInChildren(MeshRenderer).material.setProperty("albedo", Color.GRAY);
  186. this.map[k][i][j].getComponent(CubeInfo).setLock(true);
  187. }
  188. }
  189. }
  190. }
  191. }
  192. }
  193. }
  194. // public initNewLayer(index: number, move: number) {
  195. // console.log("新增实例化层");
  196. // this.MaxK++;
  197. // for (let i = 0; i < this.MaxI; i++) {
  198. // for (let j = 0; j < this.MaxJ; j++) {
  199. // let value = this.getRandomInt(0, 3);
  200. // if (LoadRes.getInstance().cubePrefab !== null) {
  201. // let cube = this.cubePool.getCube();
  202. // cube.setPosition(new Vec3((i - 8.5) * 1.3, 9 - (index - move) * 1.2, (4.5 - j) * 1.3));
  203. // cube.addComponent(CubeInfo);
  204. // cube.getComponent(CubeInfo).setPos(index, i, j);
  205. // cube.getComponent(CubeInfo).setId(value);
  206. // cube.getComponent(CubeInfo).setMaterial(LoadRes.getInstance().materials[value]);
  207. // // cube.getComponentInChildren(MeshRenderer).setMaterialInstance(this.materials[value],0);
  208. // // cube.getComponentInChildren(MeshRenderer).material.setProperty("albedo", cube.getComponent(CubeInfo).getColor());
  209. // cube.getComponent(CubeInfo).setLock(true);
  210. // this.container.addChild(cube);
  211. // this.map[index][i][j] = cube;
  212. // }
  213. // }
  214. // }
  215. // }
  216. public destroyNode(pos: Vec3): void {
  217. if (this.map[pos.x][pos.y][pos.z]) {
  218. this.map[pos.x][pos.y][pos.z].getComponentInChildren(MeshRenderer).setMaterialInstance(LoadRes.getInstance().materials[4], 0);
  219. this.cubePool.recycleCube(this.map[pos.x][pos.y][pos.z]);
  220. this.map[pos.x][pos.y][pos.z] = null;
  221. }
  222. }
  223. }