添加持久化逻辑

This commit is contained in:
2025-05-13 20:31:44 +08:00
parent dcb12db40d
commit cbee313bb5
17 changed files with 512 additions and 213 deletions

View File

@@ -1,6 +1,6 @@
{
"_$ver": 1,
"_$id": "gvlq012i",
"_$id": "dzyyfm4n",
"_$type": "Box",
"name": "Home",
"width": 1080,
@@ -15,13 +15,16 @@
"_$type": "f2970e4d-7a09-4a9b-aef3-45d330c86f4f",
"scriptPath": "../src/views/Home.ts",
"btn_start": {
"_$ref": "098pc00r"
"_$ref": "vfxym76v"
},
"btn_clean": {
"_$ref": "ttvxhmrp"
}
}
],
"_$child": [
{
"_$id": "0dr4l4hi",
"_$id": "x8wx2qo1",
"_$type": "Image",
"name": "Mask",
"width": 1080,
@@ -34,7 +37,26 @@
"color": "#ffffff"
},
{
"_$id": "098pc00r",
"_$id": "pd4fx8i2",
"_$type": "Button",
"name": "btn_continue",
"x": 440,
"y": 737,
"width": 200,
"height": 100,
"_mouseState": 2,
"centerX": 0,
"centerY": -173,
"skin": "res://7eeb7e11-21ca-41ca-b7c2-dd2afa0c3ba7",
"label": "继续",
"labelFont": "res://55f943ef-58d2-4b30-8498-271c4213cce4",
"labelSize": 60,
"labelColors": "#ffffff,#32cc6b,#ff0000",
"labelAlign": "center",
"labelVAlign": "middle"
},
{
"_$id": "vfxym76v",
"_$type": "Button",
"name": "btn_start",
"x": 440,
@@ -54,23 +76,24 @@
"labelStrokeColor": "rgba(216, 208, 208, 1)"
},
{
"_$id": "xi28zcch",
"_$id": "ttvxhmrp",
"_$type": "Button",
"name": "btn_continue",
"name": "btn_clean",
"x": 440,
"y": 737,
"y": 1820,
"width": 200,
"height": 100,
"_mouseState": 2,
"bottom": 0,
"centerX": 0,
"centerY": -173,
"skin": "res://7eeb7e11-21ca-41ca-b7c2-dd2afa0c3ba7",
"label": "继续",
"label": "清理",
"labelFont": "res://55f943ef-58d2-4b30-8498-271c4213cce4",
"labelSize": 60,
"labelColors": "#ffffff,#32cc6b,#ff0000",
"labelAlign": "center",
"labelVAlign": "middle"
"labelVAlign": "middle",
"labelStrokeColor": "rgba(216, 208, 208, 1)"
}
]
}

View File

@@ -1,3 +1,3 @@
{
"uuid": "9d706cf1-ff30-4183-b441-c24d9b329c53"
"uuid": "ed2f6a24-7574-4f6a-b76b-816fe2521be2"
}

View File

