| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- import { _decorator, assetManager, Component, Node, Prefab, instantiate, Vec3, Material, Color, MeshRenderer, color, find, tween } from 'cc';
- import { config,IdToName } from './CubeConfig';
- import { CubeInfo } from './CubeInfo';
- import { PlayerCtl } from './PlayerCtl';
- const { ccclass, property } = _decorator;
- @ccclass('CreateMap')
- export class CreateMap extends Component {
- private resourcesLoadedCount: number = 0;
- @property(Prefab)
- private cubePrefab: Prefab=null;
- private map: Node[][][] = [];
- private MaxK = 3; // 最大纵深
- private MaxI = 15; // 最大行
- private MaxJ = 7; // 最大列
- start() {
- // 读取Json配置表
- assetManager.loadBundle('CubePrefab', (err, bundle) => {
- bundle.load('Cube', Prefab, (err, prefab) => {
- if (err) {
- console.error(err);
- return;
- }
- this.cubePrefab = prefab;
- this.checkAllResourcesLoaded();
- });
- });
- }
- update(deltaTime: number) { }
- //检查资源加载
- private checkAllResourcesLoaded(): void {
- this.resourcesLoadedCount++;
- if (this.resourcesLoadedCount === 1) {
- this.initMap();
- }
- }
- // 获取一个随机数
- getRandomInt(min: number, max: number): number {
- min = Math.ceil(min);
- max = Math.floor(max);
- return Math.floor(Math.random() * (max - min + 1)) + min;
- }
- // 根据配置表信息初始化地图
- private initMap(): void {
- this.map = new Array(this.MaxK).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 (this.cubePrefab !== null) {
- let cube = instantiate(this.cubePrefab);
- cube.setPosition(new Vec3((i - 7), 16 - k, (3.05 - j)));
- cube.addComponent(CubeInfo);
- cube.getComponent(CubeInfo).setPos(k, i, j);
- cube.getComponent(CubeInfo).setId(value);
- cube.getComponent(CubeInfo).setColor(config[IdToName[value]].color);
- if (k != 0) {
- cube.getComponentInChildren(MeshRenderer).material.setProperty("albedo", Color.GRAY);
- cube.getComponent(CubeInfo).setLock(true);
- }
- else
- {
- cube.getComponentInChildren(MeshRenderer).material.setProperty("albedo", config[IdToName[value]].color);
- cube.getComponent(CubeInfo).setLock(false);
- }
- this.node.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): void {
- this.map[index] = newMap;
- for (let i = 0; i < this.MaxI; i++) {
- for (let j = 0; j < this.MaxJ; j++) {
- if (newMap[i][j]) {
- // newMap[i][j].setPosition(new Vec3((i - 7), 16 - index, (3.05 - j)));
- // 移动一个方块到新位置
- tween(newMap[i][j])
- .to(0.5, { position: new Vec3((i - 7), 16 - index, (3.05 - j)) }, { easing: 'quadInOut' })
- .start();
- }
- // else {
- // // 如果新地图中某个位置没有节点,确保原来的节点也被销毁或者移除
- // if (this.map[index][i][j]) {
- // this.map[index][i][j].destroy();
- // }
- // }
- }
- }
- }
- //解锁新层后改变颜色,颜色也需伴随移动改变
- public changeColor(index: number) {
- for (let k = 0; 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])
- if (this.map[k - 1][i][j] === null) {
- this.map[k][i][j].getComponentInChildren(MeshRenderer).material.setProperty("albedo", this.map[k][i][j].getComponent(CubeInfo).getColor());
- 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);
- }
- }
- }
- }
- }
- }
- }
|