player.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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: MoveDest = null;
  19. bulletCD:number = 0.2;
  20. bulletCDTime: number = 0;
  21. airStartPos: cc.Vec2;
  22. static PlayerAttack = true;
  23. protected onLoad(): void {
  24. this.moveCpt = this.node.getComponent(MoveDest);
  25. if(!this.moveCpt) {
  26. this.moveCpt = this.node.addComponent(MoveDest);
  27. }
  28. this.moveCpt.speed= 800;
  29. this.moveCpt.enabled = false;
  30. }
  31. protected onEnable(): void {
  32. // 跟随手指移动
  33. EventMgr.inst.on(Touch_Start, this.onTouchStart, this);
  34. EventMgr.inst.on(Touch_Move, this.onTouchMove, this);
  35. EventMgr.inst.on(Touch_End, this.onTouchEnd, this);
  36. }
  37. protected onDisable(): void {
  38. EventMgr.inst.off(Touch_Start, this.onTouchStart, this);
  39. EventMgr.inst.off(Touch_Move, this.onTouchMove, this);
  40. EventMgr.inst.off(Touch_End, this.onTouchEnd, this);
  41. }
  42. gameUpdate(dt: number): void {
  43. this.bulletCDTime -= dt;
  44. if(this.bulletCDTime <= 0){
  45. this.bulletCDTime = this.bulletCD;
  46. player.PlayerAttack && Barrage.inst.createPlayerBullet(this.node.convertToWorldSpaceAR(cc.Vec2.ZERO));
  47. }
  48. }
  49. onTouchStart(){
  50. this.airStartPos = this.node.getPosition();
  51. this.moveCpt.destination = this.airStartPos;
  52. this.moveCpt.enabled = true;
  53. }
  54. onTouchMove(offset: cc.Vec2){
  55. this.moveCpt.destination = this.airStartPos.add(offset);
  56. }
  57. onTouchEnd(){
  58. this.moveCpt.enabled = false;
  59. }
  60. }