| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- 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
- }
- }
|