开始生成地图 效果如下。
修改 puzzle_date_controller.js
开始 随机生成地图
import defines from './defines' const PuzzleDateControoler = function () { let that = {}; const getRandomValue = function () { let random = parseInt(Math.random() * defines.PuzzleLineValueList.length);// 0-2 return defines.PuzzleLineValue[defines.PuzzleLineValueList[random]]; }; that.getGameData = function () { //得到一组,游戏数据 let list = []; for (let i = 0; i < 2; i++) { for (let j = 0; j < 3; j++) { let cellData = {}; list.push(cellData); // cellData[defines.DirectionMap.UP] = defines.PuzzleLineValue.OUT; //先设置成都是出来的 // cellData[defines.DirectionMap.DOWN] = defines.PuzzleLineValue.OUT; //都是出来的 // cellData[defines.DirectionMap.LEFT] = defines.PuzzleLineValue.OUT; // cellData[defines.DirectionMap.RIGHT] = defines.PuzzleLineValue.OUT; //开始 生成里面的部分 先右边 cellData[defines.DirectionMap.RIGHT] = getRandomValue(); //生成下边缘 cellData[defines.DirectionMap.DOWN] = getRandomValue(); //然后根据数组长度 来计算左边缘和上边缘 if (list.length > 1) { //计算左边缘 //取出列表中的上一个块 let nextPuzzle = list[list.length-2]; cellData[defines.DirectionMap.LEFT]= 0- nextPuzzle[defines.DirectionMap.RIGHT] } if(list.length>3){ //计算上边缘, let nextPuzzle= list[list.length-4]; cellData[defines.DirectionMap.UP] = 0- nextPuzzle[defines.DirectionMap.DOWN] } //开始生成地图 //首先生成边缘的边 //上边缘 if (i == 0) { cellData[defines.DirectionMap.UP] = defines.PuzzleLineValue.MID; } //下边缘 if (i == 1) { cellData[defines.DirectionMap.DOWN] = defines.PuzzleLineValue.MID; } //左边缘 if (j == 0) { cellData[defines.DirectionMap.LEFT] = defines.PuzzleLineValue.MID; } //右边缘 if (j == 2) { cellData[defines.DirectionMap.RIGHT] = defines.PuzzleLineValue.MID; } } } return list; }; return that; }; export default PuzzleDateControoler;
修改defines.js
//碎片四个边的方向以及突起值,我们来写成宏定义,方便 左右的类 都 访问到 const defines = {}; defines.DirectionList = [ "UP", "DOWN", "LEFT", "RIGHT"]; defines.DirectionMap = { "UP": 1, "DOWN": 2, "LEFT": 3, "RIGHT": 4 }; defines.PuzzleLineValueList = ["IN","OUT","MID"]; defines.PuzzleLineValue = { IN: -1, //进去的 OUT: 1, //出来的 MID: 0 //平的 }; export default defines
站长微信:xiaomao0055
站长QQ:14496453