gameCtr.ts 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  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. public 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. private lastVote:number[] = [0,0,0,0]
  113. init() {
  114. //播放第一个背景音乐
  115. this.bg1AudioSource.play()
  116. console.log(location.href)
  117. if (location.hostname === 'localhost') {
  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. this.lastVote = [0,0,0,0]
  346. }else
  347. {
  348. console.log('handleOptions处理失败'+' storyActionStatus'+this.storyActionStatus)
  349. this.processCtr.doneProcessing()
  350. }
  351. } else {
  352. console.log('handleOptions处理完成处理下一个')
  353. this.processCtr.doneProcessing()
  354. }
  355. }
  356. //处理投票进度
  357. handleVotes(data: any) {
  358. let vote:number[] = data.data
  359. if (this.realStart && Math.max(...vote) > 0 && JSON.stringify(this.lastVote) !== JSON.stringify(vote)) {
  360. // console.log("this22222"+this+" processCtr!!!!!"+this.processCtr)
  361. this.optionsCtr.showVotes(vote,this.lastVote, this.processCtr)
  362. this.lastVote = vote
  363. }
  364. else {
  365. console.log('handle votes do nothing:' + this.realStart + " " + this.storyActionStatus + " " + Math.max(...(data.data as number[])))
  366. this.processCtr.doneProcessing()
  367. }
  368. }
  369. //处理收到被选项事件
  370. handleVoteResul(data: any) {
  371. if (this.realStart) {
  372. this.modifyHp = data.data.modifyHp
  373. for (let i = 0; i < 4; i++) {
  374. if (this.options[i].content == data.data.content) {
  375. this.selectedOptionNum = i
  376. }
  377. }
  378. if (this.storyActionStatus === 1) {
  379. this.optionsAppearAction()
  380. }
  381. else
  382. {
  383. console.log('handleVoteResul处理失败'+' storyActionStatus'+this.storyActionStatus)
  384. }
  385. } else {
  386. console.log('handleVoteResul未处理')
  387. this.processCtr.doneProcessing()
  388. }
  389. }
  390. //处理新玩家加入
  391. handleNewPlayer(data: any) {
  392. this.player.push(data.data)
  393. let playerIndex = this.player.length - 1
  394. if (playerIndex === 8) {
  395. return
  396. }
  397. const rolePannelName: string = "role_icon_player" + (playerIndex + 1)
  398. const role: Node = this.roleCtr.node.getChildByName(rolePannelName)
  399. role.active = true
  400. console.log("new player@!!!!!!:" + data.data)
  401. this.roleCtr.newPlayer(role, this.player[playerIndex])
  402. this.roleNodes[data.data.name] = role
  403. console.log('handleNewPlayer处理完成处理下一个')
  404. this.processCtr.doneProcessing()
  405. }
  406. //单独处理死亡
  407. handleDeath(data: any) {
  408. this.roleCtr.deathNodes = data.data
  409. this.processCtr.doneProcessing()
  410. }
  411. //处理收到state时间:如果realStart == false ,则更新游戏初始状态
  412. handleStateResul(data: any) {
  413. if (!this.realStart) {
  414. console.log("处理开始前的status")
  415. this.historyInfo.push(data.data)
  416. this.updateGame(this.gameInfo, this.historyInfo)
  417. console.log('handleStateResul处理完成处理下一个')
  418. this.processCtr.doneProcessing()
  419. }
  420. else {
  421. console.log("处理开始后的status完成,处理下一个")
  422. this.processCtr.doneProcessing()
  423. }
  424. }
  425. //处理重置
  426. handleReset(data: any) {
  427. //director.reset()
  428. console.log("处理重置消息")
  429. director.loadScene("game")
  430. this.processCtr.doneProcessing()
  431. }
  432. //处理所有剧情、选项的移动逻辑
  433. optionsAppearAction() {
  434. console.log("storyActionStatus:" + this.storyActionStatus)
  435. //展示选项,折叠剧情,开启投票统计
  436. if (this.storyActionStatus == 0) {
  437. const storyNode = this.storyPannelCtr.node
  438. const storyUI = storyNode.getComponent(UITransform)
  439. if (storyUI.height > this.storyHeight) {
  440. this.initStoryPosition = storyNode.position.clone()
  441. tween(this.storyPannelCtr.node)
  442. .by(0.5, { position: new Vec3(0, storyUI.height - this.storyHeight, 0) })
  443. .call(() => {
  444. this.optionsCtr.setOptions(this.optionsLabel,this)
  445. //此处需要额外处理死亡,因为前一步有演示播放攻击动画,不排除死亡名单中有攻击后自然死亡角色,最后一个死亡的没啥用,暂没处理
  446. this.roleCtr.dealDeath(this.roleNodes)
  447. })
  448. .start()
  449. } else {
  450. this.optionsCtr.setOptions(this.optionsLabel,this)
  451. }
  452. this.playShowOptionsOneShot()
  453. } else if (this.storyActionStatus === 1) {
  454. //选中备选项
  455. this.optionsCtr.resetVotes()
  456. console.log("selectedOptionNum:" + this.selectedOptionNum)
  457. this.playSelectedOneShot()
  458. this.optionsCtr.selectOption(this.selectedOptionNum + 1,this)
  459. } else if (this.storyActionStatus === 2) {
  460. this.scheduleOnce(() => {
  461. // 隐藏未被选中选项
  462. for (let i = 0; i < 4; i++) {
  463. var optionNode: Node = this.optionsCtr.optionNodes[i]
  464. if (i !== this.selectedOptionNum) {
  465. const optionTween = tween(optionNode.getComponent(UIOpacity))
  466. optionTween.to(2.5, { opacity: 0 }).start()
  467. }
  468. }
  469. //播放移动声音
  470. this.playMoveOneShot()
  471. //隐藏上边剧情面板
  472. tween(this.storyPannelCtr.node.parent.getComponent(UIOpacity))
  473. .to(2.5, { opacity: 0 })
  474. .call(() => {
  475. this.storyPannelCtr.node.getComponent(Label).string = ""
  476. this.storyPannelCtr.node.position = new Vec3(0, 0, 0)
  477. this.initOptionsPosition = this.optionsCtr.node.position.clone()
  478. const storyY = this.storyPannelCtr.node.parent.position.y
  479. const optionsY = this.optionsCtr.node.position.y
  480. const selectedOption = this.optionsCtr.optionNodes[this.selectedOptionNum]
  481. const selectedOptionHeight = selectedOption.getComponent(UITransform).contentSize.height
  482. //选中选项上移
  483. tween(this.optionsCtr.node)
  484. .by(1.5, {
  485. position: new Vec3(
  486. 0,
  487. storyY - optionsY - selectedOption.position.y - selectedOptionHeight / 2,
  488. 0
  489. )
  490. })
  491. .call(() => {
  492. this.storyPannelCtr.node.parent.getComponent(UIOpacity).opacity = 255
  493. //剧情面板在下方出现并开始展示
  494. tween(this.storyPannelCtr.node)
  495. .by(0.01, { position: new Vec3(0, -selectedOptionHeight, 0) })
  496. .call(() => {
  497. this.storyPannelCtr.isOnProcess = true
  498. this.storyPannelCtr.updateStory(this.storyPannelCtr.storyContent)
  499. this.storyActionStatus = 3
  500. //延时触发扣血加血动画
  501. this.scheduleOnce(() => {
  502. this.updateHp()
  503. }, 2)
  504. })
  505. .start()
  506. })
  507. .start()
  508. })
  509. .start()
  510. }, 2)
  511. } else if (this.storyActionStatus == 3) {
  512. const selectedOption = this.optionsCtr.optionNodes[this.selectedOptionNum]
  513. //顶部备选项消失
  514. tween(selectedOption.getComponent(UIOpacity))
  515. .to(0.5, { opacity: 1 })
  516. .call(() => {
  517. //重置选项位置与透明度
  518. this.optionsCtr.node.position = this.initOptionsPosition
  519. // 这是被选中的选项
  520. selectedOption.getChildByName("text_option").getComponent(Label).color = Color.WHITE
  521. // 移除选中背景图
  522. const spriteComponent = selectedOption.getComponent(Sprite)
  523. if (spriteComponent) {
  524. spriteComponent.spriteFrame = this.optionsCtr.notSelectedSprite.spriteFrame
  525. }
  526. //剧情上移开始展示
  527. tween(this.storyPannelCtr.node)
  528. .to(0.2, { position: new Vec3(0, 0, 0) })
  529. .call(() => {
  530. this.storyActionStatus = 0
  531. this.optionsAppearAction()
  532. // console.log("剧情上移处理完成,处理下一个")
  533. // this.processCtr.doneProcessing()
  534. })
  535. .start()
  536. })
  537. .start()
  538. }
  539. }
  540. updateGame(gameInfo: any, historyInfo: any[]) {
  541. if (gameInfo && history) {
  542. this.updateDateByHistory(historyInfo[0], historyInfo[historyInfo.length - 1])
  543. const firstHistory = historyInfo[0]
  544. const lastHistory = historyInfo[historyInfo.length - 1]
  545. const charactors: any[] = lastHistory.charactors
  546. //初始化剧情!!!
  547. //触发剧情框文字
  548. if (this.storyPannelCtr) {
  549. this.storyPannelCtr.initStory(lastHistory.plot)
  550. } else {
  551. console.log("plot error")
  552. }
  553. //初始化角色!!!
  554. this.player = []
  555. this.npc = []
  556. this.roleNodes = {}
  557. for (let i = 0; i < charactors.length; i++) {
  558. if (charactors[i].danmuUserId) {
  559. this.player.push(charactors[i])
  560. } else {
  561. this.npc.push(charactors[i])
  562. }
  563. }
  564. for (let i = 0; i < this.npc.length; i++) {
  565. const rolePannelName: string = "role_icon_npc" + (i + 1)
  566. console.log(rolePannelName)
  567. const role: Node = this.roleCtr.node.getChildByName(rolePannelName)
  568. role.active = true
  569. this.roleCtr.newPlayer(role, this.npc[i])
  570. this.roleNodes[this.npc[i].name] = role
  571. }
  572. for (let i = 0; i < this.player.length; i++) {
  573. const rolePannelName: string = "role_icon_player" + (i + 1)
  574. const role: Node = this.roleCtr.node.getChildByName(rolePannelName)
  575. role.active = true
  576. this.roleCtr.newPlayer(role, this.player[i])
  577. this.roleNodes[this.player[i].name] = role
  578. }
  579. } else {
  580. throw new Error("gameInfo or historyInfo is null")
  581. }
  582. }
  583. //更新日期
  584. updateDateByHistory(firstHistory: any, lastHistory: any) {
  585. //初始化标题与天数!!!
  586. const time: string = lastHistory.time
  587. if (time === "evening") {
  588. this.sunIsUp = false
  589. this.exchangeTime()
  590. }
  591. this.titlePannel.getChildByName("text_title").getComponent(Label).string = this.gameInfo.name
  592. this.titlePannel.getChildByName("text_gameNo").getComponent(Label).string = "No." + (this.resetNum + 1)
  593. // 计算日期差(结果单位为毫秒)
  594. const lastHistoryDate = new Date(lastHistory.date)
  595. const firstHistoryDate = new Date(firstHistory.date)
  596. const differenceInMilliseconds = lastHistoryDate.getTime() - firstHistoryDate.getTime()
  597. // 将毫秒转为天数
  598. const differenceInDays = differenceInMilliseconds / (1000 * 60 * 60 * 24) + 1
  599. this.dayLabel.string = "第" + differenceInDays + "天"
  600. }
  601. updateDateByWS(data: any) {
  602. const time: string = data.data.time
  603. if (time === "evening") {
  604. this.sunIsUp = false
  605. this.exchangeTime()
  606. } else {
  607. this.sunIsUp = true
  608. this.exchangeTime()
  609. }
  610. this.titlePannel.getChildByName("text_title").getComponent(Label).string = this.gameInfo.name
  611. this.titlePannel.getChildByName("text_gameNo").getComponent(Label).string = "No." + (this.resetNum + 1)
  612. // 计算日期差(结果单位为毫秒)
  613. const lastHistoryDate = new Date(data.data.date)
  614. const firstHistoryDate = new Date(this.historyInfo[0].date)
  615. const differenceInMilliseconds = lastHistoryDate.getTime() - firstHistoryDate.getTime()
  616. // 将毫秒转为天数
  617. const differenceInDays = differenceInMilliseconds / (1000 * 60 * 60 * 24) + 1
  618. this.dayLabel.string = "第" + differenceInDays + "天"
  619. console.log("更新日期处理完成,处理下一个")
  620. this.processCtr.doneProcessing()
  621. }
  622. exchangeTime() {
  623. if (this.sunIsUp) {
  624. this.MoonAnim.play("moonUp")
  625. this.MoonAnim.on(Animation.EventType.FINISHED, () => {
  626. this.SunAnim.play("sunDown")
  627. this.MoonAnim.off(Animation.EventType.FINISHED)
  628. })
  629. this.sunIsUp = false
  630. } else {
  631. this.SunAnim.play("sunUp")
  632. this.SunAnim.on(
  633. Animation.EventType.FINISHED,
  634. () => {
  635. this.MoonAnim.play("moonDown")
  636. this.SunAnim.off(Animation.EventType.FINISHED)
  637. },
  638. this
  639. )
  640. this.sunIsUp = true
  641. }
  642. }
  643. updateHp() {
  644. if (this.modifyHp) {
  645. let changBgm2 = false
  646. for (let i = 0; i < this.modifyHp.length; i++) {
  647. const roleNode: Node = this.roleNodes[this.modifyHp[i].name]
  648. const pbNode: Node = roleNode.getChildByName("progressbar_hp")
  649. const orgHp = pbNode.getComponent(ProgressBar).progress
  650. //已死亡不触发
  651. if (orgHp == 0) {
  652. continue
  653. }
  654. let newHp = orgHp + this.modifyHp[i].changeValue / 100
  655. if (newHp <= 0) {
  656. newHp = 0
  657. } else if (newHp > 1) {
  658. newHp = 1
  659. }
  660. pbNode.getComponent(ProgressBar).progress = newHp
  661. console.log("updata HP: name:" + this.modifyHp[i].name + " orgHp:" + orgHp + " newHp:" + newHp)
  662. console.log(pbNode)
  663. if (this.modifyHp[i].changeValue > 0) {
  664. this.roleCtr.treat(roleNode)
  665. } else {
  666. this.roleCtr.attack(roleNode)
  667. //发生战斗,更换BGM
  668. changBgm2 = true
  669. }
  670. }
  671. if (changBgm2 && this.isPlayBgm1) {
  672. this.bg1AudioSource.pause()
  673. this.bg2AudioSource.play()
  674. this.isPlayBgm1 = false
  675. } else if (!changBgm2 && !this.isPlayBgm1) {
  676. this.bg2AudioSource.pause()
  677. this.bg1AudioSource.play()
  678. this.isPlayBgm1 = true
  679. }
  680. }
  681. }
  682. playMoveOneShot() {
  683. this.audioSource.playOneShot(this.moveClip, 0.2)
  684. }
  685. playSelectedOneShot() {
  686. this.audioSource.playOneShot(this.selectedOptionClip, 0.2)
  687. }
  688. playShowOptionsOneShot() {
  689. this.audioSource.playOneShot(this.showOptionsClip, 0.2)
  690. }
  691. }