ResUtil.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. import { _decorator, AnimationState, Asset, Constructor, director, Node, ParticleSystem, Prefab, SceneAsset, SkeletalAnimation, Sprite, SpriteFrame, tween, UIOpacity, v2, v3, Vec3 } from 'cc';
  2. import { Utils } from './Utils';
  3. import { Constants } from '../data/Constants';
  4. import { bundleMgr } from '../core/manager/BundleManager';
  5. import { Logger } from '../extend/Logger';
  6. import { PoolManager } from '../core/manager/PoolManager';
  7. import { uiMgr } from '../core/manager/UIManager';
  8. const { ccclass, property } = _decorator;
  9. /**
  10. * 游戏道具类型枚举
  11. * @description 道具类型标识
  12. */
  13. export enum ITEM_TYPE {
  14. Diamond = "diamond",//钻石
  15. Coin = "coin",//金币
  16. Boomerang = "boomerang",//飞镖
  17. }
  18. /**
  19. * 游戏特效类型枚举
  20. * @description 所有特效资源分类标识
  21. */
  22. export enum EffectType {
  23. Bleeding = "effects/Prefabs/blood", //流血效果(子弹命中敌人)
  24. BarrelExplosion = "effects/Prefabs/OilBoom", //油桶爆炸(基础爆炸)
  25. RockDebris = "effects/Prefabs/StoneBoom",// 沙堆/石礅碎裂烟雾
  26. VehicleSmoke = "effects/Prefabs/HeavySmoke", // 坦克/汽车受损烟雾(浓烟)
  27. VehicleExplosion = "effects/Prefabs/TankBoom" // 坦克/汽车爆炸(强烈爆炸)
  28. }
  29. @ccclass('ResUtil')
  30. export class ResUtil {
  31. /**
  32. * 设置精灵贴图 只能prefabs包 texture目录下
  33. * @param path 资源路径
  34. * 只能是在prefabs这个bundle目录下、然后如果使用
  35. * @param target 精灵或者节点
  36. * @param cb 回调函数
  37. */
  38. public static setSpriteFrame(name: string, target: Sprite | Node) {
  39. if(Utils.isNull(name))return;
  40. const path:string = `texture/${name}/spriteFrame`;
  41. bundleMgr.loadAsset(Constants.bundleName.prefabs,path,SpriteFrame, (err: Error, clip: SpriteFrame) => {
  42. if(!err) {
  43. let sprite: Sprite = target instanceof Node ? target.getComponent(Sprite) : target;
  44. if (!sprite) {
  45. console.error('Target node does not have a Sprite component!');
  46. }else{
  47. sprite.spriteFrame = clip;
  48. }
  49. }
  50. });
  51. }
  52. /**
  53. * 设置精灵贴图 指定自定义的包
  54. * @param bundle 包名如果传空 默认prefabs包名
  55. * @param path 路径 如果在prefabs包下,就可以直接传递传图片名称或者path路径(hit_m or texture/hit_m/spriteFrame)
  56. * 如果不在prefabs包下就只能传递完成的路径(texture/hit_m/spriteFrame)
  57. * @param type 类型
  58. * @param target 图片或者节点
  59. * @param onComplete 设置完成的回调
  60. */
  61. public static setSpriteFrameAsset(
  62. bundle: string,
  63. path: string,
  64. type: Constructor<Asset>,
  65. target: Sprite | Node,
  66. onComplete?: (err: Error | null, asset?: any) => void){
  67. if(Utils.isNull(bundle)){
  68. bundle = Constants.bundleName.prefabs;
  69. if(!path.startsWith("texture/")){
  70. path = `texture/${path}/spriteFrame`;
  71. }
  72. }
  73. if(Utils.isNull(path))return;
  74. bundleMgr.loadAsset(bundle,path,type, (err: Error, clip: SpriteFrame) => {
  75. if(!err) {
  76. let sprite: Sprite = target instanceof Node ? target.getComponent(Sprite) : target;
  77. if (!sprite) {
  78. console.error('Target node does not have a Sprite component!');
  79. }else{
  80. sprite.spriteFrame = clip;
  81. }
  82. }
  83. onComplete?.(err,clip);
  84. });
  85. }
  86. /**
  87. * 预加载场景资源(支持进度回调)
  88. * @param {string} sceneName - 场景名称(如 "main" 或者"level1")
  89. * @param {Function} [onComplete] - 完成回调
  90. * @param {Function} [onProgress] - 加载进度回调
  91. * @param {Error | null} onProgress.err - 错误信息(成功时为 null)
  92. * @param {number} onProgress.progress - 加载进度(0~1)
  93. */
  94. public static preloadScene(
  95. sceneName: string,
  96. onComplete?: () => void,
  97. onProgress?: (err: Error | null, progress: number) => void
  98. ): void {
  99. if (Utils.isNull(sceneName)) {
  100. Logger.warn("[SceneLoader] Scene name cannot be null or empty!");
  101. return;
  102. }//开始预加载场景
  103. director.preloadScene(
  104. sceneName,
  105. (loadedCount: number, totalCount: number) => {//进度更新回调
  106. const progress = totalCount > 0 ? loadedCount / totalCount : 0;
  107. onProgress?.(null, progress); // 通知进度(未完成)
  108. },//加载完成回调
  109. (error: Error | null, sceneAsset?: SceneAsset) => {
  110. if (error) {
  111. Logger.error(`[SceneLoader] Failed to preload scene "${sceneName}":`, error);
  112. onProgress?.(error,0); // 通知失败
  113. return;
  114. }
  115. onComplete?.();
  116. //预加载成功
  117. Logger.log(`[SceneLoader] Scene "${sceneName}" preloaded successfully`);
  118. onProgress?.(null, 1);
  119. }
  120. );
  121. }
  122. /**
  123. * 只加载prefabs资源ui和页面 预制体
  124. * @param name 名字 只能是预知体
  125. * @param parent 父节点
  126. * @returns 返回一个节点
  127. */
  128. public static loadRes(path: string, parent?: Node): Promise<Node | null> {
  129. return new Promise(async (resolve, reject) => {
  130. try {
  131. if (Utils.isNull(path)) {
  132. resolve(null);
  133. return;
  134. }
  135. //加载资源 从对象池获取实例
  136. const res = await bundleMgr.loadAsset(Constants.bundleName.prefabs, path, Prefab) as Prefab;
  137. const n = PoolManager.getNode(res,parent) as Node;
  138. //返回结果
  139. resolve(n);
  140. } catch (error) {
  141. console.error(`加载资源失败: ${path}`, error);
  142. reject(new Error(`加载资源失败:: ${path}`));
  143. }
  144. });
  145. }
  146. /**
  147. * 购买金币和钻石的动画
  148. * @param type
  149. * @param starNode
  150. * @param targetNode
  151. * @param count
  152. * @param radius
  153. * @param callback
  154. */
  155. public static async flyAnim(type: ITEM_TYPE, starNode: Node, targetNode: Node, count: number, radius: number, callback: Function) {
  156. let getPoint = (r, ox, oy, count) => {
  157. var point = []; //结果
  158. var radians = (Math.PI / 180) * Math.round(360 / count), //弧度
  159. i = 0;
  160. for (; i < count; i++) {
  161. var x = ox + r * Math.sin(radians * i),
  162. y = oy + r * Math.cos(radians * i);
  163. point.unshift(v2(x, y)); //为保持数据顺时针
  164. }
  165. return point;
  166. }
  167. let start = starNode.worldPosition;
  168. var array = getPoint(radius, start.x, start.y, count);
  169. var nodeArray = new Array();
  170. for (var i = 0; i < array.length; i++) {
  171. let ret: any = await bundleMgr.loadAsset(Constants.bundleName.prefabs, `items/${type}`, Prefab);
  172. var gold = PoolManager.getNode(ret);
  173. gold.setWorldScale(v3(0.7, 0.7, 0.7));
  174. gold.parent = uiMgr.getCurentSceneRoot();
  175. var randPos = v3(array[i].x + Utils.getRandomInt(0, 50), array[i].y + Utils.getRandomInt(0, 50), 1);
  176. gold.setWorldPosition(start);
  177. nodeArray.push({ gold, randPos });
  178. }
  179. var notPlay = false;
  180. let dstPos = targetNode.worldPosition.clone();
  181. for (let i = 0; i < nodeArray.length; i++) {
  182. let pos = nodeArray[i].randPos;
  183. let node = nodeArray[i].gold;
  184. nodeArray[i].gold.id = i;
  185. tween(node)
  186. .to(0.2, { worldPosition: pos })
  187. .delay(i * 0.03)
  188. .to(0.7, { worldPosition: v3(dstPos.x, dstPos.y, 1) })
  189. .call(() => {
  190. if(!notPlay) {
  191. notPlay = true;
  192. tween(targetNode)
  193. .to(0.1, { scale: v3(2, 2, 2) })
  194. .to(0.1, { scale: v3(1, 1, 1) })
  195. .call(() => {
  196. notPlay = false;
  197. }).start()
  198. }
  199. let idx: number = node["_id"];
  200. callback(idx == nodeArray.length - 1);
  201. PoolManager.putNode(node);
  202. })
  203. .start()
  204. }
  205. }
  206. /**
  207. * 加载特效
  208. * @param type 路径
  209. * @param worldPos 位置
  210. * @param parent 父节点
  211. * @param callback 回调函数
  212. */
  213. public static async loadEffect(type: EffectType,worldPos:Vec3,removeTime: number = 0, parent?: Node, callback?: Function){
  214. let res: Node = await ResUtil.loadRes(type,parent);
  215. if(worldPos){
  216. res.worldPosition = worldPos;
  217. }
  218. if(parent){
  219. res.getComponentsInChildren(ParticleSystem)
  220. .forEach(e => {
  221. e.play();
  222. });
  223. }
  224. if(removeTime > 0){
  225. res.getComponent(UIOpacity).scheduleOnce(function(){
  226. this.getComponentsInChildren(ParticleSystem)
  227. .forEach(e => {
  228. e.stop();
  229. });
  230. PoolManager.putNode(this);
  231. }.bind(res),removeTime,);
  232. }
  233. }
  234. /**
  235. * 播放骨骼动画
  236. * @param skeletal 骨骼动画
  237. * @param name 动画名称
  238. * @param time 播放时间 播放的时长会控制播放的速度快慢
  239. * @param cb 播放完成回调
  240. */
  241. public static playSkeletalAnim(skeletal: SkeletalAnimation,name: string,time: number = 0, cb: Function = null){
  242. if(!skeletal)return;
  243. //查找目标动画剪辑
  244. const clip = skeletal.clips.find(c => c.name == name);
  245. if (!clip) {
  246. Logger.error(`[ResUtil.playSkeletalAnim] 未找到名为 "${name}" 的动画剪辑`);
  247. return;
  248. }
  249. //核心逻辑:通过调整播放速度,使动画总时长等于传入的time
  250. //公式推导:原始时长 = 播放速度 × 实际播放时间 → 播放速度 = 原始时长 / 目标时长
  251. let animState:AnimationState = skeletal.getState(name);
  252. if(time > 0) {
  253. animState.speed = clip.duration / time;
  254. }else{//恢复默认速度(避免之前的修改影响后续播放)
  255. animState.speed = 1;
  256. }
  257. skeletal.play(name);
  258. if(cb){
  259. skeletal.scheduleOnce(cb,time > 0 ? time : clip.duration);
  260. }
  261. }
  262. /**
  263. *播放粒子
  264. * @param path 粒子路径
  265. * @param time 粒子持续时间(自动回收时间,单位秒)
  266. * @param onSetup 加载完成后外部设置粒子属性的回调(接收粒子节点参数)
  267. * @param cb 播放完成回调
  268. */
  269. public static playParticle(
  270. path: string,
  271. time: number = 0,
  272. worldScale: Vec3 = v3(1,1,1),
  273. onSetup?: (particle: Node) => void,
  274. cb?: Function
  275. ): void {
  276. if(Utils.isNull(path))return;
  277. ResUtil.loadRes(path) // 使用传入的path参数加载资源
  278. .then((particle: Node | null) => {
  279. if(!particle)return;
  280. //触发外部设置回调(由外部设置parent、position等属性)
  281. onSetup?.(particle);
  282. //播放粒子(外部设置完成后执行)
  283. particle.getComponentsInChildren(ParticleSystem)
  284. .forEach(e => {
  285. e.node.setWorldScale(worldScale);
  286. //1、World - 世界空间 缩放会受父节点影响 在世界坐标系中计算
  287. //2、Local - 本地空间 缩放不受父节点影响 在粒子本地坐标系中计算
  288. e.scaleSpace = 1;
  289. e.stop();
  290. e.play();
  291. });
  292. //定时回收 外部设置完成后启动计时
  293. if(time > 0){
  294. setTimeout(() => {
  295. if(particle.parent){
  296. PoolManager.putNode(particle);
  297. }
  298. }, time * 1000);
  299. }
  300. cb?.(); // 执行最终回调
  301. });
  302. }
  303. }