import { _decorator, Animation, AnimationClip, CharacterController, Color, Component, EventMouse, EventTouch, Label, Node, NodeEventType, ProgressBar, Sprite, tween, Tween, UIOpacity, UITransform, v3, Vec2, Vec3 } from "cc" import { noticePannelCtr } from "./noticePannelCtr" import { StoryPannelCtr } from "./StoryPannelCtr" import { OptionsCtr } from "./OptionsCtr" import { rankCtr } from "./rankCtr" import { RoleCtr } from "./RoleCtr" const { ccclass, property } = _decorator @ccclass("home") export class home extends Component { @property({ type: noticePannelCtr }) public noticeCtr: noticePannelCtr | null = null @property({ type: RoleCtr }) public roleCtr: RoleCtr | null = null @property({ type: StoryPannelCtr }) public storyPannelCtr: StoryPannelCtr | null = null @property({ type: OptionsCtr }) public optionsCtr: OptionsCtr | null = null @property({ type: rankCtr }) public rankCtr: rankCtr | null = null //标题相关 @property({ type: Node }) public titlePannel: Node = null @property({ type: Label }) private dayLabel: Label | null = null @property(Animation) SunAnim: Animation = null @property(Animation) MoonAnim: Animation = null private sunIsUp: boolean = false private num: number = 1 public roomId: number = 1 public gameId: number = 1 private initStoryPosition: Vec3 = null private initOptionsPosition: Vec3 = null //剧情布局变化状态:0:展示剧情 ,1:出现选项框 2:已选选项下方展示剧情,3:已选移除剧情上移出现选项框 private storyActionStatus: number = 0 //重启动画 @property({ type: Label }) public resetLabel: Label = null @property({ type: ProgressBar }) public progressBar: ProgressBar = null @property({ type: Node }) public resetNode: Node = null private _dotsCounter: number = 0 private _dotsInterval: any = null //剧情选项相关 private options: string[] = null private storyHeight = 510 private selectedOptionNum: number = 4 init() { const roomUrl = `https://airpg.izouma.com/api/room/${this.roomId}` fetch(roomUrl) .then((response) => { if (!response.ok) { throw new Error("Network response was not ok") } return response.json() }) .then((data) => { //获取游戏ID console.log(data.currentGameId + " " + data.active) this.gameId = data.currentGameId const gameUrl = `https://airpg.izouma.com/api/game/${this.gameId}` fetch(gameUrl) .then((response) => { if (!response.ok) { throw new Error("Network response was not ok") } return response.json() }) .then((data) => { console.log(data) const historyUrl = `https://airpg.izouma.com/api/game/${this.gameId}/history` const gameInfo = data fetch(historyUrl) .then((response) => { if (!response.ok) { throw new Error("Network response was not ok") } return response.json() }) .then((data) => { console.log("history") console.log(data) const historyInfo = data //根据游戏信息与历史信息更新房间组件 updateGame(gameInfo, historyInfo) }) .catch((error) => { console.error("Fetch error:", error) }) }) .catch((error) => { console.error("Fetch error:", error) }) }) .catch((error) => { console.error("Fetch error:", error) }) } start() { this.init() } update(deltaTime: number) {} } function updateGame(gameInfo: any, historyInfo: any[]) { if (gameInfo && history) { const firstHistory = historyInfo[0] const lastHistory = historyInfo[historyInfo.length - 1] //初始化标题与天数 const time: string = lastHistory.time if (time === "evening") { this.sunIsUp = false this.exchangeTime() } this.titlePannel.getChildByName("text_title").getComponent(Label).string = gameInfo.name this.titlePannel.getChildByName("game_No").getComponent(Label).string = "No." + this.gameId // 计算日期差(结果单位为毫秒) const differenceInMilliseconds = lastHistory.data.getTime() - firstHistory.data.getTime() // 将毫秒转为天数 const differenceInDays = differenceInMilliseconds / (1000 * 60 * 60 * 24) +1 this.dayLabel.string = "第" + differenceInDays + "天" } else { throw new Error("gameInfo or historyInfo is null") } } function exchangeTime() { if (this.sunIsUp) { this.MoonAnim.play("moonUp") this.MoonAnim.on(Animation.EventType.FINISHED, () => { this.SunAnim.play("sunDown") this.MoonAnim.off(Animation.EventType.FINISHED) }) this.sunIsUp = false this.num += 1 this.dayLabel.string = "第" + this.num + "天" } else { this.SunAnim.play("sunUp") this.SunAnim.on( Animation.EventType.FINISHED, () => { this.MoonAnim.play("moonDown") this.SunAnim.off(Animation.EventType.FINISHED) }, this ) this.sunIsUp = true } }