RoleControl.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { _decorator, CircleCollider2D, Component, find, log, Node, RigidBody2D, Vec2, Vec3 } from 'cc';
  2. import { EventManager } from './EventManager';
  3. import { PropBase } from './Prop/Base/PropBase';
  4. import { diban } from './Prop/Type/diban';
  5. import { DifficultyManager } from './DifficultyManager';
  6. import { AudioManager } from './Set/AudioManager';
  7. const { ccclass, property } = _decorator;
  8. @ccclass('RoleControl')
  9. export class RoleControl extends Component {
  10. @property(Node)
  11. Role: Node = null;
  12. @property(Node)
  13. LeftBtn: Node = null;
  14. @property(Node)
  15. RightBtn: Node = null;
  16. private Left_Right_Speed: number = 20;
  17. // G: number = 20;
  18. MyDir: number = 0;
  19. protected onLoad(): void {
  20. EventManager.instance.et.on(EventManager.EventType.Reset_Role, this.Reset_Role, this);
  21. EventManager.instance.et.on(EventManager.EventType.Die_Role, this.Die_Role, this);
  22. }
  23. Die_Role() {
  24. this.scheduleOnce(() => {
  25. this.Role.active = false
  26. }, 0)
  27. }
  28. Reset_Role() {
  29. this.Role.active = true
  30. let Nodes = this.getComponentsInChildren(PropBase).filter((item) => {
  31. return item.node.position.y < 0
  32. })
  33. let Nodes1 = Nodes.filter((item) => {
  34. return item.node.name == 'diban'
  35. })
  36. let SortNodes = Nodes1.sort((a, b) => {
  37. return a.node.position.y - b.node.position.y
  38. })
  39. let TempNode = SortNodes.pop()
  40. this.scheduleOnce(() => {
  41. this.Role.setPosition(new Vec3(TempNode.node.position.x, TempNode.node.position.y + 45, 0))
  42. this.Role.getComponent(RigidBody2D).linearVelocity = new Vec2(0, -DifficultyManager.G); // 速度值可以根据需要调整
  43. }, 0)
  44. }
  45. protected start(): void {
  46. this.Role.getComponent(RigidBody2D).linearVelocity = new Vec2(0, - DifficultyManager.G); // 速度值可以根据需要调整
  47. this.LeftBtn.on(Node.EventType.TOUCH_START, this.LeftStart, this);
  48. this.RightBtn.on(Node.EventType.TOUCH_START, this.RightStart, this);
  49. this.LeftBtn.on(Node.EventType.TOUCH_END, this.LeftEnd, this);
  50. this.RightBtn.on(Node.EventType.TOUCH_END, this.RightEnd, this);
  51. }
  52. LeftStart() {
  53. this.MyDir = -1
  54. }
  55. RightStart() {
  56. this.MyDir = 1
  57. }
  58. LeftEnd() {
  59. if (this.MyDir == 1) {
  60. return
  61. }
  62. this.MyDir = 0
  63. }
  64. RightEnd() {
  65. if (this.MyDir == -1) {
  66. return
  67. }
  68. this.MyDir = 0
  69. }
  70. updateTime: number;
  71. update(deltaTime: number) {
  72. this.updateTime += deltaTime;
  73. let _time = 1 / 60//60FPS
  74. if (this.updateTime < _time) return;
  75. this.updateTime = this.updateTime - _time;
  76. this.FixUpdate()
  77. }
  78. FixUpdate() {
  79. if (!this.Role.getComponent(RigidBody2D)) {
  80. return
  81. }
  82. switch (this.MyDir) {
  83. case 0:
  84. this.Role.getComponent(RigidBody2D).linearVelocity = new Vec2(0, -DifficultyManager.G); // 速度值可以根据需要调整
  85. break;
  86. case -1:
  87. this.Role.getComponent(RigidBody2D).linearVelocity = new Vec2(-this.Left_Right_Speed, -DifficultyManager.G); // 速度值可以根据需要调整
  88. break;
  89. case 1:
  90. this.Role.getComponent(RigidBody2D).linearVelocity = new Vec2(this.Left_Right_Speed, -DifficultyManager.G); // 速度值可以根据需要调整
  91. break;
  92. default:
  93. break;
  94. }
  95. }
  96. }