Sundries.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. import { _decorator, Animation, Collider, Enum, Node, RigidBody, Vec3 } from 'cc';
  2. import { BaseExp } from '../core/base/BaseExp';
  3. import { PoolManager } from '../core/manager/PoolManager';
  4. import { userIns } from '../data/UserData';
  5. import { Logger } from '../extend/Logger';
  6. import { ResUtil } from '../utils/ResUtil';
  7. import { Game } from './Game';
  8. import { Enemy, EPartType } from './Enemy';
  9. import { audioMgr } from '../core/manager/AudioManager';
  10. import { Constants } from '../data/Constants';
  11. const { ccclass, property } = _decorator;
  12. //沙袋堆动画
  13. const sandbge_take = "take"
  14. /**
  15. * 战场障碍物类型
  16. */
  17. export enum ObstacleType {
  18. /** 沙袋堆 */
  19. SANDBAG_PILE = "沙袋堆",
  20. /** 油桶 */
  21. OIL_BARREL = "油桶",
  22. /** 军车 */
  23. MILITARY_TRUCK = "军车",
  24. /** 小沙袋 */
  25. SMALL_SANDBAG = "小沙袋"
  26. }
  27. /**
  28. * 杂物建筑物类
  29. */
  30. @ccclass('Sundries')
  31. export class Sundries extends BaseExp {
  32. @property({ type: Enum(ObstacleType), tooltip: "沙袋堆:SANDBAG_PILE 油桶:OIL_BARREL 军车: MILITARY_TRUCK 小沙袋:SMALL_SANDBAG", displayName: "障碍物类型"})
  33. public obstacleType: ObstacleType = ObstacleType.SANDBAG_PILE;
  34. //添加爆炸队列和标记
  35. private static explosionQueue: {
  36. target: Enemy | Sundries,
  37. damage: number,
  38. source: any }[] = [];
  39. //标记是否正在处理爆炸队列
  40. private static isProcessingQueue = false;
  41. //杂物信息数据
  42. public data: any = null;
  43. //杂物是否被打报废
  44. private isDead: boolean = false;
  45. //杂物当前的总血量
  46. private totalHp: number = 0;
  47. start() {
  48. //给碰撞体绑定数据
  49. this.setupColliders(this.node);
  50. //根据障碍物类型设置总血量
  51. let sundrie_id: number = -1;
  52. switch (this.obstacleType) {
  53. case ObstacleType.SANDBAG_PILE:
  54. sundrie_id = 11001;
  55. break;
  56. case ObstacleType.OIL_BARREL:
  57. sundrie_id = 11002;
  58. break;
  59. case ObstacleType.MILITARY_TRUCK:
  60. sundrie_id = 11003;
  61. break;
  62. case ObstacleType.SMALL_SANDBAG:
  63. sundrie_id = 11004;
  64. break;
  65. }
  66. if(sundrie_id !== -1){
  67. const data: any = userIns.sundriesTable.find(e=>e.id == sundrie_id);
  68. this.data = data;
  69. this.totalHp = data.hp;
  70. }else{
  71. Logger.log('未找到对应的杂物信息');
  72. }
  73. }
  74. /**
  75. * 杂物被打掉血
  76. * @param hp 血
  77. * @param pData 玩家数据
  78. */
  79. public subHP(hp: number, pData:any){
  80. if(!this.node
  81. ||Game.I.isPause
  82. ||this.isDead
  83. ||hp == null
  84. ||this.totalHp <= 0){
  85. return;
  86. }
  87. this.totalHp -= hp;
  88. //杂物打打死报废
  89. if(this.totalHp <= 0
  90. && !this.isDead){
  91. this.isDead = true;
  92. this.scrap();
  93. }
  94. }
  95. /**
  96. * 杂物被打报废
  97. */
  98. public scrap(){
  99. switch (this.obstacleType) {
  100. case ObstacleType.SANDBAG_PILE:{//沙袋堆
  101. //沙袋掀开的动画
  102. let anim:Animation = this.node.getComponent(Animation);
  103. if(!anim)return;
  104. anim.getState(sandbge_take).repeatCount = 1;
  105. anim.play(sandbge_take);
  106. }
  107. break;
  108. case ObstacleType.OIL_BARREL:{//油桶
  109. //油漆桶音效
  110. audioMgr.playOneShot(Constants.audios.Oildrum_explosion);
  111. //爆炸后生成爆炸特效 粒子路径、自动回收时间、外部设置回调
  112. ResUtil.playParticle(
  113. `effects/Prefabs/OilBoom`,
  114. 4,
  115. (particle) => {
  116. particle.parent = this.node.parent;
  117. const targetPos: Vec3 = this.node.worldPosition.clone();
  118. particle.worldPosition = targetPos;
  119. particle.active = true;
  120. this.recycle();
  121. //爆炸后生成爆炸伤害
  122. this.explosionDamage(targetPos);
  123. }
  124. );
  125. }
  126. break;
  127. case ObstacleType.MILITARY_TRUCK:{//军车
  128. //加载报废的军车
  129. ResUtil.loadRes(`enemy/sundries/scrap_car`, this.node)
  130. .then((military: Node | null) => {
  131. if(!military)return;
  132. military.active = true;
  133. military.parent = this.node.parent;
  134. const targetPos: Vec3 = this.node.worldPosition.clone();
  135. military.worldPosition = targetPos;
  136. military.eulerAngles = this.node.eulerAngles.clone();
  137. military.scale = this.node.scale.clone();
  138. //回收原有的军车
  139. this.recycle();
  140. //播放爆炸过后的浓烟
  141. ResUtil.playParticle(
  142. `effects/Prefabs/HeavySmoke`,
  143. 3,
  144. (particle) => {
  145. particle.parent = Game.I.map.node;
  146. particle.worldPosition = targetPos;
  147. particle.active = true;
  148. //爆炸后生成爆炸伤害
  149. this.explosionDamage(targetPos);
  150. }
  151. );
  152. });
  153. }
  154. break;
  155. case ObstacleType.SMALL_SANDBAG:{//小沙袋
  156. //获取刚体组件
  157. const rigidBody = this.node.getComponent(RigidBody);
  158. if (!rigidBody) return;
  159. //计算抛射方向
  160. const startPos = this.node.worldPosition.clone();
  161. const playerPos = Game.I.player.node.worldPosition.clone();
  162. const direction = startPos.subtract(playerPos).normalize();
  163. //随机抛射参数 水平力1500-2500N
  164. const horizontalForce = 1500 + Math.random() * 1000;
  165. //垂直力800-1500N
  166. const verticalForce = 800 + Math.random() * 700;
  167. //旋转扭矩50-150
  168. const torqueForce = 50 + Math.random() * 100;
  169. //施加作用力
  170. rigidBody.applyForce(new Vec3(
  171. direction.x * horizontalForce,
  172. verticalForce,
  173. direction.z * horizontalForce
  174. ));
  175. //施加旋转扭矩
  176. rigidBody.applyTorque(new Vec3(0, torqueForce, 0));
  177. }
  178. break;
  179. }
  180. }
  181. /**
  182. * 爆炸物产生爆炸伤害
  183. * @param targetPos 爆炸物的位置
  184. */
  185. public explosionDamage(targetPos: Vec3){
  186. //爆炸半径
  187. const eRadius: number = this.data.explosion_radius / 10;
  188. //查找不是自身的所有杂物 然后去看看是否有连续爆炸
  189. const allSundries: Sundries[] = Game.I.map.sundries.children
  190. .filter(node => node.active && node.parent && node != this.node)
  191. .map(node => node.getComponent(Sundries))
  192. .filter(comp =>
  193. comp && !comp.isDead
  194. ) as Sundries[];
  195. const allEnemys = [...Game.I.buildEnemys.allEnemys.filter(e=>!e.isDead),...allSundries];
  196. if(allEnemys.length <= 0)return;
  197. //爆炸伤害
  198. const damage: number = this.data.explosion_injury;
  199. allEnemys.forEach((e: any) => {
  200. if(e.node){
  201. const ePos: Vec3 = e.node.worldPosition;
  202. const distance = ePos.clone().subtract(targetPos).length();
  203. if(distance <= eRadius) {
  204. if(e instanceof Enemy){//敌人直接扣血
  205. e.subHP(damage, this.data);
  206. }else{//爆炸物 如果队列未在处理则开始处理 将爆炸目标加入队列而不是立即触发
  207. Sundries.explosionQueue.push({
  208. target: e,
  209. damage: damage,
  210. source: this.data
  211. });
  212. if(!Sundries.isProcessingQueue) {
  213. this.processExplosionQueue();
  214. }
  215. }
  216. }
  217. }
  218. })
  219. }
  220. /**
  221. * 处理爆炸队列(新增方法)
  222. */
  223. private processExplosionQueue() {
  224. if (Sundries.explosionQueue.length == 0) {
  225. Sundries.isProcessingQueue = false;
  226. return;
  227. }
  228. Sundries.isProcessingQueue = true;
  229. const explosion = Sundries.explosionQueue.shift()!;
  230. //触发当前爆炸
  231. explosion.target.subHP(explosion.damage, explosion.source);
  232. //添加0.2秒延迟后处理下一个爆炸
  233. this.scheduleOnce(()=>{
  234. this.processExplosionQueue();
  235. },0.2)
  236. }
  237. /**
  238. * 递归设置所有子节点的Collider参数
  239. * @param node 起始节点
  240. */
  241. private setupColliders(node: Node) {
  242. const collider = node.getComponent(Collider);
  243. if(collider){
  244. collider["args"] = [EPartType.sundries, this];
  245. }
  246. //递归处理所有子节点
  247. node.children.forEach(child => {
  248. this.setupColliders(child);
  249. });
  250. }
  251. /**
  252. * 杂物回收
  253. */
  254. public recycle(){
  255. if(this.node.parent){
  256. this.isDead = true;
  257. PoolManager.putNode(this.node);
  258. }
  259. }
  260. }