Player.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import { _decorator, Node, SkeletalAnimation, Tween, Vec3 } from 'cc';
  2. import { BaseExp } from '../core/base/BaseExp';
  3. import { PoolManager } from '../core/manager/PoolManager';
  4. import { uiMgr } from '../core/manager/UIManager';
  5. import { Constants } from '../data/Constants';
  6. import { userIns } from '../data/UserData';
  7. import { autoBind } from '../extend/AutoBind';
  8. import { GunBase } from '../items/base/GunBase';
  9. import { GunfightShootUI } from '../ui/GunfightShootUI';
  10. import { ResUtil } from '../utils/ResUtil';
  11. import { Game } from './Game';
  12. import MsgHints from '../utils/MsgHints';
  13. import { PlayerCamera } from './PlayerCamera';
  14. import { audioMgr } from '../core/manager/AudioManager';
  15. import { Utils } from '../utils/Utils';
  16. const { ccclass, property } = _decorator;
  17. //玩家动作类型
  18. export enum PAnimType {
  19. idle = "idle",//idle 待机状态
  20. die = "die",//die 死亡状态
  21. reload = "reload",//reload 换弹夹状态
  22. }
  23. @ccclass('Player')
  24. export class Player extends BaseExp {
  25. @autoBind({type: Node,tooltip: "创建枪的位置"})
  26. public gun_pos: Node = null!;
  27. @autoBind({type: Node,tooltip: "玩家头的位置"})
  28. public head: Node = null!;
  29. @autoBind({type: Node,tooltip: "敌人埋伏点"})
  30. public ambush: Node = null!;
  31. @autoBind({type: Node,tooltip: "玩家身子的位置"})
  32. public body: Node = null!;
  33. @autoBind({type: SkeletalAnimation,tooltip: "玩家人物动画节点"})
  34. public player_skeletal: SkeletalAnimation = null!;
  35. @autoBind({type: PlayerCamera,tooltip: "玩家摄像机抖动控制"})
  36. public mainCamera: PlayerCamera = null!;
  37. @autoBind({type: Node,tooltip: "玩家摄像机抖动控制"})
  38. public GGGGGGG: Node = null!;
  39. /**玩家的位置节点*/
  40. public playPosNodes: Node[] = []!;
  41. /**玩家当前血量*/
  42. public curHP: number = 0;
  43. /**玩家是否死亡*/
  44. public isDead: boolean = false;
  45. /**玩家拥有的枪*/
  46. public gun: GunBase = null;
  47. //玩家数据
  48. public pData:any = null
  49. //是否去设置gunfightShootUI的基础信息
  50. public isCutShoot:boolean = false;
  51. //射击ui类
  52. private _shootUI: GunfightShootUI | null = null;
  53. //懒获取射击界面
  54. public get shootUI(): GunfightShootUI {
  55. if (!this._shootUI) {
  56. this._shootUI = uiMgr.getPageComponent(Constants.popUIs.gunfightShootUI);
  57. }
  58. return this._shootUI!;
  59. }
  60. //是否是换弹夹状态
  61. public isReloadMagazine: boolean = false;
  62. //玩家打敌人爆头的个数
  63. public headShotNum:number = 0;
  64. start() {
  65. //隐藏玩家设置的位置坐标节点
  66. (this.playPosNodes = this.node.parent.getChildByName('player_points').children)
  67. .forEach(e => e.active = false);
  68. //隐藏的枪的位置节点
  69. this.gun_pos.children.forEach(e => e.active = false);
  70. this.pData = userIns.getCurUseGun();
  71. //创建玩家拥有的枪
  72. this.loadGunName(this.pData.prb_name);
  73. }
  74. /**
  75. * 加载枪
  76. * @param name 枪的名字
  77. * @param isCut 是否是切换枪
  78. */
  79. public loadGunName(name: string = this.pData.prb_name,isCut: boolean = false){
  80. if(Utils.isNull(name))return;
  81. //玩家待机状态
  82. this.player_skeletal.play(PAnimType.idle);
  83. this.pData = userIns.getCurUseGun();
  84. this.curHP = this.pData.hp;
  85. if(!isCut && this.gun)return;
  86. if(isCut && this.gun){
  87. PoolManager.putNode(this.gun.node);
  88. }
  89. ResUtil.loadRes(`player/${name}`).then((gunNode: Node) => {
  90. if(!gunNode)return;
  91. this.isCutShoot = true;
  92. gunNode.parent = this.node;
  93. this.gun = gunNode.getComponent(GunBase);
  94. let pos_ui: Node = this.gun_pos.children.find(e=>e.name == this.pData.prb_name);
  95. if(!pos_ui){
  96. MsgHints.show("未找到武器没有对应的位置节点");
  97. }
  98. pos_ui = pos_ui || this.gun_pos;
  99. if(pos_ui){
  100. gunNode.worldPosition = pos_ui.worldPosition.clone();
  101. gunNode.eulerAngles = pos_ui.parent.eulerAngles.clone();
  102. gunNode.scale = pos_ui.scale.clone();
  103. }
  104. this.gun.init(this.pData,this,()=>{},this.reloadMagazine.bind(this));
  105. });
  106. }
  107. /**
  108. * 更换弹匣
  109. */
  110. public reloadMagazine(data:any){
  111. if(!data || this.isReloadMagazine)return;
  112. this.isReloadMagazine = true;
  113. const factor: number = 130;
  114. let time: number = data.reloadingSpeed / factor;
  115. //换弹匣动画
  116. ResUtil.playSkeletalAnim(this.player_skeletal,PAnimType.reload,time);
  117. this.shootUI.reloadMagazineing(time,()=>{
  118. this.isReloadMagazine = false;
  119. this.shootUI.gunDataUI();
  120. this.player_skeletal?.play(PAnimType.idle);
  121. });
  122. }
  123. /**
  124. * 重置
  125. */
  126. public restart(){
  127. Game.I.isGameOver = Game.I.isPause = false;
  128. this.isDead = false;
  129. this.isReloadMagazine = false;
  130. if(this.shootUI){
  131. this.shootUI.hpProgressBar.progress = 1;
  132. Tween.stopAllByTarget(this.shootUI.injury_blood);
  133. this.shootUI.injury_blood.active = false;
  134. }
  135. this.loadGunName();
  136. this.player_skeletal.unscheduleAllCallbacks();
  137. this.player_skeletal.stop();
  138. this.player_skeletal.node.active = true;
  139. this.player_skeletal.play(PAnimType.idle);
  140. }
  141. /**
  142. * 玩家发射子弹
  143. */
  144. public shoot() {
  145. if(!this.gun
  146. || this.isReloadMagazine)return;
  147. this.gun.fire();
  148. this.shootUI.crossHairStability();
  149. Game.I.player.mainCamera.shake(0.2,0,0.1);
  150. }
  151. /**
  152. * 随机切枪
  153. */
  154. public randomCutGun(){
  155. let guns = userIns.data.guns;
  156. if(guns.length > 1){
  157. let idx : number = guns.findIndex(e=>e.id == this.pData.id);
  158. let next:number = (idx + 1) % guns.length;
  159. const cutGun:any = guns[next];
  160. userIns.changeGun(cutGun.id);
  161. this.loadGunName(cutGun.prb_name,true);
  162. }
  163. }
  164. /**
  165. * 移除枪回收武器
  166. */
  167. public removeGun(){
  168. if(!this.gun)return;
  169. this.gun.endFire();
  170. PoolManager.putNode(this.gun.node);
  171. this.gun = null;
  172. }
  173. /**
  174. * 玩家扣掉血
  175. * @param hp 血
  176. * @param eData 敌人数据
  177. */
  178. public subHP(hp: number,eData:any){
  179. if(Game.I.isPause
  180. ||Game.I.isGameOver
  181. ||this.isDead
  182. ||hp <= 0){
  183. return;
  184. }
  185. this.curHP -= hp;
  186. this.curHP = this.curHP < 0 ? 0 : this.curHP;
  187. //设置血条
  188. const progress:number = this.curHP / this.pData.hp;
  189. this.shootUI.playerHurtTwinkle(progress);
  190. //敌人死亡
  191. if(this.curHP <= 0 && !this.isDead){
  192. Game.I.isGameOver = true;
  193. audioMgr.playOneShot(Constants.audios.loss);
  194. uiMgr.show(Constants.popUIs.settleUI,[{
  195. isWin: false,
  196. headShotNum: this.headShotNum,
  197. cb: ()=>{}
  198. }]);
  199. this.isDead = true;
  200. this.recycle();
  201. ResUtil.playSkeletalAnim(this.player_skeletal,PAnimType.die,1.5,()=>{
  202. this.player_skeletal.stop();
  203. this.player_skeletal.node.active = false;
  204. });
  205. }
  206. }
  207. /**
  208. * 根据关卡设置玩家的位置
  209. */
  210. public setPlayerPos(){
  211. if(!this.pData)return;
  212. let length:number = this.playPosNodes.length;
  213. const data = userIns.getCurLevelData();
  214. const safeIndex = Math.min(data.point -1, length - 1);
  215. const n: Node = this.playPosNodes[safeIndex];
  216. this.node.worldPosition = n.worldPosition.clone();
  217. }
  218. /**
  219. * 回收
  220. */
  221. public recycle(){
  222. this.removeGun();
  223. this.isReloadMagazine = false;
  224. this.headShotNum = 0;
  225. }
  226. /**
  227. * 设置枪的数据
  228. */
  229. protected update(dt: number): void {
  230. if(this.isCutShoot){
  231. //设置玩家枪的信息
  232. if(this.shootUI && this.pData){
  233. this.shootUI.gunDataUI();
  234. this.isCutShoot = false;
  235. }
  236. }
  237. }
  238. }