light-h5-core-lib 2.10.11 → 2.11.0

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.
package/LightCore.js CHANGED
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.MapUtil = exports.CollisionDetectionUtil = exports.SelfDecrypt = exports.GetDecorateUtils = exports.NumberUtil = exports.BooleanUtil = exports.Crc32Util = exports.PromiseUtil = exports.BeizerUtil = exports.UniqueIDUtil = exports.StringUtil = exports.RandomNumUtil = exports.FloatDigitUtil = exports.DateUtil = exports.BitUtil = exports.MD5 = exports.BaseSingleton = exports.SetterGetterBase = exports.ClassNameUtil = exports.PoolObj = exports.PoolObjPutStatus = void 0;
6
+ exports.MapUtil = exports.CollisionDetectionUtil = exports.SelfDecrypt = exports.GetDecorateUtils = exports.NumberUtil = exports.BooleanUtil = exports.Crc32Util = exports.PromiseUtil = exports.BeizerUtil = exports.UniqueIDUtil = exports.StringUtil = exports.RandomNumUtil = exports.FloatDigitUtil = exports.DateUtil = exports.BitUtil = exports.MD5 = exports.BaseSingleton = exports.SetterGetterBase = exports.ClassNameUtil = exports.PoolObj = exports.PoolObjPutStatus = exports.BeizerPositionsLerpUtil = void 0;
7
7
  const PoolObj_1 = require("./pool/PoolObj");
8
8
  Object.defineProperty(exports, "PoolObj", { enumerable: true, get: function () { return PoolObj_1.PoolObj; } });
9
9
  const BaseSingleton_1 = require("./common/base/BaseSingleton");
@@ -46,7 +46,10 @@ const MapUtil_1 = require("./common/util/MapUtil");
46
46
  Object.defineProperty(exports, "MapUtil", { enumerable: true, get: function () { return MapUtil_1.MapUtil; } });
47
47
  const PoolObjPutStatus_1 = require("./pool/PoolObjPutStatus");
48
48
  Object.defineProperty(exports, "PoolObjPutStatus", { enumerable: true, get: function () { return PoolObjPutStatus_1.PoolObjPutStatus; } });
