MonsterBase.ts 9.2 KB

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