MonsterBase.ts 8.3 KB

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