49
+ const BeizerPositionsLerpUtil_1 = require("./common/util/BeizerPositionsLerpUtil");
50
+ Object.defineProperty(exports, "BeizerPositionsLerpUtil", { enumerable: true, get: function () { return BeizerPositionsLerpUtil_1.BeizerPositionsLerpUtil; } });
49
51
  module.exports = {
52
+ BeizerPositionsLerpUtil: BeizerPositionsLerpUtil_1.BeizerPositionsLerpUtil,
50
53
  PoolObjPutStatus: PoolObjPutStatus_1.PoolObjPutStatus,
51
54
  PoolObj: PoolObj_1.PoolObj,
52
55
  ClassNameUtil: ClassNameUtil_1.ClassNameUtil,
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BeizerPositionsLerpUtil = void 0;
4
+ /**
5
+ * 贝塞尔曲线坐标点插值算法
6
+ * @author Aonaufly
7
+ */
8
+ class BeizerPositionsLerpUtil {
9
+ /**
10
+ * 根据开始坐标和结束坐标计算整个贝塞尔曲线的坐标点集合
11
+ * @param startPosition 开始坐标
12
+ * @param endPosition 结束坐标
13
+ * @param addCount 再添加的坐标点数(startPosition + addCount + endPosition) default 1
14
+ * @param amplitude 幅度(1: 最大为开始坐标与结束坐标的距离的一半)default 0.4
15
+ * @param distanceMultiple 距离倍数(1=线段内, >1则控制点可向两端外延,如1.5=两端各外延0.25倍线段长度)default 1.0
16
+ * @returns 控制点集合
17
+ */
18
+ static generateControlPoints(startPosition, endPosition, addCount = 1, amplitude = 0.4, distanceMultiple = 1.0) {
19
+ const result = [{ x: startPosition.x, y: startPosition.y }];
20
+ if (addCount <= 0) {
21
+ result.push({ x: endPosition.x, y: endPosition.y });
22
+ return result;
23
+ }
24
+ // 连接线方向向量
25
+ const dx = endPosition.x - startPosition.x;
26
+ const dy = endPosition.y - startPosition.y;
27
+ const len = Math.sqrt(dx * dx + dy * dy);
28
+ if (len < 0.000001) {
29
+ // 起点与终点重合,直接返回
30
+ for (let i = 0; i < addCount; i++) {
31
+ result.push({ x: startPosition.x, y: startPosition.y });
32
+ }
33
+ result.push({ x: endPosition.x, y: endPosition.y });
34
+ return result;
35
+ }
36
+ // 单位方向向量
37
+ const ux = dx / len;
38
+ const uy = dy / len;
39
+ // 垂直方向向量(法向量),旋转90度
40
+ const nx = -uy;
41
+ const ny = ux;
42
+ // 添加点到连接线的最大距离:amplitude * 0.5 * 连接线长度
43
+ const maxDistance = amplitude * 0.5 * len;
44
+ // 投影延伸:distanceMultiple=1时投影范围在[0,1]内,>1时向两端外延
45
+ const extension = (distanceMultiple - 1) * 0.5;
46
+ // 上一个添加点所在的侧(1 或 -1),用于交替偏向
47
+ let prevSide = 0;
48
+ for (let i = 0; i < addCount; i++) {
49
+ // 在连接线上的投影比例 t ∈ (0, 1),均匀分布
50
+ const t = (i + 1) / (addCount + 1);
51
+ // 映射到实际投影比例:t=0时映射到-extension(起点外侧),t=1时映射到1+extension(终点外侧)
52
+ const effectiveT = t * distanceMultiple - extension;
53
+ // 投影点坐标
54
+ const px = startPosition.x + dx * effectiveT;
55
+ const py = startPosition.y + dy * effectiveT;
56
+ // 随机距离 ∈ (0, maxDistance],确保不在连接线上
57
+ const randomFactor = 0.1 + Math.random() * 0.9;
58
+ const distance = randomFactor * maxDistance;
59
+ // 选择连接线的一侧
60
+ // 若有上一个点,偏向另一侧的概率为 75%(基础50%增加50%)
61
+ // 若无上一个点(第一个添加点),各侧50%
62
+ let side;
63
+ if (prevSide === 0) {
64
+ side = Math.random() > 0.5 ? 1 : -1;
65
+ }
66
+ else {
67
+ side = Math.random() < 0.75 ? -prevSide : prevSide;
68
+ }
69
+ // 沿法向量偏移得到最终坐标
70
+ result.push({
71
+ x: px + nx * distance * side,
72
+ y: py + ny * distance * side,
73
+ });
74
+ prevSide = side;
75
+ }
76
+ result.push({ x: endPosition.x, y: endPosition.y });
77
+ return result;
78
+ }
79
+ }
80
+ exports.BeizerPositionsLerpUtil = BeizerPositionsLerpUtil;
@@ -24,6 +24,29 @@ class BeizerUtil {
24
24
  }
25
25
  return { x, y };
26
26
  }
27
+ /**
28
+ * 获得当前时间的切线(运动方向)(慎用: 耗性能)
29
+ * @param timer 时间[0,1]
30
+ * @returns 切线
31
+ */
32
+ static getTangent(points, timer) {
33
+ if (points.length < 2) {
34
+ return { x: 0, y: 0 };
35
+ }
36
+ // 导数控制点: Q_i = P_{i+1} - P_i
37
+ const derivativePoints = [];
38
+ for (let i = 0; i < points.length - 1; i++) {
39
+ derivativePoints.push({
40
+ x: points[i + 1].x - points[i].x,
41
+ y: points[i + 1].y - points[i].y,
42
+ });
43
+ }
44
+ // 在导数控制点上计算 n-2 阶贝塞尔值
45
+ const pos = BeizerUtil.getPosition(derivativePoints, timer);
46
+ // 乘以 (n-1) 得到最终切线
47
+ const n = points.length;
48
+ return { x: pos.x * (n - 1), y: pos.y * (n - 1) };
49
+ }
27
50
  //#endregion
28
51
  //region 获得路线的长度
29
52
  static distance(p1, p2) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "light-h5-core-lib",
3
- "version": "2.10.11",
3
+ "version": "2.11.0",
4
4
  "description": "light core lib",
5
5
  "main": "./LightCore.js",
6
6
  "types": "./type/index.d.ts",
package/type/index.d.ts CHANGED
@@ -309,6 +309,18 @@ export declare class BeizerUtil {
309
309
  x: number;
310
310
  y: number;
311
311
  };
312
+ /**
313
+ * 获得当前时间的切线(运动方向)(慎用: 耗性能)
314
+ * @param timer 时间[0,1]
315
+ * @returns 切线
316
+ */
317
+ static getTangent(points: {
318
+ x: number;
319
+ y: number;
320
+ }[], timer: number): {
321
+ x: number;
322
+ y: number;
323
+ };
312
324
  private static calculationC;
313
325
  private static factorial;
314
326
  private static distance;
