gameCtr.ts 28 KB

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