Player.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import Armature from "./Armature";
  2. import Role from "./Role";
  3. const {ccclass, property} = cc._decorator;
  4. /**玩家类 */
  5. @ccclass
  6. export default class Player extends Role {
  7. public onLoad():void{
  8. //初始化骨骼
  9. this.armature = new Armature('armature/player',this.node);
  10. //初始动画
  11. this.armature.playAnimation(Role.AnimationName_Idle,0);
  12. //玩家死亡惨叫
  13. this.node.on(Role.EventType_Die,()=>{
  14. cc.audioEngine.playEffect(cc.loader.getRes('audio/die',cc.AudioClip),false);
  15. },this);
  16. super.onLoad();
  17. }
  18. public leftDodge():boolean{
  19. if(super.leftDodge()){
  20. //躲避加入视觉上的物理位移
  21. this.node.runAction(cc.sequence(cc.moveTo(0.3,cc.v2(-250,this.node.y)),cc.moveTo(0.3,cc.v2(0,this.node.y))));
  22. return true;
  23. }
  24. return false;
  25. }
  26. public rightDodge():boolean{
  27. if(super.rightDodge()){
  28. //躲避加入视觉上的物理位移
  29. this.node.runAction(cc.sequence(cc.moveTo(0.3,cc.v2(250,this.node.y)),cc.moveTo(0.3,cc.v2(0,this.node.y))));
  30. return true;
  31. }
  32. return false;
  33. }
  34. public hurt(damage:number):boolean{
  35. if(super.hurt(damage)){
  36. //受伤闷声
  37. cc.audioEngine.playEffect(cc.loader.getRes('audio/hurt',cc.AudioClip),false);
  38. //震动屏幕
  39. window.gameMgr.background.runAction(cc.sequence(
  40. cc.shake(0.3,15,15),
  41. cc.callFunc(()=>{
  42. window.gameMgr.background.setPosition(0,0);
  43. },this)
  44. ));
  45. return true;
  46. }
  47. return false;
  48. }
  49. /**相机浮动方向 */
  50. private cameraActionDirection:number = 1;
  51. /**相机y方向偏移量 */
  52. private cameraOffsetY:number = 0;
  53. private redScreen:cc.Node;
  54. private tip_hpLow:cc.Node;
  55. private tip_enduranceLow:cc.Node;
  56. public update(dt:number):void{
  57. super.update(dt);
  58. ////耐力不足
  59. if(this.property_endurance<25&&!this.pausing){
  60. //添加耐力不足警告标志
  61. if(!this.tip_enduranceLow){
  62. this.tip_enduranceLow = new cc.Node();
  63. this.tip_enduranceLow.addComponent(cc.Sprite).spriteFrame = cc.loader.getRes('texture/font_tip1',cc.SpriteFrame);
  64. this.tip_enduranceLow.setPosition(0,-350);
  65. window.gameMgr.background.addChild(this.tip_enduranceLow);
  66. this.tip_enduranceLow.runAction(cc.repeatForever(cc.sequence(
  67. cc.scaleTo(0.3,1.2).easing(cc.easeBackInOut()),
  68. cc.scaleTo(0.3,1).easing(cc.easeBackInOut())
  69. )));
  70. }
  71. //相机浮动规律
  72. if(this.cameraOffsetY>10){
  73. this.cameraActionDirection = -1;
  74. }else if(this.cameraOffsetY<-10){
  75. this.cameraActionDirection = 1;
  76. }
  77. this.cameraOffsetY += this.cameraActionDirection*dt*300;
  78. window.gameMgr.camera.y = this.cameraOffsetY;
  79. }else{
  80. //取消相机浮动和耐力警告标志
  81. window.gameMgr.camera.y = 0
  82. if(this.tip_enduranceLow){
  83. this.tip_enduranceLow.destroy();
  84. this.tip_enduranceLow = null;
  85. }
  86. }
  87. //血量不足
  88. if(this.property_hp<25&&!this.pausing){
  89. //添加红屏
  90. if(!this.redScreen){
  91. this.redScreen = new cc.Node();
  92. let child = new cc.Node();
  93. child.addComponent(cc.Sprite).spriteFrame = cc.loader.getRes('texture/white',cc.SpriteFrame);
  94. child.color = cc.Color.RED;
  95. child.opacity = 100;
  96. child.setContentSize(window.gameMgr.gameViewSize);
  97. this.redScreen.addChild(child);
  98. window.gameMgr.background.addChild(this.redScreen);
  99. this.redScreen.runAction(cc.repeatForever(cc.sequence(
  100. cc.fadeOut(0.3).easing(cc.easeBackInOut()),
  101. cc.fadeIn(0.3).easing(cc.easeBackInOut())
  102. )));
  103. }
  104. //添加血量不足警告标志
  105. if(!this.tip_hpLow){
  106. this.tip_hpLow = new cc.Node();
  107. this.tip_hpLow.addComponent(cc.Sprite).spriteFrame = cc.loader.getRes('texture/font_tip2',cc.SpriteFrame);
  108. this.tip_hpLow.setPosition(0,-430);
  109. window.gameMgr.background.addChild(this.tip_hpLow);
  110. this.tip_hpLow.runAction(cc.repeatForever(cc.sequence(
  111. cc.scaleTo(0.3,1.2).easing(cc.easeBackInOut()),
  112. cc.scaleTo(0.3,1).easing(cc.easeBackInOut())
  113. )));
  114. }
  115. }else{
  116. //取消红屏和血量不足警告标志
  117. if(this.redScreen){
  118. this.redScreen.destroy();
  119. this.redScreen = null;
  120. }
  121. if(this.tip_hpLow){
  122. this.tip_hpLow.destroy();
  123. this.tip_hpLow = null;
  124. }
  125. }
  126. }
  127. }