player.ts 2.9 KB

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