RoleCtr.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. {
  72. this.audioSource.playOneShot(this.welcomeClip, 0.2);
  73. }
  74. attack(node: Node) {
  75. console.log(node)
  76. var attackFrameId: number = 1
  77. node.getChildByName("effect_pannel").scale = new Vec3(0.8, 0.8, 0.8)
  78. this.schedule(
  79. () => {
  80. resources.load(`effect/attack1/图层 ${attackFrameId}/spriteFrame`, SpriteFrame, (err, spriteFrame) => {
  81. if (!err && spriteFrame) {
  82. node.getChildByName("effect_pannel").getComponent(Sprite).spriteFrame = spriteFrame
  83. if (attackFrameId == 15) {
  84. attackFrameId = 1
  85. node.getChildByName("effect_pannel").scale = new Vec3(1, 1, 1)
  86. } else {
  87. attackFrameId++
  88. }
  89. } else {
  90. console.log(err)
  91. }
  92. })
  93. },
  94. 0.03,
  95. 15,
  96. 0
  97. )
  98. //播放音效
  99. this.playAttackOneShot()
  100. //红色效果
  101. const effectPannel2: Node = node.getChildByName("effect_pannel2")
  102. const uiopa = effectPannel2.getComponent(UIOpacity)
  103. uiopa.opacity = 200
  104. tween(uiopa).to(0.5, { opacity: 0 }).start()
  105. //shake
  106. tween(node)
  107. .by(0.1, { position: new Vec3(5, 0, 0) }) // 向右移动5个单位
  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(-10, 0, 0) }) // 向左移动10个单位
  111. .by(0.1, { position: new Vec3(5, 0, 0) }) // 向右移动5个单位回到原位置
  112. .call(() => {
  113. //角色死亡,处理为透明
  114. let hp = node.getChildByName("progressbar_hp").getComponent(ProgressBar).progress
  115. if (hp == 0) {
  116. const imgMask: Node = node.getChildByName("icon_mask")
  117. const default_icon: Node = node.getChildByName("img_icon")
  118. const imgNode: Node = imgMask.getChildByName("user_icon")
  119. imgNode.getComponent(UIOpacity).opacity = 100
  120. default_icon.active = false
  121. }
  122. })
  123. .start()
  124. }
  125. //单独处理死亡
  126. dealDeath(roleNodes: any) {
  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. }
  138. }
  139. treat(node: Node) {
  140. let attackFrameId: number = 1
  141. let frameId: number = 1
  142. node.getChildByName("effect_pannel").scale = new Vec3(0.8, 0.8, 0.8)
  143. this.schedule(
  144. () => {
  145. if (frameId <= 62 && this.treatFrames[frameId - 1]) {
  146. node.getChildByName("effect_pannel").getComponent(Sprite).spriteFrame =
  147. this.treatFrames[frameId - 1]
  148. node.getChildByName("effect_pannel").scale = new Vec3(1, 1, 1)
  149. frameId++
  150. }
  151. },
  152. 0.02,
  153. 62,
  154. 0
  155. )
  156. //白色效果
  157. const effectPannel2: Node = node.getChildByName("effect_pannel3")
  158. const uiopa = effectPannel2.getComponent(UIOpacity)
  159. uiopa.opacity = 240
  160. tween(uiopa).to(2, { opacity: 0 }).start()
  161. this.playhealOneShot();
  162. }
  163. newPlayer(node: Node, person: any) {
  164. const imgMask: Node = node.getChildByName("icon_mask")
  165. const imgNode: Node = imgMask.getChildByName("user_icon")
  166. const nameNode: Node = node.getChildByName("text_name")
  167. const pbNode: Node = node.getChildByName("progressbar_hp")
  168. const effect: Node = node.getChildByName("effect_pannel")
  169. const default_icon: Node = node.getChildByName("img_icon")
  170. //新角色席位刷新头像
  171. if (nameNode.active) {
  172. //已存在则只更新hp
  173. pbNode.getComponent(ProgressBar).progress = person.hp / 100
  174. } else {
  175. // 远程 url 带图片后缀名
  176. let remoteUrl = person.avatar
  177. assetManager.loadRemote<ImageAsset>(remoteUrl, (err, imageAsset) => {
  178. const spriteFrame = new SpriteFrame()
  179. const texture = new Texture2D()
  180. texture.image = imageAsset
  181. spriteFrame.texture = texture
  182. // ...
  183. imgNode.getComponent(Sprite).spriteFrame = spriteFrame
  184. let frameId: number = 1
  185. this.schedule(
  186. () => {
  187. if (frameId <= 79 && this.loadedFrames[frameId - 1]) {
  188. node.getChildByName("effect_pannel").getComponent(Sprite).spriteFrame =
  189. this.loadedFrames[frameId - 1]
  190. frameId++
  191. }
  192. },
  193. 0.0168,
  194. 79,
  195. 0
  196. )
  197. tween(imgMask)
  198. .by(2, { position: new Vec3(0, -100, 0) }) // 父节点向下移动
  199. .start()
  200. tween(imgNode)
  201. .by(2, { position: new Vec3(0, 100, 0) }) // 子节点向上移动
  202. .call(() => {
  203. nameNode.getComponent(Label).string = person.name
  204. nameNode.active = true
  205. pbNode.getComponent(ProgressBar).progress = person.hp / 100
  206. pbNode.active = true
  207. if (person.hp === 0) {
  208. default_icon.active = false
  209. imgNode.getComponent(UIOpacity).opacity = 130
  210. }
  211. })
  212. .start()
  213. this.playScanOneShot()
  214. this.scheduleOnce(() => { this.playWelcomOneShot() }, 3)
  215. })
  216. }
  217. }
  218. }