添加撤回逻辑
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -22,6 +22,14 @@ export const config = {
|
||||
GREY: "#f0f2f7",
|
||||
RED: "#ef3e67",
|
||||
BLUE: "#2559c0",
|
||||
}
|
||||
},
|
||||
|
||||
UNDO_TYPE: {
|
||||
NORMAL: 1,
|
||||
NOTE: 2,
|
||||
},
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,33 +1,27 @@
|
||||
|
||||
|
||||
import { DataCandy } from "../types/global"
|
||||
import { StorageManager } from "./StorageManager"
|
||||
|
||||
|
||||
export class DOCandy {
|
||||
|
||||
private data: DataCandy
|
||||
private show: number
|
||||
private left: number
|
||||
|
||||
constructor(dataCandy: DataCandy, show: number) {
|
||||
this.data = dataCandy
|
||||
constructor(show: number, left: number) {
|
||||
this.show = show
|
||||
this.left = left
|
||||
}
|
||||
|
||||
//====================================持久化数据
|
||||
public get_left(): number {
|
||||
return this.data.left
|
||||
}
|
||||
public set_left(left: number): void {
|
||||
this.data.left = left
|
||||
StorageManager.getInstance().onChanged()
|
||||
}
|
||||
//====================================持久化数据
|
||||
|
||||
public get_show(): number {
|
||||
return this.show
|
||||
}
|
||||
|
||||
public get_left(): number {
|
||||
return this.left
|
||||
}
|
||||
public set_left(left: number): void {
|
||||
this.left = left
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -2,15 +2,13 @@
|
||||
|
||||
import { StorageManager } from "./StorageManager"
|
||||
import { DOBlock } from "./DOBlock"
|
||||
import { DOCandy } from "./DOCandy"
|
||||
import { DataBlock, DataStage } from "../types/global"
|
||||
import { DataBlock, DataUndo, DataStage } from "../types/global"
|
||||
|
||||
|
||||
export class DOStage {
|
||||
|
||||
private data: DataStage
|
||||
private blocks: Array<DOBlock> = new Array()
|
||||
private candys: Array<DOCandy> = new Array()
|
||||
|
||||
private letterToNumber: Map<string, number>;
|
||||
private numberToLetter: Map<number, string>;
|
||||
@@ -80,27 +78,14 @@ export class DOStage {
|
||||
}
|
||||
}
|
||||
}
|
||||
for (var i=1; i<=9; i++) {
|
||||
var dataCandy = this.data.candys[i]
|
||||
if (dataCandy) {
|
||||
var doCandy = new DOCandy(dataCandy, i)
|
||||
this.candys.push(doCandy)
|
||||
}
|
||||
else {
|
||||
var left = 0;
|
||||
var c = this.numberToLetter.get(i)
|
||||
if (list.has(c))
|
||||
left = list.get(c);
|
||||
dataCandy = {left: left}
|
||||
this.data.candys.push(dataCandy)
|
||||
var doCandy = new DOCandy(dataCandy, i)
|
||||
this.candys.push(doCandy)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//====================================持久化数据
|
||||
public get_difficulty(): number {
|
||||
return this.data.difficulty
|
||||
}
|
||||
|
||||
public get_mistake(): number {
|
||||
return this.data.mistake
|
||||
}
|
||||
@@ -109,10 +94,6 @@ export class DOStage {
|
||||
StorageManager.getInstance().onChanged()
|
||||
}
|
||||
|
||||
public get_difficulty(): number {
|
||||
return this.data.difficulty
|
||||
}
|
||||
|
||||
public get_duration(): number {
|
||||
return this.data.duration
|
||||
}
|
||||
@@ -129,6 +110,14 @@ export class DOStage {
|
||||
StorageManager.getInstance().onChanged()
|
||||
}
|
||||
|
||||
public get_select_index(): number {
|
||||
return this.data.select_index
|
||||
}
|
||||
public set_select_index(select_index: number): void {//当前选中
|
||||
this.data.select_index = select_index
|
||||
StorageManager.getInstance().onChanged()
|
||||
}
|
||||
|
||||
public get_hint(): number {
|
||||
return this.data.hint
|
||||
}
|
||||
@@ -136,13 +125,28 @@ export class DOStage {
|
||||
this.data.hint = hint
|
||||
StorageManager.getInstance().onChanged()
|
||||
}
|
||||
|
||||
public get_undos(): Array<DataUndo> {
|
||||
return this.data.undos
|
||||
}
|
||||
public add_undo(type: number, index: number, value: number): void {
|
||||
var dataUndo: DataUndo = {type: type, index: index, value: value}
|
||||
console.log("add_undo >>>>>>>>>>>>>>>>>>>>>>>>>>>>", JSON.stringify(dataUndo))
|
||||
this.data.undos.push(dataUndo)
|
||||
StorageManager.getInstance().onChanged()
|
||||
}
|
||||
public pop_undo(): DataUndo {
|
||||
var dataUndo: DataUndo
|
||||
if (this.data.undos.length > 0) {
|
||||
dataUndo = this.data.undos[this.data.undos.length-1]
|
||||
this.data.undos.splice(this.data.undos.length-1, 1)
|
||||
StorageManager.getInstance().onChanged()
|
||||
}
|
||||
return dataUndo
|
||||
}
|
||||
//====================================持久化数据
|
||||
|
||||
public get_blocks(): Array<DOBlock> {
|
||||
return this.blocks
|
||||
}
|
||||
|
||||
public get_candys(): Array<DOCandy> {
|
||||
return this.candys
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { config } from "../constants/config"
|
||||
import { DataStage } from "../types/global"
|
||||
import { DataStage, createDataStage } from "../types/global"
|
||||
import { DOStage } from "./DOStage"
|
||||
|
||||
|
||||
@@ -35,7 +35,7 @@ export class StorageManager {
|
||||
this.stageMap.set(stageID, dataStage)
|
||||
}
|
||||
else {
|
||||
dataStage = {mistake: 0, difficulty: config.DIFFICULTY_TYPE.Easy, duration: 0, note_open: false, hint: 2, blocks: new Array(), candys: new Array()}
|
||||
dataStage = createDataStage(config.DIFFICULTY_TYPE.Easy)
|
||||
this.stageMap.set(stageID, dataStage)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,20 +8,32 @@ export interface DataBlock {
|
||||
notes: Array<number>,
|
||||
}
|
||||
|
||||
|
||||
//可选数字
|
||||
export interface DataCandy {
|
||||
left: number,
|
||||
//撤回数据结构
|
||||
export interface DataUndo {
|
||||
type: number,
|
||||
index: number,
|
||||
value: number,
|
||||
}
|
||||
|
||||
|
||||
//关卡数据
|
||||
export interface DataStage {
|
||||
mistake: number,
|
||||
difficulty: number,
|
||||
duration: number,
|
||||
note_open: boolean,
|
||||
hint: number
|
||||
blocks: Array<DataBlock>,
|
||||
candys: Array<DataCandy>,
|
||||
mistake?: number,
|
||||
duration?: number,
|
||||
note_open?: boolean,
|
||||
hint?: number,
|
||||
select_index?: number,
|
||||
blocks?: Array<DataBlock>,
|
||||
undos?: Array<DataUndo>,
|
||||
}
|
||||
export function createDataStage(difficulty: number): DataStage {
|
||||
var data: DataStage = {difficulty: difficulty}
|
||||
data.mistake = 0
|
||||
data.duration = 0
|
||||
data.note_open = false
|
||||
data.hint = 2
|
||||
data.select_index = 0
|
||||
data.blocks = new Array()
|
||||
data.undos = new Array()
|
||||
return data
|
||||
}
|
||||
@@ -41,6 +41,7 @@ export class Block extends Laya.Script {
|
||||
}
|
||||
}
|
||||
this.label_show = label_obj.getChildByName("label_show") as Laya.Label
|
||||
// this.label_show.text = XGroup + "" + YGroup
|
||||
VBox = label_obj.getChildByName("VBox")
|
||||
for (var i=0; i<VBox.numChildren; i++) {
|
||||
var hbox = VBox.getChildAt(i);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { DOCandy } from "../models/DOCandy";
|
||||
|
||||
|
||||
const { regClass, property } = Laya;
|
||||
|
||||
@@ -10,14 +10,14 @@ export class Candy extends Laya.Script {
|
||||
private label_show: Laya.Label;
|
||||
private label_left: Laya.Label;
|
||||
|
||||
private data: DOCandy
|
||||
private show: number
|
||||
private left: number
|
||||
|
||||
public onInit(label_obj: Laya.Box, handler: any, func: any): void {
|
||||
this.owner.on(Laya.Event.CLICK, handler, func)
|
||||
this.show_sprite = this.owner.getChildByName("Sprite") as Laya.Sprite
|
||||
this.label_show = label_obj.getChildByName("label") as Laya.Label
|
||||
this.label_left = label_obj.getChildByName("label_left") as Laya.Label
|
||||
|
||||
}
|
||||
|
||||
public setNoteStatus(note_open: boolean) {
|
||||
@@ -31,29 +31,26 @@ export class Candy extends Laya.Script {
|
||||
}
|
||||
}
|
||||
|
||||
public setCandy(data: DOCandy): void {
|
||||
this.data = data
|
||||
public setCandy(show: number, left: number): void {
|
||||
this.show = show
|
||||
this.left = left
|
||||
this.updateCandy()
|
||||
}
|
||||
public getData(): DOCandy {
|
||||
return this.data
|
||||
}
|
||||
|
||||
public setLeft(left: number): void {
|
||||
this.data.set_left(left)
|
||||
this.updateCandy()
|
||||
public get_show(): number {
|
||||
return this.show
|
||||
}
|
||||
|
||||
updateCandy(): void {
|
||||
if (this.data.get_show() > 0) {
|
||||
this.label_show.text = this.data.get_show().toString()
|
||||
if (this.show > 0) {
|
||||
this.label_show.text = this.show.toString()
|
||||
}
|
||||
else {
|
||||
this.label_show.text = ""
|
||||
}
|
||||
|
||||
if (this.data.get_left() > 0) {
|
||||
this.label_left.text = this.data.get_left().toString();
|
||||
if (this.left > 0) {
|
||||
this.label_left.text = this.left.toString();
|
||||
this.owner.visible = true
|
||||
}
|
||||
else {
|
||||
|
||||
@@ -4,6 +4,7 @@ import { Block } from "./Block";
|
||||
import { Candy } from "./Candy";
|
||||
import { DOStage } from "../models/DOStage";
|
||||
import { config } from "../constants/config";
|
||||
import { DataUndo } from "../types/global";
|
||||
|
||||
const { regClass, property } = Laya;
|
||||
|
||||
@@ -48,8 +49,8 @@ export class Stage extends Laya.Script {
|
||||
private btn_hint_ad: Laya.Box
|
||||
private btn_hint_label: Laya.Label
|
||||
|
||||
private selectedBlock: Block;
|
||||
private data: DOStage;
|
||||
private selectedBlock: Block;
|
||||
|
||||
//组件被激活后执行,此时所有节点和组件均已创建完毕,此方法只执行一次
|
||||
onAwake(): void {
|
||||
@@ -64,17 +65,17 @@ export class Stage extends Laya.Script {
|
||||
var block = hbox.getChildAt(j).getComponent(Block);
|
||||
this.blocks.push(block);
|
||||
var XGroup = 0;
|
||||
if (i==3 || i==4 || i==5)
|
||||
if (j==3 || j==4 || j==5)
|
||||
XGroup = 1
|
||||
if (i==6 || i==7 || i==8)
|
||||
if (j==6 || j==7 || j==8)
|
||||
XGroup = 2
|
||||
var YGroup = 0;
|
||||
if (j==3 || j==4 || j==5)
|
||||
if (i==3 || i==4 || i==5)
|
||||
YGroup = 1
|
||||
if (j==6 || j==7 || j==8)
|
||||
if (i==6 || i==7 || i==8)
|
||||
YGroup = 2
|
||||
var l_vbox = l_hbox.getChildAt(j) as Laya.Box
|
||||
block.onInit(i, j, XGroup, YGroup, l_vbox, this, this.onClickBlock)
|
||||
block.onInit(j, i, XGroup, YGroup, l_vbox, this, this.onClickBlock)
|
||||
}
|
||||
}
|
||||
for (var i=0; i<this.obj_candys.numChildren; i++) {
|
||||
@@ -93,8 +94,6 @@ export class Stage extends Laya.Script {
|
||||
this.btn_hint_normal = this.btn_hint.getChildByName("Image").getChildByName("normal") as Laya.Box
|
||||
this.btn_hint_ad = this.btn_hint.getChildByName("Image").getChildByName("ad") as Laya.Box
|
||||
this.btn_hint_label = this.btn_hint.getChildByName("Image").getChildByName("normal").getChildByName("label") as Laya.Label
|
||||
|
||||
this.selectedBlock = null;
|
||||
}
|
||||
|
||||
public onLoadStage(doStage: DOStage): void {
|
||||
@@ -124,15 +123,12 @@ export class Stage extends Laya.Script {
|
||||
block.setBlock(doBlock)
|
||||
block.setTipsBgColor(false)
|
||||
}
|
||||
var candys = doStage.get_candys()
|
||||
for (var i=0; i<candys.length; i++) {
|
||||
var doCandy = candys[i]
|
||||
var candy = this.candys[i]
|
||||
candy.setCandy(doCandy)
|
||||
}
|
||||
|
||||
this.onUpdateCandys()
|
||||
this.onClickNote(null)
|
||||
this.onUpdateHintStatus()
|
||||
|
||||
this.selectedBlock = this.blocks[this.data.get_select_index()]
|
||||
this.onApplyBlock(this.selectedBlock)
|
||||
}
|
||||
|
||||
onUpdateScore(): void {
|
||||
@@ -200,27 +196,51 @@ export class Stage extends Laya.Script {
|
||||
}
|
||||
}
|
||||
}
|
||||
onClickBlock(evt: Laya.Event): void {
|
||||
if (this.selectedBlock)
|
||||
|
||||
onApplyBlock(block: Block): void {
|
||||
this.selectedBlock.setSelected(false)
|
||||
var block = evt.target.getComponent(Block);
|
||||
block.setSelected(true)
|
||||
this.onSetBlock(block)
|
||||
this.selectedBlock = block
|
||||
this.selectedBlock.setSelected(true)
|
||||
this.onSetBlock(this.selectedBlock)
|
||||
this.data.set_select_index(this.selectedBlock.getData().get_index())
|
||||
}
|
||||
onClickBlock(evt: Laya.Event): void {
|
||||
var block = evt.target.getComponent(Block)
|
||||
this.onApplyBlock(block)
|
||||
}
|
||||
|
||||
|
||||
onClickUndo(evt: Laya.Event): void {
|
||||
onApplyUndo(undo: DataUndo): void {
|
||||
|
||||
}
|
||||
onClickUndo(evt: Laya.Event): void {
|
||||
var undo = this.data.pop_undo()
|
||||
if (undo) {
|
||||
// console.log("onClickUndo>>>>", JSON.stringify(undo))
|
||||
// this.selectedBlock = this.blocks[undo.index]
|
||||
// this.onApplyBlock(this.selectedBlock)
|
||||
// if (undo.type == config.UNDO_TYPE.NORMAL) {
|
||||
// var block = this.blocks[undo.index]
|
||||
|
||||
// this.selectedBlock.setShowNumber(undo.value)
|
||||
// this.onSetBlock(this.selectedBlock)
|
||||
// }
|
||||
// else if (undo.type == config.UNDO_TYPE.NOTE) {
|
||||
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
onClickErase(evt: Laya.Event): void {
|
||||
if (this.selectedBlock && this.selectedBlock.getData().get_checked() == false) {
|
||||
if (this.selectedBlock.getData().get_checked() == false) {//未完成切有数字或者笔记才可以使用橡皮
|
||||
if (this.selectedBlock.getData().get_show() > 0 || this.selectedBlock.getData().get_notes().length > 0) {
|
||||
this.data.add_undo(config.UNDO_TYPE.NORMAL, this.selectedBlock.getData().get_index(), 0)
|
||||
this.selectedBlock.setShowNumber(0)
|
||||
this.onUpdateScore()
|
||||
this.onSetBlock(this.selectedBlock)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
onClickNote(evt: Laya.Event): void {
|
||||
if (evt != null) {
|
||||
@@ -236,10 +256,9 @@ export class Stage extends Laya.Script {
|
||||
onClickHint(evt: Laya.Event): void {
|
||||
var hint = this.data.get_hint()
|
||||
if (hint > 0) {
|
||||
if (this.selectedBlock && this.selectedBlock.getData().get_checked() == false) {//当前选中的格子没有完成
|
||||
if (this.selectedBlock.getData().get_checked() == false) {//当前选中的格子没有完成
|
||||
var correct = this.selectedBlock.getData().get_correct()
|
||||
var candy = this.candys[correct-1]
|
||||
this.onApplyCandy(candy)
|
||||
this.onApplyCandy(correct)
|
||||
this.data.set_hint(hint-1)
|
||||
this.onUpdateHintStatus()
|
||||
}
|
||||
@@ -256,10 +275,9 @@ export class Stage extends Laya.Script {
|
||||
if (index >= list.length)
|
||||
index = list.length -1
|
||||
var block = list[index]
|
||||
this.selectedBlock = block
|
||||
this.onApplyBlock(block)
|
||||
var correct = block.getData().get_correct()
|
||||
var candy = this.candys[correct-1]
|
||||
this.onApplyCandy(candy)
|
||||
this.onApplyCandy(correct)
|
||||
this.data.set_hint(hint-1)
|
||||
this.onUpdateHintStatus()
|
||||
}
|
||||
@@ -278,9 +296,8 @@ export class Stage extends Laya.Script {
|
||||
this.btn_hint_label.text = hint.toString()
|
||||
}
|
||||
|
||||
public onApplyCandy(candy: Candy): void {
|
||||
if (this.selectedBlock && this.selectedBlock.getData().get_checked() == false) {
|
||||
var showNumber = candy.getData().get_show()
|
||||
public onApplyCandy(showNumber: number): void {
|
||||
if (this.selectedBlock.getData().get_checked() == false) {
|
||||
if (this.data.get_note_open()) {
|
||||
var relateds = this.findRelatedBlocks(this.selectedBlock)
|
||||
var find = false
|
||||
@@ -304,16 +321,16 @@ export class Stage extends Laya.Script {
|
||||
}
|
||||
else {
|
||||
if (this.selectedBlock.getData().get_show() == showNumber) {
|
||||
this.data.add_undo(config.UNDO_TYPE.NORMAL, this.selectedBlock.getData().get_index(), 0)
|
||||
this.selectedBlock.setShowNumber(0);
|
||||
this.onSetBlock(this.selectedBlock)
|
||||
}
|
||||
else {
|
||||
var b = this.selectedBlock.setShowNumber(showNumber);
|
||||
this.data.add_undo(config.UNDO_TYPE.NORMAL, this.selectedBlock.getData().get_index(), showNumber)
|
||||
var b = this.selectedBlock.setShowNumber(showNumber)
|
||||
this.onUpdateScore()
|
||||
this.onSetBlock(this.selectedBlock, true)
|
||||
if (b) {
|
||||
var left = candy.getData().get_left();
|
||||
candy.setLeft(left-1);
|
||||
if (this.getIsComplete()) {
|
||||
console.log("完成关卡")
|
||||
UIManager.getInstance().loadGameDone()
|
||||
@@ -328,20 +345,39 @@ export class Stage extends Laya.Script {
|
||||
}
|
||||
}
|
||||
}
|
||||
this.onUpdateCandys()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public onClickCandy(evt: Laya.Event): void {
|
||||
var candy = evt.target.getComponent(Candy);
|
||||
this.onApplyCandy(candy)
|
||||
this.onApplyCandy(candy.get_show())
|
||||
}
|
||||
|
||||
onKeyUp(evt: Laya.Event): void {
|
||||
var n = Number(evt.key)
|
||||
if (n && n > 0) {
|
||||
var candy = this.candys[n-1]
|
||||
this.onApplyCandy(candy)
|
||||
this.onApplyCandy(n)
|
||||
}
|
||||
}
|
||||
|
||||
onUpdateCandys(): void {
|
||||
var list: Map<number, number> = new Map()
|
||||
for (var i=1; i<=9; i++)
|
||||
list.set(i, 0)
|
||||
for (var i=0; i<this.blocks.length; i++) {
|
||||
var block = this.blocks[i]
|
||||
if (block.getData().get_checked() == false) {
|
||||
var correct = block.getData().get_correct()
|
||||
list.set(correct, list.get(correct)+1)
|
||||
}
|
||||
}
|
||||
for (var i=0; i<this.candys.length; i++) {
|
||||
var candy = this.candys[i]
|
||||
var show = i+1
|
||||
var left = list.get(show)
|
||||
candy.setCandy(show, left)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user