252 lines
9.3 KiB
TypeScript
252 lines
9.3 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 icon_trophy: Laya.Image
|
|
@property(Laya.Box)
|
|
public btn_left: Laya.Box
|
|
@property(Laya.Box)
|
|
public btn_right: Laya.Box
|
|
|
|
@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;
|
|
private currentIndex: number
|
|
private indexMax: number
|
|
|
|
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()
|
|
})
|
|
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()
|
|
})
|
|
|
|
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 unit = hbox.getChildAt(j).getComponent(CalendarUnit)
|
|
var l_obj = l_hbox.getChildAt(j) as Laya.Label
|
|
this.units.push(unit)
|
|
unit.onInit(l_obj)
|
|
}
|
|
}
|
|
|
|
this.indexMax = config.MONTH_LIST.length - 1
|
|
var now = new Date()
|
|
var nowYear = now.getFullYear()
|
|
var nowMonth = now.getMonth() + 1
|
|
for (var i=0; i<config.MONTH_LIST.length; i++) {
|
|
var obj = config.MONTH_LIST[i]
|
|
if (nowYear == obj.year && nowMonth == obj.month) {
|
|
this.indexMax = i;
|
|
break
|
|
}
|
|
}
|
|
this.btn_left.on(Laya.Event.CLICK, this, ()=>{
|
|
if (this.currentIndex > 0) {
|
|
this.currentIndex --
|
|
this.loadWithIndex(this.currentIndex)
|
|
}
|
|
})
|
|
this.btn_right.on(Laya.Event.CLICK, this, ()=>{
|
|
if (this.currentIndex < this.indexMax) {
|
|
this.currentIndex ++
|
|
this.loadWithIndex(this.currentIndex)
|
|
}
|
|
})
|
|
}
|
|
|
|
loadWithMonth(): void {
|
|
var now = new Date()
|
|
var nowYear = now.getFullYear()
|
|
var nowMonth = now.getMonth() + 1
|
|
this.currentIndex = 0
|
|
for (var i=0; i<config.MONTH_LIST.length; i++) {
|
|
var obj = config.MONTH_LIST[i]
|
|
if (nowYear == obj.year && nowMonth == obj.month) {
|
|
this.currentIndex = i;
|
|
break
|
|
}
|
|
}
|
|
console.log("DailyChallenge loadWithMonth >>>>>>>>>>>>>>>", nowYear, nowMonth, this.currentIndex)
|
|
this.loadWithIndex(this.currentIndex)
|
|
}
|
|
|
|
public loadWithIndex(index: number): void {
|
|
this.btn_left.visible = this.currentIndex > 0
|
|
this.btn_right.visible = this.currentIndex < this.indexMax
|
|
var info = config.MONTH_LIST[index]
|
|
var user = StorageManager.getInstance().getUser()
|
|
var now = new Date()
|
|
var nowYear = now.getFullYear()
|
|
var nowMonth = now.getMonth() + 1
|
|
var nowDay = new Date().getDate()
|
|
|
|
this.icon_trophy.skin = info.icon_res
|
|
//设置背景
|
|
var starCount = user.get_doneCountBydate(info.year, info.month)
|
|
if (starCount >= info.dayCount) {
|
|
this.bg_dc.skin = respath.dc_top_bg_month_finish
|
|
}
|
|
else {
|
|
if (info.year == nowYear && info.month == nowMonth) {//当前月份
|
|
var stageID = `${info.year}-${info.month}-${nowDay}`
|
|
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
|
|
}
|
|
}
|
|
else {
|
|
this.bg_dc.skin = respath.dc_top_bg_normal
|
|
}
|
|
}
|
|
this.label_year.text = info.year.toString()
|
|
this.label_month.text = config.MONTH_FULLNAMES[info.month-1]
|
|
this.label_star.text = `${starCount}/${info.dayCount}`
|
|
|
|
this.selectedUnit = null
|
|
var day = 1
|
|
for (var i=0; i<this.units.length; i++) {
|
|
var unit = this.units[i]
|
|
if (i >= info.begin && i < info.begin+info.dayCount) {
|
|
unit.owner.visible = true
|
|
unit.label_show.visible = true
|
|
var open = true
|
|
if (info.year > nowYear) {
|
|
open = false
|
|
}
|
|
else if (info.year == nowYear) {
|
|
if (info.month > nowMonth) {
|
|
open = false
|
|
}
|
|
else if (info.month == nowMonth) {
|
|
if (day > nowDay) {
|
|
open = false
|
|
}
|
|
}
|
|
}
|
|
var stageID = `${info.year}-${info.month}-${day}`
|
|
var progress = user.get_done(stageID)
|
|
unit.setCalendarUnit(info.year, info.month, day, open, progress, this, this.onClickUnit)
|
|
if (info.year == nowYear && info.month == nowMonth && day == nowDay) {
|
|
this.selectedUnit = unit
|
|
}
|
|
day ++
|
|
}
|
|
else {
|
|
unit.owner.visible = false
|
|
unit.label_show.visible = false
|
|
}
|
|
}
|
|
if (this.selectedUnit == null)
|
|
this.selectedUnit = this.units[info.begin+info.dayCount-1]
|
|
this.onApplyUnit(this.selectedUnit)
|
|
}
|
|
|
|
public onApplyUnit(unit: CalendarUnit): void {
|
|
this.selectedUnit.setSelected(false)
|
|
this.selectedUnit = unit
|
|
this.selectedUnit.setSelected(true)
|
|
|
|
var stageID = `${unit.year}-${unit.month}-${unit.day}`
|
|
this.label_date.text = `${config.MONTH_ABBRS[unit.month-1]} ${unit.day},${unit.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)
|
|
}
|
|
} |