Files
sudoku/src/views/Block.ts
2025-06-06 14:42:20 +08:00

209 lines
6.1 KiB
TypeScript

import type { DOBlock } from "../models/DOBlock"
import { G_ShowScoreTips } from "../views/common/GUITips"
const { regClass, property } = Laya
@regClass()
export class Block extends Laya.Script {
declare owner: Laya.Box
@property(Number)
public XIndex: number = 0
@property(Number)
public YIndex: number = 0
@property(Number)
public XGroup: number = 0
@property(Number)
public YGroup: number = 0
private sprite_show: Laya.Sprite
private blockSize: number
private obj_notes: Laya.Box
private label_show: Laya.Label
private tips_bgs: Array<Laya.Sprite> = []
private tips_labels: Array<Laya.Label> = []
private tipsSize: number
private selected: boolean = false
private data: DOBlock
public onInit(XIndex: number, YIndex: number, XGroup: number, YGroup: number, handler: any, func: any): void {
this.XIndex = XIndex
this.YIndex = YIndex
this.XGroup = XGroup
this.YGroup = YGroup
this.sprite_show = this.owner.getChildByName("Sprite") as Laya.Sprite
this.blockSize = this.sprite_show.width
this.label_show = this.owner.getChildByName("label_show") as Laya.Label
const notes = this.owner.getChildByName("notes") as Laya.Box
this.obj_notes = notes
for (let i = 0; i < notes.numChildren; i++) {
const hbox = notes.getChildAt(i)
for (let j = 0; j < hbox.numChildren; j++) {
const sp = hbox.getChildAt(j).getChildByName("Sprite") as Laya.Sprite
this.tips_bgs.push(sp)
const Label = hbox.getChildAt(j).getChildByName("Label") as Laya.Label
Label.text = ""
this.tips_labels.push(Label)
this.tipsSize = sp.width
}
}
this.owner.on(Laya.Event.CLICK, handler, func)
}
public setSelected(selected: boolean): void {
this.selected = selected
this.updateBlock()
}
public setPaused(paused: boolean): void {
this.sprite_show.visible = !paused
this.obj_notes.visible = !paused
this.label_show.visible = !paused
}
public setBlockColor(color: string, alpha: number = 1, animated: boolean = false, completedColor: string = "", completedAlpha: number = 1): void {
this.sprite_show.graphics.clear()
this.sprite_show.graphics.drawRect(0, 0, this.blockSize, this.blockSize, color)
if (animated) {
this.sprite_show.alpha = 0
Laya.Tween.to(this.sprite_show, { alpha }, 100, Laya.Ease.linearIn, Laya.Handler.create(this, () => {
Laya.Tween.to(this.sprite_show, { alpha: 0 }, 150, Laya.Ease.linearIn, Laya.Handler.create(this, () => {
Laya.Tween.to(this.sprite_show, { alpha }, 100, Laya.Ease.linearIn, Laya.Handler.create(this, () => {
Laya.Tween.to(this.sprite_show, { alpha: 0 }, 150, Laya.Ease.linearIn, Laya.Handler.create(this, () => {
this.sprite_show.graphics.clear()
this.sprite_show.graphics.drawRect(0, 0, this.blockSize, this.blockSize, completedColor)
this.sprite_show.alpha = completedAlpha
}))
}))
}))
}))
// Laya.Handler.create(this, () => {
// })
} else {
this.sprite_show.alpha = alpha
}
}
public setFadeAnimation(): void {
Laya.timer.once(200, this, () => {
this.label_show.color = "#ff0000"
Laya.timer.once(100, this, () => {
this.label_show.color = "#2d3138"
Laya.timer.once(200, this, () => {
this.label_show.color = "#ff0000"
Laya.timer.once(100, this, () => {
this.updateBlock()
})
})
})
})
}
public setTipsBgColor(visible: boolean, noteNumber: number = 0, color: string = "", alpha: number = 1): void {
if (visible) {
const obj = this.tips_bgs[noteNumber - 1]
obj.visible = true
obj.graphics.clear()
obj.graphics.drawRect(0, 0, this.tipsSize, this.tipsSize, color)
obj.alpha = alpha
} else {
for (let i = 0; i < this.tips_bgs.length; i++) {
const obj = this.tips_bgs[i]
obj.visible = false
}
}
}
public setBlock(data: DOBlock): void {
this.data = data
this.updateBlock()
}
public getData(): DOBlock {
return this.data
}
public setShowNumber(showNumber: number, score: number): boolean {
this.data.set_show(showNumber)
if (this.data.get_checked()) {
this.data.set_score(score)
if (score > 0)
G_ShowScoreTips(`+${score}`, this.owner)
} else {
if (this.data.get_score() !== 0)
this.data.set_score(0)
}
this.data.clean_notes()
this.updateBlock()
return this.data.get_checked()
}
public addNoteNumber(noteNumber: number): void {
this.data.set_show(0)
const note_numbers = this.data.get_notes()
const find = note_numbers.indexOf(noteNumber)
if (find >= 0) {
this.data.remove_note(noteNumber)
} else {
this.data.add_note(noteNumber)
}
this.updateBlock()
}
public setNotes(notes: Array<number>): void {
this.data.set_show(0)
this.data.clean_notes()
for (let i = 0; i < notes.length; i++) {
this.data.add_note(notes[i])
}
this.updateBlock()
}
updateBlock(): void {
// 设置文字颜色
if (this.selected) {
this.label_show.color = "#ffffff"
for (let i = 0; i < this.tips_labels.length; i++) {
this.tips_labels[i].color = "#ffffff"
}
} else {
if (this.data.get_show() === this.data.get_correct()) {
if (this.data.get_preset()) {
this.label_show.color = "#2d3138"
} else {
this.label_show.color = "#2559c0"
}
} else {
this.label_show.color = "#cb3256"
}
for (let i = 0; i < this.tips_labels.length; i++) {
this.tips_labels[i].color = "#2d3138"
}
}
for (let i = 0; i < this.tips_labels.length; i++) {
this.tips_labels[i].text = ""
}
if (this.data.get_show() > 0) {
this.label_show.text = this.data.get_show().toString()
} else {
this.label_show.text = ""
const note_numbers = this.data.get_notes()
for (let i = 0; i < note_numbers.length; i++) {
const value = note_numbers[i]
const label = this.tips_labels[value - 1]
label.text = value.toString()
}
}
}
}