// 2014.game.js
const clone = require('./clone.js')
function noop() {}
function Game(size, panelWidth) {
if (!(this instanceof Game)) {
return new Game(size, panelWidth)
}
this._panelWidth = panelWidth
// this._init(size)
}
Game.Reload = function({ panelWidth, matrix, size }) {
let game = new Game(size, panelWidth)
game._init(size)
game._matrix = matrix
return game
}
Game.prototype = {
get Value() {
return this._matrix
},
get BlankAni() {
return clone(this._blankAni)
},
_init(size) {
this._size = size
this._cellWidth = this._panelWidth / size
this._matrix = []
this._blankAni = []
let i = size,
animation = wx.createAnimation({ duration: 0 }),
blankAni
animation.translate3d(0, 0, 0).scale3d(1, 1, 1).step()
blankAni = animation.export()
while (i--) {
let j = size,
row = [],
css_row = [],
ani_row = []
while (j--) {
row.push(0)
ani_row.push(blankAni)
}
this._matrix.push(row)
this._blankAni.push(ani_row)
}
},
Add(fn) {
fn = fn || noop
let matrix = this._matrix,
size = this._size,
empty_cells = []
for (let i = 0; i < size; i++) {
for (let j = 0; j < size; j++) {
if (matrix[i][j] === 0) {
empty_cells.push({
r: i,
c: j,
})
}
}
}
if (empty_cells.length) {
let index = Math.floor(Math.random() * empty_cells.length),
random = Math.random(),
value = 2
if (random > 0.84) {
value = 4
if (random > 0.95) {
value = 8
}
}
let empty = empty_cells[index]
matrix[empty.r][empty.c] = value
fn(empty.r, empty.c)
}
},
/**
* 执行动作
* @Author degfy@sina.com
* @DateTime 2017-04-03T10:27:07+0800
*/
Action(direct, step1_opts, step2_opts, voice) {
let animation1 = wx.createAnimation(step1_opts),
animation1_2 = wx.createAnimation({ duration: 0 }),
animation2 = wx.createAnimation(step2_opts),
size = this._size,
cellWidth = this._cellWidth,
ani_m_1 = clone(this._blankAni),
ani_m_1_2 = clone(this._blankAni),
ani_m_2 = clone(this._blankAni),
isValid = false,
score = 0
animation1_2.translate3d(0, 0, 0).scale3d(.1, .1, .1).opacity(.4).step()
animation2.scale3d(1, 1, 1).opacity(1).step()
let animation_reset = animation1_2.export(),
animation_element_gen = animation2.export()
function merge(get, set, fn) {
let score = 0
for (let i = 0; i < size; i++) {
let valueI = get(i)
for (let j = i + 1; j < size; j++) {
let valueJ = get(j)
if (valueJ > 0) {
if (valueI === 0) {
valueI = set(i, valueJ)
set(j, 0)
fn(i, j, false)
isValid = true
continue
} else {
if (valueI === valueJ) {
set(i, valueJ * 2)
set(j, 0)
fn(i, j, true)
isValid = true
score += valueJ * 2
}
break
}
}
}
}
return score
}
switch (direct) {
case 'left':
for (let r = 0; r < size; r++) {
score += merge(index => {
return this._matrix[r][index]
}, (index, value) => {
return this._matrix[r][index] = value
}, (i, j, merged) => {
animation1.translate3d((i - j) * cellWidth, 0, 0).step()
ani_m_1[r][j] = animation1.export()
if (merged) {
ani_m_1_2[r][i] = animation_reset
ani_m_2[r][i] = animation_element_gen
}
})
}
break
case 'right':
for (let r = 0; r < size; r++) {
score += merge(index => {
return this._matrix[r][size - index - 1]
}, (index, value) => {
return this._matrix[r][size - index - 1] = value
}, (i, j, merged) => {
animation1.translate3d((j - i) * cellWidth, 0, 0).step()
ani_m_1[r][size - j - 1] = animation1.export()
if (merged) {
ani_m_1_2[r][size - i - 1] = animation_reset
ani_m_2[r][size - i - 1] = animation_element_gen
}
})
}
break
case 'up':
for (let c = 0; c < size; c++) {
score += merge(index => {
return this._matrix[index][c]
}, (index, value) => {
return this._matrix[index][c] = value
}, (i, j, merged) => {
animation1.translate3d(0, (i - j) * cellWidth, 0).step()
ani_m_1[j][c] = animation1.export()
if (merged) {
ani_m_1_2[i][c] = animation_reset
ani_m_2[i][c] = animation_element_gen
}
})
}
break
case 'down':
for (let c = 0; c < size; c++) {
score += merge(index => {
return this._matrix[size - index - 1][c]
}, (index, value) => {
return this._matrix[size - index - 1][c] = value
}, (i, j, merged) => {
animation1.translate3d(0, (j - i) * cellWidth, 0).step()
ani_m_1[size - j - 1][c] = animation1.export()
if (merged) {
ani_m_1_2[size - i - 1][c] = animation_reset
ani_m_2[size - i - 1][c] = animation_element_gen
}
})
}
break
}
if (isValid) {
this.Add((r, c) => {
ani_m_2[r][c] = animation_element_gen
ani_m_1_2[r][c] = animation_reset
})
if (voice) {
let filePath
if (score) {
filePath = voice.merge
} else {
filePath = voice.move
}
// console.log(voice)
if (filePath) {
wx.stopVoice()
wx.playVoice({
filePath,
// complete(res) {
// console.log(res)
// }
})
}
}
}
return [ani_m_1, ani_m_1_2, ani_m_2, score]
},
Reset() {
let size = this._size,
i = size
while (i--) {
let j = size
while (j--) {
this._matrix[i][j] = 0
}
}
this.Add()
return this._matrix
},
SetMode(mode) {
this._init(mode)
this.Add()
return this._matrix
},
}
module.exports = Game
HappyGirl快乐女孩
- 粉丝: 1w+
- 资源: 4152
最新资源
- 电动汽车空调制冷系统电动压缩机匹配分析1.pdf
- 二氧化碳汽车空调系统设计及研究1.pdf
- 电动汽车驱动系统散热设计与试验验证.pdf
- McQuayDuctSizer(麦克维尔风管尺寸计算软件).zip
- 麦克维尔温湿度分析仪McQuayPsychrometricAnalyzer(hvac-eng.com).zip
- 麦克维尔管道测量仪McQuaypipesizer .zip
- 信捷XC系列PLC主从通讯程序
- 基于蒙特卡洛的电动汽车充电负荷生成
- 基于遗传算法的电动汽车有序充电优化调度 软件:Matlab 利用遗传算法对电动汽车有序充电进行优化;优化目标包括充电费用最低,充电时间达到要求(电动汽车充到足够的电)考虑电动汽车充电对电网负荷的影响
- FPGA 全部verilog代码实现I2C口master端口应用场景 1、FPGA通过I2C口配置TFP410MP 2、EDID配置,FPGA通过I2C口配置AT24C02 AT24C64; 访问地
- Matlab simulink 基于光伏和蓄电池的三端口
- FFT STM32+apFFT程序源代码+lunwen资料+教程讲解 适用于STM32F103平台,使用AD7606同步采集两路正弦信号,内置1024点全相位快速傅里叶变(apFFT)算法,直接计算
- 两电平svpwm算法verilog程序
- 基于改进的快速粒子群有源配电网动态无功优化 软件:Matlab 介绍:在含分布式电源的IEEE33进行无功优化,以无功最优和运行费用最优为目标函数进行优化,采用改进的快速粒子群算法进行计算
- 混合型APF,HAPF,电力牵引系统电能质量控制,高铁谐波补偿,高铁无功补偿,混合型有源电力滤波器,单相SVG,SVG,电力牵引系统谐波无功补偿
- 单机无穷大系统静态稳定性仿真模型
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
前往页