MonsterBase.ts 8.8 KB

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