Role.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import Armature from "./Armature";
  2. const {ccclass, property} = cc._decorator;
  3. /**角色类 */
  4. @ccclass
  5. export default class Role extends cc.Component {
  6. //动画名称
  7. public static AnimationName_Idle:string = 'idle';
  8. public static AnimationName_Attack:string = 'attack';
  9. public static AnimationName_Defense:string = 'defense';
  10. public static AnimationName_Dodge:string = 'dodge';
  11. public static AnimationName_Hurt:string = 'hurt';
  12. public static FrameEvent_Hit:string = 'hit';
  13. //角色事件
  14. public static EventType_HpChange:string = 'hpChange';
  15. public static EventType_EnduranceChange:string = 'enduranceChange';
  16. public static EventType_Die:string = 'die';
  17. public static EventType_DefenseSuccess:string = 'defenseSuccess';
  18. public static EventType_DodgeSuccess:string = 'dodgeSuccess';
  19. public static EventType_BeHit:string = 'beHit';
  20. public static EventType_NoAction:string = 'noAction';
  21. public static EventType_DoAttack:string = 'doAttack';
  22. public static EventType_DoDodge:string = 'doDodge';
  23. public static EventType_DoDefense:string = 'doDefense';
  24. public static EventType_GetTarget:string = 'getTarget';
  25. //自定义骨架
  26. public armature:Armature;
  27. //攻击目标
  28. public target:Role;
  29. //角色状态
  30. public attacking:boolean;
  31. public defensing:boolean;
  32. public dodging:boolean;
  33. public hurting:boolean;
  34. public direction:number = 1;
  35. public pausing:boolean = true;
  36. //角色属性
  37. public property_hp:number = 100;
  38. public property_maxHp:number = 100;
  39. public property_endurance:number = 100;
  40. public property_maxEndurance:number = 100;
  41. public property_damage:number = 5;
  42. public property_enduranceResumeSpeed:number = 3;
  43. public onLoad():void{
  44. //动画播放完成时,对应角色状态取消
  45. this.node.on(Armature.EventType_Finish,(animationName:string)=>{
  46. if(animationName==Role.AnimationName_Attack){
  47. this.attacking = false;
  48. this.node.emit(Role.EventType_NoAction);
  49. return;
  50. }
  51. if(animationName==Role.AnimationName_Defense){
  52. this.defensing = false;
  53. this.node.emit(Role.EventType_NoAction);
  54. return;
  55. }
  56. if(animationName==Role.AnimationName_Dodge){
  57. this.dodging = false;
  58. this.node.emit(Role.EventType_NoAction);
  59. return;
  60. }
  61. if(animationName==Role.AnimationName_Hurt){
  62. this.hurting = false;
  63. this.node.emit(Role.EventType_NoAction);
  64. return;
  65. }
  66. },this);
  67. //监听攻击时的击中帧事件
  68. this.node.on(Armature.EventType_Frame,(animationName:string,processName:string)=>{
  69. if(animationName==Role.AnimationName_Attack&&processName==Role.FrameEvent_Hit){
  70. this.target.hurt(this.property_damage);
  71. }
  72. },this);
  73. }
  74. public update(dt:number):void{
  75. //待机条件满足时,自动执行动画
  76. if(!this.attacking&&!this.dodging&&!this.defensing&&!this.hurting){
  77. if(this.armature.getAnimationName()!=Role.AnimationName_Idle){
  78. this.armature.playAnimation(Role.AnimationName_Idle,0);
  79. }
  80. }
  81. if(!this.pausing){
  82. this.resumeEndurance(dt*this.property_enduranceResumeSpeed);
  83. }
  84. }
  85. public setTarget(target:Role):void{
  86. this.target = target;
  87. this.node.emit(Role.EventType_GetTarget);
  88. }
  89. public leftJay():void{
  90. if(this.pausing||this.attacking||this.dodging||this.defensing||this.hurting||!this.isEnduranceEnough(5))return;
  91. this.consumeEndurance(5);
  92. this.attacking = true;
  93. this.direction = 1;
  94. this.node.scaleX = this.direction*Math.abs(this.node.scaleX);
  95. this.armature.playAnimation(Role.AnimationName_Attack,1);
  96. this.node.emit(Role.EventType_DoAttack);
  97. }
  98. public rightJay():void{
  99. if(this.pausing||this.attacking||this.dodging||this.defensing||this.hurting||!this.isEnduranceEnough(5))return;
  100. this.consumeEndurance(5);
  101. this.attacking = true;
  102. this.direction = -1;
  103. this.node.scaleX = this.direction*Math.abs(this.node.scaleX);
  104. this.armature.playAnimation(Role.AnimationName_Attack,1);
  105. this.node.emit(Role.EventType_DoAttack);
  106. }
  107. public leftDodge():boolean{
  108. if(this.pausing||this.attacking||this.dodging||this.defensing||this.hurting||!this.isEnduranceEnough(5))return false;
  109. this.consumeEndurance(5);
  110. this.dodging = true;
  111. this.direction = -1;
  112. this.node.scaleX = this.direction*Math.abs(this.node.scaleX);
  113. this.armature.playAnimation(Role.AnimationName_Dodge,1);
  114. this.node.emit(Role.EventType_DoDodge);
  115. return true;
  116. }
  117. public rightDodge():boolean{
  118. if(this.pausing||this.attacking||this.dodging||this.defensing||this.hurting||!this.isEnduranceEnough(5))return false;
  119. this.consumeEndurance(5);
  120. this.dodging = true;
  121. this.direction = 1;
  122. this.node.scaleX = this.direction*Math.abs(this.node.scaleX);
  123. this.armature.playAnimation(Role.AnimationName_Dodge,1);
  124. this.node.emit(Role.EventType_DoDodge);
  125. return true;
  126. }
  127. public defense():void{
  128. if(this.pausing||this.attacking||this.dodging||this.defensing||this.hurting||!this.isEnduranceEnough(20))return;
  129. this.consumeEndurance(20);
  130. this.defensing = true;
  131. this.armature.playAnimation(Role.AnimationName_Defense,1);
  132. this.node.emit(Role.EventType_DoDefense);
  133. }
  134. public hurt(damage:number):boolean{
  135. //检测受伤条件是否满足
  136. if(this.property_hp<=0)return false;
  137. if(this.dodging&&this.direction==this.target.direction){
  138. //躲避成功恢复10耐力
  139. this.resumeEndurance(20);
  140. this.node.emit(Role.EventType_DodgeSuccess);
  141. return;
  142. }
  143. if(this.defensing){
  144. //防守成功恢复10hp
  145. this.resumeHp(10);
  146. cc.audioEngine.playEffect(cc.loader.getRes('audio/defense',cc.AudioClip),false);
  147. this.node.emit(Role.EventType_DefenseSuccess);
  148. return;
  149. }
  150. //状态纠正
  151. this.hurting = true;
  152. this.attacking = false;
  153. this.dodging = false;
  154. //方向纠正
  155. this.direction = this.target.direction;
  156. this.node.scaleX = this.direction*Math.abs(this.node.scaleX);
  157. //动画音效
  158. this.armature.playAnimation(Role.AnimationName_Hurt,1);
  159. cc.audioEngine.playEffect(cc.loader.getRes('audio/hit',cc.AudioClip),false);
  160. this.node.emit(Role.EventType_BeHit);
  161. this.consumeHp(damage);
  162. return true;
  163. }
  164. /**
  165. * 消耗hp
  166. * @param value 消耗值
  167. */
  168. public consumeHp(value:number):void{
  169. if(this.property_hp>0){
  170. let nextHp = this.property_hp - value;
  171. if(nextHp>0){
  172. this.property_hp = nextHp;
  173. }else{
  174. this.property_hp = 0;
  175. }
  176. this.node.emit(Role.EventType_HpChange,this.property_hp,this.property_maxHp);
  177. //死亡检测
  178. if(this.property_hp==0){
  179. this.node.emit(Role.EventType_Die);
  180. }
  181. }
  182. }
  183. /**
  184. * 恢复hp
  185. * @param value 消耗值
  186. */
  187. public resumeHp(value:number):void{
  188. if(this.property_hp<this.property_maxHp){
  189. let nextHp = this.property_hp + value;
  190. if(nextHp>this.property_maxHp){
  191. this.property_hp = this.property_maxHp;
  192. }else{
  193. this.property_hp = nextHp;
  194. }
  195. this.node.emit(Role.EventType_HpChange,this.property_hp,this.property_maxHp);
  196. }
  197. }
  198. /**判断耐力是否足够
  199. * @param value 对比值
  200. */
  201. public isEnduranceEnough(value:number):boolean{
  202. return this.property_endurance>=value;
  203. }
  204. /**
  205. * 消耗耐力
  206. * @param value 消耗值
  207. */
  208. public consumeEndurance(value:number):void{
  209. this.property_endurance -= value;
  210. this.node.emit(Role.EventType_EnduranceChange,this.property_endurance,this.property_maxEndurance);
  211. }
  212. /**
  213. * 恢复耐力
  214. * @param value 恢复值
  215. */
  216. public resumeEndurance(value:number):void{
  217. if(this.property_endurance<this.property_maxEndurance){
  218. let nextEndurance = this.property_endurance + value;
  219. if(nextEndurance>this.property_maxEndurance){
  220. this.property_endurance = this.property_maxEndurance;
  221. }else{
  222. this.property_endurance = nextEndurance;
  223. }
  224. this.node.emit(Role.EventType_EnduranceChange,this.property_endurance,this.property_maxEndurance);
  225. }
  226. }
  227. }