@nimbus2d/core 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. package/dist/index.mjs +18433 -0
  2. package/package.json +39 -0
  3. package/src/base/EventEmitter.js +187 -0
  4. package/src/base/Subsystem.js +183 -0
  5. package/src/base/index.js +6 -0
  6. package/src/core/Application/AppState.js +27 -0
  7. package/src/core/Application/Application.js +480 -0
  8. package/src/core/Application/DefaultConfig.js +63 -0
  9. package/src/core/Application/index.js +7 -0
  10. package/src/core/AssetManager/AssetManager.js +711 -0
  11. package/src/core/AssetManager/AssetTypes.js +108 -0
  12. package/src/core/AssetManager/adapters/IResourceAdapter.js +50 -0
  13. package/src/core/AssetManager/adapters/PixiAdapter.js +90 -0
  14. package/src/core/AssetManager/core/RefCounter.js +167 -0
  15. package/src/core/AssetManager/core/ResourceCache.js +281 -0
  16. package/src/core/AssetManager/core/ResourceEntry.js +191 -0
  17. package/src/core/AssetManager/core/ResourceEventBus.js +127 -0
  18. package/src/core/AssetManager/handlers/IResourceHandler.js +45 -0
  19. package/src/core/AssetManager/index.js +26 -0
  20. package/src/core/AssetManager/plugins/IResourcePlugin.js +56 -0
  21. package/src/core/Camera/Camera.js +534 -0
  22. package/src/core/Camera/CameraTypes.js +59 -0
  23. package/src/core/Camera/index.js +13 -0
  24. package/src/core/ConfigSystem/ConfigLayer.js +230 -0
  25. package/src/core/ConfigSystem/ConfigLayerType.js +31 -0
  26. package/src/core/ConfigSystem/ConfigMerger.js +50 -0
  27. package/src/core/ConfigSystem/ConfigPath.js +96 -0
  28. package/src/core/ConfigSystem/ConfigSystem.js +378 -0
  29. package/src/core/ConfigSystem/ConfigTypes.js +29 -0
  30. package/src/core/ConfigSystem/ConfigWatcher.js +84 -0
  31. package/src/core/ConfigSystem/index.js +19 -0
  32. package/src/core/EventBus/EventBus.js +797 -0
  33. package/src/core/EventBus/Interceptor.js +26 -0
  34. package/src/core/EventBus/PerformanceStats.js +105 -0
  35. package/src/core/EventBus/index.js +7 -0
  36. package/src/core/Lifecycle/Lifecycle.js +265 -0
  37. package/src/core/Lifecycle/LifecycleState.js +31 -0
  38. package/src/core/Lifecycle/index.js +6 -0
  39. package/src/core/Logger/ConsoleHandler.js +55 -0
  40. package/src/core/Logger/LogHandler.js +34 -0
  41. package/src/core/Logger/LogLevel.js +38 -0
  42. package/src/core/Logger/Logger.js +234 -0
  43. package/src/core/Logger/index.js +8 -0
  44. package/src/core/PluginSystem/Plugin.js +156 -0
  45. package/src/core/PluginSystem/PluginEntry.js +102 -0
  46. package/src/core/PluginSystem/PluginState.js +30 -0
  47. package/src/core/PluginSystem/PluginSystem.js +530 -0
  48. package/src/core/PluginSystem/index.js +8 -0
  49. package/src/core/PoolManager/DefaultConfig.js +19 -0
  50. package/src/core/PoolManager/ObjectPool.js +326 -0
  51. package/src/core/PoolManager/PoolManager.js +253 -0
  52. package/src/core/PoolManager/PoolTypes.js +92 -0
  53. package/src/core/PoolManager/index.js +15 -0
  54. package/src/core/SceneManager/FadeTransition.js +64 -0
  55. package/src/core/SceneManager/MaskTransition.js +234 -0
  56. package/src/core/SceneManager/Scene.js +221 -0
  57. package/src/core/SceneManager/SceneManager.js +658 -0
  58. package/src/core/SceneManager/SceneState.js +29 -0
  59. package/src/core/SceneManager/SlideTransition.js +113 -0
  60. package/src/core/SceneManager/Transition.js +102 -0
  61. package/src/core/SceneManager/ZoomTransition.js +119 -0
  62. package/src/core/SceneManager/index.js +12 -0
  63. package/src/core/SpriteGPULayer/SpriteGPULayer.js +321 -0
  64. package/src/core/SpriteGPULayer/SpriteGPULayerTypes.js +49 -0
  65. package/src/core/SpriteGPULayer/index.js +11 -0
  66. package/src/core/Timeline/Clip.js +101 -0
  67. package/src/core/Timeline/DefaultConfig.js +41 -0
  68. package/src/core/Timeline/Easing.js +176 -0
  69. package/src/core/Timeline/Timeline.js +458 -0
  70. package/src/core/Timeline/TimelineTypes.js +89 -0
  71. package/src/core/Timeline/Track.js +202 -0
  72. package/src/core/Timeline/index.js +22 -0
  73. package/src/errors/ConfigError.js +47 -0
  74. package/src/errors/LifecycleError.js +58 -0
  75. package/src/errors/NimbusError.js +43 -0
  76. package/src/errors/PluginError.js +138 -0
  77. package/src/errors/ResourceError.js +166 -0
  78. package/src/errors/SceneError.js +124 -0
  79. package/src/errors/SubsystemError.js +68 -0
  80. package/src/errors/index.js +53 -0
  81. package/src/index.js +86 -0
  82. package/src/types/index.js +141 -0
  83. package/src/utils/MemoryChecker.js +374 -0
  84. package/src/utils/Validator.js +60 -0
  85. package/src/utils/index.js +84 -0
