Player.ts 7.5 KB

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