添加持久化逻辑
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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
80
src/types/DOBlock.ts
Normal 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
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
3
src/types/DOBlock.ts.meta
Normal file
3
src/types/DOBlock.ts.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"uuid": "aeb99474-1a9f-4a01-ac79-3416ec39a309"
|
||||
}
|
||||
33
src/types/DOCandy.ts
Normal file
33
src/types/DOCandy.ts
Normal 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
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
3
src/types/DOCandy.ts.meta
Normal file
3
src/types/DOCandy.ts.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"uuid": "0c3f05a2-7935-4847-8760-858002151854"
|
||||
}
|
||||
139
src/types/DOStage.ts
Normal file
139
src/types/DOStage.ts
Normal 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
|
||||
}
|
||||
}
|
||||
3
src/types/DOStage.ts.meta
Normal file
3
src/types/DOStage.ts.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"uuid": "d88044d6-69d3-44d1-85bf-2d353609a8d2"
|
||||
}
|
||||
Reference in New Issue
Block a user