Sundries.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import { _decorator, Enum, Node, RigidBody, SkeletalAnimation, 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 { autoBind } from '../extend/AutoBind';
  6. import { Logger } from '../extend/Logger';
  7. import { ResUtil } from '../utils/ResUtil';
  8. import { Game } from './Game';
  9. const { ccclass, property } = _decorator;
  10. /**
  11. * 战场障碍物类型
  12. */
  13. export enum ObstacleType {
  14. /** 沙袋堆 */
  15. SANDBAG_PILE = "沙袋堆",
  16. /** 油桶 */
  17. OIL_BARREL = "油桶",
  18. /** 军车 */
  19. MILITARY_TRUCK = "军车",
  20. /** 小沙袋 */
  21. SMALL_SANDBAG = "小沙袋"
  22. }
  23. /**
  24. * 杂物建筑物类
  25. */
  26. @ccclass('Sundries')
  27. export class Sundries extends BaseExp {
  28. @property({ type: Enum(ObstacleType), tooltip: "SANDBAG_PILE: 沙袋堆 OIL_BARREL: 油桶 MILITARY_TRUCK: 军车 SMALL_SANDBAG: 小沙袋 ", displayName: "障碍物类型"})
  29. public obstacleType: ObstacleType = ObstacleType.SANDBAG_PILE;
  30. @autoBind({type: SkeletalAnimation,tooltip: "杂物动画节点"})
  31. public skeletal: SkeletalAnimation = null!;
  32. //杂物信息数据
  33. public data: any = null;
  34. //杂物是否被打报废
  35. private isDead: boolean = false;
  36. //杂物当前的总血量
  37. private totalHp: number = 0;
  38. start() {
  39. //根据障碍物类型设置总血量
  40. let sundrie_id: number = -1;
  41. switch (this.obstacleType) {
  42. case ObstacleType.SANDBAG_PILE:
  43. sundrie_id = 11001;
  44. break;
  45. case ObstacleType.OIL_BARREL:
  46. sundrie_id = 11002;
  47. break;
  48. case ObstacleType.MILITARY_TRUCK:
  49. sundrie_id = 11003;
  50. break;
  51. case ObstacleType.SMALL_SANDBAG:
  52. sundrie_id = 11004;
  53. break;
  54. }
  55. if(sundrie_id !== -1){
  56. const data: any = userIns.sundriesTable.find(e=>e.id == sundrie_id);
  57. this.data = data;
  58. this.totalHp = data.hp;
  59. }else{
  60. Logger.log('未找到对应的杂物信息');
  61. }
  62. }
  63. /**
  64. * 杂物被打掉血
  65. * @param hp 血
  66. * @param pData 玩家数据
  67. */
  68. public subHP(hp: number, pData:any){
  69. if(Game.I.isPause
  70. ||this.isDead
  71. ||hp == null
  72. ||this.totalHp <= 0){
  73. return;
  74. }
  75. this.totalHp -= hp;
  76. //杂物打打死报废
  77. if(this.totalHp <= 0 && !this.isDead){
  78. this.scrap();
  79. //audioMgr.playDieAudios();
  80. }
  81. }
  82. /**
  83. * 杂物被打报废
  84. */
  85. public async scrap(){
  86. switch (this.obstacleType) {
  87. case ObstacleType.SANDBAG_PILE:{//沙袋堆
  88. if(this.skeletal)return;
  89. //沙袋掀开的动画
  90. this.skeletal.play('take');
  91. this.scheduleOnce(this.recycle.bind(this), 1.5);
  92. }
  93. break;
  94. case ObstacleType.OIL_BARREL:{//油桶
  95. if(this.skeletal)return;
  96. //油漆桶爆炸动画
  97. this.skeletal.play('blast');
  98. this.scheduleOnce(this.recycle.bind(this),3);
  99. //爆炸后生成爆炸特效
  100. }
  101. break;
  102. case ObstacleType.MILITARY_TRUCK:{//军车
  103. //加载报废的军车
  104. const military:Node = await ResUtil.loadEnemyRes('military_scrap',this.node.parent);
  105. military.worldPosition = this.node.worldPosition.clone();
  106. //回收原有的军车
  107. this.recycle();
  108. //播放爆炸动画
  109. if(military){
  110. const skeletal: SkeletalAnimation = military.getComponent(SkeletalAnimation);
  111. if(skeletal){
  112. //skeletal.play('explosion');
  113. }
  114. }
  115. }
  116. break;
  117. case ObstacleType.SMALL_SANDBAG:{//小沙袋
  118. //获取刚体组件
  119. const rigidBody = this.node.getComponent(RigidBody);
  120. if (!rigidBody) return;
  121. //计算抛射方向
  122. const startPos = this.node.worldPosition.clone();
  123. const playerPos = Game.I.player.node.worldPosition.clone();
  124. const direction = startPos.subtract(playerPos).normalize();
  125. //随机抛射参数 水平力1500-2500N
  126. const horizontalForce = 1500 + Math.random() * 1000;
  127. //垂直力800-1500N
  128. const verticalForce = 800 + Math.random() * 700;
  129. //旋转扭矩50-150
  130. const torqueForce = 50 + Math.random() * 100;
  131. //施加作用力
  132. rigidBody.applyForce(new Vec3(
  133. direction.x * horizontalForce,
  134. verticalForce,
  135. direction.z * horizontalForce
  136. ));
  137. //施加旋转扭矩
  138. rigidBody.applyTorque(new Vec3(0, torqueForce, 0));
  139. }
  140. break;
  141. }
  142. }
  143. /**
  144. * 杂物回收
  145. */
  146. public recycle(){
  147. if(this.node.parent){
  148. this.isDead = true;
  149. PoolManager.putNode(this.node);
  150. }
  151. }
  152. }