MonsterBase.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. if(this.state>0){
  118. return;
  119. }
  120. this.removeAllAnimationEvent();
  121. this.state=0;
  122. }
  123. private MoveUpdate(deltaTime:number):void{
  124. this.animation.crossFade(this.monsterConfig.moveAnimation);
  125. let currentPos:Vec3=this.node.position;
  126. Vec3.multiplyScalar(this.speedVec3,this.speedVec3N,this.monsterConfig.moveSpeed*deltaTime);
  127. Vec3.add(currentPos,currentPos,this.speedVec3);
  128. this.node.setPosition(currentPos);
  129. }
  130. /**
  131. * 攻击
  132. */
  133. private Attack(attackType:number):void{
  134. this.attackType=attackType;
  135. if(this.state==1||this.state==2){
  136. return;
  137. }
  138. this.state=1;
  139. this.removeAllAnimationEvent();
  140. this.animation.on(AnimationComponent.EventType.FINISHED,this.AttackComplete,this,true);
  141. this.animation.play(this.monsterConfig.attackAnimation);
  142. }
  143. /**
  144. * 攻击生效帧回调
  145. */
  146. AttackDamageFrame():void{
  147. console.log(this.config.mId+"攻击"+this.attackType);
  148. GameController.single.MonsterAttack(this.attackType,this.config.damage);
  149. }
  150. private AttackComplete():void{
  151. this.state=0;
  152. this.Move();
  153. }
  154. /**
  155. * 受伤
  156. * @param value
  157. */
  158. Damage(value:number):void{
  159. if(this.hp==0){
  160. return;
  161. }
  162. this.hp-=value;
  163. if(this.hp<=0){
  164. this.hp=0;
  165. this.Die();
  166. }else{
  167. if(this.state==3){
  168. return;
  169. }
  170. this.state=2;
  171. this.removeAllAnimationEvent();
  172. if(this.animation==null){
  173. return;
  174. }
  175. this.animation.on(AnimationComponent.EventType.FINISHED,this.DamageComplete,this,true);
  176. this.animation.play(this.monsterConfig.damageAnimation);
  177. }
  178. }
  179. private DamageComplete():void{
  180. this.state=0;
  181. this.Move();
  182. }
  183. Die():void{
  184. if(this.state==3){
  185. return;
  186. }
  187. this.state=3;
  188. this.removeAllAnimationEvent();
  189. this.animation.on(AnimationComponent.EventType.FINISHED,this.DieComplete,this,true);
  190. this.animation.play(this.monsterConfig.dieAnimation);
  191. }
  192. private DieComplete():void{
  193. this.node.active=false;
  194. this.node.destroy();
  195. }
  196. private removeAllAnimationEvent():void{
  197. if(this.animation==null){
  198. return;
  199. }
  200. if(this.animation.hasEventListener(AnimationComponent.EventType.FINISHED,this.DamageComplete,this)){
  201. this.animation.off(AnimationComponent.EventType.FINISHED,this.DamageComplete,this);
  202. }
  203. if(this.animation.hasEventListener(AnimationComponent.EventType.FINISHED,this.AttackComplete,this)){
  204. this.animation.off(AnimationComponent.EventType.FINISHED,this.AttackComplete,this);
  205. }
  206. if(this.animation.hasEventListener(AnimationComponent.EventType.FINISHED,this.DieComplete,this)){
  207. this.animation.off(AnimationComponent.EventType.FINISHED,this.DieComplete,this);
  208. }
  209. }
  210. get isDie():boolean{
  211. if(this.hp<=0){
  212. return true;
  213. }
  214. return false;
  215. }
  216. /**
  217. * 血条显示位置
  218. */
  219. get hpSocket():Node{
  220. let socket:Node=find("RootNode/Root/HpSocket",this.node);
  221. return socket?socket:this.node;
  222. }
  223. }