Sundries.ts 13 KB

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