Character.ts 11 KB

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