Character.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Learn TypeScript:
  2. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
  3. // Learn Attribute:
  4. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
  5. // Learn life-cycle callbacks:
  6. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html
  7. import EventName from "../EventName/EventName";
  8. import { Global } from "../Global";
  9. import MyComponent from "../Template/MyComponent";
  10. import Parabola from "./Parabola";
  11. const { ccclass, property } = cc._decorator;
  12. @ccclass
  13. export default class Character extends MyComponent {
  14. //最高纪录
  15. @property(cc.RigidBody)
  16. CharacterRigidBody: cc.RigidBody = null;
  17. @property(cc.Graphics)
  18. graphic_line: cc.Graphics = null;
  19. @property(Parabola)
  20. Parabola: Parabola = null;
  21. //临时物理值============================================
  22. UpPower: number = 100;
  23. Xscale: number = 4;
  24. Yscale: number = 4;
  25. //跳跃的常量
  26. JumpConstX: number = 0;
  27. JumpConstY: number = 0;
  28. // LIFE-CYCLE CALLBACKS:
  29. //游戏的重量G值
  30. G: number = 4;
  31. //人物临时值
  32. Jump_before_Data: cc.Vec2 = null;
  33. Jump_late_Data: cc.Vec2 = null;
  34. onLoad() {
  35. super.onLoad()
  36. cc.director.getPhysicsManager().enabled = true;
  37. }
  38. onDestroy(): void {
  39. super.onDestroy()
  40. }
  41. start() {
  42. this.regEvent(EventName.YaoGanMove, this.onTouchMove, this)
  43. this.regEvent(EventName.YaoGanEnd, this.onTouchEnd, this)
  44. this.UpPower = Global.UpPower
  45. this.Xscale = Global.Xscale
  46. this.Yscale = Global.Yscale
  47. cc.director.getPhysicsManager().gravity = cc.v2(0, Global.G)
  48. this.G = cc.director.getPhysicsManager().gravity.y
  49. this.JumpConstX = this.UpPower * this.Xscale
  50. this.JumpConstY = this.UpPower * this.Yscale
  51. }
  52. onTouchMove(dir: cc.Vec3, power: number) {
  53. // this.CharacterRigidBody.linearVelocity = cc.v2(dir.x * 0.3 * this.JumpConstX, dir.y * 0.3 * this.JumpConstY)
  54. // return
  55. if (!this.JumpOk()) {
  56. return
  57. }
  58. this.Parabola.clear()
  59. let Temp = cc.v2(dir.x * power * this.JumpConstX, dir.y * power * this.JumpConstY)
  60. let START_POS = cc.v2(this.node.position.x, this.node.position.y)
  61. const dt = 0.2;
  62. for (let count = 1; count < 5/*5*/; count++) {
  63. const time = dt * count;
  64. // s = v_x * t
  65. const dx = Temp.x * time;
  66. // h = v_y * t + 0.5 * a * t * t
  67. const dy = Temp.y * time + 0.5 * this.G * this.CharacterRigidBody.gravityScale * time * time;
  68. // 当前时间点坐标
  69. const targetX = START_POS.x + dx;
  70. const targetY = START_POS.y + dy;
  71. // 坐标超过地板就不画了
  72. // if (targetY < START_POS.y) break;
  73. this.Parabola.circle(targetX, targetY);
  74. }
  75. // this.graphic_line.fill()
  76. // this.graphic_line.stroke()
  77. }
  78. onTouchEnd(dir: cc.Vec3, power: number) {
  79. // console.log('onTouchEnd-dir', dir);
  80. // console.log('onTouchEnd-power', power);
  81. if (!this.JumpOk()) {
  82. return
  83. }
  84. this.Parabola.clear()
  85. this.schedule(this.sendHight.bind(this), 0.1, 9999999, 0.1)
  86. //跳跃
  87. this.CharacterRigidBody.linearVelocity = cc.v2(dir.x * power * this.JumpConstX, dir.y * power * this.JumpConstY)
  88. }
  89. sendHight() {
  90. this.unschedule(this.sendHight)
  91. cc.systemEvent.emit(EventName.Update_CurrentHight, this.getHight())
  92. }
  93. getHight(): number {
  94. let y = this.node.getPosition().y
  95. y = y + (1334 / 2) + (this.node.getContentSize().height / 2)
  96. return parseInt(y.toString())
  97. }
  98. JumpOk() {
  99. if (this.CharacterRigidBody.awake == false) {
  100. return true
  101. }
  102. return false
  103. }
  104. }