180 lines
6.7 KiB
TypeScript
180 lines
6.7 KiB
TypeScript
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";
|
|
import { respath } from "../constants/respath";
|
|
|
|
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.Image)
|
|
public btn_trophy: Laya.Image
|
|
|
|
@property(Laya.Image)
|
|
public bg_dc: Laya.Image;
|
|
@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()
|
|
})
|
|
this.btn_trophy.on(Laya.Event.CLICK, this, ()=>{
|
|
UIManager.getInstance().loadTrophyUI()
|
|
})
|
|
var dayBegin = 4
|
|
var dayCount = 31
|
|
var user = StorageManager.getInstance().getUser()
|
|
var now = new Date()
|
|
var year = now.getFullYear()
|
|
var month = now.getMonth() + 1
|
|
var dayNow = new Date().getDate()
|
|
var stageID = `${year}-${month}-${dayNow}`
|
|
var starCount = user.get_doneCountBydate(year, month)
|
|
if (starCount >= dayCount) {
|
|
this.bg_dc.skin = respath.dc_top_bg_month_finish
|
|
}
|
|
else {
|
|
var progress = user.get_done(stageID)
|
|
console.log("progress >>>>>>>>>>>>>", stageID, progress)
|
|
if (progress >= 1) {
|
|
this.bg_dc.skin = respath.dc_top_bg_day_finish
|
|
}
|
|
else {
|
|
this.bg_dc.skin = respath.dc_top_bg_normal
|
|
}
|
|
}
|
|
this.label_year.text = year.toString()
|
|
this.label_month.text = config.MONTH_FULLNAMES[month-1]
|
|
this.label_star.text = `${starCount}/${dayCount}`
|
|
|
|
var day = 1
|
|
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 >= dayBegin && index < dayBegin+dayCount) {
|
|
obj.visible = true
|
|
l_obj.visible = true
|
|
var unit = obj.getComponent(CalendarUnit)
|
|
this.units.push(unit)
|
|
var stageID = `${year}-${month}-${day}`
|
|
var progress = user.get_done(stageID)
|
|
unit.onInit(day, day<=dayNow, progress, l_obj, this, this.onClickUnit)
|
|
if (day == dayNow) {
|
|
this.selectedUnit = unit
|
|
}
|
|
day ++
|
|
}
|
|
else {
|
|
obj.visible = false
|
|
l_obj.visible = false
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
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)
|
|
}
|
|
} |