GunfightShootUI.ts 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. import { _decorator, EventTouch, Node, Quat, math, Camera, sys, Vec3, Label, geometry, ProgressBar, tween, easing, Tween, UIOpacity} from 'cc';
  2. import { Game } from '../game/Game';
  3. import { BaseExp } from '../core/base/BaseExp';
  4. import { autoBind } from '../extend/AutoBind';
  5. import { Constants } from '../data/Constants';
  6. import { uiMgr } from '../core/manager/UIManager';
  7. import { GunAttribute, userIns } from '../data/UserData';
  8. import MsgHints from '../utils/MsgHints';
  9. import List from '../third/List';
  10. import { TaskEnemyItem } from '../items/item/TaskEnemyItem';
  11. import { BulletMagazine } from '../game/BulletMagazine';
  12. import { settingData } from '../data/SettingData';
  13. import i18n from '../core/i18n/runtime-scripts/LanguageData';
  14. import PlatformSystem from '../platform/PlatformSystem';
  15. import { GameEnums } from '../data/GameEnums';
  16. const { ccclass, property } = _decorator;
  17. const { clamp, toRadian } = math;
  18. @ccclass('GunfightShootUI')
  19. export class GunfightShootUI extends BaseExp {
  20. @autoBind({ type: Node, tooltip: "轮盘节点" })
  21. public wheel: Node;
  22. @autoBind({ type: Node, tooltip: "标准贴图" })
  23. public scopeOverlay: Node;
  24. @autoBind({ type: Node, tooltip: "准心" })
  25. public crossHair: Node;
  26. @autoBind({ type: ProgressBar, tooltip: "步枪子弹进度条" })
  27. public rifle_bullet_progressBar: ProgressBar;
  28. @autoBind({ type: Label, tooltip: "步枪子弹数文本" })
  29. public rifle_bullet_num_label: Label;
  30. @autoBind({ type: BulletMagazine, tooltip: "步枪子弹列表" })
  31. public rifle_bullets_scrollView: BulletMagazine;
  32. @autoBind({ type: BulletMagazine, tooltip: "狙击枪子弹列表" })
  33. public snipe_bullets_scrollView: BulletMagazine;
  34. @autoBind({ type: Node, tooltip: "步枪显示子弹的总的节点" })
  35. public rifle_bullets_bg: Node;
  36. @autoBind({ type: List, tooltip: "敌人任务列表" })
  37. public task_scrollView: List;
  38. @autoBind({ type: ProgressBar, tooltip: "玩家生命进度条" })
  39. public hpProgressBar: ProgressBar;
  40. @property({type: [Node], tooltip: "要隐藏的所有按钮"})
  41. public hiddeNodes: Array<Node> = [];
  42. @autoBind({ type: ProgressBar, tooltip: "换弹夹时间进度条" })
  43. public reloadProgressBar: ProgressBar;
  44. @autoBind({ type: Node, tooltip: "玩家受到伤害呼吸闪烁" })
  45. public injury_blood: Node;
  46. @autoBind({ type: Label, tooltip: "玩家枪的名字" })
  47. public gun_name_label: Label;
  48. @autoBind({ type: Label, tooltip: "关卡名字" })
  49. public gk_num_lable: Label;
  50. @autoBind({ type: Node, tooltip: "切枪按钮" })
  51. public cut_gun_btn: Node;
  52. @autoBind({ type: Label, tooltip: "剩余切换次数" })
  53. public cut_num_label: Label;
  54. @autoBind({ type: Node, tooltip: "切枪看视频的图标按钮" })
  55. public cut_video: Node;
  56. /** 子弹飞行距离 */
  57. public bulletDistance: number = 800;
  58. /** 当前相机绕 X 轴的旋转角度 */
  59. private currentXRotation: number = 0;
  60. /** 当前相机绕 Y 轴的旋转角度 */
  61. private currentYRotation: number = 0;
  62. /** 左右旋转角度的最大限制值 */
  63. private maxHorizontalAngle: number = 180;
  64. /** 上下旋转角度的最大限制值 */
  65. private maxVerticalAngle: number = 70;
  66. /** 相机的原始视野值 */
  67. private originalFov: number;
  68. /** 玩家节点的初始旋转四元数 */
  69. private initialPlayerRotation: Quat = new Quat();
  70. /** 相机节点的初始旋转四元数 */
  71. private initialCameraRotation: Quat = new Quat();
  72. /** 相机放大后的目标视野值 */
  73. private targetFov: number;
  74. /** 标记相机是否正在进行放大操作 */
  75. private isZoomingIn: boolean = false;
  76. /** 标记相机是否正在进行缩小操作 */
  77. private isZoomingOut: boolean = false;
  78. /** 相机视野放大的速度 */
  79. private zoomSpeed: number = 0;
  80. /** 期望的缩放时间,单位为秒 */
  81. private zoomDuration: number = 0.2;
  82. /** 记录镜头开始放大的时间 */
  83. private zoomStartTime: number = 0;
  84. /** 标记是否在2秒内完成放大 */
  85. private zoomValid: boolean = false;
  86. /** 标记是否开镜 */
  87. private _isScopeOpen: boolean = false;
  88. get isScopeOpen() {
  89. return this._isScopeOpen;
  90. }
  91. set isScopeOpen(value: boolean) {
  92. this.gunDataUI();
  93. if (this._isScopeOpen !== value) {
  94. this._isScopeOpen = value;
  95. if (value) {
  96. this.openScope();
  97. } else {
  98. this.closeScope();
  99. }
  100. }
  101. }
  102. /**镜头的位置*/
  103. private wheelPos: Vec3 = Vec3.ZERO;
  104. //任务数据
  105. private taskDatas: Array<any> = [];
  106. //是否正在被击中
  107. private isHurtRun: boolean = false;
  108. start() {
  109. this.hasAnim = false;
  110. this.closeOnBlank = false;
  111. this.injury_blood.active = false;
  112. this.reloadProgressBar.node.active = false;
  113. if(!Constants.isDebug){
  114. this.hiddeNodes.forEach(e => e.active = false);
  115. }
  116. this.wheelPos = this.wheel.position.clone();
  117. //敌人数量改变
  118. this.register(Constants.eventName.enemy_num_change, this.loadTaskData.bind(this));
  119. //枪的数量发生变化
  120. this.register(Constants.eventName.gun_num_change, this.gunNumChange.bind(this));
  121. //监听当前节点触摸事件
  122. this.node.on(Node.EventType.TOUCH_START, this.onNodeTouchMove, this);
  123. this.node.on(Node.EventType.TOUCH_MOVE, this.onNodeTouchMove, this);
  124. this.node.on(Node.EventType.TOUCH_END, this.onCustomNodeTouchEnd, this);
  125. this.node.on(Node.EventType.TOUCH_CANCEL, this.onCustomNodeTouchEnd, this);
  126. //监听轮盘点击事件 监听轮盘的触摸开始事件,当触摸开始时调用 onWheelClick 方法
  127. this.wheel.on(Node.EventType.TOUCH_START, this.onWheelClick, this);
  128. this.wheel.on(Node.EventType.TOUCH_END, this.onWheelRelease, this);
  129. this.wheel.on(Node.EventType.TOUCH_CANCEL, this.onWheelRelease, this);
  130. }
  131. public hide(){
  132. uiMgr.getPageComponent(Constants.mainUIs.main)?.playOrStopBGM(false);
  133. }
  134. /**
  135. * 摄像机初始信息
  136. * @param args
  137. */
  138. public originalInitial(){
  139. //记录摄像机原始视野
  140. this.originalFov = Game.I.camera.getComponent(Camera).fov;
  141. //录初始旋转角度
  142. this.initialPlayerRotation = Game.I.player.node.rotation.clone();
  143. this.initialCameraRotation = Game.I.camera.node.rotation.clone();
  144. this.currentYRotation = this.initialPlayerRotation.x;
  145. this.currentXRotation = this.initialPlayerRotation.y;
  146. }
  147. public show(...args: any[]){
  148. this.loadTaskData();
  149. }
  150. /**
  151. * 枪的数量发生变化 是否显示切枪按钮
  152. */
  153. public gunNumChange(){
  154. const isMoreGuns: boolean = userIns.data.guns.length > 1;
  155. this.cut_gun_btn.active = isMoreGuns;
  156. this.cut_video.active = isMoreGuns;
  157. if(userIns.data.cutNum > 0){
  158. this.cut_num_label.string = `x${userIns.data.cutNum}`;
  159. this.cut_video.active = false;
  160. }else{
  161. this.cut_num_label.string = `lack`;
  162. this.cut_video.active = true;
  163. }
  164. }
  165. /**
  166. * enemyId total count enemyData
  167. * 加载数据
  168. */
  169. public loadTaskData() {
  170. const data: any = userIns.getCurLevelData();
  171. this.gk_num_lable.string = i18n("main.关卡任务 %{value}",{value: data.id});
  172. this.taskDatas = Game.I.buildEnemys.enemyTypeRecords;
  173. this.task_scrollView.numItems = this.taskDatas.length;
  174. //加载子弹的数据
  175. this.gunDataUI();
  176. }
  177. /**
  178. * 任务数据
  179. * @param item item节点
  180. * @param idx 数据下标
  181. */
  182. public setTaskItemData(item: Node, idx: number) {
  183. item.getComponent(TaskEnemyItem).init(this.taskDatas[idx]);
  184. }
  185. /**
  186. * 设置枪的数据
  187. */
  188. public gunDataUI() {
  189. const gData:any = Game.I.player.pData;
  190. if(Game.I.isGameOver
  191. || Game.I.isPause
  192. || !Game.I.player.gun
  193. || !gData){
  194. return;
  195. }
  196. this.crossHair.active = true;//准心
  197. //步枪没有开镜贴图
  198. const isRifle: boolean = this.weaponCategoryGun() == GameEnums.weaponCategory.rifle;
  199. this.scopeOverlay.active = !isRifle;
  200. this.gun_name_label.string = gData.name_lang;
  201. const isSnipeGun: boolean = gData.type == 1;
  202. //换弹夹进度条
  203. const isMagazine: boolean = Game.I.player.isReloadMagazine;
  204. if(isMagazine){
  205. this.rifle_bullets_bg.active = false;
  206. this.snipe_bullets_scrollView.node.active = false;
  207. }else{
  208. //步枪子弹视图
  209. this.rifle_bullets_bg.active = !isSnipeGun;
  210. //狙击子弹视图列表
  211. this.snipe_bullets_scrollView.node.active = isSnipeGun;
  212. if(isSnipeGun){
  213. this.snipe_bullets_scrollView.magazineNum = gData.magazine;
  214. }else{
  215. this.rifle_bullets_scrollView.magazineNum = gData.magazine;
  216. //子弹进度条
  217. let s_bullet: number = gData.magazine - Game.I.player.gun.shotBullets;
  218. this.rifle_bullet_progressBar.progress = s_bullet / gData.magazine;
  219. this.rifle_bullet_num_label.string = `${s_bullet}/${gData.magazine}`;
  220. }
  221. }
  222. }
  223. /**
  224. * 更换弹夹动画
  225. * @param time 换弹时间 单位秒
  226. * @param complete 完成回调
  227. */
  228. public reloadMagazineing(time: number,complete?: Function) {
  229. if(time <= 0){
  230. complete?.();
  231. }else{
  232. this.reloadProgressBar.node.active = true;
  233. this.reloadProgressBar.progress = 1;
  234. this.rifle_bullets_bg.active = false;
  235. this.snipe_bullets_scrollView.node.active = false;
  236. //创建定时器动画
  237. tween(this.reloadProgressBar)
  238. .to(time, { progress: 0 }, {
  239. easing: easing.linear,
  240. onStart: () => {
  241. tween(this.reloadProgressBar).stop();
  242. },
  243. onComplete: () => {
  244. this.reloadProgressBar.node.active = false;
  245. complete?.();
  246. }
  247. })
  248. .start();
  249. }
  250. }
  251. /**
  252. * 根据切换枪的stability设置镜头的稳定性
  253. */
  254. public crossHairStability() {
  255. const gData:any = Game.I.player.pData;
  256. if(!gData)return;
  257. this.gunDataUI();
  258. this.wheel.position = this.wheelPos;
  259. //获取当前枪的最大稳定性值
  260. const maxStability:number = userIns.getGunMaxValue(gData, GunAttribute.stability).totalValue;
  261. // 计算稳定性因子
  262. const stabilityFactor = Math.max(0, 1 - (gData.stability / maxStability));
  263. //根据稳定性因子动态调整振幅
  264. const amplitude = 5 + (20 * stabilityFactor);
  265. //基础抖动持续时间0.3-0.6秒,稳定性越高持续时间越短
  266. const duration = 0.6 - (0.3 * (1 - stabilityFactor));
  267. let elapsed = 0;
  268. const updateShake = (deltaTime: number) => {
  269. elapsed += deltaTime;
  270. //双轴随机偏移
  271. const offsetX = amplitude * (Math.random() - 0.5) * Math.max(1 - elapsed/duration, 0);
  272. const offsetY = amplitude * (Math.random() - 0.5) * Math.max(1 - elapsed/duration, 0);
  273. this.wheel.position = new Vec3(
  274. this.wheelPos.x + offsetX,
  275. this.wheelPos.y + offsetY,
  276. this.wheelPos.z
  277. );
  278. //自动结束
  279. if(elapsed >= duration) {
  280. this.wheel.position = this.wheelPos;
  281. this.unschedule(updateShake);
  282. }
  283. };
  284. this.unschedule(updateShake);
  285. // 使用游戏时间而非系统时间
  286. this.schedule(updateShake, 0.02);
  287. }
  288. /**
  289. * 屏幕触摸事件
  290. * 触摸移动时,根据触摸的位置计算旋转角度,并应用到节点上
  291. * @param event
  292. */
  293. private onNodeTouchMove(event: EventTouch) {
  294. const delta = event.getDelta();
  295. let f: number = this._isScopeOpen ? 0.2 : 0.6;
  296. //计算旋转角度 相机旋转的灵敏度 值越大移动越快 灵敏度越高
  297. this.currentYRotation -= delta.x * settingData.data.sensitivity * f;
  298. this.currentXRotation -= delta.y * settingData.data.sensitivity * f;
  299. //分别限制左右和上下旋转角度
  300. this.currentYRotation = clamp(this.currentYRotation, -this.maxHorizontalAngle, this.maxHorizontalAngle);
  301. this.currentXRotation = clamp(this.currentXRotation, -this.maxVerticalAngle, this.maxVerticalAngle);
  302. //计算水平和垂直旋转的四元数
  303. const yQuat = new Quat();
  304. Quat.fromEuler(yQuat, 0, this.currentYRotation, 0);
  305. const xQuat = new Quat();
  306. Quat.fromEuler(xQuat, +this.currentXRotation, 0, 0);
  307. //合并旋转
  308. let finalQuat = new Quat();
  309. Quat.multiply(finalQuat, yQuat, xQuat);
  310. //将最终旋转与初始摄像机旋转合并
  311. Quat.multiply(finalQuat, this.initialPlayerRotation, finalQuat);
  312. //应用旋转到摄像机
  313. Game.I.player.node.rotation = finalQuat;
  314. }
  315. /**
  316. * 触摸结束
  317. * 触摸结束时 触摸结束处理方法
  318. */
  319. private onCustomNodeTouchEnd() {
  320. //触摸结束,可添加其他逻辑
  321. }
  322. /**
  323. * 开始慢慢放大视野
  324. */
  325. private onWheelClick() {
  326. this.isScopeOpen = true;
  327. }
  328. /**
  329. * 恢复原始视野
  330. */
  331. private onWheelRelease() {
  332. if(Game.I.player.isReloadMagazine)return;
  333. if(!this.zoomValid){
  334. this.isScopeOpen = false;
  335. return
  336. }
  337. const type:number = this.weaponCategoryGun();
  338. switch(type){
  339. case GameEnums.weaponCategory.sniper://狙击枪
  340. Game.I.player.shoot();
  341. this.scheduleOnce(()=>{
  342. this.isScopeOpen = false;
  343. },0.5)
  344. break
  345. case GameEnums.weaponCategory.dmr://连狙
  346. Game.I.player.shoot();
  347. break
  348. case GameEnums.weaponCategory.rifle://步枪
  349. this.isScopeOpen = false;
  350. break
  351. }
  352. }
  353. /**
  354. * 开始开镜
  355. */
  356. private openScope() {
  357. const gData = Game.I.player.pData;
  358. if(!gData)return;
  359. if(Game.I.player.isReloadMagazine)return;
  360. const isRifle: boolean = this.weaponCategoryGun() == GameEnums.weaponCategory.rifle;
  361. const type:number = this.weaponCategoryGun();
  362. switch(type){
  363. case GameEnums.weaponCategory.sniper://狙击枪
  364. //这儿已经在onWheelRelease处理过了
  365. break
  366. case GameEnums.weaponCategory.dmr://连狙
  367. //这儿已经在onWheelRelease处理过了
  368. break
  369. case GameEnums.weaponCategory.rifle://步枪 是步枪直接连续开火
  370. Game.I.player.shoot();
  371. break
  372. }
  373. this.isZoomingIn = true;
  374. //将zoomingSpeed 转换为持续时间(450对应1.2秒)
  375. this.zoomDuration = 72 / gData.zoomingSpeed;
  376. //使用枪械类型决定视口倍数 步枪使用 rifleZoom 狙击枪使用 scopeZoom
  377. const zoomMultiplier = isRifle ?
  378. 1 / gData.rifleZoom :
  379. 1 / gData.scopeZoom;
  380. this.targetFov = this.originalFov * zoomMultiplier;
  381. //保持原有速度计算逻辑
  382. const fovDifference = this.originalFov - this.targetFov;
  383. this.zoomSpeed = fovDifference / this.zoomDuration;
  384. this.zoomStartTime = sys.now();
  385. }
  386. /**
  387. * 关闭开镜
  388. */
  389. private closeScope() {
  390. this.isZoomingIn = false;
  391. this.isZoomingOut = true;
  392. const fovDifference = this.originalFov - this.targetFov;
  393. //使用相同 zoomDuration 保证缩镜速度一致
  394. this.zoomSpeed = fovDifference / this.zoomDuration;
  395. }
  396. /**
  397. * 判断是否属于某一种枪
  398. */
  399. public weaponCategoryGun():number{
  400. const gData:any = Game.I.player.pData;
  401. if(!gData)return GameEnums.weaponCategory.sniper;
  402. if(gData.id == GameEnums.palyerWeaponType.akm
  403. ||gData.id == GameEnums.palyerWeaponType.m416){
  404. return GameEnums.weaponCategory.rifle;
  405. }else if(gData.id == GameEnums.palyerWeaponType.vss
  406. ||gData.id == GameEnums.palyerWeaponType.sks){
  407. return GameEnums.weaponCategory.dmr;
  408. }else {
  409. return GameEnums.weaponCategory.sniper;
  410. }
  411. }
  412. /**
  413. * 计算从枪口出发,沿准心射线方向的点
  414. * @param isBulletEndPos 是否获得子弹的终点
  415. */
  416. public getCrossHairPos(){
  417. //获取屏幕准心位置(世界坐标)
  418. const screenPos = this.getScreenCenterPos();
  419. //校准补偿准心的偏移量
  420. //screenPos.x -= 0;
  421. //screenPos.y += 0;
  422. //从摄像机发射通过准心的射线
  423. const ray = new geometry.Ray();
  424. Game.I.camera.screenPointToRay(screenPos.x, screenPos.y, ray);
  425. //获取枪口位置
  426. const muzzlePos = Game.I.player.gun.muzzleNode.worldPosition;
  427. return new Vec3(
  428. muzzlePos.x + ray.d.x * this.bulletDistance,
  429. muzzlePos.y + ray.d.y * this.bulletDistance,
  430. muzzlePos.z + ray.d.z * this.bulletDistance);
  431. }
  432. /**
  433. * 返回屏幕中心的世界坐标
  434. */
  435. public getScreenCenterPos(){
  436. const worldPos = this.crossHair.worldPosition.clone();
  437. const camera2D: Camera = Game.I.canvas.getChildByName('Camera2D').getComponent(Camera);
  438. let screenPos = camera2D.worldToScreen(worldPos);
  439. return screenPos;
  440. }
  441. /**
  442. * 玩家血量低于30%一直闪烁、打一枪的时候闪一下
  443. * @param progress 血量进度
  444. * @returns
  445. */
  446. public playerHurtTwinkle(progress: number){
  447. this.hpProgressBar.progress = progress;
  448. const isLowHP = progress <= 0.3;
  449. const op = this.injury_blood.getComponent(UIOpacity);
  450. //如果当前是高血量且正在闪烁 停止并隐藏
  451. if (!isLowHP && this.isHurtRun) {
  452. this.isHurtRun = false;
  453. Tween.stopAllByTarget(op);
  454. this.injury_blood.active = false;
  455. return;
  456. }
  457. if (isLowHP && !this.isHurtRun) {//低血量且未在闪烁时,启动循环闪烁
  458. this.isHurtRun = true;
  459. this.injury_blood.active = true;
  460. op.opacity = 255;
  461. tween(op)
  462. .to(0.5, { opacity: 0 }, { easing: easing.linear })
  463. .to(0.5, { opacity: 255 }, { easing: easing.linear })
  464. .repeatForever()
  465. .start();
  466. }else if (!isLowHP) {//高血量时触发单次闪烁
  467. this.injury_blood.active = true;
  468. Tween.stopAllByTarget(op);
  469. op.opacity = 255;
  470. tween(op)
  471. .to(0.4, { opacity: 0 }, { easing: easing.linear })
  472. .call(() => {
  473. this.injury_blood.active = false;
  474. })
  475. .start();
  476. }
  477. }
  478. /**
  479. * 点击事件
  480. * @param event 事件
  481. * @param customEventData 数据
  482. */
  483. public onBtnClicked(event: EventTouch, customEventData: any) {
  484. super.onBtnClicked(event,customEventData);
  485. let btnName = event.target.name;
  486. if (btnName === 'pause_btn') { // 暂停页面
  487. uiMgr.show(Constants.popUIs.pauseUI);
  488. }else if (btnName === 'shot_btn') {//射击
  489. let type:number = this.weaponCategoryGun();
  490. switch(type){
  491. case GameEnums.weaponCategory.sniper://狙击枪
  492. Game.I.player.shoot();
  493. break
  494. case GameEnums.weaponCategory.rifle://步枪
  495. this._isScopeOpen = true;
  496. Game.I.player.shoot();
  497. this.scheduleOnce(()=>{
  498. this._isScopeOpen = false;
  499. },1.5)
  500. break
  501. case GameEnums.weaponCategory.dmr://连狙
  502. Game.I.player.shoot();
  503. break
  504. }
  505. }else if (btnName === 'cut_gun_btn') {//切枪
  506. if(userIns.data.guns.length <= 1){
  507. MsgHints.show('Go unlock more guns');
  508. return;
  509. }
  510. if(Game.I.player.isReloadMagazine){
  511. MsgHints.show('Loading ammo...');
  512. return;
  513. }
  514. if(userIns.data.cutNum > 0){
  515. Game.I.player.randomCutGun();
  516. }else{//看视频加切换次数
  517. PlatformSystem.platform.showRewardVideo((f) => {
  518. if(f) {//播放视频成功
  519. userIns.data.cutNum += 5;
  520. this.cut_num_label.string = `x${userIns.data.cutNum}`;
  521. this.cut_video.active = false;
  522. }
  523. });
  524. }
  525. }else if(btnName === 'clean_btn'){//重新加载数据
  526. userIns.removeData();
  527. Game.I.player.pData = userIns.getCurUseGun();
  528. MsgHints.show('重新加载数据完成');
  529. }else if(btnName === 'unlock_btn'){//解锁全部枪数据
  530. userIns.unlockAllGuns();
  531. MsgHints.show('已经解锁全部枪数据');
  532. }
  533. }
  534. /**
  535. * //基准速度值 基准时间3秒
  536. const baseSpeed = 450;
  537. const baseTime = 3;
  538. //时间 = (基准速度 / 当前速度) * 基准时间
  539. let time: number = (baseSpeed / data.reloadingSpeed) * baseTime;
  540. */
  541. /**
  542. * 每帧更新,处理视野渐变和检查 2 秒时间限制
  543. * @param deltaTime - 上一帧到当前帧的时间间隔
  544. */
  545. update(deltaTime: number) {
  546. if(this.isZoomingIn) {
  547. const currentFov = Game.I.camera.fov;
  548. if (currentFov > this.targetFov) {
  549. const newFov = currentFov - this.zoomSpeed * deltaTime;
  550. Game.I.camera.fov = Math.max(newFov, this.targetFov);
  551. }else {//检查是否在2秒内完成放大
  552. const elapsedTime = (sys.now() - this.zoomStartTime) / 1000;
  553. if (elapsedTime <= 1.5) {
  554. this.zoomValid = true;
  555. }
  556. this.isZoomingIn = false;
  557. }
  558. }else if(this.isZoomingOut) {
  559. const currentFov = Game.I.camera.fov;
  560. if (currentFov < this.originalFov) {
  561. const newFov = currentFov + this.zoomSpeed * deltaTime;
  562. Game.I.camera.fov = Math.min(newFov, this.originalFov);
  563. } else {
  564. this.isZoomingOut = false;
  565. this.zoomValid = false;
  566. }
  567. }
  568. }
  569. }