@@ -442,7 +454,7 @@ export declare class CollisionDetectionUtil {
442
454
  * @param aBox
443
455
  * @param bBox
444
456
  */
445
- static aABB(aAnchor: Vec2, bAnchor: Vec2, aBox: IBoxInfo, bBox: IBoxInfo): boolean;
457
+ static aABB(aAnchor: CoreVec2, bAnchor: CoreVec2, aBox: IBoxInfo, bBox: IBoxInfo): boolean;
446
458
  /**
447
459
  * OBB检测
448
460
  * @param aCenter A的中心位置
@@ -450,13 +462,13 @@ export declare class CollisionDetectionUtil {
450
462
  * @param aOBB
451
463
  * @param bOBB
452
464
  */
453
- static oBB(aCenter: Vec2, bCenter: Vec2, aOBB: IOBBInfo, bOBB: IOBBInfo): boolean;
465
+ static oBB(aCenter: CoreVec2, bCenter: CoreVec2, aOBB: IOBBInfo, bOBB: IOBBInfo): boolean;
454
466
  /**
455
467
  * 多边形检测
456
468
  * @param aVertices A的每个点的位置
457
469
  * @param bVertices B的每个点的位置
458
470
  */
459
- static polygon(aVertices: Vec2[], bVertices: Vec2[]): boolean;
471
+ static polygon(aVertices: CoreVec2[], bVertices: CoreVec2[]): boolean;
460
472
  /**
461
473
  * 检测旋转矩形和圆形是否相交<br>
462
474
  * <div style="color: #FF0000;">锚点都为0.5</div>
@@ -464,10 +476,10 @@ export declare class CollisionDetectionUtil {
464
476
  * @param rect 矩形
465
477
  */
466
478
  static circle2Rect(circle: {
467
- position: Vec2;
479
+ position: CoreVec2;
468
480
  radius: number;
469
481
  }, rect: {
470
- position: Vec2;
482
+ position: CoreVec2;
471
483
  width: number;
472
484
  height: number;
473
485
  angle: number;
@@ -476,10 +488,10 @@ export declare class CollisionDetectionUtil {
476
488
  * 2个圆形是否碰撞
477
489
  */
478
490
  static circle2Circle(aCircle: {
479
- position: Vec2;
491
+ position: CoreVec2;
480
492
  radius: number;
481
493
  }, bCircle: {
482
- position: Vec2;
494
+ position: CoreVec2;
483
495
  radius: number;
484
496
  }): boolean;
485
497
  /**
@@ -489,7 +501,7 @@ export declare class CollisionDetectionUtil {
489
501
  * @param ellipseAxis 椭圆轴
490
502
  * @returns
491
503
  */
492
- static IsInEllipse(pos: Vec2, centerPos: Vec2, ellipseAxis: Vec2): boolean;
504
+ static IsInEllipse(pos: CoreVec2, centerPos: CoreVec2, ellipseAxis: CoreVec2): boolean;
493
505
  }
494
506
  export interface IBoxInfo {
495
507
  x: number;
@@ -497,7 +509,7 @@ export interface IBoxInfo {
497
509
  width: number;
498
510
  height: number;
499
511
  }
500
- export interface Vec2 {
512
+ export interface CoreVec2 {
501
513
  x: number;
502
514
  y: number;
503
515
  }
@@ -594,5 +606,21 @@ export declare class MapUtil {
594
606
  */
595
607
  static clearEmptyValue<KEY, VALUE>(targetMap: Map<KEY, VALUE>, isNeedClearCallback?: (item: VALUE) => boolean): void;
596
608
  }
609
+ /**
610
+ * 贝塞尔曲线坐标点插值算法
611
+ * @author Aonaufly
612
+ */
613
+ export declare class BeizerPositionsLerpUtil {
614
+ /**
615
+ * 根据开始坐标和结束坐标计算整个贝塞尔曲线的坐标点集合
616
+ * @param startPosition 开始坐标
617
+ * @param endPosition 结束坐标
618
+ * @param addCount 再添加的坐标点数(startPosition + addCount + endPosition) default 1
619
+ * @param amplitude 幅度(1: 最大为开始坐标与结束坐标的距离的一半)default 0.4
620
+ * @param distanceMultiple 距离倍数(1=线段内, >1则控制点可向两端外延,如1.5=两端各外延0.25倍线段长度)default 1.0
621
+ * @returns 控制点集合
622
+ */
623
+ static generateControlPoints(startPosition: CoreVec2, endPosition: CoreVec2, addCount?: number, amplitude?: number, distanceMultiple?: number): CoreVec2[];
624
+ }
597
625
 
598
626
  export {};