player.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { EventMgr } from "../Core/Base/EventMgr";
  2. import Start from "../Start";
  3. import { Touch_Start, Touch_Move, Touch_End } from "./eventDef";
  4. import { Barrage } from "./nest/barrage";
  5. import { GameLogic } from "./update/GameLogic";
  6. import MoveDest from "./update/moveDest";
  7. const {ccclass, property} = cc._decorator;
  8. export let PlayerAttack = true;
  9. @ccclass
  10. export default class player extends GameLogic {
  11. /*===========================自动绑定组件开始===========================*/
  12. /*自动生成*/
  13. @property({type:cc.Node, displayName:""})
  14. private $role_node:cc.Node = null;
  15. /*===========================自动绑定组件结束===========================*/
  16. /*===========================自动生成按钮事件开始==========================*/
  17. /*===========================自动生成按钮事件结束==========================*/
  18. // moveCpt: move = null;
  19. moveCpt: MoveDest = null;
  20. // dirction: cc.Vec2 = cc.v2(0,1);
  21. bulletCD:number = 0.2;
  22. bulletCDTime: number = 0;
  23. airStartPos: cc.Vec2;
  24. static PlayerAttack = false;
  25. protected onLoad(): void {
  26. // this.moveCpt = this.node.getComponent(move);
  27. // if(!this.moveCpt) {
  28. // this.moveCpt = this.node.addComponent(move);
  29. // }
  30. // this.moveCpt.speed=500;
  31. this.moveCpt = this.node.getComponent(MoveDest);
  32. if(!this.moveCpt) {
  33. this.moveCpt = this.node.addComponent(MoveDest);
  34. }
  35. this.moveCpt.speed= 800;
  36. this.moveCpt.enabled = false;
  37. }
  38. protected onEnable(): void {
  39. // 摇杆操作
  40. // EventMgr.inst.on(Roker_Move, this.onRokerMove, this);
  41. // EventMgr.inst.on(Roker_End, this.onRokerEnd, this);
  42. // 跟随手指移动
  43. EventMgr.inst.on(Touch_Start, this.onTouchStart, this);
  44. EventMgr.inst.on(Touch_Move, this.onTouchMove, this);
  45. EventMgr.inst.on(Touch_End, this.onTouchEnd, this);
  46. }
  47. protected onDisable(): void {
  48. // EventMgr.inst.off(Roker_Move, this.onRokerMove, this);
  49. // EventMgr.inst.off(Roker_End, this.onRokerEnd, this);
  50. }
  51. gameUpdate(dt: number): void {
  52. this.bulletCDTime -= dt;
  53. if(this.bulletCDTime <= 0){
  54. this.bulletCDTime = this.bulletCD;
  55. player.PlayerAttack && Barrage.inst.createPlayerBullet(this.node.convertToWorldSpaceAR(cc.Vec2.ZERO));
  56. }
  57. }
  58. // onRokerMove(dirction: cc.Vec2){
  59. // this.moveCpt.dirction = dirction;
  60. // this.dirction = dirction;
  61. // }
  62. // onRokerEnd(){
  63. // this.moveCpt.dirction = cc.v2(0,0);
  64. // }
  65. onTouchStart(){
  66. this.airStartPos = this.node.getPosition();
  67. this.moveCpt.destination = this.airStartPos;
  68. this.moveCpt.enabled = true;
  69. }
  70. onTouchMove(offset: cc.Vec2){
  71. this.moveCpt.destination = this.airStartPos.add(offset);
  72. }
  73. onTouchEnd(){
  74. this.moveCpt.enabled = false;
  75. }
  76. }