RoleCtr.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import {
  2. _decorator,
  3. assetManager,
  4. AssetManager,
  5. AudioClip,
  6. AudioSource,
  7. Color,
  8. Component,
  9. ImageAsset,
  10. Label,
  11. Node,
  12. ProgressBar,
  13. resources,
  14. Sprite,
  15. SpriteFrame,
  16. Texture2D,
  17. tween,
  18. Tween,
  19. UIOpacity,
  20. Vec2,
  21. Vec3
  22. } from "cc"
  23. const { ccclass, property } = _decorator
  24. @ccclass("RoleCtr")
  25. export class RoleCtr extends Component {
  26. @property({ type: Node })
  27. public roleNodes: Node[] = []
  28. private loadedFrames = []
  29. private treatFrames = []
  30. public deathNodes: any[] = []
  31. @property(AudioClip)
  32. public attackClip: AudioClip = null!
  33. @property(AudioClip)
  34. public scanClip: AudioClip = null!
  35. @property(AudioClip)
  36. public treatClip: AudioClip = null!
  37. @property(AudioClip)
  38. public welcomeClip: AudioClip = null!
  39. public audioSource: AudioSource = null!
  40. start() {
  41. this.preloadAllFrames()
  42. this.audioSource = this.node.getComponent(AudioSource)
  43. }
  44. preloadAllFrames() {
  45. for (let i = 1; i <= 62; i++) {
  46. resources.load(`effect/treat/图层 ${i}/spriteFrame`, SpriteFrame, (err, spriteFrame) => {
  47. if (!err && spriteFrame) {
  48. this.treatFrames[i - 1] = spriteFrame
  49. }
  50. })
  51. }
  52. for (let i = 1; i <= 79; i++) {
  53. resources.load(`effect/scan/图层 ${i}/spriteFrame`, SpriteFrame, (err, spriteFrame) => {
  54. if (!err && spriteFrame) {
  55. this.loadedFrames[i - 1] = spriteFrame
  56. }
  57. })
  58. }
  59. }
  60. update(deltaTime: number) {}
  61. playAttackOneShot() {
  62. this.audioSource.playOneShot(this.attackClip, 0.2)
  63. }
  64. playScanOneShot() {
  65. this.audioSource.playOneShot(this.scanClip, 0.2)
  66. }
  67. playhealOneShot() {
  68. this.audioSource.playOneShot(this.treatClip, 0.2)
  69. }
  70. playWelcomOneShot() {
  71. this.audioSource.playOneShot(this.welcomeClip, 0.2)
  72. }
  73. attack(node: Node) {
  74. console.log(node)
  75. var attackFrameId: number = 1
  76. node.getChildByName("effect_pannel").scale = new Vec3(0.8, 0.8, 0.8)
  77. this.schedule(
  78. () => {
  79. resources.load(`effect/attack1/图层 ${attackFrameId}/spriteFrame`, SpriteFrame, (err, spriteFrame) => {
  80. if (!err && spriteFrame) {
  81. node.getChildByName("effect_pannel").getComponent(Sprite).spriteFrame = spriteFrame
  82. if (attackFrameId == 15) {
  83. attackFrameId = 1
  84. node.getChildByName("effect_pannel").scale = new Vec3(1, 1, 1)
  85. } else {
  86. attackFrameId++
  87. }
  88. } else {
  89. console.log(err)
  90. }
  91. })
  92. },
  93. 0.03,
  94. 15,
  95. 0
  96. )
  97. //播放音效
  98. this.playAttackOneShot()
  99. //红色效果
  100. const effectPannel2: Node = node.getChildByName("effect_pannel2")
  101. const uiopa = effectPannel2.getComponent(UIOpacity)
  102. uiopa.opacity = 200
  103. tween(uiopa).to(0.5, { opacity: 0 }).start()
  104. //shake
  105. tween(node)
  106. .by(0.1, { position: new Vec3(5, 0, 0) }) // 向右移动5个单位
  107. .by(0.1, { position: new Vec3(-10, 0, 0) }) // 向左移动10个单位
  108. .by(0.1, { position: new Vec3(10, 0, 0) }) // 向右移动10个单位
  109. .by(0.1, { position: new Vec3(-10, 0, 0) }) // 向左移动10个单位
  110. .by(0.1, { position: new Vec3(5, 0, 0) }) // 向右移动5个单位回到原位置
  111. .call(() => {
  112. //角色死亡,处理为透明
  113. let hp = node.getChildByName("progressbar_hp").getComponent(ProgressBar).progress
  114. if (hp == 0) {
  115. const imgMask: Node = node.getChildByName("icon_mask")
  116. const default_icon: Node = node.getChildByName("img_icon")
  117. const imgNode: Node = imgMask.getChildByName("user_icon")
  118. imgNode.getComponent(UIOpacity).opacity = 100
  119. default_icon.active = false
  120. }
  121. })
  122. .start()
  123. }
  124. //单独处理死亡
  125. dealDeath(roleNodes: any) {
  126. let index = 0
  127. for (let i = 0; i < this.deathNodes.length; i++) {
  128. const node = roleNodes[this.deathNodes[i].name]
  129. if (node) {
  130. node.getChildByName("progressbar_hp").getComponent(ProgressBar).progress = 0
  131. const imgMask: Node = node.getChildByName("icon_mask")
  132. const default_icon: Node = node.getChildByName("img_icon")
  133. const imgNode: Node = imgMask.getChildByName("user_icon")
  134. imgNode.getComponent(UIOpacity).opacity = 100
  135. default_icon.active = false
  136. }
  137. if (index == this.deathNodes.length - 1) {
  138. this.deathNodes = []
  139. }
  140. }
  141. }
  142. treat(node: Node) {
  143. let attackFrameId: number = 1
  144. let frameId: number = 1
  145. node.getChildByName("effect_pannel").scale = new Vec3(0.8, 0.8, 0.8)
  146. this.schedule(
  147. () => {
  148. if (frameId <= 62 && this.treatFrames[frameId - 1]) {
  149. node.getChildByName("effect_pannel").getComponent(Sprite).spriteFrame =
  150. this.treatFrames[frameId - 1]
  151. node.getChildByName("effect_pannel").scale = new Vec3(1, 1, 1)
  152. frameId++
  153. }
  154. },
  155. 0.02,
  156. 62,
  157. 0
  158. )
  159. //白色效果
  160. const effectPannel2: Node = node.getChildByName("effect_pannel3")
  161. const uiopa = effectPannel2.getComponent(UIOpacity)
  162. uiopa.opacity = 240
  163. tween(uiopa).to(2, { opacity: 0 }).start()
  164. this.playhealOneShot()
  165. }
  166. newPlayer(node: Node, person: any) {
  167. const imgMask: Node = node.getChildByName("icon_mask")
  168. const imgNode: Node = imgMask.getChildByName("user_icon")
  169. const nameNode: Node = node.getChildByName("text_name")
  170. const pbNode: Node = node.getChildByName("progressbar_hp")
  171. const effect: Node = node.getChildByName("effect_pannel")
  172. const default_icon: Node = node.getChildByName("img_icon")
  173. //新角色席位刷新头像
  174. if (nameNode.active) {
  175. //已存在则只更新hp
  176. pbNode.getComponent(ProgressBar).progress = person.hp / 100
  177. } else {
  178. // 远程 url 带图片后缀名
  179. let remoteUrl = person.avatar
  180. assetManager.loadRemote<ImageAsset>(remoteUrl, (err, imageAsset) => {
  181. const spriteFrame = new SpriteFrame()
  182. const texture = new Texture2D()
  183. texture.image = imageAsset
  184. spriteFrame.texture = texture
  185. // ...
  186. imgNode.getComponent(Sprite).spriteFrame = spriteFrame
  187. let frameId: number = 1
  188. this.schedule(
  189. () => {
  190. if (frameId <= 79 && this.loadedFrames[frameId - 1]) {
  191. node.getChildByName("effect_pannel").getComponent(Sprite).spriteFrame =
  192. this.loadedFrames[frameId - 1]
  193. frameId++
  194. }
  195. },
  196. 0.0168,
  197. 79,
  198. 0
  199. )
  200. tween(imgMask)
  201. .by(2, { position: new Vec3(0, -100, 0) }) // 父节点向下移动
  202. .start()
  203. tween(imgNode)
  204. .by(2, { position: new Vec3(0, 100, 0) }) // 子节点向上移动
  205. .call(() => {
  206. nameNode.getComponent(Label).string = person.name
  207. nameNode.active = true
  208. pbNode.getComponent(ProgressBar).progress = person.hp / 100
  209. pbNode.active = true
  210. if (person.hp === 0) {
  211. default_icon.active = false
  212. imgNode.getComponent(UIOpacity).opacity = 130
  213. }
  214. })
  215. .start()
  216. this.playScanOneShot()
  217. this.scheduleOnce(() => {
  218. this.playWelcomOneShot()
  219. }, 3)
  220. })
  221. }
  222. }
  223. }