补充UI细节 添加每日挑战
This commit is contained in:
161
src/views/DailyChallenge.ts
Normal file
161
src/views/DailyChallenge.ts
Normal file
@@ -0,0 +1,161 @@
|
||||
import { config } from "../constants/config";
|
||||
import { Utility_ConvertSecondToString } from "../utils/utility";
|
||||
import { StorageManager } from "../models/StorageManager";
|
||||
import { CalendarUnit } from "./CalendarUnit";
|
||||
import { LevelManager } from "../models/LevelManager";
|
||||
import { UIManager } from "../models/UIManager";
|
||||
|
||||
const { regClass, property } = Laya;
|
||||
|
||||
@regClass()
|
||||
export class DailyChallenge extends Laya.Script {
|
||||
declare owner : Laya.Box;
|
||||
|
||||
@property(Laya.Button)
|
||||
public btn_back: Laya.Button
|
||||
|
||||
@property(Laya.Label)
|
||||
public label_year: Laya.Label;
|
||||
@property(Laya.Label)
|
||||
public label_month: Laya.Label;
|
||||
@property(Laya.Label)
|
||||
public label_star: Laya.Label;
|
||||
|
||||
@property(Laya.Box)
|
||||
public btn_play: Laya.Box
|
||||
@property(Laya.Label)
|
||||
public label_play: Laya.Label
|
||||
@property(Laya.Box)
|
||||
public obj_summary: Laya.Box
|
||||
@property(Laya.Label)
|
||||
public label_date: Laya.Label;
|
||||
@property(Laya.Label)
|
||||
public label_diffucuty: Laya.Label
|
||||
@property(Laya.Label)
|
||||
public label_score: Laya.Label
|
||||
@property(Laya.Label)
|
||||
public label_time: Laya.Label
|
||||
|
||||
@property(Laya.Box)
|
||||
public obj_items: Laya.Box;
|
||||
@property(Laya.Box)
|
||||
public obj_labels: Laya.Box;
|
||||
private units: Array<CalendarUnit> = new Array();
|
||||
private selectedUnit: CalendarUnit;
|
||||
|
||||
//组件被激活后执行,此时所有节点和组件均已创建完毕,此方法只执行一次
|
||||
onAwake(): void {
|
||||
this.btn_back.on(Laya.Event.CLICK, this, ()=>{
|
||||
UIManager.getInstance().loadHomeUI()
|
||||
this.owner.destroy()
|
||||
})
|
||||
|
||||
var now = new Date()
|
||||
var year = now.getFullYear()
|
||||
var month = now.getMonth() + 1
|
||||
var dayNow = new Date().getDate()
|
||||
var stageID = `${year}-${month}-${day}`
|
||||
|
||||
this.label_year.text = year.toString()
|
||||
this.label_month.text = config.MONTH_ABBRS[month-1]
|
||||
var starCount = 0
|
||||
var day = 1
|
||||
var dayCount = 31
|
||||
var begin = 4
|
||||
var index = -1
|
||||
for (var i=0; i<this.obj_items.numChildren; i++) {
|
||||
var hbox = this.obj_items.getChildAt(i)
|
||||
var l_hbox = this.obj_labels.getChildAt(i)
|
||||
for (var j=0; j<hbox.numChildren; j++) {
|
||||
var obj = hbox.getChildAt(j) as Laya.Box
|
||||
var l_obj = l_hbox.getChildAt(j) as Laya.Label
|
||||
index ++
|
||||
if (index >= begin && index < begin+dayCount) {
|
||||
obj.visible = true
|
||||
l_obj.visible = true
|
||||
var unit = obj.getComponent(CalendarUnit)
|
||||
this.units.push(unit)
|
||||
var stageID = `${year}-${month}-${day}`
|
||||
var progress = StorageManager.getInstance().getUser().get_done(stageID)
|
||||
unit.onInit(day, day<=dayNow, progress, l_obj, this, this.onClickUnit)
|
||||
if (day == dayNow) {
|
||||
this.selectedUnit = unit
|
||||
}
|
||||
if (progress >= 1) {
|
||||
starCount ++
|
||||
}
|
||||
day ++
|
||||
}
|
||||
else {
|
||||
obj.visible = false
|
||||
l_obj.visible = false
|
||||
}
|
||||
}
|
||||
}
|
||||
this.label_star.text = `${starCount}/${dayCount}`
|
||||
|
||||
this.onApplyUnit(this.selectedUnit)
|
||||
|
||||
this.btn_play.on(Laya.Event.CLICK, this, (evt: Laya.Event)=>{
|
||||
// console.log(">>>>>>>>>>>>>>>>>>>>>>>", this.selectedUnit.day)
|
||||
var now = new Date()
|
||||
var year = now.getFullYear()
|
||||
var month = now.getMonth() + 1
|
||||
var dayNow = this.selectedUnit.day
|
||||
var stageID = `${year}-${month}-${dayNow}`
|
||||
var doStage = StorageManager.getInstance().loadStage(stageID)
|
||||
console.log("play >>>>>>>>>>>>>>>>>>>>>>>", stageID, doStage)
|
||||
if (!doStage) {
|
||||
var random = Math.random()
|
||||
var value = config.DIFFICULTY_LIST[Math.floor(random*config.DIFFICULTY_LIST.length)]
|
||||
var list = LevelManager.getInstance().getLevelList(value)
|
||||
random = Math.random()
|
||||
var index = Math.floor(random*list.length)
|
||||
var stageName = `${config.MONTH_ABBRS[month-1]} ${dayNow}`
|
||||
console.log("随机一个关卡", value, index)
|
||||
StorageManager.getInstance().newStage(stageID, config.STAGE_TYPE.DC, stageName, value, index)
|
||||
}
|
||||
UIManager.getInstance().loadStageUI(stageID);
|
||||
this.owner.destroy()
|
||||
})
|
||||
}
|
||||
|
||||
public onApplyUnit(unit: CalendarUnit): void {
|
||||
this.selectedUnit.setSelected(false)
|
||||
this.selectedUnit = unit
|
||||
this.selectedUnit.setSelected(true)
|
||||
|
||||
var now = new Date()
|
||||
var year = now.getFullYear()
|
||||
var month = now.getMonth() + 1
|
||||
var dayNow = unit.day
|
||||
var stageID = `${year}-${month}-${dayNow}`
|
||||
this.label_date.text = `${config.MONTH_ABBRS[month-1]} ${dayNow},${year}`
|
||||
var user = StorageManager.getInstance().getUser()
|
||||
var progress = user.get_done(stageID)
|
||||
// console.log("onApplyUnit >>>>>", stageID, progress)
|
||||
if (progress >= 1) {
|
||||
this.btn_play.visible = false
|
||||
this.obj_summary.visible = true
|
||||
var doStage = StorageManager.getInstance().loadStage(stageID)
|
||||
this.label_diffucuty.text = doStage.get_difficulty()
|
||||
this.label_score.text = doStage.get_score().toString()
|
||||
this.label_time.text = Utility_ConvertSecondToString(doStage.get_duration())
|
||||
}
|
||||
else {
|
||||
this.btn_play.visible = true
|
||||
this.obj_summary.visible = false
|
||||
var doStage = StorageManager.getInstance().loadStage(stageID)
|
||||
if (doStage) {
|
||||
this.label_play.text = "Continue"
|
||||
}
|
||||
else {
|
||||
this.label_play.text = "Play"
|
||||
}
|
||||
}
|
||||
}
|
||||
public onClickUnit(evt: Laya.Event): void {
|
||||
var unit = evt.target.getComponent(CalendarUnit)
|
||||
this.onApplyUnit(unit)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user