Files
sudoku/src/types/Block.ts

136 lines
4.0 KiB
TypeScript
Raw Normal View History

2025-05-09 19:10:40 +08:00
const { regClass, property } = Laya;
@regClass()
export class Block extends Laya.Script {
declare owner : Laya.Button;
@property(Number)
public XIndex: number = 0;
@property(Number)
public YIndex: number = 0;
@property(Number)
public XGroup: number = 0;
@property(Number)
public YGroup: number = 0;
2025-05-12 21:18:27 +08:00
private selected: boolean = false
2025-05-09 19:10:40 +08:00
private preset: boolean = false//预设格子
private checked: boolean = false//正确格子
private showNumber: number = 0;
private correctNumber: number = 0;
2025-05-12 21:18:27 +08:00
private note_numbers: Array<number> = new Array()
private label_show: Laya.Label;
private tips_labels: Array<Laya.Label> = new Array();
private animator: Laya.Animator2D
public onInit(XIndex: number, YIndex: number, XGroup: number, YGroup: number, label_obj: Laya.Box, handler: any, func: any): void {
this.XIndex = XIndex;
this.YIndex = YIndex;
this.XGroup = XGroup;
this.YGroup = YGroup;
2025-05-09 19:10:40 +08:00
2025-05-12 10:43:02 +08:00
this.label_show = label_obj.getChildByName("label_show") as Laya.Label
var VBox = label_obj.getChildByName("VBox")
for (var i=0; i<VBox.numChildren; i++) {
var hbox = VBox.getChildAt(i);
for (var j=0; j<hbox.numChildren; j++) {
var Label = hbox.getChildAt(j) as Laya.Label
Label.text = ""
this.tips_labels.push(Label)
}
}
2025-05-09 19:10:40 +08:00
this.owner.on(Laya.Event.CLICK, handler, func);
2025-05-12 21:18:27 +08:00
this.animator = this.owner.getComponent(Laya.Animator2D)
}
public setSelected(selected: boolean): void {
this.selected = selected
this.updateBlock()
2025-05-09 19:10:40 +08:00
}
public setButtonSkin(skin: string): void {
this.owner.skin = skin
}
2025-05-12 21:18:27 +08:00
public setFadeAnimation(): void {
if (this.animator) {
this.animator.play("fade_to_red")
Laya.timer.frameOnce(40, this, ()=>{
this.animator.stop()
})
}
}
2025-05-09 19:10:40 +08:00
public setBlock(preset: boolean, showNumber: number, correctNumber: number): void {
this.preset = preset;
this.checked = preset;
this.showNumber = showNumber;
this.correctNumber = correctNumber;
this.updateBlock();
}
public getIsPreset(): boolean {
return this.preset;
}
public getIsChecked(): boolean {
return this.checked;
}
public getShowNumber(): number {
return this.showNumber;
}
2025-05-12 21:18:27 +08:00
public getCorrectNumber(): number {
return this.correctNumber;
}
2025-05-09 19:10:40 +08:00
public setShowNumber(showNumber: number): boolean {
if (this.checked == false) {
if (showNumber == this.correctNumber)
this.checked = true;
this.showNumber = showNumber;
2025-05-12 21:18:27 +08:00
if (this.checked || showNumber == 0)//正确或者擦除
this.note_numbers = new Array()
2025-05-09 19:10:40 +08:00
this.updateBlock();
}
return this.checked
}
2025-05-12 21:18:27 +08:00
public addNoteNumber(noteNumber: number): void {
if (this.note_numbers.indexOf(noteNumber) < 0) {
this.note_numbers.push(noteNumber)
this.updateBlock();
}
}
2025-05-09 19:10:40 +08:00
updateBlock(): void {
2025-05-12 21:18:27 +08:00
for (var i=0; i<this.tips_labels.length; i++)
this.tips_labels[i].text = ""
2025-05-09 19:10:40 +08:00
if (this.showNumber > 0) {
2025-05-12 10:43:02 +08:00
this.label_show.text = this.showNumber.toString();
if (this.checked) {
this.label_show.color = "#000000"
}
else {
this.label_show.color = "#ff0000"
}
2025-05-09 19:10:40 +08:00
}
else {
2025-05-12 10:43:02 +08:00
this.label_show.text = "";
2025-05-12 21:18:27 +08:00
for (var i=0; i<this.note_numbers.length; i++) {
var value = this.note_numbers[i]
var label = this.tips_labels[value-1]
label.text = value.toString()
if (this.selected) {
label.color = "#ffffff"
}
else {
label.color = "#000000"
}
}
2025-05-09 19:10:40 +08:00
}
}
}