Files
sudoku/src/views/TopBar.ts

150 lines
5.7 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";
2025-05-26 19:57:17 +08:00
import { G_ShowCommonTips } from "./CommonTips";
2025-05-23 20:47:03 +08:00
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
2025-05-26 19:57:17 +08:00
@property(Laya.Label)
public obj_label: Laya.Label
2025-05-23 20:47:03 +08:00
@property(Laya.Box)
public obj_items: Laya.Box
private items: Map<string, Laya.Label> = new Map()
2025-05-26 19:57:17 +08:00
onStart(): void {
//如果是第一次以横屏启动游戏需要创建经典关卡
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().getStage(config.DEFAULT_STAGE_ID)
if (!doStage) {
StorageManager.getInstance().newStage(config.DEFAULT_STAGE_ID, config.STAGE_TYPE.MAIN, difficulty, difficulty)
}
else {
var user = StorageManager.getInstance().getUser()
if (user.get_done(config.DEFAULT_STAGE_ID) >= 1) {
console.log("当前主线关卡完成")
user.update_progress(doStage.get_difficulty(), doStage.get_stageIndex()+1)//主线关卡更新难度进度
StorageManager.getInstance().newStage(config.DEFAULT_STAGE_ID, config.STAGE_TYPE.MAIN, difficulty, difficulty)
}
else if (doStage.get_mistake() >= config.MISTAKE_MAX) {//上次失败了则重新开始
StorageManager.getInstance().newStage(config.DEFAULT_STAGE_ID, config.STAGE_TYPE.MAIN, difficulty, difficulty)
}
}
2025-05-23 20:47:03 +08:00
this.btn_classic.on(Laya.Event.CLICK, this, ()=>{
if (this.isClassic == false) {
this.isClassic = true
2025-05-26 19:57:17 +08:00
this.onClickTab()
2025-05-23 20:47:03 +08:00
}
})
this.btn_dc.on(Laya.Event.CLICK, this, ()=>{
if (this.isClassic) {
this.isClassic = false
2025-05-26 19:57:17 +08:00
this.onClickTab()
2025-05-23 20:47:03 +08:00
}
})
2025-05-26 19:57:17 +08:00
this.onClickTab()
2025-05-23 20:47:03 +08:00
2025-05-26 19:57:17 +08:00
var showToggle = Laya.LocalStorage.getItem("showToggle")
if (!showToggle || showToggle.length <= 0) {
Laya.LocalStorage.setItem("showToggle", "true")
}
2025-05-23 20:47:03 +08:00
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) {
2025-05-26 19:57:17 +08:00
if (Laya.LocalStorage.getItem("showToggle")=="true") {
var title = "Start New Game"
var content = "Current game progress will be lost"
G_ShowCommonTips(title, content, true, (ok: boolean, toggleValue: boolean)=>{
if (ok) {
if (toggleValue) {
Laya.LocalStorage.setItem("showToggle", "false")
}
this.onClickDifficulty(common.strValue)
}
})
}
else {
this.onClickDifficulty(common.strValue)
}
2025-05-23 20:47:03 +08:00
}
})
}
2025-05-26 19:57:17 +08:00
this.updateDifficulty(difficulty)
}
2025-05-23 20:47:03 +08:00
2025-05-26 19:57:17 +08:00
onClickTab(): void {
if (this.isClassic) {
UIManager.getInstance().closeTrophyUI()
UIManager.getInstance().closeDCUI()
UIManager.getInstance().loadStageUI(config.DEFAULT_STAGE_ID)
2025-05-23 20:47:03 +08:00
}
2025-05-26 19:57:17 +08:00
else {
UIManager.getInstance().closeStageUI()
UIManager.getInstance().loadDCUI()
2025-05-23 20:47:03 +08:00
}
2025-05-26 19:57:17 +08:00
this.updateLeftTab()
2025-05-23 20:47:03 +08:00
}
2025-05-26 19:57:17 +08:00
updateLeftTab(): void {
2025-05-23 20:47:03 +08:00
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"
2025-05-26 19:57:17 +08:00
this.obj_label.visible = this.isClassic
this.obj_items.visible = this.isClassic
}
onClickDifficulty(difficulty: string): void {
Laya.LocalStorage.setItem("difficulty", difficulty)//记录横屏模式用户选择的难度
StorageManager.getInstance().newStage(config.DEFAULT_STAGE_ID, config.STAGE_TYPE.MAIN, difficulty, difficulty)
UIManager.getInstance().loadStageUI(config.DEFAULT_STAGE_ID)
this.updateDifficulty(difficulty)
2025-05-23 20:47:03 +08:00
}
2025-05-26 19:57:17 +08:00
updateDifficulty(difficulty: string): void {
2025-05-23 20:47:03 +08:00
this.items.forEach((label: Laya.Label, key: string)=>{
label.color = "#0e2a53"
})
var label = this.items.get(difficulty)
label.color = "#1d5cdc"
}
}