player.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. atkRecord: Map<string, number> = new Map();
  24. protected onLoad(): void {
  25. this.moveCpt = this.node.getComponent(MoveDest);
  26. if(!this.moveCpt) {
  27. this.moveCpt = this.node.addComponent(MoveDest);
  28. }
  29. this.moveCpt.speed= 800;
  30. this.moveCpt.enabled = false;
  31. }
  32. protected onEnable(): void {
  33. // 跟随手指移动
  34. EventMgr.inst.on(Touch_Start, this.onTouchStart, this);
  35. EventMgr.inst.on(Touch_Move, this.onTouchMove, this);
  36. EventMgr.inst.on(Touch_End, this.onTouchEnd, this);
  37. }
  38. protected onDisable(): void {
  39. EventMgr.inst.off(Touch_Start, this.onTouchStart, this);
  40. EventMgr.inst.off(Touch_Move, this.onTouchMove, this);
  41. EventMgr.inst.off(Touch_End, this.onTouchEnd, this);
  42. }
  43. gameUpdate(dt: number): void {
  44. this.bulletCDTime -= dt;
  45. if(this.bulletCDTime <= 0){
  46. this.bulletCDTime = this.bulletCD;
  47. Player.PlayerAttack && Barrage.inst.createPlayerBullet(this.node.convertToWorldSpaceAR(cc.Vec2.ZERO));
  48. }
  49. }
  50. onTouchStart(){
  51. this.airStartPos = this.node.getPosition();
  52. this.moveCpt.destination = this.airStartPos;
  53. this.moveCpt.enabled = true;
  54. }
  55. onTouchMove(offset: cc.Vec2){
  56. this.moveCpt.destination = this.airStartPos.add(offset);
  57. }
  58. onTouchEnd(){
  59. this.moveCpt.enabled = false;
  60. }
  61. canAtk(key: string){
  62. if(!this.atkRecord.has(key)) return true;
  63. if(Date.now() - this.atkRecord.get(key) >= 500){
  64. cc.log('@@ key', key, ' | ',this.atkRecord.get(key));
  65. return true;
  66. }
  67. return false;
  68. }
  69. beAtked(key: string){
  70. this.atkRecord.set(key, Date.now())
  71. cc.log('@@ addkey', key, ' | ',this.atkRecord.get(key));
  72. }
  73. }