MonsterBase.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import { _decorator, Component, Node, log, AnimationComponent, RigidBodyComponent, Vec3, find } from 'cc';
  2. import CCSAnimationUtils from '../../../../engines/utils/CCSAnimationUtils';
  3. import GameConfigManager from '../../../models/GameConfigManager';
  4. import { GameController } from '../GameController';
  5. import { MonsterHP } from './MonsterHP';
  6. const { ccclass, property } = _decorator;
  7. @ccclass('MonsterBase')
  8. export class MonsterBase extends Component {
  9. config:any;
  10. monsterConfig:any;
  11. hpView:MonsterHP;
  12. /**
  13. * 血量
  14. */
  15. hp:number=0;
  16. /**
  17. * 最大血量
  18. */
  19. maxHP:number=0;
  20. /**
  21. * 伤害
  22. */
  23. damage:number;
  24. private speedVec3:Vec3=new Vec3();
  25. private speedVec3N:Vec3=new Vec3();
  26. private endNode:Node;
  27. private animation:AnimationComponent;
  28. /**
  29. * 0 移动 1攻击 2 被攻击 3死亡
  30. */
  31. private state:number=0;
  32. /**
  33. * 攻击类型 0攻击栅栏 1攻击玩家
  34. */
  35. private attackType:number=0;
  36. start () {
  37. this.monsterConfig=GameConfigManager.GetMonsterConfig(this.config.monsterId);
  38. this.hp=this.maxHP=this.config.hp;
  39. this.damage=this.config.damage;
  40. this.animation=this.getComponent(AnimationComponent);
  41. if(this.animation==null){
  42. throw new Error("动画组件为空!");
  43. }
  44. this.InitAnimations();
  45. this.InitPostitionAndEndPos();
  46. this.InitScale();
  47. }
  48. /**
  49. * 动画数据初始化
  50. */
  51. private InitAnimations():void{
  52. CCSAnimationUtils.SetAnimationSpeed(this.animation,this.monsterConfig.moveAnimation,this.monsterConfig.moveAnimationSpeed);
  53. CCSAnimationUtils.SetAnimationSpeed(this.animation,this.monsterConfig.attackAnimation,this.monsterConfig.attackAnimationSpeed);
  54. CCSAnimationUtils.SetAnimationEvent(this.animation,this.monsterConfig.attackAnimation,this.monsterConfig.attackAnimationEvents);
  55. CCSAnimationUtils.SetAnimationSpeed(this.animation,this.monsterConfig.dieAnimation,this.monsterConfig.dieAnimationSpeed);
  56. }
  57. /**
  58. * 初始化开始位置及结束位置
  59. */
  60. private InitPostitionAndEndPos():void{
  61. //左
  62. if(this.config.startPos==0){
  63. this.endNode=GameController.single.leftEndNode;
  64. }else if(this.config.startPos==1){//中
  65. this.endNode=GameController.single.middleEndNode;
  66. }else{//右
  67. this.endNode=GameController.single.rightEndNode;
  68. }
  69. Vec3.subtract(this.speedVec3N,this.endNode.worldPosition,this.node.worldPosition);
  70. this.speedVec3N.normalize();
  71. this.Move();
  72. }
  73. /**
  74. * 初始化Scale
  75. */
  76. private InitScale():void{
  77. let scale:Vec3=this.node.scale;
  78. scale.x*=this.monsterConfig.scale.x;
  79. scale.y*=this.monsterConfig.scale.y;
  80. scale.z*=this.monsterConfig.scale.z;
  81. this.node.setScale(scale);
  82. }
  83. update (deltaTime: number) {
  84. if(GameController.single.GameOver){
  85. return;
  86. }
  87. if(this.isDie){
  88. return;
  89. }
  90. let dis:number;
  91. if(GameController.single.fence.IsDie==false){
  92. dis=Vec3.distance(GameController.single.fence.node.worldPosition,this.node.worldPosition);
  93. if(dis<2){
  94. this.Attack(0);
  95. }else{
  96. this.Move();
  97. }
  98. }else{
  99. dis=Vec3.distance(this.endNode.worldPosition,this.node.worldPosition);
  100. //到达终点
  101. if(dis<0.5){
  102. //攻击
  103. this.Attack(1);
  104. return;
  105. }else{
  106. this.Move();
  107. }
  108. }
  109. if(this.state==0){
  110. this.MoveUpdate(deltaTime);
  111. }
  112. }
  113. /**
  114. * 开始移动
  115. */
  116. Move():void{
  117. this.removeAllAnimationEvent();
  118. this.state=0;
  119. }
  120. private MoveUpdate(deltaTime:number):void{
  121. this.animation.crossFade(this.monsterConfig.moveAnimation);
  122. let currentPos:Vec3=this.node.position;
  123. Vec3.multiplyScalar(this.speedVec3,this.speedVec3N,this.monsterConfig.moveSpeed*deltaTime);
  124. Vec3.add(currentPos,currentPos,this.speedVec3);
  125. this.node.setPosition(currentPos);
  126. }
  127. /**
  128. * 攻击
  129. */
  130. private Attack(attackType:number):void{
  131. this.attackType=attackType;
  132. if(this.state==1||this.state==2){
  133. return;
  134. }
  135. this.state=1;
  136. this.removeAllAnimationEvent();
  137. this.animation.on(AnimationComponent.EventType.FINISHED,this.AttackComplete,this,true);
  138. this.animation.play(this.monsterConfig.attackAnimation);
  139. }
  140. /**
  141. * 攻击生效帧回调
  142. */
  143. AttackDamageFrame():void{
  144. console.log(this.config.mId+"攻击"+this.attackType);
  145. GameController.single.MonsterAttack(this.attackType,this.config.damage);
  146. }
  147. private AttackComplete():void{
  148. this.Move();
  149. }
  150. /**
  151. * 受伤
  152. * @param value
  153. */
  154. Damage(value:number):void{
  155. if(this.hp==0){
  156. return;
  157. }
  158. this.hp-=value;
  159. if(this.hp<=0){
  160. this.hp=0;
  161. this.Die();
  162. }else{
  163. if(this.state==3){
  164. return;
  165. }
  166. this.state=2;
  167. this.removeAllAnimationEvent();
  168. if(this.animation==null){
  169. return;
  170. }
  171. this.animation.on(AnimationComponent.EventType.FINISHED,this.DamageComplete,this,true);
  172. this.animation.play(this.monsterConfig.damageAnimation);
  173. }
  174. }
  175. private DamageComplete():void{
  176. this.Move();
  177. }
  178. Die():void{
  179. if(this.state==3){
  180. return;
  181. }
  182. this.state=3;
  183. this.removeAllAnimationEvent();
  184. this.animation.on(AnimationComponent.EventType.FINISHED,this.DieComplete,this,true);
  185. this.animation.play(this.monsterConfig.dieAnimation);
  186. }
  187. private DieComplete():void{
  188. this.node.active=false;
  189. this.node.destroy();
  190. }
  191. private removeAllAnimationEvent():void{
  192. if(this.animation==null){
  193. return;
  194. }
  195. if(this.animation.hasEventListener(AnimationComponent.EventType.FINISHED,this.DamageComplete,this)){
  196. this.animation.off(AnimationComponent.EventType.FINISHED,this.DamageComplete,this);
  197. }
  198. if(this.animation.hasEventListener(AnimationComponent.EventType.FINISHED,this.AttackComplete,this)){
  199. this.animation.off(AnimationComponent.EventType.FINISHED,this.AttackComplete,this);
  200. }
  201. if(this.animation.hasEventListener(AnimationComponent.EventType.FINISHED,this.DieComplete,this)){
  202. this.animation.off(AnimationComponent.EventType.FINISHED,this.DieComplete,this);
  203. }
  204. }
  205. get isDie():boolean{
  206. if(this.hp<=0){
  207. return true;
  208. }
  209. return false;
  210. }
  211. /**
  212. * 血条显示位置
  213. */
  214. get hpSocket():Node{
  215. let socket:Node=find("RootNode/Root/HpSocket",this.node);
  216. return socket?socket:this.node;
  217. }
  218. }