@@ -0,0 +1,49 @@
1
+ // ============================================================================
2
+ // Nimbus2D - SpriteGPULayer 类型定义
3
+ // ----------------------------------------------------------------------------
4
+ // 职责:
5
+ // 1. 定义 SpriteGPULayer 相关的事件类型枚举
6
+ // 2. 定义 SpriteGPULayer 错误类
7
+ //
8
+ // 四期新增(任务 1.1):GPU 批量精灵容器的类型定义
9
+ // ============================================================================
10
+
11
+ import { NimbusError } from '../../errors/NimbusError.js'
12
+
13
+ // ----------------------------------------------------------------------------
14
+ // 事件类型枚举
15
+ // ----------------------------------------------------------------------------
16
+
17
+ /**
18
+ * SpriteGPULayer 事件类型
19
+ * @enum {string}
20
+ */
21
+ const SpriteGPULayerEventType = {
22
+ /** 纹理组 Sprite 数量达到 maxBatchSize 上限时发射 */
23
+ BATCH_FULL: 'gpu:spriteBatch:full',
24
+ /** SpriteGPULayer 渲染完成后发射 */
25
+ RENDERED: 'gpu:spriteBatch:rendered'
26
+ }
27
+
28
+ // ----------------------------------------------------------------------------
29
+ // 错误类
30
+ // ----------------------------------------------------------------------------
31
+
32
+ /**
33
+ * SpriteGPULayer 错误基类
34
+ * 所有 SpriteGPULayer 错误构造函数设置 module='SpriteGPULayer'
35
+ * @extends NimbusError
36
+ */
37
+ class SpriteGPULayerError extends NimbusError {
38
+ /**
39
+ * 构造函数
40
+ * @param {string} message - 错误消息
41
+ * @param {string} [code='SPRITE_GPU_LAYER_ERROR'] - 错误码
42
+ * @param {Error|null} [cause=null] - 原始错误
43
+ */
44
+ constructor(message, code = 'SPRITE_GPU_LAYER_ERROR', cause = null) {
45
+ super(message, code, 'SpriteGPULayer', cause)
46
+ }
47
+ }
48
+
49
+ export { SpriteGPULayerEventType, SpriteGPULayerError }
@@ -0,0 +1,11 @@
1
+ // ============================================================================
2
+ // Nimbus2D - SpriteGPULayer 模块入口
3
+ // ----------------------------------------------------------------------------
4
+ // 导出 GPU 批量精灵容器的所有公开 API
5
+ // ============================================================================
6
+
7
+ // GPU 批量精灵容器主类
8
+ export { SpriteGPULayer } from './SpriteGPULayer.js'
9
+
10
+ // 类型定义(事件枚举 + 错误类)
11
+ export { SpriteGPULayerEventType, SpriteGPULayerError } from './SpriteGPULayerTypes.js'
@@ -0,0 +1,101 @@
1
+ // ============================================================================
2
+ // Nimbus2D - Clip 动画片段
3
+ // ----------------------------------------------------------------------------
4
+ // 职责:
5
+ // 1. 封装单个属性动画片段(startTime/duration/from/to/easing)
6
+ // 2. 提供 evaluate(progress) 方法计算指定进度处的值
7
+ // 3. 仅支持数值类型插值(from/to 必须为 number)
8
+ // 4. 提供 endTime getter 返回片段结束时间
9
+ //
10
+ // 设计决策:
11
+ // - 不继承任何类:Clip 是纯数据 + 行为的简单对象
12
+ // - 公开属性(startTime/duration/from/to/easing):便于 Track 直接访问
13
+ // - evaluate 方法内部使用 Easing.get 获取缓动后的进度
14
+ // - 输入校验:duration 必须 > 0,from/to 必须为 number
15
+ // - 防御性编程:progress 范围约束 [0,1],避免越界值
16
+ // - 构造函数参数校验:抛出 TimelineError,便于调用方定位问题
17
+ //
18
+ // 四期新增(任务 1.6/4.2):Timeline 动画引擎的动画片段
19
+ // ============================================================================
20
+
21
+ import { Easing } from './Easing.js'
22
+ import { TimelineError } from './TimelineTypes.js'
23
+
24
+ class Clip {
25
+ /**
26
+ * 创建动画片段
27
+ * @param {Object} options - 片段配置
28
+ * @param {number} options.startTime - 起始时间(秒,>=0)
29
+ * @param {number} options.duration - 持续时间(秒,>0)
30
+ * @param {number} options.from - 起始值(必须为 number)
31
+ * @param {number} options.to - 结束值(必须为 number)
32
+ * @param {string} [options.easing='linear'] - 缓动函数名(参考 Easing.list())
33
+ * @throws {TimelineError} 参数校验失败
34
+ */
35
+ constructor(options) {
36
+ // 参数对象校验
37
+ if (!options || typeof options !== 'object') {
38
+ throw new TimelineError('Clip 配置必须为对象', 'CLIP_INVALID_OPTIONS')
39
+ }
40
+
41
+ // 校验 startTime(必须为非负数)
42
+ if (typeof options.startTime !== 'number' || options.startTime < 0 || Number.isNaN(options.startTime)) {
43
+ throw new TimelineError('Clip.startTime 必须 >= 0', 'CLIP_INVALID_START_TIME')
44
+ }
45
+
46
+ // 校验 duration(必须为正数)
47
+ if (typeof options.duration !== 'number' || options.duration <= 0 || Number.isNaN(options.duration)) {
48
+ throw new TimelineError('Clip.duration 必须 > 0', 'CLIP_INVALID_DURATION')
49
+ }
50
+
51
+ // 校验 from/to(必须为 number,允许 NaN 检查)
52
+ if (typeof options.from !== 'number' || Number.isNaN(options.from) ||
53
+ typeof options.to !== 'number' || Number.isNaN(options.to)) {
54
+ throw new TimelineError('Clip.from/to 必须为有效 number', 'CLIP_INVALID_FROM_TO')
55
+ }
56
+
57
+ /** @type {number} 起始时间(秒) */
58
+ this.startTime = options.startTime
59
+
60
+ /** @type {number} 持续时间(秒) */
61
+ this.duration = options.duration
62
+
63
+ /** @type {number} 起始值 */
64
+ this.from = options.from
65
+
66
+ /** @type {number} 结束值 */
67
+ this.to = options.to
68
+
69
+ /** @type {string} 缓动函数名(未知名称会在 evaluate 时回退到 linear) */
70
+ this.easing = typeof options.easing === 'string' ? options.easing : 'linear'
71
+ }
72
+
73
+ /**
74
+ * 获取片段结束时间(startTime + duration)
75
+ * @returns {number}
76
+ */
77
+ get endTime() {
78
+ return this.startTime + this.duration
79
+ }
80
+
81
+ /**
82
+ * 评估片段在指定进度处的值
83
+ * @param {number} progress - 进度 [0,1](越界值会被自动约束)
84
+ * @returns {number} 插值结果(from + (to - from) * easing(progress))
85
+ */
86
+ evaluate(progress) {
87
+ // 校验 progress 是否为有效数字(防御 NaN/非数字输入导致插值结果为 NaN)
88
+ // NaN 时回退到 0(等同于片段起始值 from)
89
+ if (typeof progress !== 'number' || Number.isNaN(progress)) {
90
+ progress = 0
91
+ }
92
+ // 进度范围约束 [0,1]
93
+ const clampedProgress = progress < 0 ? 0 : progress > 1 ? 1 : progress
94
+ // 应用缓动函数(未知名称回退到 linear)
95
+ const easedProgress = Easing.get(this.easing, clampedProgress)
96
+ // 线性插值
97
+ return this.from + (this.to - this.from) * easedProgress
98
+ }
99
+ }
100
+
101
+ export { Clip }
@@ -0,0 +1,41 @@
1
+ // ============================================================================
2
+ // Nimbus2D - Timeline 默认配置
3
+ // ----------------------------------------------------------------------------
4
+ // 职责:
5
+ // 1. 提供 Timeline 模块的默认配置常量
6
+ // 2. 集中管理可调参数,避免硬编码
7
+ //
8
+ // 设计决策:
9
+ // - 使用 Object.freeze 冻结,防止运行时篡改
10
+ // - 所有默认值集中管理,便于后续扩展和维护
11
+ // - 与 Camera/SpriteGPULayer 等模块的 DefaultConfig 风格保持一致
12
+ //
13
+ // 四期新增(任务 1.6/4.5):Timeline 模块默认配置
14
+ // ============================================================================
15
+
16
+ /**
17
+ * Timeline 默认配置
18
+ * @type {Object}
19
+ * @readonly
20
+ */
21
+ const TimelineDefaultConfig = Object.freeze({
22
+ /** 默认是否循环 */
23
+ loop: false,
24
+
25
+ /** 默认循环次数(Infinity 表示无限) */
26
+ loopCount: Infinity,
27
+
28
+ /** 默认是否反向播放 */
29
+ autoReverse: false,
30
+
31
+ /** 默认完成后自动销毁 */
32
+ autoDestroy: false,
33
+
34
+ /** 默认缓动函数名(参考 Easing.list()) */
35
+ defaultEasing: 'linear',
36
+
37
+ /** 默认片段持续时间(秒) */
38
+ defaultClipDuration: 1
39
+ })
40
+
41
+ export { TimelineDefaultConfig }
@@ -0,0 +1,176 @@
1
+ // ============================================================================
2
+ // Nimbus2D - Easing 缓动函数库
3
+ // ----------------------------------------------------------------------------
4
+ // 职责:
5
+ // 1. 提供 10 个内置缓动函数(linear/easeIn/easeOut/easeInOut 等)
6
+ // 2. 提供 get(name, t) 查询方法(未知名称回退到 linear)
7
+ // 3. 提供 register(name, fn) 注册自定义缓动函数
8
+ // 4. 提供 unregister(name) 注销自定义缓动函数(仅允许注销自定义,不可注销内置)
9
+ // 5. 提供 has(name) 查询 + list() 列出所有缓动函数名
10
+ //
11
+ // 设计决策:
12
+ // - 内置缓动函数表使用 Object.freeze 冻结,防止运行时篡改
13
+ // - 自定义缓动函数存储在独立的 _customEasings 表,可动态增删
14
+ // - get() 方法对输入 t 做 [0,1] 范围约束,避免越界值导致缓动函数异常
15
+ // - register() 方法对 name 和 fn 类型校验,避免无效函数污染查询表
16
+ // - unregister() 方法禁止注销内置缓动函数,保证基础能力稳定
17
+ // - easeOutBounce 实现中避免修改入参 t(使用局部变量替代)
18
+ //
19
+ // 四期新增(任务 1.6/4.1):Timeline 动画引擎的缓动函数库
20
+ // ============================================================================
21
+
22
+ /**
23
+ * 内置缓动函数表(冻结,不可变)
24
+ * 每个函数签名:(t: number) => number,输入 t ∈ [0,1],输出缓动后的值
25
+ * @type {Object<string, Function>}
26
+ */
27
+ const BUILTIN_EASINGS = Object.freeze({
28
+ /** 线性(匀速) */
29
+ linear: (t) => t,
30
+
31
+ /** 二次缓入(先慢后快) */
32
+ easeIn: (t) => t * t,
33
+
34
+ /** 二次缓出(先快后慢) */
35
+ easeOut: (t) => t * (2 - t),
36
+
37
+ /** 二次缓入缓出(先慢后快再慢) */
38
+ easeInOut: (t) => (t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t),
39
+
40
+ /** 三次缓入(更陡的先慢后快) */
41
+ easeInCubic: (t) => t * t * t,
42
+
43
+ /** 三次缓出(更陡的先快后慢) */
44
+ easeOutCubic: (t) => 1 - Math.pow(1 - t, 3),
45
+
46
+ /** 三次缓入缓出(更陡的 S 形曲线) */
47
+ easeInOutCubic: (t) => (t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2),
48
+
49
+ /** 弹性缓入(震荡进入) */
50
+ easeInElastic: (t) => {
51
+ // 边界值直接返回,避免 sin 计算精度问题
52
+ if (t === 0 || t === 1) return t
53
+ return -Math.pow(2, 10 * (t - 1)) * Math.sin((t - 1.1) * 5 * Math.PI)
54
+ },
55
+
56
+ /** 弹性缓出(震荡结束) */
57
+ easeOutElastic: (t) => {
58
+ // 边界值直接返回,避免 sin 计算精度问题
59
+ if (t === 0 || t === 1) return t
60
+ return Math.pow(2, -10 * t) * Math.sin((t - 0.1) * 5 * Math.PI) + 1
61
+ },
62
+
63
+ /** 弹跳缓出(模拟球落地弹跳) */
64
+ easeOutBounce: (t) => {
65
+ // 使用局部变量避免修改入参 t
66
+ if (t < 1 / 2.75) {
67
+ return 7.5625 * t * t
68
+ }
69
+ if (t < 2 / 2.75) {
70
+ const localT = t - 1.5 / 2.75
71
+ return 7.5625 * localT * localT + 0.75
72
+ }
73
+ if (t < 2.5 / 2.75) {
74
+ const localT = t - 2.25 / 2.75
75
+ return 7.5625 * localT * localT + 0.9375
76
+ }
77
+ const localT = t - 2.625 / 2.75
78
+ return 7.5625 * localT * localT + 0.984375
79
+ }
80
+ })
81
+
82
+ /**
83
+ * 自定义缓动函数表(可变,用户通过 register/unregister 管理)
84
+ * @type {Object<string, Function>}
85
+ */
86
+ const _customEasings = {}
87
+
88
+ /**
89
+ * Easing 缓动函数库
90
+ * 提供内置缓动函数查询 + 自定义缓动函数注册
91
+ */
92
+ const Easing = {
93
+ /**
94
+ * 获取缓动后的值
95
+ * @param {string} name - 缓动函数名
96
+ * @param {number} t - 进度值 [0,1]
97
+ * @returns {number} 缓动后的值(未知名称回退到 linear,无效 t 回退到 0)
98
+ */
99
+ get(name, t) {
100
+ // 校验 t 是否为有效数字(防御 NaN/非数字输入导致缓动函数返回 NaN)
101
+ if (typeof t !== 'number' || Number.isNaN(t)) {
102
+ t = 0
103
+ }
104
+ // 输入 t 范围约束 [0,1],避免越界值导致缓动函数异常
105
+ const clampedT = t < 0 ? 0 : t > 1 ? 1 : t
106
+ // 优先查找自定义表,其次内置表,最后回退到 linear
107
+ const fn = _customEasings[name] || BUILTIN_EASINGS[name] || BUILTIN_EASINGS.linear
108
+ return fn(clampedT)
109
+ },
110
+
111
+ /**
112
+ * 注册自定义缓动函数
113
+ * @param {string} name - 缓动函数名(不可与内置重名)
114
+ * @param {Function} fn - 缓动函数 (t: number) => number
115
+ * @returns {boolean} 是否注册成功(与内置重名或参数无效时返回 false)
116
+ */
117
+ register(name, fn) {
118
+ // 校验 name 类型
119
+ if (typeof name !== 'string' || name.length === 0) {
120
+ return false
121
+ }
122
+ // 校验 fn 类型
123
+ if (typeof fn !== 'function') {
124
+ return false
125
+ }
126
+ // 禁止覆盖内置缓动函数
127
+ if (Object.prototype.hasOwnProperty.call(BUILTIN_EASINGS, name)) {
128
+ return false
129
+ }
130
+ _customEasings[name] = fn
131
+ return true
132
+ },
133
+
134
+ /**
135
+ * 注销自定义缓动函数
136
+ * @param {string} name - 缓动函数名
137
+ * @returns {boolean} 是否注销成功(不存在或为内置时返回 false)
138
+ */
139
+ unregister(name) {
140
+ if (!Object.prototype.hasOwnProperty.call(_customEasings, name)) {
141
+ return false
142
+ }
143
+ delete _customEasings[name]
144
+ return true
145
+ },
146
+
147
+ /**
148
+ * 判断缓动函数是否已注册(含内置)
149
+ * @param {string} name - 缓动函数名
150
+ * @returns {boolean}
151
+ */
152
+ has(name) {
153
+ return Object.prototype.hasOwnProperty.call(BUILTIN_EASINGS, name) ||
154
+ Object.prototype.hasOwnProperty.call(_customEasings, name)
155
+ },
156
+
157
+ /**
158
+ * 列出所有缓动函数名(含内置和自定义)
159
+ * @returns {string[]}
160
+ */
161
+ list() {
162
+ return [...Object.keys(BUILTIN_EASINGS), ...Object.keys(_customEasings)]
163
+ },
164
+
165
+ /**
166
+ * 清空所有自定义缓动函数(不影响内置)
167
+ * 主要用于测试隔离
168
+ */
169
+ clearCustom() {
170
+ for (const key of Object.keys(_customEasings)) {
171
+ delete _customEasings[key]
172
+ }
173
+ }
174
+ }
175
+
176
+ export { Easing, BUILTIN_EASINGS }