Player.ts 8.6 KB

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