MonsterBase.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 { 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:AnimationComponent;
  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(this.isDie){
  108. return;
  109. }
  110. let dis:number;
  111. if(GameController.single.fence.IsDie==false){
  112. dis=Vec3.distance(GameController.single.fence.node.worldPosition,this.node.worldPosition);
  113. if(dis<2){
  114. this.Attack(0);
  115. }else{
  116. this.Move();
  117. }
  118. }else{
  119. dis=Vec3.distance(this.endNode.worldPosition,this.node.worldPosition);
  120. //到达终点
  121. if(dis<0.5){
  122. //攻击
  123. this.Attack(1);
  124. return;
  125. }else{
  126. this.Move();
  127. }
  128. }
  129. if(this.state==0){
  130. this.MoveUpdate(deltaTime);
  131. }
  132. }
  133. /**
  134. * 开始移动
  135. */
  136. Move():void{
  137. if(this.state>0){
  138. return;
  139. }
  140. this.removeAllAnimationEvent();
  141. this.state=0;
  142. }
  143. private MoveUpdate(deltaTime:number):void{
  144. this.animation.crossFade(this.monsterConfig.moveAnimation);
  145. let currentPos:Vec3=this.node.position;
  146. Vec3.multiplyScalar(this.speedVec3,this.speedVec3N,this.monsterConfig.moveSpeed*deltaTime);
  147. Vec3.add(currentPos,currentPos,this.speedVec3);
  148. this.node.setPosition(currentPos);
  149. }
  150. /**
  151. * 攻击
  152. */
  153. private Attack(attackType:number):void{
  154. this.attackType=attackType;
  155. if(this.state==1||this.state==2){
  156. return;
  157. }
  158. this.state=1;
  159. this.removeAllAnimationEvent();
  160. this.animation.on(AnimationComponent.EventType.FINISHED,this.AttackComplete,this,true);
  161. this.animation.play(this.monsterConfig.attackAnimation);
  162. }
  163. // /**
  164. // * 攻击生效帧回调
  165. // */
  166. // AttackDamageFrame():void{
  167. // console.log(this.config.mId+"攻击"+this.attackType);
  168. // GameController.single.MonsterAttack(this.attackType,this.config.damage);
  169. // }
  170. private AttackComplete():void{
  171. console.log(this.config.mId+"攻击"+this.attackType);
  172. GameController.single.MonsterAttack(this.attackType,this.config.damage);
  173. this.state=0;
  174. this.Move();
  175. }
  176. /**
  177. * 受伤
  178. * @param value
  179. */
  180. Damage(value:number):void{
  181. if(this.hp==0){
  182. return;
  183. }
  184. this.hp-=value;
  185. if(this.hp<=0){
  186. this.hp=0;
  187. this.Die();
  188. }else{
  189. if(this.state==3){
  190. return;
  191. }
  192. this.state=2;
  193. this.removeAllAnimationEvent();
  194. if(this.animation==null){
  195. return;
  196. }
  197. this.animation.on(AnimationComponent.EventType.FINISHED,this.DamageComplete,this,true);
  198. this.animation.play(this.monsterConfig.damageAnimation);
  199. }
  200. //音效
  201. let v:number=Math.random()*10;
  202. let url:string;
  203. if(v<5){
  204. url="sounds/zombieDamage";
  205. }else{
  206. url="sounds/zombieDamage1";
  207. }
  208. if(SoundManager.single.ChannelIsPlaying(this.config.id)){
  209. SoundManager.single.PlaySound(this.config.id,url);
  210. }
  211. //弹痕
  212. if(this.danHen==null){
  213. let danhenNode:Node=instantiate(GameController.single.monsterZhouDanPrefab);
  214. this.danHen=danhenNode.getComponent(AnimationComponent);
  215. this.node.addChild(danhenNode);
  216. }
  217. if(this.playingDanHen==false){
  218. this.danHen.node.active=true;
  219. let pos:Vec3=this.danHen.node.position;
  220. pos.x=MathUtils.RandomRange(-0.08,0.08);
  221. pos.y=MathUtils.RandomRange(0.5,0.8);
  222. this.danHen.node.position=pos;
  223. this.danHen.on(AnimationComponent.EventType.FINISHED,this.DanHenComplete,this,true);
  224. this.danHen.play("Damage");
  225. }
  226. }
  227. private playingDanHen:boolean=false;
  228. private DanHenComplete():void{
  229. this.danHen.off(AnimationComponent.EventType.FINISHED,this.DanHenComplete,this);
  230. this.danHen.node.active=false;
  231. this.playingDanHen=false;
  232. }
  233. private DamageComplete():void{
  234. this.state=0;
  235. this.Move();
  236. }
  237. Die():void{
  238. if(this.state==3){
  239. return;
  240. }
  241. this.state=3;
  242. SoundManager.single.PlaySound(this.config.id,"sounds/zombieDie");
  243. this.removeAllAnimationEvent();
  244. this.animation.on(AnimationComponent.EventType.FINISHED,this.DieComplete,this,true);
  245. this.animation.play(this.monsterConfig.dieAnimation);
  246. }
  247. private DieComplete():void{
  248. this.node.active=false;
  249. this.node.destroy();
  250. }
  251. onDestroy():void{
  252. SoundManager.single.ClearSound(this.config.id);
  253. this.danHen=null;
  254. MonsterHPUIPool.Recycle(this.hpView);
  255. this.hpView=null;
  256. }
  257. private removeAllAnimationEvent():void{
  258. if(this.animation==null){
  259. return;
  260. }
  261. if(this.animation.hasEventListener(AnimationComponent.EventType.FINISHED,this.DamageComplete,this)){
  262. this.animation.off(AnimationComponent.EventType.FINISHED,this.DamageComplete,this);
  263. }
  264. if(this.animation.hasEventListener(AnimationComponent.EventType.FINISHED,this.AttackComplete,this)){
  265. this.animation.off(AnimationComponent.EventType.FINISHED,this.AttackComplete,this);
  266. }
  267. if(this.animation.hasEventListener(AnimationComponent.EventType.FINISHED,this.DieComplete,this)){
  268. this.animation.off(AnimationComponent.EventType.FINISHED,this.DieComplete,this);
  269. }
  270. }
  271. get isDie():boolean{
  272. if(this.hp<=0){
  273. return true;
  274. }
  275. return false;
  276. }
  277. /**
  278. * 血条显示位置
  279. */
  280. get hpSocket():Node{
  281. let socket:Node=find("RootNode/Root/HpSocket",this.node);
  282. return socket?socket:this.node;
  283. }
  284. }