Player.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import { _decorator, Node, SkeletalAnimation } 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. /**玩家的位置节点*/
  37. public playPosNodes: Node[] = []!;
  38. /**玩家当前血量*/
  39. public curHP: number = 0;
  40. /**玩家是否死亡*/
  41. public isDead: boolean = false;
  42. /**玩家拥有的枪*/
  43. public gun: GunBase = null;
  44. //玩家数据
  45. public pData:any = null
  46. //是否去设置gunfightShootUI的基础信息
  47. public isCutShoot:boolean = false;
  48. //射击ui类
  49. private _shootUI: GunfightShootUI | null = null;
  50. //懒获取射击界面
  51. public get shootUI(): GunfightShootUI {
  52. if (!this._shootUI) {
  53. this._shootUI = uiMgr.getPageComponent(Constants.popUIs.gunfightShootUI);
  54. }
  55. return this._shootUI!;
  56. }
  57. //是否是换弹夹状态
  58. public isReloadMagazine: boolean = false;
  59. //玩家击杀敌人过后 敌人累加的精准度 玩家每杀3个敌人AI增加1%的精准度
  60. private addPrecision: number = 0;
  61. //连续通关 玩家每杀3个敌人AI增加15%的血量 AI增加5%的精准度
  62. private addhp: number = 0;
  63. //玩家打敌人爆头的个数
  64. public headShotNum:number = 0;
  65. start() {
  66. //隐藏玩家设置的位置坐标节点
  67. (this.playPosNodes = this.node.parent.getChildByName('player_points').children)
  68. .forEach(e => e.active = false);
  69. //隐藏的枪的位置节点
  70. this.gun_pos.children.forEach(e => e.active = false);
  71. this.pData = userIns.getCurUseGun();
  72. //创建玩家拥有的枪
  73. this.loadGunName(this.pData.prb_name);
  74. }
  75. /**
  76. * 加载枪
  77. * @param name 枪的名字
  78. * @param isCut 是否是切换枪
  79. */
  80. public loadGunName(name: string,isCut: boolean = false){
  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){
  87. PoolManager.putNode(this.gun.node);
  88. }
  89. ResUtil.loadGunRes(`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. let time: number = (data.reloadingSpeed * 4) / 1000;
  114. //换弹匣动画
  115. this.player_skeletal.play(PAnimType.reload);
  116. //获取动画原始时长
  117. const clip = this.player_skeletal.clips.find(c => c.name === PAnimType.reload);
  118. //计算需要设置的速度
  119. clip.speed = clip!.duration / time;
  120. this.shootUI.reloadMagazineing(time,()=>{
  121. this.isReloadMagazine = false;
  122. this.shootUI.gunDataUI();
  123. this.player_skeletal.play(PAnimType.idle);
  124. });
  125. }
  126. /**
  127. * 玩家发射子弹
  128. */
  129. public shoot() {
  130. if(!this.gun
  131. || this.isReloadMagazine)return;
  132. this.gun.fire();
  133. this.shootUI.crossHairStability();
  134. Game.I.player.mainCamera.shake(0.2,0,0.1);
  135. }
  136. /**
  137. * 随机切枪
  138. */
  139. public randomCutGun(){
  140. let guns = userIns.data.guns;
  141. if(guns.length > 1){
  142. let idx : number = guns.findIndex(e=>e.id == this.pData.id);
  143. let next:number = (idx + 1) % guns.length;
  144. const cutGun:any = guns[next];
  145. userIns.changeGun(cutGun.id);
  146. this.loadGunName(cutGun.prb_name,true);
  147. }
  148. }
  149. /**
  150. * 移除枪回收武器
  151. */
  152. public removeGun(){
  153. if(!this.gun)return;
  154. this.gun.endFire();
  155. PoolManager.putNode(this.gun.node);
  156. this.gun = null;
  157. }
  158. /**
  159. * 玩家扣掉血
  160. * @param hp 血
  161. * @param eData 敌人数据
  162. */
  163. public subHP(hp: number,eData:any){
  164. if(Game.I.isPause
  165. ||Game.I.isGameOver
  166. ||this.isDead
  167. ||hp <= 0){
  168. return;
  169. }
  170. this.curHP -= hp;
  171. this.curHP = this.curHP < 0 ? 0 : this.curHP;
  172. //设置血条
  173. const progress:number = this.curHP / this.pData.hp;
  174. this.shootUI.playerHurtTwinkle(progress);
  175. //敌人死亡
  176. if(this.curHP <= 0 && !this.isDead){
  177. Game.I.isGameOver = true;
  178. audioMgr.playOneShot(Constants.audios.loss);
  179. uiMgr.show(Constants.popUIs.settleUI,[{
  180. isWin: false,
  181. headShotNum: Game.I.player.headShotNum,
  182. cb: ()=>{}
  183. }]);
  184. this.isDead = true;
  185. this.recycle();
  186. this.player_skeletal.play(PAnimType.die);
  187. this.scheduleOnce(() => {
  188. this.player_skeletal.stop();
  189. this.player_skeletal.node.active = false;
  190. }, 2);
  191. }
  192. }
  193. /**
  194. * 根据关卡设置玩家的位置
  195. */
  196. public setPlayerPos(){
  197. if(!this.pData)return;
  198. let length:number = this.playPosNodes.length;
  199. const data = userIns.getCurLevelData();
  200. const safeIndex = Math.min(data.point -1, length - 1);
  201. const n: Node = this.playPosNodes[safeIndex];
  202. this.node.worldPosition = n.worldPosition.clone();
  203. }
  204. /**
  205. * 回收
  206. */
  207. public recycle(){
  208. this.removeGun();
  209. this.isReloadMagazine = false;
  210. this.addPrecision = 0;
  211. this.addhp = 0;
  212. this.headShotNum = 0;
  213. }
  214. /**
  215. * 设置枪的数据
  216. */
  217. protected update(dt: number): void {
  218. if(this.isCutShoot){
  219. //设置玩家枪的信息
  220. if(this.shootUI && this.pData){
  221. this.shootUI.gunDataUI();
  222. this.isCutShoot = false;
  223. }
  224. }
  225. }
  226. }