mirror of
https://github.com/Binaryify/vue-tetris.git
synced 2026-01-21 05:57:57 +08:00
remove immutable.js
This commit is contained in:
@@ -7,7 +7,6 @@ import Number from './components/number/index.vue'
|
||||
import Point from './components/point/index.vue'
|
||||
import Keyboard from './components/keyboard/index.vue'
|
||||
import Logo from './components/logo/index.vue'
|
||||
import { List } from 'immutable'
|
||||
import Matrix from './components/matrix/index.vue'
|
||||
import { mapState } from 'vuex'
|
||||
import { transform, lastRecord, speeds, i18n, lan } from './unit/const'
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { List, fromJS } from 'immutable'
|
||||
import { isClear } from '../../unit/'
|
||||
import { fillLine, blankLine } from '../../unit/const'
|
||||
import states from '../../control/states'
|
||||
@@ -20,7 +19,7 @@ export default {
|
||||
} else {
|
||||
matrix = this.getResult()
|
||||
}
|
||||
matrix = matrix.toJS()
|
||||
|
||||
return (
|
||||
<div class="matrix">
|
||||
{matrix.map((p, k1) =>
|
||||
@@ -62,43 +61,41 @@ export default {
|
||||
}
|
||||
const cur = props.cur
|
||||
const shape = cur && cur.shape
|
||||
const xy = fromJS(cur && cur.xy)
|
||||
let matrix = fromJS(props.propMatrix)
|
||||
const xy = cur && cur.xy
|
||||
let matrix = JSON.parse(JSON.stringify(props.propMatrix))
|
||||
const clearLines = this.clearLines
|
||||
if (clearLines) {
|
||||
const animateColor = this.animateColor
|
||||
clearLines.forEach(index => {
|
||||
matrix = matrix.set(
|
||||
index,
|
||||
List([
|
||||
animateColor,
|
||||
animateColor,
|
||||
animateColor,
|
||||
animateColor,
|
||||
animateColor,
|
||||
animateColor,
|
||||
animateColor,
|
||||
animateColor,
|
||||
animateColor,
|
||||
animateColor
|
||||
])
|
||||
)
|
||||
matrix[index]=[
|
||||
animateColor,
|
||||
animateColor,
|
||||
animateColor,
|
||||
animateColor,
|
||||
animateColor,
|
||||
animateColor,
|
||||
animateColor,
|
||||
animateColor,
|
||||
animateColor,
|
||||
animateColor
|
||||
]
|
||||
|
||||
})
|
||||
} else if (shape) {
|
||||
shape.forEach((m, k1) =>
|
||||
m.forEach((n, k2) => {
|
||||
if (n && xy.get(0) + k1 >= 0) {
|
||||
if (n && xy[0] + k1 >= 0) {
|
||||
// 竖坐标可以为负
|
||||
let line = matrix.get(xy.get(0) + k1)
|
||||
let line = matrix[xy[0]+k1]
|
||||
let color
|
||||
if (line.get(xy.get(1) + k2) === 1 && !clearLines) {
|
||||
if (line[xy[1] + k2] === 1 && !clearLines) {
|
||||
// 矩阵与方块重合
|
||||
color = 2
|
||||
} else {
|
||||
color = 1
|
||||
}
|
||||
line = line.set(xy.get(1) + k2, color)
|
||||
matrix = matrix.set(xy.get(0) + k1, line)
|
||||
line[xy[1] + k2]=color
|
||||
matrix[xy[0] + k1]=line
|
||||
}
|
||||
})
|
||||
)
|
||||
@@ -129,17 +126,19 @@ export default {
|
||||
},
|
||||
over(nextProps) {
|
||||
let overState = this.getResult(nextProps)
|
||||
this.overState = overState
|
||||
|
||||
this.overState = JSON.parse(JSON.stringify(overState))
|
||||
const exLine = index => {
|
||||
if (index <= 19) {
|
||||
overState = overState.set(19 - index, List(fillLine))
|
||||
overState[19 - index]=fillLine
|
||||
} else if (index >= 20 && index <= 39) {
|
||||
overState = overState.set(index - 20, List(blankLine))
|
||||
overState[index - 20]=blankLine
|
||||
} else {
|
||||
states.overEnd()
|
||||
return
|
||||
}
|
||||
this.overState = overState
|
||||
this.overState = JSON.parse(JSON.stringify(overState))
|
||||
// console.log(JSON.stringify(overState))
|
||||
}
|
||||
|
||||
for (let i = 0; i <= 40; i++) {
|
||||
|
||||
@@ -7,7 +7,6 @@ import {
|
||||
clearPoints,
|
||||
eachLines
|
||||
} from '../unit/const'
|
||||
const { fromJS, List } = require('immutable')
|
||||
import { music } from '../unit/music'
|
||||
|
||||
const getStartMatrix = startLines => {
|
||||
@@ -26,27 +25,27 @@ const getStartMatrix = startLines => {
|
||||
line.splice(index, 0, 0)
|
||||
}
|
||||
|
||||
return List(line)
|
||||
return line
|
||||
}
|
||||
let startMatrix = List([])
|
||||
let startMatrix = []
|
||||
|
||||
for (let i = 0; i < startLines; i++) {
|
||||
if (i <= 2) {
|
||||
// 0-3
|
||||
startMatrix = startMatrix.push(getLine(5, 8))
|
||||
startMatrix.push(getLine(5, 8))
|
||||
} else if (i <= 6) {
|
||||
// 4-6
|
||||
startMatrix = startMatrix.push(getLine(4, 9))
|
||||
startMatrix.push(getLine(4, 9))
|
||||
} else {
|
||||
// 7-9
|
||||
startMatrix = startMatrix.push(getLine(3, 9))
|
||||
startMatrix.push(getLine(3, 9))
|
||||
}
|
||||
}
|
||||
for (let i = 0, len = 20 - startLines; i < len; i++) {
|
||||
// 插入上部分的灰色
|
||||
startMatrix = startMatrix.unshift(List(blankLine))
|
||||
startMatrix.unshift(blankLine)
|
||||
}
|
||||
return startMatrix.toJS()
|
||||
return startMatrix
|
||||
}
|
||||
|
||||
const states = {
|
||||
@@ -82,16 +81,16 @@ const states = {
|
||||
store.commit('moveBlock', next)
|
||||
states.fallInterval = setTimeout(fall, speeds[state.speedRun - 1])
|
||||
} else {
|
||||
let matrix = fromJS(state.matrix)
|
||||
let matrix = JSON.parse(JSON.stringify(state.matrix))
|
||||
const shape = cur && cur.shape
|
||||
const xy = fromJS(cur && cur.xy)
|
||||
const xy = cur && cur.xy
|
||||
shape.forEach((m, k1) =>
|
||||
m.forEach((n, k2) => {
|
||||
if (n && xy.get(0) + k1 >= 0) {
|
||||
if (n && xy[0] + k1 >= 0) {
|
||||
// 竖坐标可以为负
|
||||
let line = matrix.get(xy.get(0) + k1)
|
||||
line = line.set(xy.get(1) + k2, 1)
|
||||
matrix = matrix.set(xy.get(0) + k1, line)
|
||||
let line = matrix[xy[0] + k1]
|
||||
line[xy[1] + k2]=1
|
||||
matrix[xy[0] + k1]=line
|
||||
}
|
||||
})
|
||||
)
|
||||
@@ -165,12 +164,13 @@ const states = {
|
||||
// 消除行
|
||||
clearLines: (matrix, lines) => {
|
||||
const state = store.state
|
||||
let newMatrix = fromJS(matrix)
|
||||
let newMatrix = JSON.parse(JSON.stringify(matrix))
|
||||
lines.forEach(n => {
|
||||
newMatrix = newMatrix.splice(n, 1)
|
||||
newMatrix = newMatrix.unshift(List(blankLine))
|
||||
newMatrix.splice(n, 1)
|
||||
// newMatrix = newMatrix.unshift(List(blankLine))
|
||||
newMatrix.unshift(blankLine)
|
||||
})
|
||||
store.commit('matrix', newMatrix.toJS())
|
||||
store.commit('matrix', newMatrix)
|
||||
store.commit('moveBlock', { type: state.next })
|
||||
store.commit('nextBlock', '')
|
||||
states.auto()
|
||||
|
||||
@@ -2,7 +2,6 @@ import { want } from '../../unit/'
|
||||
import event from '../../unit/event'
|
||||
import states from '../states'
|
||||
import { music } from '../../unit/music'
|
||||
import { fromJS, List } from 'immutable'
|
||||
const down = store => {
|
||||
store.commit('key_down', true)
|
||||
if (store.state.cur !== null) {
|
||||
@@ -32,16 +31,17 @@ const down = store => {
|
||||
// store.dispatch(actions.moveBlock(next));
|
||||
states.auto()
|
||||
} else {
|
||||
let matrix = fromJS(state.matrix)
|
||||
let matrix =JSON.parse(JSON.stringify( state.matrix))
|
||||
const shape = cur.shape
|
||||
const xy = fromJS(cur.xy)
|
||||
const xy = cur.xy
|
||||
shape.forEach((m, k1) =>
|
||||
m.forEach((n, k2) => {
|
||||
if (n && xy.get(0) + k1 >= 0) {
|
||||
if (n && xy[0] + k1 >= 0) {
|
||||
// 竖坐标可以为负
|
||||
let line = matrix.get(xy.get(0) + k1)
|
||||
line = line.set(xy.get(1) + k2, 1)
|
||||
matrix = matrix.set(xy.get(0) + k1, line)
|
||||
let line = matrix[xy[0] + k1]
|
||||
line[xy[1] + k2]=1
|
||||
|
||||
matrix[xy[0] + k1]=line
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
@@ -5,6 +5,7 @@ import { music } from '../../unit/music'
|
||||
const down = store => {
|
||||
store.commit('key_rotate', true)
|
||||
if (store.state.cur !== null) {
|
||||
console.log("rotate")
|
||||
event.down({
|
||||
key: 'rotate',
|
||||
once: true,
|
||||
|
||||
@@ -2,7 +2,6 @@ import { want } from '../../unit/'
|
||||
import event from '../../unit/event'
|
||||
import states from '../states'
|
||||
import { music } from '../../unit/music'
|
||||
import { fromJS, List } from 'immutable'
|
||||
const down = store => {
|
||||
store.commit('key_drop', true)
|
||||
event.down({
|
||||
@@ -29,7 +28,7 @@ const down = store => {
|
||||
bottom = cur.fall(index)
|
||||
index++
|
||||
}
|
||||
let matrix = fromJS(state.matrix)
|
||||
let matrix =JSON.parse(JSON.stringify( state.matrix))
|
||||
bottom = cur.fall(index - 2)
|
||||
store.commit('moveBlock', bottom)
|
||||
const shape = bottom.shape
|
||||
@@ -38,9 +37,9 @@ const down = store => {
|
||||
m.forEach((n, k2) => {
|
||||
if (n && xy[0] + k1 >= 0) {
|
||||
// 竖坐标可以为负
|
||||
let line = matrix.get(xy[0] + k1)
|
||||
line = line.set(xy[1] + k2, 1)
|
||||
matrix = matrix.set(xy[0] + k1, line)
|
||||
let line = matrix[xy[0] + k1]
|
||||
line[xy[1] + k2]=1
|
||||
matrix[xy[0] + k1]=line
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { List, fromJS } from 'immutable'
|
||||
import { blockShape, origin } from './const'
|
||||
|
||||
class Block {
|
||||
@@ -54,16 +53,18 @@ class Block {
|
||||
}
|
||||
}
|
||||
rotate() {
|
||||
const shape = fromJS(this.shape)
|
||||
let result = List([])
|
||||
const shape = this.shape
|
||||
let result = []
|
||||
shape.forEach(m =>
|
||||
m.forEach((n, k) => {
|
||||
const index = m.size - k - 1
|
||||
if (result.get(index) === undefined) {
|
||||
result = result.set(index, List([]))
|
||||
const index = m.length - k - 1
|
||||
if (result[index] === undefined) {
|
||||
result[index]=[]
|
||||
}
|
||||
const tempK = result.get(index).push(n)
|
||||
result = result.set(index, tempK)
|
||||
|
||||
result[index].push(n)
|
||||
const tempK = JSON.parse(JSON.stringify(result[index]))
|
||||
result[index]=tempK
|
||||
})
|
||||
)
|
||||
const nextXy = [
|
||||
@@ -74,7 +75,7 @@ class Block {
|
||||
? 0
|
||||
: this.rotateIndex + 1
|
||||
return {
|
||||
shape: result.toJS(),
|
||||
shape: result,
|
||||
type: this.type,
|
||||
xy: nextXy,
|
||||
rotateIndex: nextRotateIndex,
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { blockType, StorageKey } from './const'
|
||||
import { fromJS, List } from 'immutable'
|
||||
const hiddenProperty = (() => {
|
||||
// document[hiddenProperty] 可以判断页面是否失焦
|
||||
let names = ['hidden', 'webkitHidden', 'mozHidden', 'msHidden']
|
||||
@@ -13,11 +12,10 @@ const unit = {
|
||||
return blockType[Math.floor(Math.random() * len)]
|
||||
},
|
||||
want(next, matrix) {
|
||||
matrix = fromJS(matrix)
|
||||
// 方块是否能移到到指定位置
|
||||
const xy = next.xy
|
||||
const shape = fromJS(next.shape)
|
||||
const horizontal = shape.get(0).size
|
||||
const shape = next.shape
|
||||
const horizontal = shape[0].length
|
||||
return shape.every((m, k1) =>
|
||||
m.every((n, k2) => {
|
||||
if (xy[1] < 0) {
|
||||
@@ -37,7 +35,7 @@ const unit = {
|
||||
return false
|
||||
}
|
||||
if (n) {
|
||||
if (matrix.get(xy[0] + k1).get(xy[1] + k2)) {
|
||||
if (matrix[xy[0] + k1][xy[1] + k2]) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
@@ -61,9 +59,7 @@ const unit = {
|
||||
},
|
||||
isOver(matrix) {
|
||||
// 游戏是否结束, 第一行落下方块为依据
|
||||
if (List.isList(matrix)) {
|
||||
matrix = matrix.toJS()
|
||||
}
|
||||
|
||||
return matrix[0].some(n => !!n)
|
||||
},
|
||||
subscribeRecord(store) {
|
||||
|
||||
@@ -6,7 +6,6 @@ import { isFocus } from '../unit/'
|
||||
import { blankMatrix, lastRecord, maxPoint, blockType } from '../unit/const'
|
||||
import Block from '../unit/block'
|
||||
import { hasWebAudioAPI } from '../unit/music'
|
||||
const { fromJS, List } = require('immutable')
|
||||
Vue.use(Vuex)
|
||||
|
||||
let clearLinesInitState = lastRecord &&
|
||||
|
||||
Reference in New Issue
Block a user