Files
sudoku/src/views/Block.ts

195 lines
6.1 KiB
TypeScript
Raw Normal View History

2025-05-13 14:17:33 +08:00
import { G_ShowScoreTips } from "../views/GUITips";
2025-05-14 19:22:21 +08:00
import { DOBlock } from "../models/DOBlock";
2025-05-13 14:17:33 +08:00
2025-05-09 19:10:40 +08:00
const { regClass, property } = Laya;
@regClass()
export class Block extends Laya.Script {
2025-05-15 21:36:03 +08:00
declare owner : Laya.Sprite;
2025-05-09 19:10:40 +08:00
@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-15 21:36:03 +08:00
private tips_bgs: Array<Laya.Sprite> = new Array();
2025-05-12 21:18:27 +08:00
private label_show: Laya.Label;
private tips_labels: Array<Laya.Label> = new Array();
2025-05-13 20:31:44 +08:00
private selected: boolean = false
private newSelect: boolean = false
private data: DOBlock
2025-05-12 21:18:27 +08:00
2025-05-15 21:36:03 +08:00
2025-05-12 21:18:27 +08:00
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-15 21:36:03 +08:00
var VBox = this.owner.getChildByName("VBox")
for (var i=0; i<VBox.numChildren; i++) {
var hbox = VBox.getChildAt(i)
for (var j=0; j<hbox.numChildren; j++) {
var sp = hbox.getChildAt(j) as Laya.Sprite
this.tips_bgs.push(sp)
}
}
2025-05-12 10:43:02 +08:00
this.label_show = label_obj.getChildByName("label_show") as Laya.Label
2025-05-16 21:10:37 +08:00
// this.label_show.text = XGroup + "" + YGroup
2025-05-15 21:36:03 +08:00
VBox = label_obj.getChildByName("VBox")
2025-05-12 10:43:02 +08:00
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
}
public setSelected(selected: boolean): void {
this.selected = selected
2025-05-13 14:17:33 +08:00
if (selected)
this.newSelect = true
2025-05-12 21:18:27 +08:00
this.updateBlock()
2025-05-09 19:10:40 +08:00
}
2025-05-15 21:36:03 +08:00
public setBlockColor(color: string, alpha: number=1, animated: boolean=false, completedColor: string = "", completedAlpha: number=1): void {
this.owner.graphics.clear()
this.owner.graphics.drawRect(0, 0, 116, 116, color)
if (animated) {
Laya.Tween.to(this.owner, {alpha:alpha}, 500, Laya.Ease.elasticOut, Laya.Handler.create(this, () => {
this.owner.graphics.clear()
this.owner.graphics.drawRect(0, 0, 116, 116, completedColor)
this.owner.alpha = completedAlpha
}))
}
else {
this.owner.alpha = alpha
}
2025-05-09 19:10:40 +08:00
}
2025-05-12 21:18:27 +08:00
public setFadeAnimation(): void {
2025-05-15 21:36:03 +08:00
var color = this.label_show.color
var flag = true
Laya.timer.loop(200, this, ()=>{
flag = !flag
if (flag) {
this.label_show.color = color
}
else {
this.label_show.color = "#ff0000"
}
})
Laya.Tween.to(this.label_show, {color:"#ff0000"}, 1000, Laya.Ease.strongIn, Laya.Handler.create(this, () => {
this.label_show.color = color
Laya.timer.clearAll(this)
}))
}
public setTipsBgColor(visible: boolean, noteNumber: number=0, color: string="", alpha: number=1): void {
if (visible) {
var obj = this.tips_bgs[noteNumber-1]
obj.visible = true
obj.graphics.clear()
obj.graphics.drawRect(0, 0, 38, 38, color)
obj.alpha = alpha
}
else {
for (var i=0; i<this.tips_bgs.length; i++) {
var obj = this.tips_bgs[i]
obj.visible = false
}
2025-05-12 21:18:27 +08:00
}
}
2025-05-09 19:10:40 +08:00
2025-05-13 20:31:44 +08:00
public setBlock(data: DOBlock): void {
this.data = data;
2025-05-09 19:10:40 +08:00
this.updateBlock();
}
2025-05-13 20:31:44 +08:00
public getData(): DOBlock {
return this.data
2025-05-09 19:10:40 +08:00
}
public setShowNumber(showNumber: number): boolean {
2025-05-13 20:31:44 +08:00
if (this.data.get_checked() == false) {
this.data.set_show(showNumber)
var score: number = 0
2025-05-13 14:17:33 +08:00
if (this.newSelect) {
this.newSelect = false
2025-05-13 20:31:44 +08:00
if (this.data.get_checked()) {
score = 150
this.data.set_score(score)
2025-05-15 21:36:03 +08:00
G_ShowScoreTips(score.toString(), this.label_show)
2025-05-13 14:17:33 +08:00
}
}
2025-05-15 21:36:03 +08:00
this.data.clean_notes()
2025-05-13 20:31:44 +08:00
this.updateBlock()
2025-05-09 19:10:40 +08:00
}
2025-05-13 20:31:44 +08:00
return this.data.get_checked()
2025-05-13 14:17:33 +08:00
}
2025-05-12 21:18:27 +08:00
public addNoteNumber(noteNumber: number): void {
2025-05-15 21:36:03 +08:00
this.data.set_show(0)
var note_numbers = this.data.get_notes()
var find = note_numbers.indexOf(noteNumber)
if (find >= 0) {
this.data.remove_note(noteNumber)
}
else {
this.data.add_note(noteNumber)
2025-05-12 21:18:27 +08:00
}
2025-05-15 21:36:03 +08:00
this.updateBlock()
2025-05-12 21:18:27 +08:00
}
2025-05-09 19:10:40 +08:00
updateBlock(): void {
2025-05-15 21:36:03 +08:00
//设置文字颜色
if (this.selected) {
this.label_show.color = "#ffffff"
for (var 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"
}
2025-05-12 10:43:02 +08:00
}
else {
2025-05-15 21:36:03 +08:00
this.label_show.color = "#cb3256"
}
for (var i=0; i<this.tips_labels.length; i++) {
this.tips_labels[i].color = "#2d3138"
2025-05-12 10:43:02 +08:00
}
2025-05-09 19:10:40 +08:00
}
2025-05-15 21:36:03 +08:00
for (var 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()
}
2025-05-09 19:10:40 +08:00
else {
2025-05-12 10:43:02 +08:00
this.label_show.text = "";
2025-05-13 20:31:44 +08:00
var note_numbers = this.data.get_notes()
for (var i=0; i<note_numbers.length; i++) {
var value = note_numbers[i]
2025-05-12 21:18:27 +08:00
var label = this.tips_labels[value-1]
label.text = value.toString()
}
2025-05-09 19:10:40 +08:00
}
}
}