EventProcess.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /** @format */
  2. import {EventBase, EventGraphicsDraw} from './NodeEvent'
  3. import {Data} from '../../GameControl'
  4. import {FightWorld} from '../worlds/FightWorld'
  5. import FrameAnimation from '../../uiutils/FrameAnimation'
  6. import {ccUtils} from '../../utils/ccUtils'
  7. const {ccclass, property} = cc._decorator
  8. @ccclass
  9. export class EventProcess extends cc.Component {
  10. world: FightWorld
  11. frameAnimation: FrameAnimation
  12. graphics: cc.Graphics = null
  13. public onAttach(): void {}
  14. public onDetach(): void {}
  15. public processEvent(event: EventBase): void {}
  16. public syncPos(x: number, y: number) {
  17. this.node.x = x
  18. this.node.y = y
  19. this.node.zIndex = -y
  20. }
  21. public syncDir(dir: cc.Vec2) {}
  22. public playFrameAni(url, isLoop: boolean = false, time: number = Infinity) {
  23. let res = Data.game.gameAssetMap.get(`atlas/${url}`)
  24. if (this.frameAnimation && this.frameAnimation.node && res) {
  25. this.frameAnimation.init(res, time / Data.game.gameSpeed, isLoop)
  26. this.frameAnimation.play(0, isLoop)
  27. }
  28. }
  29. public playAnimation(url) {
  30. let res = Data.game.gameAssetMap.get(`animation/${url}`)
  31. if (this.frameAnimation && this.frameAnimation.node && res) {
  32. let animation = this.frameAnimation.node.getComponent(cc.Animation)
  33. for (let i = animation.getClips().length - 1; i >= 0; i--) {
  34. if (animation.getClips()[i]) animation.removeClip(animation.getClips()[i])
  35. }
  36. animation.addClip(res as cc.AnimationClip)
  37. ccUtils.playAni(res.name, 0, this.frameAnimation.node)
  38. }
  39. }
  40. protected graphicsDraw(event: EventGraphicsDraw) {
  41. let graphics = cc.find('graphics', this.node)?.getComponent(cc.Graphics)
  42. if (!graphics) return
  43. if (!event || event.points.length <= 0) {
  44. graphics.clear()
  45. return
  46. }
  47. for (const p of event.points) {
  48. p.subSelf(this.node.getPosition())
  49. }
  50. graphics.strokeColor = event.color
  51. if (event.radius) {
  52. graphics.circle(event.points[0].x, event.points[0].y, event.radius)
  53. } else {
  54. graphics.moveTo(event.points[0].x, event.points[0].y)
  55. for (let i = 1; i < event.points.length; i++) {
  56. graphics.lineTo(event.points[i].x, event.points[i].y)
  57. }
  58. graphics.lineTo(event.points[0].x, event.points[0].y)
  59. }
  60. graphics.stroke()
  61. event.points.length = 0
  62. }
  63. }