@@ -14,6 +14,9 @@
{
"_$type": "692a672b-2457-456c-8093-321ea7a10e1b",
"scriptPath": "../src/views/Stage.ts",
"btn_back": {
"_$ref": "l1ebdt6a"
},
"label_stage_name": {
"_$ref": "w8r3l8ie"
},
@@ -123,6 +126,7 @@
"x": 116,
"width": 200,
"height": 64,
"visible": false,
"text": "[color=#000000]关卡 [/color][color=#1d5cdc]20[/color]",
"font": "res://55f943ef-58d2-4b30-8498-271c4213cce4",
"fontSize": 60,
@@ -208,7 +212,7 @@
"height": 60,
"centerX": 369,
"centerY": -550,
"text": "99:99:99",
"text": "00:00:00",
"font": "res://55f943ef-58d2-4b30-8498-271c4213cce4",
"fontSize": 30,
"color": "rgba(123, 129, 140, 1)",

View File

@@ -1,9 +1,18 @@
export const config = {
debug: false,
openGuide: true,
breakpoint: 1, // 竖版和横版的分界宽度
DEBUG: false,
OPEN_GUIDE: true,
BREAK_POINT: 1, // 竖版和横版的分界宽度
DIFFICULTY_TYPE: {//难度
Easy: 1,
Medium: 2,
Hard: 3,
Expert: 4,
Extreme: 5,
},
}

View File

@@ -0,0 +1,79 @@
import { config } from "../constants/config"
import { DOStage } from "../types/DOStage"
export class StorageManager {
private static instance: StorageManager
public static getInstance(): StorageManager {
if (!StorageManager.instance) {
StorageManager.instance = new StorageManager()
}
return StorageManager.instance
}
public static createDataBlock(index: number, show: number, score: number=0, notes: Array<number>=new Array()): any {//格子
var dataBlock = {
index: index,
show: show,
score: score,
notes: notes,
}
return dataBlock
}
public static createDataCandy(left: number): any {//可选数字
var dataCandy = {
left: left,
}
return dataCandy
}
public static createDataStage(mistake: number, difficulty: number, duration: number=0, blocks: Array<any>=new Array(), candys: Array<any>=new Array()): any {//关卡数据
var dataStage = {
mistake: mistake,
difficulty: difficulty,
duration: duration,
note_open: false,
blocks: blocks,
candys: candys,
}
return dataStage
}
private stageID: string = "";
private stageMap: Map<string, any> = new Map();
public init(): void {
}
public loadStage(stageID: string): DOStage {
this.stageID = stageID
var dataStage
if (this.stageMap.has(stageID)) {
dataStage = this.stageMap.get(stageID)
}
else {
var jsonStr = Laya.LocalStorage.getItem(stageID)
if (jsonStr) {
dataStage = JSON.parse(jsonStr)
this.stageMap.set(stageID, dataStage)
}
else {
dataStage = StorageManager.createDataStage(0, config.DIFFICULTY_TYPE.Easy)
this.stageMap.set(stageID, dataStage)
}
}
return new DOStage(dataStage)
}
public saveStage(): void {
if (this.stageMap.has(this.stageID)) {
var dataStage = this.stageMap.get(this.stageID)
var jsonStr = JSON.stringify(dataStage)
Laya.LocalStorage.setItem(this.stageID, jsonStr)
}
}
}

View File

@@ -0,0 +1,3 @@
{
"uuid": "7503e6c9-bbbe-40ca-813c-660b4927d3a6"
}

View File

@@ -1,7 +1,8 @@
import { respath } from "../constants/respath"
import { Home } from "../views/Home"
import { StorageManager } from "./StorageManager"
import { Stage } from "../views/Stage"
const { regClass, property } = Laya
@regClass()
@@ -11,6 +12,9 @@ export class UIManager extends Laya.Script {
private static _instance: UIManager
onAwake(): void {
UIManager._instance = this
StorageManager.getInstance().init()
this.loadHome()
}
@@ -29,7 +33,7 @@ export class UIManager extends Laya.Script {
Laya.loader.load(respath.stage_ui_res).then((go)=>{
var prefab = go.create()
var stage = this.owner.addChild(prefab).getComponent(Stage)
stage.onLoadStage("aBCeIGFhdEDFcAHBGIhigBdfecAgHDiFBCaeBEAGHCIdffcIdEAHbgcABfGEdiHIFHaCDGEBDGEHbiaFC");
stage.onLoadStage(StorageManager.getInstance().loadStage("first"))
})
}

View File

@@ -1,4 +1,5 @@
import { G_ShowScoreTips } from "../views/GUITips";
import { DOBlock } from "./DOBlock";
const { regClass, property } = Laya;
@@ -15,18 +16,14 @@ export class Block extends Laya.Script {
@property(Number)
public YGroup: number = 0;
private selected: boolean = false
private newSelect: boolean = false
private preset: boolean = false//预设格子
private checked: boolean = false//正确格子
private showNumber: number = 0;
private correctNumber: number = 0;
private score: number = 0
private note_numbers: Array<number> = new Array()
private label_show: Laya.Label;
private tips_labels: Array<Laya.Label> = new Array();
private animator: Laya.Animator2D
private selected: boolean = false
private newSelect: boolean = false
private data: DOBlock
public onInit(XIndex: number, YIndex: number, XGroup: number, YGroup: number, label_obj: Laya.Box, handler: any, func: any): void {
this.XIndex = XIndex;
@@ -68,69 +65,43 @@ export class Block extends Laya.Script {
}
}
public setBlock(preset: boolean, showNumber: number, correctNumber: number): void {
this.preset = preset;
this.checked = preset;
this.showNumber = showNumber;
this.correctNumber = correctNumber;
this.score = 0
public setBlock(data: DOBlock): void {
this.data = data;
this.updateBlock();
}
public getIsPreset(): boolean {
return this.preset;
}
public getIsChecked(): boolean {
return this.checked;
}
public getShowNumber(): number {
return this.showNumber;
public getData(): DOBlock {
return this.data
}
public setShowNumber(showNumber: number): boolean {
if (this.checked == false) {
if (showNumber == this.correctNumber)
this.checked = true;
this.showNumber = showNumber;
if (this.checked || showNumber == 0) {//正确或者擦除
this.note_numbers = new Array()
}
this.score = 0
if (this.data.get_checked() == false) {
this.data.set_show(showNumber)
var score: number = 0
if (this.newSelect) {
this.newSelect = false
if (this.checked) {
this.score = 150
G_ShowScoreTips(this.score.toString(), this.owner)
if (this.data.get_checked()) {
score = 150
this.data.set_score(score)
G_ShowScoreTips(score.toString(), this.owner)
}
}
this.updateBlock();
this.updateBlock()
}
return this.checked
}
public getCorrectNumber(): number {
return this.correctNumber;
}
public getScore(): number {
return this.score
return this.data.get_checked()
}
public addNoteNumber(noteNumber: number): void {
if (this.note_numbers.indexOf(noteNumber) < 0) {
this.note_numbers.push(noteNumber)
this.updateBlock();
if (this.data.add_note(noteNumber)) {
this.updateBlock()
}
}
updateBlock(): void {
for (var i=0; i<this.tips_labels.length; i++)
this.tips_labels[i].text = ""
if (this.showNumber > 0) {
this.label_show.text = this.showNumber.toString();
if (this.checked) {
if (this.data.get_show() > 0) {
this.label_show.text = this.data.get_show().toString()
if (this.data.get_checked()) {
this.label_show.color = "#000000"
}
else {
@@ -139,8 +110,9 @@ export class Block extends Laya.Script {
}
else {
this.label_show.text = "";
for (var i=0; i<this.note_numbers.length; i++) {
var value = this.note_numbers[i]
var note_numbers = this.data.get_notes()
for (var i=0; i<note_numbers.length; i++) {
var value = note_numbers[i]
var label = this.tips_labels[value-1]
label.text = value.toString()
if (this.selected) {

View File

@@ -1,3 +1,5 @@
import { DOCandy } from "./DOCandy";
const { regClass, property } = Laya;
@regClass()
@@ -7,42 +9,41 @@ export class Candy extends Laya.Script {
private label_show: Laya.Label;
private label_left: Laya.Label;
private showNumber: number = 0;
private left: number = 0;
public onInit(showNumber: number, label_obj: Laya.Box, handler: any, func: any): void {
this.showNumber = showNumber
private data: DOCandy
public onInit(label_obj: Laya.Box, handler: any, func: any): void {
this.label_show = label_obj.getChildByName("label") as Laya.Label
this.label_left = label_obj.getChildByName("label_left") as Laya.Label
this.owner.on(Laya.Event.CLICK, handler, func)
this.updateCandy()
}
public getShowNumber(): number {
return this.showNumber;
public setCandy(data: DOCandy): void {
this.data = data
this.updateCandy()
}
public getData(): DOCandy {
return this.data
}
public setLeft(left: number): void {
this.left = left;
this.data.set_left(left)
this.updateCandy()
}
public getLeft(): number {
return this.left;
return this.data.get_left();
}
updateCandy(): void {
this.owner.active = this.left > 0
if (this.showNumber > 0) {
this.label_show.text = this.showNumber.toString();
this.owner.active = this.getLeft() > 0
if (this.data.get_show() > 0) {
this.label_show.text = this.data.get_show().toString()
}
else {
this.label_show.text = ""
}
if (this.left > 0) {
this.label_left.text = this.left.toString();
if (this.getLeft() > 0) {
this.label_left.text = this.getLeft().toString();
}
else {
this.label_left.text = ""

80
src/types/DOBlock.ts Normal file
View File

@@ -0,0 +1,80 @@
import { StorageManager } from "../models/StorageManager"
export class DOBlock {
private data: any
private preset: boolean = false//预设格子
private correct: number = 0;
private checked: boolean = false//是否正确
constructor(dataBlock: any, preset: boolean, correct: number) {
this.data = dataBlock
this.preset = preset
this.correct = correct
this.checked = preset || (dataBlock.show == correct)
}
//====================================持久化数据
public get_index(): number {
return this.data.index
}
public get_show(): number {
return this.data.show
}
public set_show(show: number): void {
this.data.show = show
if (show == this.get_correct())
this.checked = true
if (this.get_checked() || show == 0)
this.clean_notes()//正确或者擦除
StorageManager.getInstance().saveStage()
}
public get_score(): number {
return this.data.score
}
public set_score(score: number): void {
this.data.score = score
StorageManager.getInstance().saveStage()
}
public get_notes(): Array<number> {
return this.data.notes
}
public add_note(noteNumber: number): boolean {
var b = false
if (this.data.notes.indexOf(noteNumber) < 0) {
this.data.notes.push(noteNumber)
StorageManager.getInstance().saveStage()
b = true
}
return b
}
public clean_notes(): void {
if (this.data.notes.length > 0) {
this.data.notes = new Array()
StorageManager.getInstance().saveStage()
}
}
//====================================持久化数据
public get_preset(): boolean {
return this.preset
}
public get_correct(): number {
return this.correct
}
public get_checked(): boolean {
return this.checked
}
}

View File

@@ -0,0 +1,3 @@
{
"uuid": "aeb99474-1a9f-4a01-ac79-3416ec39a309"
}

33
src/types/DOCandy.ts Normal file
View File

@@ -0,0 +1,33 @@
import { StorageManager } from "../models/StorageManager"
export class DOCandy {
private data: any
private show: number
constructor(dataCandy: any, show: number) {
this.data = dataCandy
this.show = show
}
//====================================持久化数据
public get_left(): number {
return this.data.left
}
public set_left(left: number): void {
this.data.left = left
StorageManager.getInstance().saveStage()
}
//====================================持久化数据
public get_show(): number {
return this.show
}
}

View File

@@ -0,0 +1,3 @@
{
"uuid": "0c3f05a2-7935-4847-8760-858002151854"
}

139
src/types/DOStage.ts Normal file
View File

@@ -0,0 +1,139 @@
import { StorageManager } from "../models/StorageManager"
import { DOBlock } from "./DOBlock"
import { DOCandy } from "./DOCandy"
export class DOStage {
private data: any
private blocks: Array<DOBlock> = new Array()
private candys: Array<DOCandy> = new Array()
private letterToNumber: any;
private numberToLetter: any;
constructor(dataStage: any) {
this.data = dataStage
this.letterToNumber = new Map();
this.letterToNumber.set("A", 1);
this.letterToNumber.set("B", 2);
this.letterToNumber.set("C", 3);
this.letterToNumber.set("D", 4);
this.letterToNumber.set("E", 5);
this.letterToNumber.set("F", 6);
this.letterToNumber.set("G", 7);
this.letterToNumber.set("H", 8);
this.letterToNumber.set("I", 9);
this.numberToLetter = new Map();
this.numberToLetter.set(1, "a");
this.numberToLetter.set(2, "b");
this.numberToLetter.set(3, "c");
this.numberToLetter.set(4, "d");
this.numberToLetter.set(5, "e");
this.numberToLetter.set(6, "f");
this.numberToLetter.set(7, "g");
this.numberToLetter.set(8, "h");
this.numberToLetter.set(9, "i");
var blockMap: Map<number, any> = new Map()
for (var i=0; i<this.data.blocks.length; i++) {
var dataBlock = this.data.blocks[i]
blockMap.set(dataBlock.index, dataBlock)
}
var levelStr = "aBCeIGFhdEDFcAHBGIhigBdfecAgHDiFBCaeBEAGHCIdffcIdEAHbgcABfGEdiHIFHaCDGEBDGEHbiaFC"
if (levelStr.length != 81) {
console.log("levelStr error", levelStr.length)
}
var list = new Map();
for (var i=0; i<levelStr.length; i++) {
var letter = levelStr[i];
if (this.letterToNumber.has(letter)) {//预设格子 使用配置文件初始化
var show = this.letterToNumber.get(letter)
var dataBlock = StorageManager.createDataBlock(i, show)
var doBlock = new DOBlock(dataBlock, true, show)
this.blocks.push(doBlock)
}
else {
var show = this.letterToNumber.get(letter.toUpperCase())
var dataBlock = blockMap.get(i)
if (dataBlock) {//本地有持久化数据
var doBlock = new DOBlock(dataBlock, false, show)
this.blocks.push(doBlock)
}
else {
dataBlock = StorageManager.createDataBlock(i, 0)
this.data.blocks.push(dataBlock)
var doBlock = new DOBlock(dataBlock, false, show)
this.blocks.push(doBlock)
}
if (list.has(letter)) {
list.set(letter, list.get(letter)+1);
}
else {
list.set(letter, 1);
}
}
}
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 = StorageManager.createDataCandy(left)
this.data.candys.push(dataCandy)
var doCandy = new DOCandy(dataCandy, i)
this.candys.push(doCandy)
}
}
}
//====================================持久化数据
public get_mistake(): number {
return this.data.mistake
}
public set_mistake(mistake: number): void {
this.data.mistake = mistake
StorageManager.getInstance().saveStage()
}
public get_difficulty(): number {
return this.data.difficulty
}
public get_duration(): number {
return this.data.duration
}
public set_duration(duration: number): void {//关卡计时
this.data.duration = duration
// StorageManager.getInstance().saveStage()
}
public get_note_open(): boolean {
return this.data.note_open
}
public set_note_open(note_open: boolean): void {
this.data.note_open = note_open
StorageManager.getInstance().saveStage()
}
//====================================持久化数据
public get_blocks(): Array<DOBlock> {
return this.blocks
}
public get_candys(): Array<DOCandy> {
return this.candys
}
}

View File

@@ -0,0 +1,3 @@
{
"uuid": "d88044d6-69d3-44d1-85bf-2d353609a8d2"
}

View File

@@ -9,36 +9,22 @@ export class Home extends Laya.Script {
@property(Laya.Button)
public btn_start: Laya.Button;
@property(Laya.Button)
public btn_clean: Laya.Button;
//组件被激活后执行,此时所有节点和组件均已创建完毕,此方法只执行一次
onAwake(): void {
this.btn_start.on(Laya.Event.CLICK, this, (evt: Laya.Event) => {
console.log("btn_start=", evt);
this.owner.visible = false;
UIManager.getInstance().loadStage();
this.owner.destroy()
})
this.btn_clean.on(Laya.Event.CLICK, this, (evt: Laya.Event) => {
console.log("btn_clean=", evt);
Laya.LocalStorage.clear()
})
}
//组件被启用后执行,例如节点被添加到舞台后
//onEnable(): void {}
//组件被禁用时执行,例如从节点从舞台移除后
//onDisable(): void {}
//第一次执行update之前执行只会执行一次
// onStart(): void {}
//手动调用节点销毁时执行
//onDestroy(): void {}
//每帧更新时执行尽量不要在这里写大循环逻辑或者使用getComponent方法
//onUpdate(): void {}
//每帧更新时执行在update之后执行尽量不要在这里写大循环逻辑或者使用getComponent方法
//onLateUpdate(): void {}
//鼠标点击后执行。与交互相关的还有onMouseDown等十多个函数具体请参阅文档。
//onMouseClick(): void {}
}

View File

@@ -1,6 +1,8 @@
import { UIManager } from "../models/UIManager";
import { Block } from "../types/Block";
import { Candy } from "../types/Candy";
import { DOStage } from "../types/DOStage";
const { regClass, property } = Laya;
@@ -8,22 +10,17 @@ const { regClass, property } = Laya;
export class Stage extends Laya.Script {
declare owner : Laya.Box;
private letterToNumber: any;
private numberToLetter: any;
@property(Laya.Text)
public label_stage_name: Laya.Text;
@property(Laya.Button)
public btn_back: Laya.Button
@property(Laya.Label)
public label_score: Laya.Label;
@property(Laya.Label)
public label_mistake: Laya.Label;
private mistake_now = 0
private mistake_all = 3
@property(Laya.Label)
public label_diff: Laya.Label;
@property(Laya.Label)
public label_time: Laya.Label;
private stage_last: number
@property(Laya.Box)
public obj_blocks: Laya.Box;
@@ -43,12 +40,13 @@ export class Stage extends Laya.Script {
public btn_erase: Laya.Box;
@property(Laya.Box)
public btn_note: Laya.Box;
private btn_note_normal: Laya.Box
private btn_note_checked: Laya.Box
@property(Laya.Box)
public btn_hint: Laya.Box;
private is_note_open: boolean = false
private btn_note_normal: Laya.Box
private btn_note_checked: Laya.Box
private selectedBlock: Block;
private data: DOStage;
@property(Laya.Box)
public obj_block_colors: Laya.Box;
@@ -65,33 +63,12 @@ export class Stage extends Laya.Script {
@property(Laya.Image)
public img_block_weak_wrong: Laya.Image;
private selectedBlock: Block;
//组件被激活后执行,此时所有节点和组件均已创建完毕,此方法只执行一次
onAwake(): void {
this.letterToNumber = new Map();
this.letterToNumber.set("A", 1);
this.letterToNumber.set("B", 2);
this.letterToNumber.set("C", 3);
this.letterToNumber.set("D", 4);
this.letterToNumber.set("E", 5);
this.letterToNumber.set("F", 6);
this.letterToNumber.set("G", 7);
this.letterToNumber.set("H", 8);
this.letterToNumber.set("I", 9);
this.numberToLetter = new Map();
this.numberToLetter.set(1, "a");
this.numberToLetter.set(2, "b");
this.numberToLetter.set(3, "c");
this.numberToLetter.set(4, "d");
this.numberToLetter.set(5, "e");
this.numberToLetter.set(6, "f");
this.numberToLetter.set(7, "g");
this.numberToLetter.set(8, "h");
this.numberToLetter.set(9, "i");
this.btn_back.on(Laya.Event.CLICK, this, ()=>{
UIManager.getInstance().loadHome();
this.owner.destroy()
})
for (var i=0; i<this.obj_blocks.numChildren; i++) {
var hbox = this.obj_blocks.getChildAt(i);
var l_hbox = this.obj_block_labels.getChildAt(i);
@@ -116,70 +93,32 @@ export class Stage extends Laya.Script {
var candy = this.obj_candys.getChildAt(i).getComponent(Candy);
this.candys.push(candy);
var box = this.obj_candy_labels.getChildAt(i) as Laya.Box
candy.onInit(i+1, box, this, this.onClickCandy)
candy.onInit(box, this, this.onClickCandy)
}
this.btn_undo.on(Laya.Event.CLICK, this, this.onClickUndo)
this.btn_erase.on(Laya.Event.CLICK, this, this.onClickErase)
this.btn_note.on(Laya.Event.CLICK, this, this.onClickNote)
this.btn_hint.on(Laya.Event.CLICK, this, this.onClickHint)
this.btn_note_normal = this.btn_note.getChildByName("normal") as Laya.Box
this.btn_note_checked = this.btn_note.getChildByName("checked") as Laya.Box
this.btn_note_normal.visible = this.is_note_open == false
this.btn_note_checked.visible = this.is_note_open
this.btn_hint.on(Laya.Event.CLICK, this, this.onClickHint)
this.selectedBlock = null;
this.obj_block_colors.visible = false
}
public onLoadStage(levelStr: string): void {
this.mistake_now = 0
this.selectedBlock = null;
if (levelStr.length == 81 && this.blocks.length == 81) {
var list = new Map();
for (var i=0; i<levelStr.length; i++) {
var letter = levelStr[i];
var block = this.blocks[i];
if (this.letterToNumber.has(letter)) {
var showNumber = this.letterToNumber.get(letter);
block.setBlock(true, showNumber, showNumber);
}
else {
var showNumber = this.letterToNumber.get(letter.toUpperCase());
block.setBlock(false, 0, showNumber);
if (list.has(letter)) {
list.set(letter, list.get(letter)+1);
}
else {
list.set(letter, 1);
}
}
}
for (var i=0; i<this.candys.length; i++) {
var left = 0;
var c = this.numberToLetter.get(i+1);
if (list.has(c))
left = list.get(c);
var candy = this.candys[i];
candy.setLeft(left);
}
}
else {
console.log("onLoadStage error", levelStr.length, this.blocks.length)
}
public onLoadStage(doStage: DOStage): void {
this.data = doStage
this.onUpdateScore()
this.onUpdateMistake()
this.stage_last = 0
var begin = Date.now()
Laya.timer.loop(1000, this, ()=>{
var now = Date.now()
this.stage_last += Math.floor((now-begin)/1000)
this.data.set_duration(this.data.get_duration() + Math.floor((now-begin)/1000))
begin = now
var last = this.stage_last
var last = this.data.get_duration()
var hour = Math.floor(last/3600)
last = last - hour * 3600
var minite = Math.floor(last/60)
@@ -187,18 +126,34 @@ export class Stage extends Laya.Script {
var second = Math.floor(last)
this.label_time.text = `${hour>=10?hour:("0"+hour)}:${minite>=10?minite:("0"+minite)}:${second>10?second:("0"+second)}`
})
var blocks = doStage.get_blocks()
for (var i=0; i<blocks.length; i++) {
var doBlock = blocks[i]
var block = this.blocks[i]
block.setBlock(doBlock)
}
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.onClickNote(null)
}
onUpdateScore(): void {
var score = 0
for (var i=0; i<this.blocks.length; i++) {
score += this.blocks[i].getScore()
var blocks = this.data.get_blocks()
for (var i=0; i<blocks.length; i++) {
score += blocks[i].get_score()
}
this.label_score.text = `分数: ${score}`
}
onUpdateMistake(): void {
this.label_mistake.text = `${this.mistake_now}/${this.mistake_all}`
this.label_mistake.text = `${this.data.get_mistake()}/${this.mistake_all}`
}
onSetBlock(block: Block): void {
@@ -212,7 +167,7 @@ export class Stage extends Laya.Script {
relateds[i].setButtonSkin(this.img_block_grey.skin)
//空白格子
if (block.getShowNumber() <= 0) {
if (block.getData().get_show() <= 0) {
block.setButtonSkin(this.img_block_correct.skin)
}
else {
@@ -221,12 +176,12 @@ export class Stage extends Laya.Script {
for (var i=0; i<sames.length; i++)
sames[i].setButtonSkin(this.img_block_weak_correct.skin)
if (block.getIsChecked()) {//正确的格子
if (block.getData().get_checked()) {//正确的格子
block.setButtonSkin(this.img_block_correct.skin)
}
else {//错误的格子
for (var i=0; i<relateds.length; i++) {
if (relateds[i].getShowNumber() == block.getShowNumber())
if (relateds[i].getData().get_show() == block.getData().get_show())
relateds[i].setButtonSkin(this.img_block_weak_wrong.skin)
}
block.setButtonSkin(this.img_block_wrong.skin)
@@ -248,7 +203,7 @@ export class Stage extends Laya.Script {
}
onClickErase(evt: Laya.Event): void {
if (this.selectedBlock && this.selectedBlock.getIsChecked() == false) {
if (this.selectedBlock && this.selectedBlock.getData().get_checked() == false) {
this.selectedBlock.setShowNumber(0)
this.onUpdateScore()
this.onSetBlock(this.selectedBlock)
@@ -256,16 +211,18 @@ export class Stage extends Laya.Script {
}
onClickNote(evt: Laya.Event): void {
this.is_note_open = !this.is_note_open
this.btn_note_normal.visible = this.is_note_open == false
this.btn_note_checked.visible = this.is_note_open
if (evt != null) {
this.data.set_note_open(!this.data.get_note_open())
}
this.btn_note_normal.visible = this.data.get_note_open() == false
this.btn_note_checked.visible = this.data.get_note_open()
}
onClickHint(evt: Laya.Event): void {
var list: Array<Block> = new Array()
for (var i=0; i<this.blocks.length; i++) {
var block = this.blocks[i]
if (block.getIsChecked() == false)
if (block.getData().get_checked() == false)
list.push(block)
}
if (list.length > 0) {
@@ -274,7 +231,7 @@ export class Stage extends Laya.Script {
if (index >= list.length)
index = list.length -1
var block = list[index]
block.setShowNumber(block.getCorrectNumber())
block.setShowNumber(block.getData().get_correct())
this.onUpdateScore()
this.onSetBlock(block)
this.selectedBlock = block
@@ -282,13 +239,13 @@ export class Stage extends Laya.Script {
}
public onApplyCandy(candy: Candy): void {
if (this.selectedBlock && this.selectedBlock.getIsChecked() == false) {
var showNumber = candy.getShowNumber()
if (this.is_note_open) {
if (this.selectedBlock && this.selectedBlock.getData().get_checked() == false) {
var showNumber = candy.getData().get_show()
if (this.data.get_note_open()) {
var relateds = this.findRelatedBlocks(this.selectedBlock)
var find = false
for (var i=0; i<relateds.length; i++) {
if (relateds[i].getShowNumber() == showNumber) {
if (relateds[i].getData().get_show() == showNumber) {
find = true
break
}
@@ -299,7 +256,7 @@ export class Stage extends Laya.Script {
else {
for (var i=0; i<relateds.length; i++) {
var obj = relateds[i]
if (obj.getShowNumber() == showNumber) {
if (obj.getData().get_show() == showNumber) {
obj.setFadeAnimation()
}
}
@@ -317,9 +274,9 @@ export class Stage extends Laya.Script {
}
}
else {//失败
this.mistake_now ++
this.data.set_mistake(this.data.get_mistake()+1)
this.onUpdateMistake()
if (this.mistake_now >= this.mistake_all) {
if (this.data.get_mistake() >= this.mistake_all) {
//关卡失败
}
}
@@ -358,7 +315,7 @@ export class Stage extends Laya.Script {
var list: Array<Block> = new Array()
for (var i=0; i<this.blocks.length; i++) {
var block = this.blocks[i]
if (block.getShowNumber() == obj.getShowNumber())
if (block.getData().get_show() == obj.getData().get_show())
list.push(block)
}
return list
@@ -368,7 +325,7 @@ export class Stage extends Laya.Script {
var b = true;
for (var i=1; i<this.blocks.length; i++) {
var block = this.blocks[i];
if (block.getIsChecked() == false) {
if (block.getData().get_checked() == false) {
b = false;
break
}