| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- import { _decorator, assetManager, Component, Node, Prefab, instantiate, Vec3, Material, Color, MeshRenderer, color, find, tween, director } from 'cc';
- import { CubeInfo } from './CubeInfo';
- import { PlayerCtl } from './PlayerCtl';
- import { CubePool } from './CubePool';
- import { GameState, PlayState, StateManager } from './StateManager';
- import { HardColorStrategy, NormalColorStrategy, StrategyManager } from './StrategyManager';
- import { LoadRes } from '../Config/LoadRes';
- import { Singleton } from '../Tools/Singleton';
- import ch_util from '../../ch/ch_util';
- const { ccclass, property } = _decorator;
- export class CreateMap {
- private resourcesLoadedCount: number = 0;
- public container: Node = null;
- private mainCanvas: Node = null;
- private settingsPanel:Node=null;
- private map: Node[][][] = [];
- private MaxDeep = 1000;
- public MaxK = 4; // 最大纵深
- private MaxI = 18; // 最大行
- private MaxJ = 10; // 最大列
- public MaxColorNum = 0;
- private cubePool: CubePool;
- public static getInstance(): CreateMap {
- return Singleton.getInstance(CreateMap);
- }
- onLoad() {
- this.initScene();
- this.cubePool = new CubePool(LoadRes.getInstance().cubePrefab, LoadRes.getInstance().materials, 720);
- this.initMap();
- StateManager.getInstance().setState(new PlayState());
- }
- start() {
- }
- initScene() {
- this.container = instantiate(LoadRes.getInstance().container);
- this.container.parent = director.getScene();
- this.mainCanvas = instantiate(LoadRes.getInstance().mainCanvas);
- this.mainCanvas.parent = director.getScene();
- // this.settingsPanel=instantiate(LoadRes.getInstance().settingsPanel);
- // this.settingsPanel.parent=this.mainCanvas;
- // this.settingsPanel.active=false;
-
- }
- update(deltaTime: number) {
- }
- // // 获取一个随机数
- // getRandomInt(min: number, max: number): number {
- // min = Math.ceil(min);
- // max = Math.floor(max);
- // return Math.floor(Math.random() * (max - min + 1)) + min;
- // }
- private setupCube(cube: Node, k: number, i: number, j: number, value: number): void {
- const position = new Vec3((i - 8.5) * 1.3, 9 - (k * 1.2), (4.5 - j) * 1.3);
- cube.setPosition(position);
- // cube.addComponent(CubeInfo);
- cube.getComponent(CubeInfo).setPos(k, i, j);
- cube.getComponent(CubeInfo).setId(value);
- cube.getComponent(CubeInfo).setMaterial(LoadRes.getInstance().materials[value]);
- cube.getComponent(CubeInfo).setOriginalMaterial(LoadRes.getInstance().materials[value]);
- this.container.addChild(cube);
- this.map[k][i][j] = cube;
- if (k !== 0) {
- cube.getComponent(CubeInfo).setLock(true);
- } else {
- cube.getComponentInChildren(MeshRenderer).setMaterialInstance(LoadRes.getInstance().materials[value], 0);
- cube.getComponent(CubeInfo).setOriginalMaterial(LoadRes.getInstance().materials[value]);
- cube.getComponent(CubeInfo).setLock(false);
- }
- }
- // 根据配置表信息初始化地图
- /*
- 分批实例化,默认只有四层,每开新一层就再实例化一层
- */
- private initMap() {
- this.map = new Array(this.MaxDeep).fill(0).map(() => new Array(this.MaxI).fill(0).map(() => new Array(this.MaxJ)));
- for (let k = 0; k < this.MaxK; k++) {
- for (let i = 0; i < this.MaxI; i++) {
- for (let j = 0; j < this.MaxJ; j++) {
- let value = ch_util.getRandomInt(0, this.MaxColorNum);
- if (LoadRes.getInstance().cubePrefab !== null) {
- let cube = this.cubePool.getCube();
- this.setupCube(cube, k, i, j, value);
- }
- }
- }
- if (k === 0) {
- console.log(StrategyManager.getInstance().getColorStrategy().strategyType);
- StrategyManager.getInstance().setColorStrategy(new NormalColorStrategy());
- }
- }
- }
- public initNewLayer(index: number, move: number): void {
- console.log("新增实例化层");
- StrategyManager.getInstance().setColorStrategy(new HardColorStrategy());
- this.MaxK++;
- for (let i = 0; i < this.MaxI; i++) {
- for (let j = 0; j < this.MaxJ; j++) {
- let value = ch_util.getRandomInt(0, this.MaxColorNum);
- if (LoadRes.getInstance().cubePrefab !== null) {
- let cube = this.cubePool.getCube();
- this.setupCube(cube, index, i, j, value);
- }
- }
- }
- }
- // private initMap(): void {
- // this.map = new Array(this.MaxDeep).fill(0).map(() => new Array(this.MaxI).fill(0).map(() => new Array(this.MaxJ)));
- // for (let k = 0; k < this.MaxK; k++) {
- // for (let i = 0; i < this.MaxI; i++) {
- // for (let j = 0; j < this.MaxJ; j++) {
- // let value = this.getRandomInt(0, 3);
- // if (LoadRes.getInstance().cubePrefab !== null) {
- // let cube = this.cubePool.getCube();
- // cube.setPosition(new Vec3((i - 8.5) * 1.3, 9 - (k * 1.2), (4.5 - j) * 1.3));
- // cube.addComponent(CubeInfo);
- // cube.getComponent(CubeInfo).setPos(k, i, j);
- // cube.getComponent(CubeInfo).setId(value);
- // // cube.getComponent(CubeInfo).setColor(config[IdToName[value]].color);
- // cube.getComponent(CubeInfo).setMaterial(LoadRes.getInstance().materials[value]);
- // if (k !== 0) {
- // cube.getComponent(CubeInfo).setLock(true);
- // }
- // else {
- // cube.getComponentInChildren(MeshRenderer).setMaterialInstance(LoadRes.getInstance().materials[value], 0);
- // //material.setProperty("albedo", config[IdToName[value]].color);
- // cube.getComponent(CubeInfo).setLock(false);
- // }
- // this.container.addChild(cube);
- // this.map[k][i][j] = cube;
- // }
- // }
- // }
- // }
- // }
- public GetMap(): Node[][][] {
- return this.map;
- }
- public GetMaxKIJ(): Vec3 {
- return new Vec3(this.MaxK, this.MaxI, this.MaxJ);
- }
- public setMap(newMap: Node[][], index: number, move: number): void {
- this.map[index] = newMap;
- console.log("正在移动第" + index + "层");
- let hasMovingCube = false;
- let pendingTweens = 0;
- for (let i = 0; i < this.MaxI; i++) {
- for (let j = 0; j < this.MaxJ; j++) {
- const cube = newMap[i][j];
- if (cube) {
- const targetPosition = new Vec3((i - 8.5) * 1.3, 9 - (index - move) * 1.2, (4.5 - j) * 1.3);
- if (!cube.position.equals(targetPosition)) {
- hasMovingCube = true;
- pendingTweens++;
- this.moveCube(cube, targetPosition, pendingTweens);
- }
- }
- }
- }
- if (!hasMovingCube) {
- PlayerCtl.instance.touchLock = false;
- }
- }
- moveCube(cube, targetPosition, pendingTweens) {
- tween(cube)
- .to(0.4, { position: targetPosition }, { easing: 'smooth' })
- .call(() => {
- pendingTweens--;
- if (pendingTweens === 0) {
- PlayerCtl.instance.touchLock = false;
- }
- })
- .start();
- }
- //解锁新层后改变颜色,颜色也需伴随移动改变
- public changeColor(surface: number, index: number) {
- for (let k = surface; k < index; k++) {
- if (k < this.MaxK && k !== 0) {
- for (let i = 0; i < this.MaxI; i++) {
- for (let j = 0; j < this.MaxJ; j++) {
- if (this.map[k][i][j]) {
- this.map[k][i][j].getComponentInChildren(MeshRenderer).setMaterialInstance(this.map[k][i][j].getComponent(CubeInfo).getMaterial(), 0);
- if (this.map[k - 1][i][j] === null) {
- this.map[k][i][j].getComponent(CubeInfo).setLock(false);
- }
- else {
- // this.map[k][i][j].getComponentInChildren(MeshRenderer).material.setProperty("albedo", Color.GRAY);
- this.map[k][i][j].getComponent(CubeInfo).setLock(true);
- }
- }
- }
- }
- }
- }
- }
- // public initNewLayer(index: number, move: number) {
- // console.log("新增实例化层");
- // this.MaxK++;
- // for (let i = 0; i < this.MaxI; i++) {
- // for (let j = 0; j < this.MaxJ; j++) {
- // let value = this.getRandomInt(0, 3);
- // if (LoadRes.getInstance().cubePrefab !== null) {
- // let cube = this.cubePool.getCube();
- // cube.setPosition(new Vec3((i - 8.5) * 1.3, 9 - (index - move) * 1.2, (4.5 - j) * 1.3));
- // cube.addComponent(CubeInfo);
- // cube.getComponent(CubeInfo).setPos(index, i, j);
- // cube.getComponent(CubeInfo).setId(value);
- // cube.getComponent(CubeInfo).setMaterial(LoadRes.getInstance().materials[value]);
- // // cube.getComponentInChildren(MeshRenderer).setMaterialInstance(this.materials[value],0);
- // // cube.getComponentInChildren(MeshRenderer).material.setProperty("albedo", cube.getComponent(CubeInfo).getColor());
- // cube.getComponent(CubeInfo).setLock(true);
- // this.container.addChild(cube);
- // this.map[index][i][j] = cube;
- // }
- // }
- // }
- // }
- public destroyNode(pos: Vec3): void {
- if (this.map[pos.x][pos.y][pos.z]) {
- this.map[pos.x][pos.y][pos.z].getComponentInChildren(MeshRenderer).setMaterialInstance(LoadRes.getInstance().materials[4], 0);
- this.cubePool.recycleCube(this.map[pos.x][pos.y][pos.z]);
- this.map[pos.x][pos.y][pos.z] = null;
- }
- }
- }
|