gameCtr.ts 30 KB

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