MonsterBase.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. }
  82. update (deltaTime: number) {
  83. if(GameController.single.GameOver){
  84. return;
  85. }
  86. if(this.isDie){
  87. return;
  88. }
  89. let dis:number;
  90. if(GameController.single.fence.IsDie==false){
  91. dis=Vec3.distance(GameController.single.fence.node.worldPosition,this.node.worldPosition);
  92. if(dis<2){
  93. this.Attack(0);
  94. }else{
  95. this.Move();
  96. }
  97. }else{
  98. dis=Vec3.distance(this.endNode.worldPosition,this.node.worldPosition);
  99. //到达终点
  100. if(dis<0.5){
  101. //攻击
  102. this.Attack(1);
  103. return;
  104. }else{
  105. this.Move();
  106. }
  107. }
  108. if(this.state==0){
  109. this.MoveUpdate(deltaTime);
  110. }
  111. }
  112. /**
  113. * 开始移动
  114. */
  115. Move():void{
  116. this.removeAllAnimationEvent();
  117. this.state=0;
  118. }
  119. private MoveUpdate(deltaTime:number):void{
  120. this.animation.crossFade(this.monsterConfig.moveAnimation);
  121. let currentPos:Vec3=this.node.position;
  122. Vec3.multiplyScalar(this.speedVec3,this.speedVec3N,this.monsterConfig.moveSpeed*deltaTime);
  123. Vec3.add(currentPos,currentPos,this.speedVec3);
  124. this.node.setPosition(currentPos);
  125. }
  126. /**
  127. * 攻击
  128. */
  129. private Attack(attackType:number):void{
  130. this.attackType=attackType;
  131. if(this.state==1||this.state==2){
  132. return;
  133. }
  134. this.state=1;
  135. this.removeAllAnimationEvent();
  136. this.animation.on(AnimationComponent.EventType.FINISHED,this.AttackComplete,this,true);
  137. this.animation.play(this.monsterConfig.attackAnimation);
  138. }
  139. /**
  140. * 攻击生效帧回调
  141. */
  142. AttackDamageFrame():void{
  143. console.log(this.config.mId+"攻击"+this.attackType);
  144. GameController.single.MonsterAttack(this.attackType,this.config.damage);
  145. }
  146. private AttackComplete():void{
  147. this.Move();
  148. }
  149. /**
  150. * 受伤
  151. * @param value
  152. */
  153. Damage(value:number):void{
  154. if(this.hp==0){
  155. return;
  156. }
  157. this.hp-=value;
  158. if(this.hp<=0){
  159. this.hp=0;
  160. this.Die();
  161. }else{
  162. if(this.state==3){
  163. return;
  164. }
  165. this.state=2;
  166. this.removeAllAnimationEvent();
  167. if(this.animation==null){
  168. return;
  169. }
  170. this.animation.on(AnimationComponent.EventType.FINISHED,this.DamageComplete,this,true);
  171. this.animation.play(this.monsterConfig.damageAnimation);
  172. }
  173. }
  174. private DamageComplete():void{
  175. this.Move();
  176. }
  177. Die():void{
  178. if(this.state==3){
  179. return;
  180. }
  181. this.state=3;
  182. this.removeAllAnimationEvent();
  183. this.animation.on(AnimationComponent.EventType.FINISHED,this.DieComplete,this,true);
  184. this.animation.play(this.monsterConfig.dieAnimation);
  185. }
  186. private DieComplete():void{
  187. this.node.active=false;
  188. this.node.destroy();
  189. }
  190. private removeAllAnimationEvent():void{
  191. if(this.animation==null){
  192. return;
  193. }
  194. if(this.animation.hasEventListener(AnimationComponent.EventType.FINISHED,this.DamageComplete,this)){
  195. this.animation.off(AnimationComponent.EventType.FINISHED,this.DamageComplete,this);
  196. }
  197. if(this.animation.hasEventListener(AnimationComponent.EventType.FINISHED,this.AttackComplete,this)){
  198. this.animation.off(AnimationComponent.EventType.FINISHED,this.AttackComplete,this);
  199. }
  200. if(this.animation.hasEventListener(AnimationComponent.EventType.FINISHED,this.DieComplete,this)){
  201. this.animation.off(AnimationComponent.EventType.FINISHED,this.DieComplete,this);
  202. }
  203. }
  204. get isDie():boolean{
  205. if(this.hp<=0){
  206. return true;
  207. }
  208. return false;
  209. }
  210. /**
  211. * 血条显示位置
  212. */
  213. get hpSocket():Node{
  214. let socket:Node=find("RootNode/HpSocket",this.node);
  215. return socket?socket:this.node;
  216. }
  217. }