Files
sudoku/src/views/TopBar.ts

103 lines
3.6 KiB
TypeScript
Raw Normal View History

2025-05-23 20:47:03 +08:00
import { config } from "../constants/config";
import { StorageManager } from "../models/StorageManager";
import { UIManager } from "../models/UIManager";
import { CommonData } from "./common/CommonData";
const { regClass, property } = Laya;
@regClass()
export class TopBar extends Laya.Script {
declare owner : Laya.Box;
@property(Laya.Box)
public btn_classic: Laya.Box
@property(Laya.Sprite)
public sprite_classic: Laya.Sprite
@property(Laya.Label)
public label_classic: Laya.Label
@property(Laya.Box)
public btn_dc: Laya.Box
@property(Laya.Sprite)
public sprite_dc: Laya.Sprite
@property(Laya.Label)
public label_dc: Laya.Label
private isClassic: boolean = true
@property(Laya.Box)
public obj_items: Laya.Box
private items: Map<string, Laya.Label> = new Map()
onStart(): void {
this.btn_classic.on(Laya.Event.CLICK, this, ()=>{
if (this.isClassic == false) {
this.isClassic = true
this.setLeftTab()
}
})
this.btn_dc.on(Laya.Event.CLICK, this, ()=>{
if (this.isClassic) {
this.isClassic = false
this.setLeftTab()
}
})
this.setLeftTab()
for (var i=0; i<this.obj_items.numChildren; i++) {
var obj = this.obj_items.getChildAt(i)
var value = config.DIFFICULTY_LIST[i]
var label = obj.getChildByName("Label") as Laya.Label
label.text = value
this.items.set(value, label)
var common = obj.getComponent(CommonData)
common.strValue = value
obj.on(Laya.Event.CLICK, this, (evt: Laya.Event)=>{
var common = evt.target.getComponent(CommonData)
var difficulty = Laya.LocalStorage.getItem("difficulty")
if (common.strValue != difficulty) {
this.setDifficulty(common.strValue)
Laya.LocalStorage.setItem("difficulty", common.strValue)//记录横屏模式用户选择的难度
StorageManager.getInstance().cleanStage()
StorageManager.getInstance().newStage(config.DEFAULT_STAGE_ID, config.STAGE_TYPE.MAIN, difficulty, difficulty)
UIManager.getInstance().loadStageUI(config.DEFAULT_STAGE_ID)
}
})
}
//如果是第一次以横屏启动游戏需要创建经典关卡
var difficulty = Laya.LocalStorage.getItem("difficulty")//读取用户上次选择的难度
if (!difficulty || difficulty.length<=0) {
difficulty = config.DIFFICULTY_TYPE.Easy
Laya.LocalStorage.setItem("difficulty", difficulty)
}
var doStage = StorageManager.getInstance().loadStage(config.DEFAULT_STAGE_ID)
if (!doStage) {
StorageManager.getInstance().newStage(config.DEFAULT_STAGE_ID, config.STAGE_TYPE.MAIN, difficulty, difficulty)
}
this.setDifficulty(difficulty)
}
setLeftTab(): void {
this.sprite_classic.alpha = this.isClassic ? 1 : 0.1
this.label_classic.color = this.isClassic ? "#ffffff" : "#0e2a53"
this.sprite_dc.alpha = this.isClassic ? 0.1 : 1
this.label_dc.color = this.isClassic ? "#0e2a53" : "#ffffff"
if (this.isClassic) {
UIManager.getInstance().loadStageUI(config.DEFAULT_STAGE_ID)
}
}
setDifficulty(difficulty: string): void {
this.items.forEach((label: Laya.Label, key: string)=>{
label.color = "#0e2a53"
})
var label = this.items.get(difficulty)
label.color = "#1d5cdc"
}
}