Character.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 CanmeraScrpts from "../Canmera/CanmeraScrpts";
  8. import EventName from "../EventName/EventName";
  9. import { Global } from "../Global";
  10. import MyComponent from "../Template/MyComponent";
  11. import RewardMnr from "../Template/RewardMnr";
  12. import Parabola from "./Parabola";
  13. export enum SpineAnimationState {
  14. Null = 'Null',
  15. Idle = 'idle',
  16. Xuli = 'ready',
  17. Fly = 'jump',
  18. Round = 'dowm'
  19. }
  20. const { ccclass, property } = cc._decorator;
  21. @ccclass
  22. export default class Character extends MyComponent {
  23. @property(cc.RigidBody)
  24. CharacterRigidBody: cc.RigidBody = null;
  25. @property(sp.Skeleton)
  26. Spine: sp.Skeleton = null;
  27. Parabola: Parabola = null;
  28. //临时物理值============================================
  29. UpPower: number = 100;
  30. Xscale: number = 4;
  31. Yscale: number = 4;
  32. //跳跃的常量
  33. JumpConstX: number = 0;
  34. JumpConstY: number = 0;
  35. // LIFE-CYCLE CALLBACKS:
  36. //游戏的重量G值
  37. G: number = 4;
  38. //人物临时值
  39. Jump_before_Data: cc.Vec2 = null;
  40. Jump_late_Data: cc.Vec2 = null;
  41. FirstBornRoundFlag: boolean = true;
  42. onLoad() {
  43. super.onLoad()
  44. cc.director.getPhysicsManager().enabled = true;
  45. cc.director.getCollisionManager().enabled = true;
  46. }
  47. onDestroy(): void {
  48. super.onDestroy()
  49. }
  50. start() {
  51. this.regEvent(EventName.YaoGanMove, this.onTouchMove, this)
  52. this.regEvent(EventName.YaoGanEnd, this.onTouchEnd, this)
  53. this.regEvent(EventName.YaoGanCancel, this.YaoGanCancel, this)
  54. this.regEvent(EventName.ChangeRoleState, this.ChangeRoleState, this)
  55. // cc.director.getPhysicsManager().debugDrawFlags = cc.PhysicsManager.DrawBits.e_aabbBit |
  56. // cc.PhysicsManager.DrawBits.e_jointBit |
  57. // cc.PhysicsManager.DrawBits.e_shapeBit
  58. // ;
  59. //开启碰撞监听
  60. this.CharacterRigidBody.enabledContactListener = true;
  61. this.UpPower = Global.UpPower
  62. this.Xscale = Global.Xscale
  63. this.Yscale = Global.Yscale
  64. cc.director.getPhysicsManager().gravity = cc.v2(0, Global.G)
  65. this.G = cc.director.getPhysicsManager().gravity.y
  66. this.JumpConstX = this.UpPower * this.Xscale
  67. this.JumpConstY = this.UpPower * this.Yscale
  68. let ParabolaCom = cc.Canvas.instance.node.getChildByName("Parabola").getComponent(Parabola)
  69. this.Parabola = ParabolaCom
  70. }
  71. onBeginContact(contact, selfCollider: cc.PhysicsBoxCollider, otherCollider) {
  72. if (selfCollider.tag == 1) {
  73. cc.systemEvent.emit(EventName.ChangeRoleState, SpineAnimationState.Round);
  74. if (this.FirstBornRoundFlag) {
  75. this.FirstBornRoundFlag = false
  76. cc.Camera.main.node.getComponent(CanmeraScrpts).BronAni()
  77. }
  78. }
  79. }
  80. YaoGanCancel() {
  81. this.Parabola.clear()
  82. }
  83. onTouchMove(dir: cc.Vec3, power: number) {
  84. // this.CharacterRigidBody.linearVelocity = cc.v2(dir.x * 0.3 * this.JumpConstX, dir.y * 0.3 * this.JumpConstY)
  85. // return
  86. if (!this.JumpOk()) {
  87. return
  88. }
  89. //抛物线点是从人物的锚点开始的
  90. let TempCount = 0
  91. if (RewardMnr.getInstance().ParabolaCount > 0) {
  92. TempCount = 15
  93. } else {
  94. TempCount = 5
  95. }
  96. this.Parabola.clear()
  97. let Temp = cc.v2(dir.x * power * this.JumpConstX, dir.y * power * this.JumpConstY)
  98. let START_POS = cc.v2(this.node.position.x, this.node.position.y)
  99. const dt = 0.2;
  100. for (let count = 1; count < TempCount/*5*/; count++) {
  101. const time = dt * count;
  102. // s = v_x * t
  103. const dx = Temp.x * time;
  104. // h = v_y * t + 0.5 * a * t * t
  105. const dy = Temp.y * time + 0.5 * this.G * this.CharacterRigidBody.gravityScale * time * time;
  106. // 当前时间点坐标
  107. const targetX = START_POS.x + dx;
  108. const targetY = START_POS.y + dy;
  109. // 坐标超过地板就不画了
  110. // if (targetY < START_POS.y) break;
  111. this.Parabola.circle(targetX, targetY);
  112. }
  113. }
  114. onTouchEnd(dir: cc.Vec3, power: number) {
  115. // console.log('onTouchEnd-dir', dir);
  116. // console.log('onTouchEnd-power', power);
  117. if (!this.JumpOk()) {
  118. return
  119. }
  120. this.Parabola.clear()
  121. RewardMnr.getInstance().useOnceParabola()
  122. this.schedule(this.sendHight.bind(this), 0.1, 9999999, 0.1)
  123. if (dir.x >= 0) {
  124. this.node.scaleX = Math.abs(this.node.scaleX)
  125. } else {
  126. this.node.scaleX = -Math.abs(this.node.scaleX)
  127. }
  128. //跳跃
  129. this.CharacterRigidBody.linearVelocity = cc.v2(dir.x * power * this.JumpConstX, dir.y * power * this.JumpConstY)
  130. }
  131. sendHight() {
  132. this.unschedule(this.sendHight)
  133. cc.systemEvent.emit(EventName.Update_CurrentHight, this.getHight())
  134. }
  135. getHight(): number {
  136. let y = this.node.getPosition().y
  137. y = y + (1334 / 2) + (this.node.getContentSize().height / 2)
  138. return parseInt(y.toString())
  139. }
  140. JumpOk() {
  141. if (this.CharacterRigidBody.awake == false) {
  142. return true
  143. }
  144. return false
  145. }
  146. currentState: SpineAnimationState = SpineAnimationState.Idle;
  147. changeState: SpineAnimationState;
  148. ChangeRoleState(changeState: SpineAnimationState) {
  149. this.changeState = changeState
  150. switch (this.currentState) {
  151. case SpineAnimationState.Idle:
  152. switch (this.changeState) {
  153. case SpineAnimationState.Idle:
  154. break;
  155. case SpineAnimationState.Xuli:
  156. this.currentState = SpineAnimationState.Xuli;
  157. this.changeState = SpineAnimationState.Null;
  158. this.Spine.setAnimation(0, SpineAnimationState.Xuli, false);
  159. break;
  160. case SpineAnimationState.Fly:
  161. //cc.error('不可以 从站立直接转向空中');
  162. break;
  163. case SpineAnimationState.Round:
  164. //cc.error('不可以 从站立直接转向落地');
  165. break;
  166. default:
  167. break;
  168. }
  169. break;
  170. case SpineAnimationState.Xuli:
  171. switch (this.changeState) {
  172. case SpineAnimationState.Idle:
  173. this.currentState = SpineAnimationState.Idle;
  174. this.changeState = SpineAnimationState.Null;
  175. this.Spine.setAnimation(0, SpineAnimationState.Idle, true);
  176. break;
  177. case SpineAnimationState.Xuli:
  178. //cc.error('蓄力->蓄力');
  179. break;
  180. case SpineAnimationState.Fly:
  181. this.currentState = SpineAnimationState.Fly;
  182. this.changeState = SpineAnimationState.Null;
  183. this.Spine.setAnimation(0, SpineAnimationState.Fly, false);
  184. break;
  185. case SpineAnimationState.Round:
  186. //cc.error('不可以 从站立直接转向落地');
  187. break;
  188. default:
  189. break;
  190. }
  191. break;
  192. case SpineAnimationState.Fly:
  193. switch (this.changeState) {
  194. case SpineAnimationState.Idle:
  195. //cc.error('飞翔->站立');
  196. break;
  197. case SpineAnimationState.Xuli:
  198. //cc.error('飞翔->蓄力');
  199. break;
  200. case SpineAnimationState.Fly:
  201. //cc.error('飞翔->飞翔');
  202. break;
  203. case SpineAnimationState.Round:
  204. this.currentState = SpineAnimationState.Round;
  205. this.changeState = SpineAnimationState.Null;
  206. let RoundState = this.Spine.setAnimation(0, SpineAnimationState.Round, false);
  207. this.scheduleOnce(() => {
  208. cc.systemEvent.emit(EventName.ChangeRoleState, SpineAnimationState.Idle);
  209. }, RoundState.animationEnd)
  210. break;
  211. default:
  212. break;
  213. }
  214. break;
  215. case SpineAnimationState.Round:
  216. switch (this.changeState) {
  217. case SpineAnimationState.Idle:
  218. this.currentState = SpineAnimationState.Idle;
  219. this.changeState = SpineAnimationState.Null;
  220. this.Spine.setAnimation(0, SpineAnimationState.Idle, true);
  221. break;
  222. case SpineAnimationState.Xuli:
  223. //cc.error('落地->蓄力');
  224. break;
  225. case SpineAnimationState.Fly:
  226. //cc.error('落地->飞翔');
  227. break;
  228. case SpineAnimationState.Round:
  229. //cc.error('落地->落地');
  230. break;
  231. default:
  232. break;
  233. }
  234. break;
  235. default:
  236. break;
  237. }
  238. }
  239. }