temp.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import {
  2. _decorator,
  3. Animation,
  4. AnimationClip,
  5. CharacterController,
  6. Color,
  7. Component,
  8. EventMouse,
  9. EventTouch,
  10. Label,
  11. Node,
  12. NodeEventType,
  13. ProgressBar,
  14. Sprite,
  15. tween,
  16. Tween,
  17. UIOpacity,
  18. UITransform,
  19. v3,
  20. Vec2,
  21. Vec3
  22. } from "cc"
  23. import { noticePannelCtr } from "./noticePannelCtr"
  24. import { StoryPannelCtr } from "./StoryPannelCtr"
  25. import { OptionsCtr } from "./OptionsCtr"
  26. import { rankCtr } from "./rankCtr"
  27. import { RoleCtr } from "./RoleCtr"
  28. const { ccclass, property } = _decorator
  29. @ccclass("home")
  30. export class home extends Component {
  31. @property({ type: noticePannelCtr })
  32. public noticeCtr: noticePannelCtr | null = null
  33. @property({ type: RoleCtr })
  34. public roleCtr: RoleCtr | null = null
  35. @property({ type: StoryPannelCtr })
  36. public storyPannelCtr: StoryPannelCtr | null = null
  37. @property({ type: OptionsCtr })
  38. public optionsCtr: OptionsCtr | null = null
  39. @property({ type: rankCtr })
  40. public rankCtr: rankCtr | null = null
  41. //标题相关
  42. @property({ type: Node })
  43. public titlePannel: Node = null
  44. @property({ type: Label })
  45. private dayLabel: Label | null = null
  46. @property(Animation)
  47. SunAnim: Animation = null
  48. @property(Animation)
  49. MoonAnim: Animation = null
  50. private sunIsUp: boolean = false
  51. private num: number = 1
  52. public roomId: number = 1
  53. public gameId: number = 1
  54. private initStoryPosition: Vec3 = null
  55. private initOptionsPosition: Vec3 = null
  56. //剧情布局变化状态:0:展示剧情 ,1:出现选项框 2:已选选项下方展示剧情,3:已选移除剧情上移出现选项框
  57. private storyActionStatus: number = 0
  58. //重启动画
  59. @property({ type: Label })
  60. public resetLabel: Label = null
  61. @property({ type: ProgressBar })
  62. public progressBar: ProgressBar = null
  63. @property({ type: Node })
  64. public resetNode: Node = null
  65. private _dotsCounter: number = 0
  66. private _dotsInterval: any = null
  67. //剧情选项相关
  68. private options: string[] = null
  69. private storyHeight = 510
  70. private selectedOptionNum: number = 4
  71. init() {
  72. const roomUrl = `https://airpg.izouma.com/api/room/${this.roomId}`
  73. fetch(roomUrl)
  74. .then((response) => {
  75. if (!response.ok) {
  76. throw new Error("Network response was not ok")
  77. }
  78. return response.json()
  79. })
  80. .then((data) => {
  81. //获取游戏ID
  82. console.log(data.currentGameId + " " + data.active)
  83. this.gameId = data.currentGameId
  84. const gameUrl = `https://airpg.izouma.com/api/game/${this.gameId}`
  85. fetch(gameUrl)
  86. .then((response) => {
  87. if (!response.ok) {
  88. throw new Error("Network response was not ok")
  89. }
  90. return response.json()
  91. })
  92. .then((data) => {
  93. console.log(data)
  94. const historyUrl = `https://airpg.izouma.com/api/game/${this.gameId}/history`
  95. const gameInfo = data
  96. fetch(historyUrl)
  97. .then((response) => {
  98. if (!response.ok) {
  99. throw new Error("Network response was not ok")
  100. }
  101. return response.json()
  102. })
  103. .then((data) => {
  104. console.log("history")
  105. console.log(data)
  106. const historyInfo = data
  107. //根据游戏信息与历史信息更新房间组件
  108. updateGame(gameInfo, historyInfo)
  109. })
  110. .catch((error) => {
  111. console.error("Fetch error:", error)
  112. })
  113. })
  114. .catch((error) => {
  115. console.error("Fetch error:", error)
  116. })
  117. })
  118. .catch((error) => {
  119. console.error("Fetch error:", error)
  120. })
  121. }
  122. start() {
  123. this.init()
  124. }
  125. update(deltaTime: number) {}
  126. }
  127. function updateGame(gameInfo: any, historyInfo: any[]) {
  128. if (gameInfo && history) {
  129. const firstHistory = historyInfo[0]
  130. const lastHistory = historyInfo[historyInfo.length - 1]
  131. //初始化标题与天数
  132. const time: string = lastHistory.time
  133. if (time === "evening") {
  134. this.sunIsUp = false
  135. this.exchangeTime()
  136. }
  137. this.titlePannel.getChildByName("text_title").getComponent(Label).string = gameInfo.name
  138. this.titlePannel.getChildByName("game_No").getComponent(Label).string = "No." + this.gameId
  139. // 计算日期差(结果单位为毫秒)
  140. const differenceInMilliseconds = lastHistory.data.getTime() - firstHistory.data.getTime()
  141. // 将毫秒转为天数
  142. const differenceInDays = differenceInMilliseconds / (1000 * 60 * 60 * 24) +1
  143. this.dayLabel.string = "第" + differenceInDays + "天"
  144. } else {
  145. throw new Error("gameInfo or historyInfo is null")
  146. }
  147. }
  148. function exchangeTime() {
  149. if (this.sunIsUp) {
  150. this.MoonAnim.play("moonUp")
  151. this.MoonAnim.on(Animation.EventType.FINISHED, () => {
  152. this.SunAnim.play("sunDown")
  153. this.MoonAnim.off(Animation.EventType.FINISHED)
  154. })
  155. this.sunIsUp = false
  156. this.num += 1
  157. this.dayLabel.string = "第" + this.num + "天"
  158. } else {
  159. this.SunAnim.play("sunUp")
  160. this.SunAnim.on(
  161. Animation.EventType.FINISHED,
  162. () => {
  163. this.MoonAnim.play("moonDown")
  164. this.SunAnim.off(Animation.EventType.FINISHED)
  165. },
  166. this
  167. )
  168. this.sunIsUp = true
  169. }
  170. }