Commit a4a9ea8b authored by huangwenjie's avatar huangwenjie

1

parent 1a1c751b
export const noBlockLeft = (x, y, yl, beforeArr) => {
if (y > yl + 1) {
for (let t = yl - 1; t < y; t++) {
if (beforeArr[x][y] !== 0) {
return false
}
}
}
if (y < yl + 1) {
for (let t = y - 1; t < yl; t++) {
if (beforeArr[x][y] !== 0) {
return false
}
}
}
return true
}
export const noBlockRight = (x, y, yr, beforeArr) => {
if (yr > y + 1) {
for (let t = y - 1; t < yr; t++) {
if (beforeArr[x][y] !== 0) {
return false
}
}
}
if (yr < y + 1) {
for (let t = yr - 1; t < y; t++) {
if (beforeArr[x][y] !== 0) {
return false
}
}
}
return true
}
export const noBlockTop = (x, xt, y, beforeArr) => {
if (x > xt + 1) {
for (let t = xt - 1; t < x; t++) {
if (beforeArr[x][y] !== 0) {
return false
}
}
}
if (x < xt + 1) {
for (let t = x - 1; t < xt; t++) {
if (beforeArr[x][y] !== 0) {
return false
}
}
}
return true
}
export const noBlockBottom = (x, xb, y, beforeArr) => {
if (xb > x + 1) {
for (let t = x - 1; t < xb; t++) {
if (beforeArr[x][y] !== 0) {
return false
}
}
}
if (xb < x + 1) {
for (let t = x - 1; t < xb; t++) {
if (beforeArr[x][y] !== 0) {
return false
}
}
}
return true
}
\ No newline at end of file
...@@ -17,55 +17,58 @@ export class GameEle extends Sprite { ...@@ -17,55 +17,58 @@ export class GameEle extends Sprite {
switch (eleType + "") { switch (eleType + "") {
case "2": case "2":
this.texture = RES.getRes("ele2.png") this.texture = RES.getRes("ele2.png")
this.x = 5 this.x = 10
this.y = -5 this.y = 20
break break
case "4": case "4":
this.texture = RES.getRes("ele4.png") this.texture = RES.getRes("ele4.png")
this.x = -10 this.x = -2
this.y = -5 this.y = 20
break break
case "8": case "8":
this.texture = RES.getRes("ele8.png") this.texture = RES.getRes("ele8.png")
this.y = -5 this.x = 5
this.y = 20
break break
case "16": case "16":
this.texture = RES.getRes("ele16.png") this.texture = RES.getRes("ele16.png")
this.x = 5 this.x = 12
this.y = -15 this.y = 15
break break
case "32": case "32":
this.texture = RES.getRes("ele32.png") this.texture = RES.getRes("ele32.png")
this.y = -10 this.x = 8
this.y = 20
break break
case "64": case "64":
this.texture = RES.getRes("ele64.png") this.texture = RES.getRes("ele64.png")
this.y = -15 this.x = 8
this.y = 16
break break
case "128": case "128":
this.texture = RES.getRes("ele128.png") this.texture = RES.getRes("ele128.png")
this.x = 5 this.x = 8
this.y = -10 this.y = 20
break break
case "256": case "256":
this.texture = RES.getRes("ele256.png") this.texture = RES.getRes("ele256.png")
this.x = 5 this.x = 8
this.y = -10 this.y = 20
break break
case "512": case "512":
this.texture = RES.getRes("ele512.png") this.texture = RES.getRes("ele512.png")
this.x = 5 this.x = 8
this.y = -10 this.y = 20
break break
case "1024": case "1024":
this.texture = RES.getRes("ele1024.png") this.texture = RES.getRes("ele1024.png")
this.x = 10 this.x = 10
this.y = -10 this.y = 20
break break
case "2048": case "2048":
this.texture = RES.getRes("ele2048.png") this.texture = RES.getRes("ele2048.png")
this.x = 5 this.x = 8
this.y = -10 this.y = 20
break break
default: default:
this.texture = Texture.EMPTY this.texture = Texture.EMPTY
......
import { noBlockLeft, noBlockRight, noBlockTop, noBlockBottom } from './Check';
import { DisplayObject } from './../../../engine/2d/display/DisplayObject'; import { DisplayObject } from './../../../engine/2d/display/DisplayObject';
import { GDispatcher } from './../../../engine/2d/events/GDispatcher'; import { GDispatcher } from './../../../engine/2d/events/GDispatcher';
import { Button } from './../../../engine/2d/ui/Button'; import { Button } from './../../../engine/2d/ui/Button';
...@@ -87,24 +88,77 @@ export class PlayScene extends Scene { ...@@ -87,24 +88,77 @@ export class PlayScene extends Scene {
for (let x = 0; x < 4; x++) { for (let x = 0; x < 4; x++) {
this.board[x] = [] this.board[x] = []
this.containerPool[x] = [] this.containerPool[x] = []
let py = 615 + x * 139 let py = 596 + x * 136
for (let y = 0; y < 4; y++) { for (let y = 0; y < 4; y++) {
let num = 0 let num = 0
this.board[x][y] = num this.board[x][y] = num
let px = 110 + y * 138 let px = 104 + y * 138
let container = new Container(); let container = new Container();
container.x = px container.x = px
container.y = py container.y = py
container["idx"] = x + y + "" // container["idx"] = x + y + ""
container['cx'] = x
container['cy'] = y
this.addChild(container); this.addChild(container);
container.addChild(new GameEle(num)) container.addChild(new GameEle(num))
this.containerPool[x][y] = container this.containerPool[x][y] = container
} }
} }
this.exchangeTarget1 = new Sprite(RES.getRes("focusTips.png"))
this.exchangeTarget2 = new Sprite(RES.getRes("focusTips.png"))
if (!localStorage.getItem("game2048Guide")) {
localStorage.setItem("game2048Guide", "true")
this.board[1][1] = 2
this.board[1][3] = 2
this.renderBoard()
this.initGuide()
} else {
this.generateOneElement() this.generateOneElement()
this.generateOneElement() this.generateOneElement()
} }
}
guideMask
initGuide() {
this.guideMask = new Graphics();
this.guideMask.mouseChildren = true
this.guideMask.mouseEnable = true
this.guideMask.beginFill(0x000000, 0.6);
this.guideMask.moveTo(0, 0)
this.guideMask.lineTo(750, 0)
this.guideMask.lineTo(750, 586)
this.guideMask.lineTo(94, 586)
this.guideMask.lineTo(94, 1140)
this.guideMask.lineTo(656, 1140)
this.guideMask.lineTo(656, 586)
this.guideMask.lineTo(750, 586)
this.guideMask.lineTo(750, 1624)
this.guideMask.lineTo(0, 1624)
this.guideMask.lineTo(0, 0)
this.guideMask.endFill();
this.addChild(this.guideMask)
let arrow = new Sprite(RES.getRes("guideArrow.png"))
arrow.x = 350
arrow.y = 900
this.guideMask.addChild(arrow)
let finger = new Sprite(RES.getRes("guideFinger.png"))
finger.x = 325
finger.y = 900
this.guideMask.addChild(finger)
Tween.get(finger, { loop: true })
.to({ x: 375 }, 1000)
let tips = this.drawTxt("滑动屏幕让2个相同食物碰撞", 250, 1000)
this.guideMask.addChild(tips)
}
updateScoreTxt() { updateScoreTxt() {
this.scoreTxt.text = this._score + "" this.scoreTxt.text = this._score + ""
...@@ -113,21 +167,382 @@ export class PlayScene extends Scene { ...@@ -113,21 +167,382 @@ export class PlayScene extends Scene {
initEvents() { initEvents() {
super.initEvents() super.initEvents()
this.addEventListener(MouseEvent.MOUSE_DOWN, this.onDown_this, this) this.addEventListener(MouseEvent.MOUSE_DOWN, this.onDown_this, this)
this.addEventListener(MouseEvent.CLICK, this.onClick_exchangeBtn, this) GDispatcher.addEvent("updateProp", this.updateProp, this)
this.addEventListener(MouseEvent.CLICK, this.onClick_bombBtn, this) this.exchangeBtn.addEventListener(MouseEvent.CLICK, this.onClick_exchangeBtn, this)
this.addEventListener(MouseEvent.CLICK, this.onClick_filterBtn, this) this.bombBtn.addEventListener(MouseEvent.CLICK, this.onClick_bombBtn, this)
this.filterBtn.addEventListener(MouseEvent.CLICK, this.onClick_filterBtn, this)
}
_bombCount: number
_exchangeCount: number
_netCount: number
updateProp(bombCount: number, exchangeCount: number, netCount: number) {
if (bombCount == 0 || bombCount !== this._bombCount) {
this._bombCount = bombCount
}
if (exchangeCount == 0 || exchangeCount !== this._exchangeCount) {
this._exchangeCount = exchangeCount
} }
if (netCount == 0 || netCount !== this._netCount) {
this._netCount = netCount
}
}
exchangeTarget1
exchangeTarget2
onClick_exchangeBtn() { onClick_exchangeBtn() {
if (this.exchanging) {
return
}
this.exchangeBtn.mouseEnable = false
this.showExchangeMask()
this.onExchanging()
}
exchangeMask
showExchangeMask() {
this.exchangeMask = new Graphics();
this.exchangeMask.mouseChildren = true
this.exchangeMask.mouseEnable = true
this.exchangeMask.beginFill(0x000000, 0.6);
this.exchangeMask.moveTo(0, 0)
this.exchangeMask.lineTo(750, 0)
this.exchangeMask.lineTo(750, 586)
this.exchangeMask.lineTo(94, 586)
this.exchangeMask.lineTo(94, 1140)
this.exchangeMask.lineTo(656, 1140)
this.exchangeMask.lineTo(656, 586)
this.exchangeMask.lineTo(750, 586)
this.exchangeMask.lineTo(750, 1624)
this.exchangeMask.lineTo(0, 1624)
this.exchangeMask.lineTo(0, 0)
this.exchangeMask.beginHole()
this.exchangeMask.drawCircle(30, 1000, 100)
this.exchangeMask.endHole()
this.exchangeMask.endFill();
this.addChild(this.exchangeMask)
let tips = new Sprite(RES.getRes("exchangeTips.png"))
tips.x = 140
tips.y = 1140
this.exchangeMask.addChild(tips)
let func
this.exchangeMask.addEventListener(MouseEvent.CLICK, func = () => {
this.exchangeBtn.mouseEnable = true
this.removeChild(this.exchangeMask)
this.removeAllClick()
this.removeAnis()
for (let i = 0; i < this.exchangeTargetPool.length; i++) {
if (this.exchangeTargetPool[i].children.length > 1) {
this.exchangeTargetPool[i].removeChild(this.exchangeTargetPool[i].children[1])
}
}
this.exchanging = false
this.exchangeMask.removeEventListener(MouseEvent.CLICK, func, this)
}, this)
}
exchangeTargetPool
onExchanging() {
this.exchanging = true
this.exchangeTargetPool = []
let t = 1
for (let x = 0; x < 4; x++) {
for (let y = 0; y < 4; y++) {
if (this.board[x][y] > 0) {
this.containerPool[x][y].addEventListener(MouseEvent.CLICK, (e) => {
if (this.exchangeTargetPool.length > 0) {
let element = this.exchangeTargetPool[0]
if (this.containerPool[x][y] == element) {
// 选中同一个
this.containerPool[x][y].removeChild(this.containerPool[x][y].exchangeTips)
this.exchangeTargetPool.shift()
this.removeAnis()
return
}
}
this.exchangeTargetPool.push(this.containerPool[x][y])
this.containerPool[x][y].addChild(this['exchangeTarget' + t])
this.containerPool[x][y].exchangeTips = this['exchangeTarget' + t]
t++
if (t > 2) {
t = 1
}
this.checkExchange()
}, this)
}
}
}
}
async checkExchange() {
if (this.exchangeTargetPool.length >= 2) {
let num0 = this.board[this.exchangeTargetPool[0].cx][this.exchangeTargetPool[0].cy]
let num1 = this.board[this.exchangeTargetPool[1].cx][this.exchangeTargetPool[1].cy]
this.removeAnis()
// 交换动效
await this.exchangeAni()
// if (num0 == num1) {
// console.log("选中相同元素");
// this.exchangeTargetPool[0].removeChild(this.exchangeTargetPool[0].exchangeTips)
// this.exchangeTargetPool[1].removeChild(this.exchangeTargetPool[1].exchangeTips)
// this.removeAllClick()
// this.exchangeTargetPool = []
// this.exchanging = false
// return
// }
// 进行扣道具
let tem = num0
num0 = num1
num1 = tem
this.board[this.exchangeTargetPool[0].cx][this.exchangeTargetPool[0].cy] = num0
this.board[this.exchangeTargetPool[1].cx][this.exchangeTargetPool[1].cy] = num1
this.renderBoard()
this.removeChild(this.exchangeMask)
this.removeAllClick()
this.exchangeTargetPool = []
this.exchanging = false
this.exchangeBtn.mouseEnable = true
if (!this.isGameWin())
this.isGameOver()
} else if (this.exchangeTargetPool.length = 1) {
for (let x = 0; x < 4; x++) {
for (let y = 0; y < 4; y++) {
if (this.board[x][y] > 0 && !(this.exchangeTargetPool[0].cx === x && this.exchangeTargetPool[0].cy === y)) {
this.containerPool[x][y].children[0].anchor.set(60, 60)
Tween.get(this.containerPool[x][y].children[0], { loop: true })
.to({ rotation: 15 }, 50)
.to({ rotation: -15 }, 100)
.to({ rotation: 0 }, 50)
}
}
}
}
}
exchangeAni() {
this.mouseEnable = false
this.mouseChildren = false
return new Promise((r, j) => {
this.exchangeTargetPool[0].children[0].visible = false
this.exchangeTargetPool[1].children[0].visible = false
let num0 = this.board[this.exchangeTargetPool[0].cx][this.exchangeTargetPool[0].cy]
let num1 = this.board[this.exchangeTargetPool[1].cx][this.exchangeTargetPool[1].cy]
let sprite0 = new GameEle(num0)
sprite0.x += this.exchangeTargetPool[0].x
sprite0.y += this.exchangeTargetPool[0].y
this.addChild(sprite0)
let sprite1 = new GameEle(num1)
sprite1.x += this.exchangeTargetPool[1].x
sprite1.y += this.exchangeTargetPool[1].y
this.addChild(sprite1)
Tween.get(sprite0)
.to({ x: sprite1.x, y: sprite1.y }, 500)
Tween.get(sprite1)
.to({ x: sprite0.x, y: sprite0.y }, 500)
.call(() => {
this.removeChild(sprite0)
this.removeChild(sprite1)
this.mouseEnable = true
this.mouseChildren = true
r()
})
})
}
bombMask
showBombMask() {
this.bombMask = new Graphics();
this.bombMask.mouseChildren = true
this.bombMask.mouseEnable = true
this.bombMask.beginFill(0x000000, 0.6);
this.bombMask.moveTo(0, 0)
this.bombMask.lineTo(750, 0)
this.bombMask.lineTo(750, 586)
this.bombMask.lineTo(94, 586)
this.bombMask.lineTo(94, 1140)
this.bombMask.lineTo(656, 1140)
this.bombMask.lineTo(656, 586)
this.bombMask.lineTo(750, 586)
this.bombMask.lineTo(750, 1624)
this.bombMask.lineTo(0, 1624)
this.bombMask.lineTo(0, 0)
this.bombMask.beginHole()
this.bombMask.drawCircle(30, 1000, 100)
this.bombMask.endHole()
this.bombMask.endFill();
this.addChild(this.bombMask)
let tips = new Sprite(RES.getRes("bombTips.png"))
tips.x = 280
tips.y = 1140
this.bombMask.addChild(tips)
let func
this.bombMask.addEventListener(MouseEvent.CLICK, func = () => {
this.bombBtn.mouseEnable = true
console.log(123)
this.removeChild(this.bombMask)
this.bombing = false
this.bombMask.removeEventListener(MouseEvent.CLICK, func, this)
}, this)
} }
onClick_bombBtn() { onClick_bombBtn() {
this.bombing = true
this.bombBtn.mouseEnable = false
this.showBombMask()
for (let x = 0; x < 4; x++) {
for (let y = 0; y < 4; y++) {
if (this.board[x][y] > 0) {
this.containerPool[x][y].addEventListener(MouseEvent.CLICK, (e) => {
// 炸弹动效
this.board[x][y] = 0
this.renderBoard()
this.removeAllClick()
this.removeChild(this.bombMask)
this.bombBtn.mouseEnable = true
this.bombing = false
}, this)
}
}
}
}
filterMask
doFilterBtn
showFilterMask() {
this.filterMask = new Graphics();
this.filterMask.mouseChildren = true
this.filterMask.mouseEnable = true
this.filterMask.beginFill(0x000000, 0.6);
this.filterMask.moveTo(0, 0)
this.filterMask.lineTo(750, 0)
this.filterMask.lineTo(750, 586)
this.filterMask.lineTo(94, 586)
this.filterMask.lineTo(94, 1140)
this.filterMask.lineTo(656, 1140)
this.filterMask.lineTo(656, 586)
this.filterMask.lineTo(750, 586)
this.filterMask.lineTo(750, 1624)
this.filterMask.lineTo(0, 1624)
this.filterMask.lineTo(0, 0)
this.filterMask.beginHole()
this.filterMask.drawCircle(30, 1000, 100)
this.filterMask.endHole()
this.filterMask.endFill();
this.addChild(this.filterMask)
let tips = new Sprite(RES.getRes("filterTips.png"))
tips.x = 360
tips.y = 1140
this.filterMask.addChild(tips)
let func
this.filterMask.addEventListener(MouseEvent.CLICK, func = () => {
this.filterBtn.mouseEnable = true
this.removeChild(this.filterMask)
this.removeChild(this.doFilterBtn)
this.removeAnis()
this.filtering = false
this.filterMask.removeAllEventListener()
this.doFilterBtn.removeAllEventListener()
}, this)
this.doFilterBtn = new Button(RES.getRes("doFilterBtn.png"))
this.doFilterBtn.x = 500
this.doFilterBtn.y = 1200
this.addChild(this.doFilterBtn)
let btnFunc
this.doFilterBtn.addEventListener(MouseEvent.CLICK, btnFunc = () => {
for (let x = 0; x < 4; x++) {
for (let y = 0; y < 4; y++) {
if (this.board[x][y] > 0 && this.board[x][y] < 128) {
// 滤网动效
Tween.get(this.containerPool[x][y].children[0])
.to({ scaleX: 0, scaleY: 0 }, 200)
.call(() => {
this.containerPool[x][y].removeChild(this.containerPool[x][y].children[0])
})
this.board[x][y] = 0
}
}
}
this.filterMask.removeAllEventListener()
this.doFilterBtn.removeAllEventListener()
setTimeout(() => {
this.renderBoard()
this.filterBtn.mouseEnable = true
this.removeChild(this.filterMask)
this.removeChild(this.doFilterBtn)
this.filtering = false
this.removeAnis()
if (!this.isGameWin())
this.isGameOver()
}, 300);
}, this)
} }
onClick_filterBtn() { onClick_filterBtn() {
this.filterBtn.mouseEnable = false
this.filtering = true
this.showFilterMask()
this.filterShakeAni()
}
filterShakeAni() {
for (let x = 0; x < 4; x++) {
for (let y = 0; y < 4; y++) {
if (this.board[x][y] > 0 && this.board[x][y] < 128) {
this.containerPool[x][y].children[0].anchor.set(60, 60)
Tween.get(this.containerPool[x][y].children[0], { loop: true })
.to({ rotation: 15 }, 50)
.to({ rotation: -15 }, 100)
.to({ rotation: 0 }, 50)
}
}
}
}
removeAllClick() {
for (let x = 0; x < 4; x++) {
for (let y = 0; y < 4; y++) {
if (this.board[x][y] > 0) {
this.containerPool[x][y].removeAllEventListener()
}
}
}
}
removeAnis() {
for (let x = 0; x < 4; x++) {
for (let y = 0; y < 4; y++) {
if (this.board[x][y] > 0) {
this.containerPool[x][y].children[0].rotation = 0
}
}
}
Tween.removeAllTweens()
} }
startX startX
...@@ -172,17 +587,30 @@ export class PlayScene extends Scene { ...@@ -172,17 +587,30 @@ export class PlayScene extends Scene {
super.removeEvents() super.removeEvents()
this.removeEventListener(MouseEvent.MOUSE_DOWN, this.onDown_this, this) this.removeEventListener(MouseEvent.MOUSE_DOWN, this.onDown_this, this)
this.removeEventListener(MouseEvent.CLICK, this.onClick_exchangeBtn, this) GDispatcher.removeEvent("updateProp", this.updateProp, this)
this.removeEventListener(MouseEvent.CLICK, this.onClick_bombBtn, this) this.exchangeBtn.removeEventListener(MouseEvent.CLICK, this.onClick_exchangeBtn, this)
this.removeEventListener(MouseEvent.CLICK, this.onClick_filterBtn, this) this.bombBtn.removeEventListener(MouseEvent.CLICK, this.onClick_bombBtn, this)
this.filterBtn.removeEventListener(MouseEvent.CLICK, this.onClick_filterBtn, this)
} }
async checkMove() { async checkMove() {
if (this.guideMask) {
this.removeChild(this.guideMask)
this.guideMask = null
}
if (this.checkAllZero()) {
// 只有0
this.generateOneElement()
}
this.moving = true
let beforeArr = this.cloneArr(this.board)
if (Math.abs(this.offsetX) > Math.abs(this.offsetY)) { if (Math.abs(this.offsetX) > Math.abs(this.offsetY)) {
// 横向移动 // 横向移动
if (this.offsetX > 0) { if (this.offsetX > 0) {
console.log("右滑"); console.log("右滑");
// 复制一份原先的数组
if (this.moveToRight(true)) { if (this.moveToRight(true)) {
await this.moveAni(beforeArr)
this.renderBoard() this.renderBoard()
this.generateOneElement() this.generateOneElement()
if (!this.isGameWin()) if (!this.isGameWin())
...@@ -192,7 +620,7 @@ export class PlayScene extends Scene { ...@@ -192,7 +620,7 @@ export class PlayScene extends Scene {
if (this.offsetX < 0) { if (this.offsetX < 0) {
console.log("左滑") console.log("左滑")
if (this.moveToLeft(true)) { if (this.moveToLeft(true)) {
// await this.moveAni() await this.moveAni(beforeArr)
this.renderBoard() this.renderBoard()
this.generateOneElement() this.generateOneElement()
if (!this.isGameWin()) if (!this.isGameWin())
...@@ -204,6 +632,7 @@ export class PlayScene extends Scene { ...@@ -204,6 +632,7 @@ export class PlayScene extends Scene {
if (this.offsetY > 0) { if (this.offsetY > 0) {
console.log("下滑") console.log("下滑")
if (this.moveToBottom(true)) { if (this.moveToBottom(true)) {
await this.moveAni(beforeArr)
this.renderBoard() this.renderBoard()
this.generateOneElement() this.generateOneElement()
if (!this.isGameWin()) if (!this.isGameWin())
...@@ -213,6 +642,7 @@ export class PlayScene extends Scene { ...@@ -213,6 +642,7 @@ export class PlayScene extends Scene {
if (this.offsetY < 0) { if (this.offsetY < 0) {
console.log("上滑") console.log("上滑")
if (this.moveToTop(true)) { if (this.moveToTop(true)) {
await this.moveAni(beforeArr)
this.renderBoard() this.renderBoard()
this.generateOneElement() this.generateOneElement()
if (!this.isGameWin()) if (!this.isGameWin())
...@@ -220,9 +650,33 @@ export class PlayScene extends Scene { ...@@ -220,9 +650,33 @@ export class PlayScene extends Scene {
} }
} }
} }
setTimeout(() => {
this.moving = false
}, 320);
} }
// 克隆二位数组返回新数组
cloneArr(arr) {
let b = []
for (let i = 0; i < arr.length; i++) {
b[i] = arr[i].concat()
}
return b
}
checkAllZero() {
let bool = true
for (let x = 0; x < 4; x++) {
for (let y = 0; y < 4; y++) {
if (this.board[x][y] !== 0) {
bool = false
}
}
}
return bool
}
drawRect(x, y, w, h, color) { drawRect(x, y, w, h, color) {
let sprite = new Graphics(); let sprite = new Graphics();
...@@ -257,7 +711,7 @@ export class PlayScene extends Scene { ...@@ -257,7 +711,7 @@ export class PlayScene extends Scene {
// 随机生成基础元素 // 随机生成基础元素
generateOneElement() { generateOneElement() {
// 随机生成一个数字 2or4 // 随机生成一个数字 2or4
let randNumber = Math.random() < 0.5 ? 2 : 4 let randNumber = Math.random() < 0.9 ? 2 : 4
// 随机生成位置 // 随机生成位置
let randNumberX = Math.floor(Math.random() * 4) let randNumberX = Math.floor(Math.random() * 4)
let randNumberY = Math.floor(Math.random() * 4) let randNumberY = Math.floor(Math.random() * 4)
...@@ -266,8 +720,21 @@ export class PlayScene extends Scene { ...@@ -266,8 +720,21 @@ export class PlayScene extends Scene {
this.generateOneElement() this.generateOneElement()
} else { } else {
this.board[randNumberX][randNumberY] = randNumber this.board[randNumberX][randNumberY] = randNumber
}
let sprite = new GameEle(randNumber)
this.addChild(sprite)
sprite.x += this.containerPool[randNumberX][randNumberY].x
sprite.y += this.containerPool[randNumberX][randNumberY].y
sprite.scale.set(0, 0)
sprite.anchor.set(60, 60)
Tween.get(sprite)
.to({ scaleX: 1, scaleY: 1 }, 300)
.call(() => {
this.removeChild(sprite)
this.renderBoard() this.renderBoard()
})
}
} }
// 渲染视图 // 渲染视图
...@@ -277,7 +744,10 @@ export class PlayScene extends Scene { ...@@ -277,7 +744,10 @@ export class PlayScene extends Scene {
let num = this.board[x][y] let num = this.board[x][y]
// 添加元素 // 添加元素
this.containerPool[x][y].removeChildren() this.containerPool[x][y].removeChildren()
this.containerPool[x][y].addChild(new GameEle(num)) let ele = new GameEle(num)
this.containerPool[x][y].addChild(ele)
if (num >= this.unlockScore) { if (num >= this.unlockScore) {
this.unlockScore = num this.unlockScore = num
} }
...@@ -385,8 +855,10 @@ export class PlayScene extends Scene { ...@@ -385,8 +855,10 @@ export class PlayScene extends Scene {
// 颠倒数组 // 颠倒数组
arr[x].reverse() arr[x].reverse()
tag = tag || this.canMove(arr[x]) tag = tag || this.canMove(arr[x])
if (type) this.updateArr(arr[x], x) if (type) this.updateArr(arr[x], x)
} }
// 数组再一次反向 即为此方向上的正确值顺序 // 数组再一次反向 即为此方向上的正确值顺序
if (tag && type) { if (tag && type) {
for (let x = 0; x < 4; x++) { for (let x = 0; x < 4; x++) {
...@@ -394,7 +866,7 @@ export class PlayScene extends Scene { ...@@ -394,7 +866,7 @@ export class PlayScene extends Scene {
} }
} }
arr = null arr = null
this.canMoveRight = tag // this.canMoveRight = tag
return tag return tag
} }
...@@ -428,7 +900,9 @@ export class PlayScene extends Scene { ...@@ -428,7 +900,9 @@ export class PlayScene extends Scene {
this.moveData = [[], [], [], []] this.moveData = [[], [], [], []]
// 先删除数组中的0 // 先删除数组中的0
let tag = 0 let tag = 0
for (let i = 0; i < arr.length; i++) {
for (let i = 0; i < arr.length; i++) { // 4次
if (arr[tag] === 0) { if (arr[tag] === 0) {
arr.splice(tag, 1) arr.splice(tag, 1)
arr.push(0) arr.push(0)
...@@ -469,10 +943,9 @@ export class PlayScene extends Scene { ...@@ -469,10 +943,9 @@ export class PlayScene extends Scene {
// this.addChild(clone) // this.addChild(clone)
Tween.get(clone) Tween.get(clone)
.set({ scaleX: 1, scaleY: 1, anchorX: 75, anchorY: 75 }) .set({ scaleX: 1, scaleY: 1, anchorX: 75, anchorY: 75 })
.to({ scaleX: 1.5, scaleY: 1.5 }, 300) .to({ scaleX: 0.3, scaleY: 0.3 }, 100)
.to({ scaleX: 1, scaleY: 1 }, 300) .to({ scaleX: 1.3, scaleY: 1.3 }, 300)
.to({ scaleX: 1.5, scaleY: 1.5 }, 300) .to({ scaleX: 1, scaleY: 1 }, 150)
.to({ scaleX: 1, scaleY: 1 }, 300)
arr[i - 1] *= 2 arr[i - 1] *= 2
arr.splice(i, 1) arr.splice(i, 1)
...@@ -485,32 +958,206 @@ export class PlayScene extends Scene { ...@@ -485,32 +958,206 @@ export class PlayScene extends Scene {
} }
} }
moveAni() { moveAni(beforeArr) {
return new Promise((r, j) => { return new Promise((r, j) => {
switch (this.currMoveType) { switch (this.currMoveType) {
case "top": case "top":
for (let y = 0; y < 4; y++) {
for (let x = 0; x < 4; x++) {
for (let xb = x + 1; xb < 4; xb++) {
if (beforeArr[xb][y] > 0) {
if (beforeArr[x][y] === 0) {
this.containerPool[xb][y].children[0].visible = false
let origin = new GameEle(beforeArr[xb][y])
this.addChild(origin)
let ox = origin.x
let oy = origin.y
origin.x += this.containerPool[xb][y].x
origin.y += this.containerPool[xb][y].y
let targetx = this.containerPool[x][y].x + ox
let targety = this.containerPool[x][y].y + oy
Tween.get(origin)
.to({ x: targetx, y: targety }, 200)
.call(() => {
this.removeChild(origin)
})
beforeArr[x][y] = beforeArr[xb][y]
beforeArr[xb][y] = 0
} else if (beforeArr[xb][y] === beforeArr[x][y] && noBlockBottom(x, xb, y, beforeArr)) {
this.containerPool[xb][y].children[0].visible = false
let origin = new GameEle(beforeArr[xb][y])
this.addChild(origin)
let ox = origin.x
let oy = origin.y
origin.x += this.containerPool[xb][y].x
origin.y += this.containerPool[xb][y].y
let targetx = this.containerPool[x][y].x + ox
let targety = this.containerPool[x][y].y + oy
Tween.get(origin)
.to({ x: targetx, y: targety }, 200)
.call(() => {
this.removeChild(origin)
})
beforeArr[xb][y] = 0
beforeArr[x][y] *= 2
}
}
}
}
}
break; break;
case "left": case "left":
this.moveData.reverse() for (let x = 0; x < 4; x++) {
this.moveData.forEach((arr, idx) => { for (let y = 0; y < 4; y++) {
if (arr instanceof Array && arr.length > 0) { for (let yr = y + 1; yr < 4; yr++) {
console.log(arr[0], idx); if (beforeArr[x][yr] > 0) {
if (beforeArr[x][y] === 0) {
this.containerPool[x][yr].children[0].visible = false
let origin = new GameEle(beforeArr[x][yr])
this.addChild(origin)
let ox = origin.x
let oy = origin.y
origin.x += this.containerPool[x][yr].x
origin.y += this.containerPool[x][yr].y
let targetx = this.containerPool[x][y].x + ox
let targety = this.containerPool[x][y].y + oy
Tween.get(origin)
.to({ x: targetx, y: targety }, 200)
.call(() => {
this.removeChild(origin)
})
} beforeArr[x][y] = beforeArr[x][yr]
beforeArr[x][yr] = 0
} else if (beforeArr[x][yr] === beforeArr[x][y] && noBlockRight(x, y, yr, beforeArr)) {
this.containerPool[x][yr].children[0].visible = false
let origin = new GameEle(beforeArr[x][yr])
this.addChild(origin)
let ox = origin.x
let oy = origin.y
origin.x += this.containerPool[x][yr].x
origin.y += this.containerPool[x][yr].y
let targetx = this.containerPool[x][y].x + ox
let targety = this.containerPool[x][y].y + oy
Tween.get(origin)
.to({ x: targetx, y: targety }, 200)
.call(() => {
this.removeChild(origin)
}) })
beforeArr[x][y] *= 2
beforeArr[x][yr] = 0
}
}
}
}
}
break break
case "bottom": case "bottom":
for (let y = 0; y < 4; y++) {
for (let x = 3; x >= 0; x--) {
for (let xt = x - 1; xt >= 0; xt--) {
if (beforeArr[xt][y] > 0) {
if (beforeArr[x][y] === 0) {
this.containerPool[xt][y].children[0].visible = false
let origin = new GameEle(beforeArr[xt][y])
this.addChild(origin)
let ox = origin.x
let oy = origin.y
origin.x += this.containerPool[xt][y].x
origin.y += this.containerPool[xt][y].y
let targetx = this.containerPool[x][y].x + ox
let targety = this.containerPool[x][y].y + oy
Tween.get(origin)
.to({ x: targetx, y: targety }, 200)
.call(() => {
this.removeChild(origin)
})
beforeArr[x][y] = beforeArr[xt][y]
beforeArr[xt][y] = 0
} else if (beforeArr[xt][y] === beforeArr[x][y] && noBlockTop(x, xt, y, beforeArr)) {
this.containerPool[xt][y].children[0].visible = false
let origin = new GameEle(beforeArr[xt][y])
this.addChild(origin)
let ox = origin.x
let oy = origin.y
origin.x += this.containerPool[xt][y].x
origin.y += this.containerPool[xt][y].y
let targetx = this.containerPool[x][y].x + ox
let targety = this.containerPool[x][y].y + oy
Tween.get(origin)
.to({ x: targetx, y: targety }, 200)
.call(() => {
this.removeChild(origin)
})
beforeArr[xt][y] = 0
beforeArr[x][y] *= 2
}
}
}
}
}
break break
case "right": case "right":
for (let x = 0; x < 4; x++) {
for (let y = 3; y >= 0; y--) {
for (let yl = y - 1; yl >= 0; yl--) {
if (beforeArr[x][yl] > 0) {
if (beforeArr[x][y] === 0) {
this.containerPool[x][yl].children[0].visible = false
let origin = new GameEle(beforeArr[x][yl])
this.addChild(origin)
let ox = origin.x
let oy = origin.y
origin.x += this.containerPool[x][yl].x
origin.y += this.containerPool[x][yl].y
let targetx = this.containerPool[x][y].x + ox
let targety = this.containerPool[x][y].y + oy
Tween.get(origin)
.to({ x: targetx, y: targety }, 200)
.call(() => {
this.removeChild(origin)
})
beforeArr[x][y] = beforeArr[x][yl]
beforeArr[x][yl] = 0
} else if (beforeArr[x][yl] === beforeArr[x][y] && noBlockLeft(x, y, yl, beforeArr)) {
this.containerPool[x][yl].children[0].visible = false
let origin = new GameEle(beforeArr[x][yl])
this.addChild(origin)
let ox = origin.x
let oy = origin.y
origin.x += this.containerPool[x][yl].x
origin.y += this.containerPool[x][yl].y
let targetx = this.containerPool[x][y].x + ox
let targety = this.containerPool[x][y].y + oy
Tween.get(origin)
.to({ x: targetx, y: targety }, 200)
.call(() => {
this.removeChild(origin)
})
beforeArr[x][yl] = 0
beforeArr[x][y] *= 2
}
}
}
}
}
break break
} }
setTimeout(() => {
r() r()
}, 200);
}) })
} }
canMove(arr: any[]) { canMove(arr: any[]) {
let hasZero = false let hasZero = false
for (let i = 0; i < arr.length; i++) { for (let i = 0; i < arr.length; i++) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment