gameCtr.ts 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. import {
  2. _decorator,
  3. Animation,
  4. AnimationClip,
  5. AudioClip,
  6. AudioSource,
  7. CharacterController,
  8. Color,
  9. Component,
  10. director,
  11. EventMouse,
  12. EventTouch,
  13. Label,
  14. Node,
  15. NodeEventType,
  16. ProgressBar,
  17. Sprite,
  18. tween,
  19. Tween,
  20. UIOpacity,
  21. UITransform,
  22. v3,
  23. Vec2,
  24. Vec3
  25. } from "cc"
  26. import { noticePannelCtr } from "./noticePannelCtr"
  27. import { StoryPannelCtr } from "./StoryPannelCtr"
  28. import { OptionsCtr } from "./OptionsCtr"
  29. import { rankCtr } from "./rankCtr"
  30. import { RoleCtr } from "./RoleCtr"
  31. import { processCtr } from "./socket/processCtr"
  32. import { SocketClient } from "./socket/SocketClient"
  33. const { ccclass, property } = _decorator
  34. @ccclass("GameCtr")
  35. export class GameCtr extends Component {
  36. @property({ type: noticePannelCtr })
  37. public noticeCtr: noticePannelCtr | null = null
  38. @property({ type: SocketClient })
  39. public socketClient: SocketClient | null = null
  40. @property({ type: processCtr })
  41. public processCtr: processCtr | null = null
  42. @property({ type: RoleCtr })
  43. public roleCtr: RoleCtr | null = null
  44. @property({ type: StoryPannelCtr })
  45. public storyPannelCtr: StoryPannelCtr | null = null
  46. @property({ type: OptionsCtr })
  47. public optionsCtr: OptionsCtr | null = null
  48. @property({ type: rankCtr })
  49. public rankCtr: rankCtr | null = null
  50. @property({ type: Node })
  51. public titlePannel: Node = null
  52. @property({ type: Label })
  53. private dayLabel: Label | null = null
  54. @property(Animation)
  55. SunAnim: Animation = null
  56. @property(Animation)
  57. MoonAnim: Animation = null
  58. private sunIsUp: boolean = false
  59. private num: number = 1
  60. public roomId: number = 1
  61. public gameId: number = 1
  62. public resetNum: number = 1
  63. private gameInfo: any = null
  64. private historyInfo: any[] = []
  65. private initStoryPosition: Vec3 = null
  66. private initOptionsPosition: Vec3 = null
  67. //剧情布局变化状态:0:展示剧情 ,1:出现选项框 2:已选选项下方展示剧情,3:已选移除剧情上移出现选项框
  68. private storyActionStatus: number = 0
  69. //重启动画
  70. @property({ type: Label })
  71. public resetLabel: Label = null
  72. @property({ type: ProgressBar })
  73. public progressBar: ProgressBar = null
  74. @property({ type: Node })
  75. public resetNode: Node = null
  76. private _dotsCounter: number = 0
  77. private _dotsInterval: any = null
  78. //剧情选项相关
  79. private options: any[] = []
  80. private optionsLabel: string[] = []
  81. private storyHeight = 510
  82. private selectedOptionNum: number = 4
  83. //排名相关
  84. private rankInfo: any[] = []
  85. //角色信息
  86. private npc: any[] = null
  87. private player: any[] = null
  88. //简历角色名-node 字典
  89. private roleNodes = {}
  90. private modifyHp: any[]
  91. //确认正式同步后台数据正式开始标签
  92. private realStart: boolean = false
  93. //通知相关
  94. private noticeStr: string = ""
  95. @property(AudioClip)
  96. public moveClip: AudioClip = null!
  97. @property(AudioClip)
  98. public showOptionsClip: AudioClip = null!
  99. @property(AudioClip)
  100. public selectedOptionClip: AudioClip = null!
  101. public audioSource: AudioSource = null!
  102. @property(AudioSource)
  103. public bg1AudioSource: AudioSource = null!
  104. @property(AudioSource)
  105. public bg2AudioSource: AudioSource = null!
  106. private isPlayBgm1 = true
  107. private moveNum:number = 1
  108. init() {
  109. //播放第一个背景音乐
  110. this.bg1AudioSource.play()
  111. let roomId = new URLSearchParams(location.search).get("roomId")
  112. if (roomId != null) {
  113. this.roomId = Number(roomId)
  114. console.log("get roomId from URL:" + roomId)
  115. } else {
  116. console.log("use defult roomId")
  117. }
  118. const roomUrl = `https://airpg1.izouma.com/api/room/${this.roomId}`
  119. fetch(roomUrl)
  120. .then((response) => {
  121. if (!response.ok) {
  122. throw new Error("Network response was not ok")
  123. }
  124. return response.json()
  125. })
  126. .then((data) => {
  127. //获取游戏ID,房间信息
  128. console.log(data.currentGameId + " " + data.active)
  129. this.gameId = data.currentGameId
  130. this.noticeStr = data.notice
  131. if (this.socketClient.socket) {
  132. this.socketClient.socket.disconnect()
  133. } else {
  134. this.socketClient.gameId = this.gameId
  135. this.socketClient.initSocketConnection()
  136. }
  137. const gameUrl = `https://airpg1.izouma.com/api/game/${this.gameId}`
  138. fetch(gameUrl)
  139. .then((response) => {
  140. if (!response.ok) {
  141. throw new Error("Network response was not ok")
  142. }
  143. return response.json()
  144. })
  145. .then((data) => {
  146. console.log(data)
  147. const historyUrl = `https://airpg1.izouma.com/api/game/${this.gameId}/history`
  148. this.gameInfo = data
  149. this.resetNum = data.resetNum
  150. fetch(historyUrl)
  151. .then((response) => {
  152. if (!response.ok) {
  153. throw new Error("Network response was not ok")
  154. }
  155. return response.json()
  156. })
  157. .then((data) => {
  158. console.log("history")
  159. console.log(data)
  160. this.historyInfo = data
  161. //根据游戏信息与历史信息更新房间组件
  162. this.updateGame(this.gameInfo, this.historyInfo)
  163. })
  164. .catch((error) => {
  165. console.error("Fetch error:", error)
  166. })
  167. })
  168. .catch((error) => {
  169. console.error("Fetch error:", error)
  170. })
  171. })
  172. .catch((error) => {
  173. console.error("Fetch error:", error)
  174. })
  175. this.initRank(roomUrl + "/survivalRank")
  176. }
  177. initRank(url: string) {
  178. fetch(url)
  179. .then((response) => {
  180. if (!response.ok) {
  181. throw new Error("Network response was not ok")
  182. }
  183. return response.json()
  184. })
  185. .then((data) => {
  186. console.log("rank")
  187. console.log(data)
  188. this.rankInfo = data
  189. //根据游戏信息与历史信息更新房间组件
  190. this.rankCtr.updateRank(this.rankInfo)
  191. })
  192. .catch((error) => {
  193. console.error("Fetch error:", error)
  194. })
  195. }
  196. start() {
  197. //加载音频播放组件
  198. this.audioSource = this.node.getComponent(AudioSource)
  199. // this.startResetAnimation()
  200. // this.scheduleOnce(() => { this.accessTargetComponent() }, 0.1)
  201. this.scheduleOnce(() => {
  202. this.init()
  203. }, 5)
  204. this.processCtr.init(this)
  205. this.schedule(this.chackGameInfo, 5 * 60)
  206. }
  207. chackGameInfo() {
  208. const roomUrl = `https://airpg1.izouma.com/api/room/${this.roomId}`
  209. fetch(roomUrl)
  210. .then((response) => {
  211. if (!response.ok) {
  212. throw new Error("Network response was not ok")
  213. }
  214. return response.json()
  215. })
  216. .then((data) => {
  217. //获取游戏ID,房间信息
  218. console.log(data.currentGameId + " " + data.active)
  219. if (this.gameId == data.currentGameId) {
  220. if (this.noticeStr == data.notice) {
  221. return
  222. } else {
  223. this.noticeCtr.updateText(data.notice)
  224. this.noticeStr = data.notice
  225. }
  226. } else {
  227. director.loadScene("game")
  228. }
  229. })
  230. .catch((error) => {
  231. console.error("Fetch error:", error)
  232. })
  233. }
  234. update(deltaTime: number) {}
  235. onButtonClick() {
  236. //director.loadScene(director.getScene().name);
  237. // this.exchangeTime()
  238. // this.noticeCtr.updateText(
  239. // "在 Cocos Creator 编辑器中,将富文本组件拖放到脚本的 richText 属性框中。运行游戏,现在你应该可以看到富文本的内容逐字出现。"
  240. // )
  241. //options初始化
  242. this.moveNum++
  243. // this.optionsCtr.showVotes(this.moveNum)
  244. // this.optionsCtr.selectOption(3)
  245. //重置测试
  246. //this.startResetAnimation()
  247. //测试排行切换
  248. // this.rankCtr.updateRankTop()
  249. //测试剧情Pannel移动
  250. // this.optionsAppearAction()
  251. //测试被攻击
  252. this.roleCtr.attack(this.roleCtr.node.getChildByName("role_icon_npc1"))
  253. // this.roleCtr.attack(this.roleCtr.node.getChildByName('role_icon_npc3'));
  254. this.roleCtr.treat(this.roleCtr.node.getChildByName("role_icon_npc2"))
  255. // this.roleCtr.newPlayer(this.roleCtr.node.getChildByName('role_icon_player1'));
  256. }
  257. //重启动画相关
  258. startResetAnimation() {
  259. // Fade in the black mask
  260. if (this._dotsInterval) {
  261. clearInterval(this._dotsInterval)
  262. this._dotsInterval = null
  263. }
  264. this.resetNode.getComponent(UIOpacity).opacity = 0
  265. this.progressBar.progress = 0
  266. this.resetNode.active = true
  267. tween(this.resetNode.getComponent(UIOpacity))
  268. .to(1, { opacity: 255 })
  269. .call(() => this.showResetTextAndProgress())
  270. .start()
  271. }
  272. showResetTextAndProgress() {
  273. this.updateDots()
  274. this._dotsInterval = setInterval(() => this.updateDots(), 500)
  275. // Simulate progress
  276. let progress = 0
  277. const progressInterval = setInterval(() => {
  278. progress += 0.1
  279. this.progressBar.progress = progress
  280. if (progress >= 1) {
  281. clearInterval(progressInterval)
  282. this.endResetAnimation()
  283. }
  284. }, 1000)
  285. }
  286. updateDots() {
  287. const dots = ".".repeat((this._dotsCounter % 3) + 1)
  288. this.resetLabel.string = `世界重置中${dots}`
  289. this._dotsCounter++
  290. }
  291. endResetAnimation() {
  292. clearInterval(this._dotsInterval)
  293. // Fade out the black mask and reset
  294. tween(this.resetNode.getComponent(UIOpacity))
  295. .to(1, { opacity: 0 })
  296. .call(() => {
  297. this.progressBar.progress = 0
  298. this.resetNode.active = false
  299. })
  300. .start()
  301. }
  302. handleStoryPannelMove(data: any) {
  303. this.storyPannelCtr.storyContent = data.data
  304. this.storyPannelCtr.isOnProcess = true
  305. //0:初始化后第一次收到剧情,直接展示剧情
  306. //3:接收到了最后一次剧情展示后清除队列
  307. if (this.storyActionStatus == 0 || this.storyActionStatus == 3) {
  308. if (this.storyPannelCtr) {
  309. this.storyPannelCtr.updateStory(data.data)
  310. } else {
  311. console.log("plot error")
  312. }
  313. this.realStart = true
  314. this.noticeCtr.updateText(this.noticeStr)
  315. } else if (this.storyActionStatus == 2) {
  316. this.optionsAppearAction()
  317. }
  318. }
  319. //处理收到选项事件
  320. handleOptions(data: any) {
  321. if (this.realStart) {
  322. this.options = data.data
  323. //更新optionsLabel
  324. for (let i = 0; i < 4; i++) {
  325. this.optionsLabel[i] = this.options[i].content
  326. }
  327. if (this.storyActionStatus === 0 || this.storyActionStatus === 3) {
  328. this.optionsAppearAction()
  329. }
  330. } else {
  331. this.processCtr.doneProcessing()
  332. }
  333. }
  334. //处理收到被选项事件
  335. handleVoteResul(data: any) {
  336. if (this.realStart) {
  337. this.modifyHp = data.data.modifyHp
  338. for (let i = 0; i < 4; i++) {
  339. if (this.options[i].content == data.data.content) {
  340. this.selectedOptionNum = i
  341. }
  342. }
  343. if (this.storyActionStatus === 1) {
  344. this.optionsAppearAction()
  345. }
  346. } else {
  347. this.processCtr.doneProcessing()
  348. }
  349. }
  350. //处理新玩家加入
  351. handleNewPlayer(data: any) {
  352. this.player.push(data.data)
  353. let playerIndex = this.player.length - 1
  354. if (playerIndex === 8) {
  355. return
  356. }
  357. const rolePannelName: string = "role_icon_player" + (playerIndex + 1)
  358. const role: Node = this.roleCtr.node.getChildByName(rolePannelName)
  359. role.active = true
  360. console.log("new player@!!!!!!:" + data.data)
  361. this.roleCtr.newPlayer(role, this.player[playerIndex])
  362. this.roleNodes[data.data.name] = role
  363. this.processCtr.doneProcessing()
  364. }
  365. //单独处理死亡
  366. handleDeath(data: any) {
  367. this.roleCtr.deathNodes = data.data
  368. this.processCtr.doneProcessing()
  369. }
  370. //处理收到state时间:如果realStart == false ,则更新游戏初始状态
  371. handleStateResul(data: any) {
  372. if (!this.realStart) {
  373. this.historyInfo.push(data.data)
  374. this.updateGame(this.gameInfo, this.historyInfo)
  375. }
  376. this.processCtr.doneProcessing()
  377. }
  378. //处理重置
  379. handleReset(data: any) {
  380. //director.reset()
  381. director.loadScene("game")
  382. this.processCtr.doneProcessing()
  383. }
  384. //处理投票进度
  385. handleVotes(data: any) {
  386. if(this.realStart && this.storyActionStatus == 1 && Math.max(...(data.data as number[]))>0)
  387. {
  388. let votes:number[] = data.data
  389. this.optionsCtr.showVotes(votes,this.processCtr)
  390. }
  391. else
  392. {
  393. console.log('handle votes do nothing:'+this.realStart+" "+this.storyActionStatus+" "+Math.max(...(data.data as number[])))
  394. this.processCtr.doneProcessing()
  395. }
  396. }
  397. //处理所有剧情、选项的移动逻辑
  398. optionsAppearAction() {
  399. console.log("storyActionStatus:" + this.storyActionStatus)
  400. //展示选项,折叠剧情,开启投票统计
  401. if (this.storyActionStatus == 0) {
  402. const storyNode = this.storyPannelCtr.node
  403. const storyUI = storyNode.getComponent(UITransform)
  404. if (storyUI.height > this.storyHeight) {
  405. this.initStoryPosition = storyNode.position.clone()
  406. tween(this.storyPannelCtr.node)
  407. .by(0.5, { position: new Vec3(0, storyUI.height - this.storyHeight, 0) })
  408. .call(() => {
  409. this.optionsCtr.setOptions(this.optionsLabel)
  410. //此处需要额外处理死亡,因为前一步有演示播放攻击动画,不排除死亡名单中有攻击后自然死亡角色
  411. this.roleCtr.dealDeath(this.roleNodes)
  412. })
  413. .start()
  414. } else {
  415. this.optionsCtr.setOptions(this.optionsLabel)
  416. }
  417. this.playShowOptionsOneShot()
  418. this.processCtr.doneProcessing()
  419. this.storyActionStatus = 1
  420. } else if (this.storyActionStatus === 1) {
  421. //选中备选项
  422. this.optionsCtr.resetVotes()
  423. console.log("selectedOptionNum:" + this.selectedOptionNum)
  424. this.optionsCtr.selectOption(this.selectedOptionNum + 1)
  425. this.playSelectedOneShot()
  426. this.processCtr.doneProcessing()
  427. this.storyActionStatus = 2
  428. } else if (this.storyActionStatus === 2) {
  429. this.scheduleOnce(() => {
  430. // 隐藏未被选中选项
  431. for (let i = 0; i < 4; i++) {
  432. var optionNode: Node = this.optionsCtr.optionNodes[i]
  433. if (i !== this.selectedOptionNum) {
  434. const optionTween = tween(optionNode.getComponent(UIOpacity))
  435. optionTween.to(2.5, { opacity: 0 }).start()
  436. }
  437. }
  438. //播放移动声音
  439. this.playMoveOneShot()
  440. //隐藏上边剧情面板
  441. tween(this.storyPannelCtr.node.parent.getComponent(UIOpacity))
  442. .to(2.5, { opacity: 0 })
  443. .call(() => {
  444. this.storyPannelCtr.node.getComponent(Label).string = ""
  445. this.storyPannelCtr.node.position = new Vec3(0, 0, 0)
  446. this.initOptionsPosition = this.optionsCtr.node.position.clone()
  447. const storyY = this.storyPannelCtr.node.parent.position.y
  448. const optionsY = this.optionsCtr.node.position.y
  449. const selectedOption = this.optionsCtr.optionNodes[this.selectedOptionNum]
  450. const selectedOptionHeight = selectedOption.getComponent(UITransform).contentSize.height
  451. //选中选项上移
  452. tween(this.optionsCtr.node)
  453. .by(1.5, {
  454. position: new Vec3(
  455. 0,
  456. storyY - optionsY - selectedOption.position.y - selectedOptionHeight / 2,
  457. 0
  458. )
  459. })
  460. .call(() => {
  461. this.storyPannelCtr.node.parent.getComponent(UIOpacity).opacity = 255
  462. //剧情面板在下方出现并开始展示
  463. tween(this.storyPannelCtr.node)
  464. .by(0.01, { position: new Vec3(0, -selectedOptionHeight, 0) })
  465. .call(() => {
  466. this.storyPannelCtr.isOnProcess = true
  467. this.storyPannelCtr.updateStory(this.storyPannelCtr.storyContent)
  468. this.storyActionStatus = 3
  469. //延时触发扣血加血动画
  470. this.scheduleOnce(() => {
  471. this.updateHp()
  472. }, 2)
  473. })
  474. .start()
  475. })
  476. .start()
  477. })
  478. .start()
  479. }, 2)
  480. } else if (this.storyActionStatus == 3) {
  481. const selectedOption = this.optionsCtr.optionNodes[this.selectedOptionNum]
  482. //顶部备选项消失
  483. tween(selectedOption.getComponent(UIOpacity))
  484. .to(0.5, { opacity: 0 })
  485. .call(() => {
  486. //重置选项位置与透明度
  487. this.optionsCtr.node.position = this.initOptionsPosition
  488. // this.optionsCtr.node.getComponent(UIOpacity).opacity = 0
  489. // this.optionsCtr.node.getChildByName()
  490. // 这是被选中的选项
  491. selectedOption.getChildByName("text_option").getComponent(Label).color = Color.WHITE
  492. // 移除选中背景图
  493. const spriteComponent = selectedOption.getComponent(Sprite)
  494. if (spriteComponent) {
  495. spriteComponent.spriteFrame = this.optionsCtr.notSelectedSprite.spriteFrame
  496. }
  497. for (let i = 0; i < 4; i++) {
  498. this.optionsCtr.optionNodes[i].getComponent(UIOpacity).opacity = 255
  499. const vote:Node = this.optionsCtr.optionNodes[i].getChildByName("votes").getChildByName("toupiaojindu")
  500. console.log("vote info!!!!!!!!!!!!!!"+vote.active+" "+vote.getPosition+" "+vote.getComponent(UIOpacity).opacity)
  501. }
  502. //剧情上移开始展示
  503. tween(this.storyPannelCtr.node)
  504. .to(0.2, { position: new Vec3(0, 0, 0) })
  505. .call(() => {
  506. this.storyActionStatus = 0
  507. this.optionsAppearAction()
  508. this.processCtr.doneProcessing()
  509. })
  510. .start()
  511. })
  512. .start()
  513. }
  514. }
  515. updateGame(gameInfo: any, historyInfo: any[]) {
  516. if (gameInfo && history) {
  517. this.updateDateByHistory(historyInfo[0], historyInfo[historyInfo.length - 1])
  518. const firstHistory = historyInfo[0]
  519. const lastHistory = historyInfo[historyInfo.length - 1]
  520. const charactors: any[] = lastHistory.charactors
  521. //初始化剧情!!!
  522. //触发剧情框文字
  523. if (this.storyPannelCtr) {
  524. this.storyPannelCtr.updateStory(lastHistory.plot)
  525. } else {
  526. console.log("plot error")
  527. }
  528. //初始化角色!!!
  529. this.player = []
  530. this.npc = []
  531. this.roleNodes = {}
  532. for (let i = 0; i < charactors.length; i++) {
  533. if (charactors[i].danmuUserId) {
  534. this.player.push(charactors[i])
  535. } else {
  536. this.npc.push(charactors[i])
  537. }
  538. }
  539. for (let i = 0; i < this.npc.length; i++) {
  540. const rolePannelName: string = "role_icon_npc" + (i + 1)
  541. console.log(rolePannelName)
  542. const role: Node = this.roleCtr.node.getChildByName(rolePannelName)
  543. role.active = true
  544. this.roleCtr.newPlayer(role, this.npc[i])
  545. this.roleNodes[this.npc[i].name] = role
  546. }
  547. for (let i = 0; i < this.player.length; i++) {
  548. const rolePannelName: string = "role_icon_player" + (i + 1)
  549. const role: Node = this.roleCtr.node.getChildByName(rolePannelName)
  550. role.active = true
  551. this.roleCtr.newPlayer(role, this.player[i])
  552. this.roleNodes[this.player[i].name] = role
  553. }
  554. this.scheduleOnce(() => {
  555. this.processCtr.doneProcessing()
  556. }, 3)
  557. } else {
  558. throw new Error("gameInfo or historyInfo is null")
  559. }
  560. }
  561. //更新日期
  562. updateDateByHistory(firstHistory: any, lastHistory: any) {
  563. //初始化标题与天数!!!
  564. const time: string = lastHistory.time
  565. if (time === "evening") {
  566. this.sunIsUp = false
  567. this.exchangeTime()
  568. }
  569. this.titlePannel.getChildByName("text_title").getComponent(Label).string = this.gameInfo.name
  570. this.titlePannel.getChildByName("text_gameNo").getComponent(Label).string = "No." + (this.resetNum + 1)
  571. // 计算日期差(结果单位为毫秒)
  572. const lastHistoryDate = new Date(lastHistory.date)
  573. const firstHistoryDate = new Date(firstHistory.date)
  574. const differenceInMilliseconds = lastHistoryDate.getTime() - firstHistoryDate.getTime()
  575. // 将毫秒转为天数
  576. const differenceInDays = differenceInMilliseconds / (1000 * 60 * 60 * 24) + 1
  577. this.dayLabel.string = "第" + differenceInDays + "天"
  578. }
  579. updateDateByWS(data: any) {
  580. const time: string = data.data.time
  581. if (time === "evening") {
  582. this.sunIsUp = false
  583. this.exchangeTime()
  584. } else {
  585. this.sunIsUp = true
  586. this.exchangeTime()
  587. }
  588. this.titlePannel.getChildByName("text_title").getComponent(Label).string = this.gameInfo.name
  589. this.titlePannel.getChildByName("text_gameNo").getComponent(Label).string = "No." + (this.resetNum + 1)
  590. // 计算日期差(结果单位为毫秒)
  591. const lastHistoryDate = new Date(data.data.date)
  592. const firstHistoryDate = new Date(this.historyInfo[0].date)
  593. const differenceInMilliseconds = lastHistoryDate.getTime() - firstHistoryDate.getTime()
  594. // 将毫秒转为天数
  595. const differenceInDays = differenceInMilliseconds / (1000 * 60 * 60 * 24) + 1
  596. this.dayLabel.string = "第" + differenceInDays + "天"
  597. this.processCtr.doneProcessing()
  598. }
  599. exchangeTime() {
  600. if (this.sunIsUp) {
  601. this.MoonAnim.play("moonUp")
  602. this.MoonAnim.on(Animation.EventType.FINISHED, () => {
  603. this.SunAnim.play("sunDown")
  604. this.MoonAnim.off(Animation.EventType.FINISHED)
  605. })
  606. this.sunIsUp = false
  607. } else {
  608. this.SunAnim.play("sunUp")
  609. this.SunAnim.on(
  610. Animation.EventType.FINISHED,
  611. () => {
  612. this.MoonAnim.play("moonDown")
  613. this.SunAnim.off(Animation.EventType.FINISHED)
  614. },
  615. this
  616. )
  617. this.sunIsUp = true
  618. }
  619. }
  620. updateHp() {
  621. if (this.modifyHp) {
  622. let changBgm2 = false
  623. for (let i = 0; i < this.modifyHp.length; i++) {
  624. const roleNode: Node = this.roleNodes[this.modifyHp[i].name]
  625. const pbNode: Node = roleNode.getChildByName("progressbar_hp")
  626. const orgHp = pbNode.getComponent(ProgressBar).progress
  627. //已死亡不触发
  628. if (orgHp == 0) {
  629. return
  630. }
  631. let newHp = orgHp + this.modifyHp[i].changeValue / 100
  632. if (newHp <= 0) {
  633. newHp = 0
  634. } else if (newHp > 1) {
  635. newHp = 1
  636. }
  637. pbNode.getComponent(ProgressBar).progress = newHp
  638. console.log("updata HP: name:" + this.modifyHp[i].name + " orgHp:" + orgHp + " newHp:" + newHp)
  639. console.log(pbNode)
  640. if (this.modifyHp[i].changeValue > 0) {
  641. this.roleCtr.treat(roleNode)
  642. } else {
  643. this.roleCtr.attack(roleNode)
  644. //发生战斗,更换BGM
  645. changBgm2 = true
  646. }
  647. }
  648. if (changBgm2 && this.isPlayBgm1) {
  649. this.bg1AudioSource.pause()
  650. this.bg2AudioSource.play()
  651. this.isPlayBgm1 = false
  652. } else if (!changBgm2 && !this.isPlayBgm1) {
  653. this.bg2AudioSource.pause()
  654. this.bg1AudioSource.play()
  655. this.isPlayBgm1 = true
  656. }
  657. }
  658. }
  659. playMoveOneShot() {
  660. this.audioSource.playOneShot(this.moveClip, 0.2)
  661. }
  662. playSelectedOneShot() {
  663. this.audioSource.playOneShot(this.selectedOptionClip, 0.2)
  664. }
  665. playShowOptionsOneShot() {
  666. this.audioSource.playOneShot(this.showOptionsClip, 0.2)
  667. }
  668. }