Sundries.ts 12 KB

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