slib.js
71.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
var slib = window.slib = window.slib || {};var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
/**
* sLib
* Base TS game lib, S for simple, standard, stable, and Sean;
* 通用基础库,设计理念为所有项目共用的一些基础方法,不于具体业务相关联,纯TS实现,不对外部及任何引擎有依赖
* @author Sean
*/
/**
* sLib
* Base TS game lib, S for simple, standard, stable, and Sean;
* 通用基础库,设计理念为所有项目共用的一些基础方法,不于具体业务相关联,纯TS实现,不对外部及任何引擎有依赖
* @author Sean
*/
(function (slib) {
var LIB_NAME = "[slib]";
/**控制logI,logW,logE输出的开关 */
slib.LOG_ENABLE = true;
function logI(message) {
if (message === void 0) { message = ""; }
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
slib.LOG_ENABLE && console.info(LIB_NAME, StringUtil.format.apply(StringUtil, __spreadArray([message], args, false)));
}
slib.logI = logI;
function logW(message) {
if (message === void 0) { message = ""; }
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
slib.LOG_ENABLE && console.warn(LIB_NAME, StringUtil.format.apply(StringUtil, __spreadArray([message], args, false)));
}
slib.logW = logW;
function logE(message) {
if (message === void 0) { message = ""; }
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
slib.LOG_ENABLE && console.error(LIB_NAME, StringUtil.format.apply(StringUtil, __spreadArray([message], args, false)));
}
slib.logE = logE;
var StringUtil = /** @class */ (function () {
function StringUtil() {
}
/**
* 是否为无效字符串,null或undefined都会返回true
* @param str
*/
StringUtil.isEmpty = function (str) {
return TypeUtil.isUndefinedOrNull(str) || str.length == 0;
};
/**
* 格式化,类似c++的sprintf
* @param text
* @param formatParam
*/
StringUtil.format = function (text) {
var formatParam = [];
for (var _i = 1; _i < arguments.length; _i++) {
formatParam[_i - 1] = arguments[_i];
}
if (text) {
return StringUtil.formatWithArray(text, formatParam);
}
return "";
};
StringUtil.formatWithArray = function (text, formatParam, paramCnt) {
var maxParamCnt = formatParam.length;
if (typeof paramCnt != "undefined" && paramCnt < maxParamCnt) {
maxParamCnt = paramCnt;
}
return text.replace(/(\{\d+\})/g, function (substr) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
var index = Number(substr.substr(1, substr.length - 2));
if (index >= 0 && index < maxParamCnt) {
return formatParam[index];
}
return substr;
});
};
/**
* 获取字符串长度,中文字当2个字符
* @static
* @param {string} str
* @return {*} {number}
* @memberof StringUtil
*/
StringUtil.getStringLen = function (str) {
var len = 0, charCode = 0;
for (var i = 0; i < str.length; i++) {
charCode = str.charCodeAt(i);
if (charCode >= 0 && charCode <= 128)
len += 1;
else
len += 2;
}
return len;
};
/**
* 将指定的秒数转换为 hh:mm:ss 的字符串
* @param sec 秒数
*/
StringUtil.formatTimeBySecs = function (secs) {
var hour = Math.floor(secs / 3600);
var min = Math.floor((secs - (hour * 3600)) / 60);
var sec = Math.floor(secs % 60);
var hourStr = (hour < 10 ? '0' : '') + hour.toString();
var minStr = (min < 10 ? '0' : '') + min.toString();
var secStr = (sec < 10 ? '0' : '') + sec.toString();
return hourStr + ':' + minStr + ':' + secStr;
};
return StringUtil;
}());
slib.StringUtil = StringUtil;
var TypeUtil = /** @class */ (function () {
function TypeUtil() {
}
/**是否为undefined或null */
TypeUtil.isUndefinedOrNull = function (obj) {
return (typeof obj) === 'undefined' || obj == null;
};
return TypeUtil;
}());
slib.TypeUtil = TypeUtil;
var ArrayUtil = /** @class */ (function () {
function ArrayUtil() {
}
/**
* 从目标数组中删掉指定的元素
* @param list 目标数组
* @param element 要删除的元素
* @returns 要删除的元素,若不在目标数组中,则返回null
*/
ArrayUtil.removeElement = function (list, element) {
var index = list.indexOf(element);
if (index >= 0) {
list.splice(index, 1);
}
else {
return null;
}
};
return ArrayUtil;
}());
slib.ArrayUtil = ArrayUtil;
var MathUtil = /** @class */ (function () {
function MathUtil() {
}
/**
* 获得[n-m)区间中的一个随机整数,包括n,不包括m
*/
MathUtil.getRandom = function (n, m) {
return n + Math.floor(Math.random() * (m - n));
};
/**是否为有效数据,null,undefined,NaN,null皆为无效 */
MathUtil.isValidNumber = function (num) {
return !TypeUtil.isUndefinedOrNull(num) && num != NaN;
};
/**
* 限制value的值在min和max之间, 如果value小于min,返回min。 如果value大于max,返回max,否则返回value
*/
MathUtil.clamp = function (value, min, max) {
return value < min ? min : (value > max ? max : value);
};
/**
* 插值
* @param min 最小值
* @param max 最大值
* @param t 系数
* @returns 所求值
*/
MathUtil.lerp = function (min, max, t) {
if (t > 0 && t < 1) {
return (max - min) * t + min;
}
else {
return t <= 0 ? min : max;
}
};
/**
* 将弧度转化为角度
* @param rad 弧度
*/
MathUtil.getDegreeByRadian = function (rad) { return rad * 180 / Math.PI; };
/**
* 将角度转化为弧度
* @param degree 角度
*/
MathUtil.getRadianByDegree = function (degree) { return degree * Math.PI / 180; };
/**
* 根据向量求角度
* @param x
* @param y
* @returns 返回值为与x轴的夹角,带正负号,正值为x轴逆时针方向,负值为x轴顺时针方向
*/
MathUtil.getDegByVec = function (x, y) {
var rad = Math.atan2(y, x);
return rad * 180 / Math.PI;
};
/**
* 根据向量求弧度
* @param x
* @param y
* @returns 返回值为与x轴的夹角,带正负号,正值为x轴逆时针方向,负值为x轴顺时针方向
*/
MathUtil.getRadianByVec = function (x, y) {
return Math.atan2(y, x);
};
/**根据向量求角度,返回值在 [0,360]之间,代表从x轴逆时针方向旋转的角度 */
MathUtil.get360DegByVec = function (x, y) {
var deg = this.getDegByVec(x, y);
if (deg < 0) {
deg = 360 + deg;
}
return deg;
};
/**根据向量求弧度,返回值在 [0,2Pi]之间,代表从x轴逆时针方向旋转的弧度 */
MathUtil.get2PiRadianByVec = function (x, y) {
var rad = this.getRadianByVec(x, y);
if (rad < 0) {
rad += (Math.PI * 2);
}
return rad;
};
/**
* 根据权重列表进行一次随机,返回命中的索引
* @param list 权重列表
* @returns 命中的索引
*/
MathUtil.randomTestByWeightList = function (list) {
var total = 0;
var pointList = [];
for (var i = 0; i < list.length; ++i) {
var point = slib.math.Vector.get();
point.setValue(total, total + list[i] - 1);
pointList.push(point);
total += list[i];
}
var r = this.getRandom(0, total);
var index = -1;
for (var i = 0; i < pointList.length; ++i) {
if (r >= pointList[i].x && r <= pointList[i].y) {
index = i;
break;
}
}
for (var i = 0; i < pointList.length; ++i) {
slib.math.Vector.recover(pointList[i]);
}
pointList.length = 0;
return index;
};
return MathUtil;
}());
slib.MathUtil = MathUtil;
})(slib || (slib = {}));
(function (slib) {
var astar;
(function (astar) {
/**格子节点 */
var GridNode = /** @class */ (function () {
//#endregion
/**
*
* @param x
* @param y
* @param weight 权重,计算G值时使用此值,越大代表移动代价越高,0表示为障碍物
*/
function GridNode(x, y, weight) {
//#region 内部使用,外部无需关注
/**是否已遍历过,内部使用,外部无需关注 */
this.visited = false;
/**父节点 内部使用,外部无需关注 */
this.parent = null;
/**是否在关闭列表中 内部使用,外部无需关注*/
this.closed = false;
/**内部使用,外部无需关注 */
this.h = 0;
/**内部使用,外部无需关注 */
this.g = 0;
/**内部使用,外部无需关注 */
this.f = 0;
this._x = x;
this._y = y;
this.weight = weight;
}
Object.defineProperty(GridNode.prototype, "x", {
get: function () {
return this._x;
},
enumerable: false,
configurable: true
});
Object.defineProperty(GridNode.prototype, "y", {
get: function () {
return this._y;
},
enumerable: false,
configurable: true
});
/**内部使用,外部无需关注 */
GridNode.prototype.cleanNode = function () {
this.f = 0;
this.g = 0;
this.h = 0;
this.visited = false;
this.closed = false;
this.parent = null;
};
Object.defineProperty(GridNode.prototype, "isWall", {
/**是否是障碍物 */
get: function () { return this.weight === 0; },
enumerable: false,
configurable: true
});
/**移动代价,用于计算G值 */
GridNode.prototype.getCost = function (fromNeighbor) {
// Take diagonal weight into consideration.
if (fromNeighbor && fromNeighbor.x != this.x && fromNeighbor.y != this.y) {
// return this._weight * 1.41421;
return this.weight * 1.4;
}
return this.weight;
};
GridNode.prototype.toString = function () { return '[GridNode x:' + this._x.toString() + ", y:" + this._y.toString() + "]"; };
return GridNode;
}());
astar.GridNode = GridNode;
/**A星格子网格类 */
var Graph = /** @class */ (function () {
/**
*
* @param gridIn 权重的二维数组,格式为[x][y],第一个下标为列数,第二个下表为行数
* @param diagonal 是否允许斜向移动
*/
function Graph(gridIn, diagonal) {
if (diagonal === void 0) { diagonal = false; }
this._neighborsRet = [];
// this._nodes = [];
this._diagonal = diagonal;
this._grid = [];
for (var x = 0; x < gridIn.length; ++x) {
this._grid[x] = [];
for (var y = 0, col = gridIn[x]; y < col.length; ++y) {
var node = new GridNode(x, y, col[y]);
this._grid[x][y] = node;
node.cleanNode();
}
}
this._dirtyNodes = [];
}
Object.defineProperty(Graph.prototype, "diagonal", {
get: function () { return this._diagonal; },
enumerable: false,
configurable: true
});
Object.defineProperty(Graph.prototype, "grid", {
get: function () { return this._grid; },
enumerable: false,
configurable: true
});
Graph.prototype.cleanDirty = function () {
for (var i = 0; i < this._dirtyNodes.length; i++) {
this._dirtyNodes[i].cleanNode();
}
this._dirtyNodes = [];
};
Graph.prototype.markDirty = function (node) { this._dirtyNodes.push(node); };
/**
* 获取目标格子相邻的格子
* @param node
* @returns 注意返回的数组为唯一实例,会随着下一次调用改变,请及时处理
*/
Graph.prototype.neighbors = function (node) {
var ret = this._neighborsRet;
ret = [];
var x = node.x;
var y = node.y;
var grid = this.grid;
// West
if (grid[x - 1] && grid[x - 1][y]) {
ret.push(grid[x - 1][y]);
}
// East
if (grid[x + 1] && grid[x + 1][y]) {
ret.push(grid[x + 1][y]);
}
// South
if (grid[x] && grid[x][y - 1]) {
ret.push(grid[x][y - 1]);
}
// North
if (grid[x] && grid[x][y + 1]) {
ret.push(grid[x][y + 1]);
}
if (this.diagonal) {
// Southwest
if (grid[x - 1] && grid[x - 1][y - 1]) {
ret.push(grid[x - 1][y - 1]);
}
// Southeast
if (grid[x + 1] && grid[x + 1][y - 1]) {
ret.push(grid[x + 1][y - 1]);
}
// Northwest
if (grid[x - 1] && grid[x - 1][y + 1]) {
ret.push(grid[x - 1][y + 1]);
}
// Northeast
if (grid[x + 1] && grid[x + 1][y + 1]) {
ret.push(grid[x + 1][y + 1]);
}
}
return ret;
};
return Graph;
}());
astar.Graph = Graph;
function pathTo(node) {
var curr = node;
var path = [];
while (curr.parent) {
path.unshift(curr);
curr = curr.parent;
}
return path;
}
astar.pathTo = pathTo;
var AStar = /** @class */ (function () {
function AStar() {
}
/**
*
* @param graph 寻路网格
* @param start 起点
* @param end 终点
* @param closest 当目标点不可到达时,是否寻路至最近的点
* @param heuristic 启发函数,用于在寻路时估计当前格子到终点的距离,即计算H值,默认使用manhattan方法
* @returns 找不到路径时返回null
*/
AStar.prototype.search = function (graph, start, end, closest, heuristic) {
if (closest === void 0) { closest = false; }
if (heuristic === void 0) { heuristic = null; }
graph.cleanDirty();
heuristic = heuristic || heuristics.manhattan;
var openHeap = this.getHeap();
var closestNode = start;
start.h = heuristic(start, end);
graph.markDirty(start);
openHeap.push(start);
//遍历open列表
while (openHeap.size > 0) {
//取出一个f值最小的节点
var currentNode = openHeap.pop();
//到达终点了
if (currentNode === end) {
return pathTo(end);
}
//放入close列表,尝试向周围行走
currentNode.closed = true;
var neighbors = graph.neighbors(currentNode);
for (var i = 0, il = neighbors.length; i < il; i++) {
var neighbor = neighbors[i];
if (neighbor.closed || neighbor.isWall) {
continue; //已经行走过,或是不能行走,跳过
}
var gScore = currentNode.g + neighbor.getCost(currentNode); //当前行走节点的g值 + 走到这个格子花费的g值
var beenVisited = neighbor.visited;
if (!beenVisited || gScore < neighbor.g) {
neighbor.visited = true; //标记一下,已经被父节点尝试过
neighbor.parent = currentNode;
neighbor.h = neighbor.h || heuristic(neighbor, end);
neighbor.g = gScore;
neighbor.f = neighbor.g + neighbor.h;
graph.markDirty(neighbor);
if (closest) {
if (neighbor.h < closestNode.h || (neighbor.h === closestNode.h) && neighbor.g < closestNode.g) {
//距离更小 或是 更容易走到, 更新最近的节点
closestNode = neighbor;
}
}
if (!beenVisited) {
openHeap.push(neighbor); //待探索的格子,加入到open列表中
}
else {
openHeap.rescoreElement(neighbor, true); //g值变小了,父节点更新了,重排二叉堆
}
}
}
}
if (closest) {
return pathTo(closestNode);
}
return null;
};
AStar.prototype.getHeap = function () {
return new slib.math.BinaryHeap(function (node) { return node.f; });
};
return AStar;
}());
astar.AStar = AStar;
var heuristics;
(function (heuristics) {
function manhattan(pos0, pos1) {
return Math.abs(pos1.x - pos0.x) + Math.abs(pos1.y - pos0.y);
}
heuristics.manhattan = manhattan;
})(heuristics = astar.heuristics || (astar.heuristics = {}));
})(astar = slib.astar || (slib.astar = {}));
})(slib || (slib = {}));
(function (slib) {
var comp;
(function (comp) {
/**摇杆数据模型,设计思路为,传入摇杆半径和按压点,可获得摇杆坐标和力度数据
* 使用方法,设置半径和基准点,当操作摇杆时调用tap接口传入按压点,释放时调用release接口,修改基准点时调用setStartPos接口,所有坐标都需要与基准点在同一坐标系下
*/
var JoyStick = /** @class */ (function () {
/**
* @param radius 最大半径
*/
function JoyStick(radius, startX, startY) {
if (radius === void 0) { radius = 100; }
if (startX === void 0) { startX = 0; }
if (startY === void 0) { startY = 0; }
this.destroyed = false;
this._startX = 0;
this._startY = 0;
this._radius = 100;
this._stickX = 0;
this._stickY = 0;
this._tapping = false;
this._radius = radius;
this.setStartPos(startX, startY);
}
Object.defineProperty(JoyStick.prototype, "startX", {
/**基准点X */
get: function () { return this._startX; },
enumerable: false,
configurable: true
});
Object.defineProperty(JoyStick.prototype, "startY", {
/**基准点Y */
get: function () { return this._startY; },
enumerable: false,
configurable: true
});
Object.defineProperty(JoyStick.prototype, "radius", {
get: function () { return this._radius; },
enumerable: false,
configurable: true
});
Object.defineProperty(JoyStick.prototype, "strengthX", {
/**X轴方向力度,正负代表方向,范围 0-1 */
get: function () { return (this._stickX - this._startX) / this._radius; },
enumerable: false,
configurable: true
});
Object.defineProperty(JoyStick.prototype, "strengthY", {
/**Y轴方向力度,正负代表方向,范围 0-1 */
get: function () { return (this._stickY - this._startY) / this._radius; },
enumerable: false,
configurable: true
});
Object.defineProperty(JoyStick.prototype, "stickX", {
/**摇杆坐标X */
get: function () { return this._stickX; },
enumerable: false,
configurable: true
});
Object.defineProperty(JoyStick.prototype, "stickY", {
/**摇杆坐标Y */
get: function () { return this._stickY; },
enumerable: false,
configurable: true
});
Object.defineProperty(JoyStick.prototype, "tapping", {
/**是否在按压中 */
get: function () { return this._tapping; },
enumerable: false,
configurable: true
});
/**修改摇杆基准点 */
JoyStick.prototype.setStartPos = function (posX, posY) {
this._startX = posX;
this._startY = posY;
if (this._tapping) {
this.tap(this._stickX, this._stickY); //重新刷新下
}
else {
this._stickX = this._startX;
this._stickY = this._startY;
}
};
JoyStick.prototype.tap = function (tapX, tapY) {
this._tapping = true;
var vec = slib.math.Vector.get().setValue(tapX - this._startX, tapY - this._startY);
if (vec.x * vec.x + vec.y * vec.y > this._radius * this._radius) { //效果等价于比较距离,用平方数比较可以减少一次开方运算
vec.normalize();
vec.scale(this._radius);
}
this._stickX = this._startX + vec.x;
this._stickY = this._startY + vec.y;
slib.math.Vector.recover(vec);
// console.log("start", this._startX, this._startY, "stick", this._stickX, this._stickY);
};
JoyStick.prototype.release = function () {
if (this._tapping) {
this._tapping = false;
this._stickX = this._startX;
this._stickY = this._startY;
}
};
JoyStick.prototype.destroy = function () {
this.destroyed = true;
};
return JoyStick;
}());
comp.JoyStick = JoyStick;
})(comp = slib.comp || (slib.comp = {}));
})(slib || (slib = {}));
(function (slib) {
var config;
(function (config) {
/**配置基类 */
var ConfigBase = /** @class */ (function () {
function ConfigBase() {
}
return ConfigBase;
}());
config.ConfigBase = ConfigBase;
})(config = slib.config || (slib.config = {}));
})(slib || (slib = {}));
(function (slib) {
var config;
(function (config) {
/**配置管理器,使用方法:setTable 后 parse, 即可获取数据 */
var ConfigManager = /** @class */ (function () {
function ConfigManager() {
this._map = null;
this.m_temp = new Map();
}
Object.defineProperty(ConfigManager.prototype, "map", {
/**所有的配置都在这里 */
get: function () { return this._map; },
enumerable: false,
configurable: true
});
/**
* 传入配置表数据,传入后需调用parse方法,将传入的数据解析为对应的类
* @param name 表的名字,区分表用
* @param data 数据数组,json导出为数组形式,解析后的object
* @param cls 对应的类
*/
ConfigManager.prototype.setTable = function (name, data, cls) {
this.m_temp.set(name, { data: data, cls: cls });
};
/**获取某张表的配置数量 */
ConfigManager.prototype.getTableCount = function (name) {
return this._map[name]["#count"];
};
/**获取某张表的最大ID */
ConfigManager.prototype.getTableMaxID = function (name) {
return this._map[name]["#maxId"];
};
/**解析后将清空所有缓存的数据 */
ConfigManager.prototype.parse = function () {
var _this = this;
this._map = {};
this.m_temp.forEach(function (value, key, map) {
//单张表处理
var cls = value.cls;
var loaded = value.data;
if (!_this._map[key]) {
_this._map[key] = {};
}
var maxId = 0;
for (var j = 0; j < loaded.length; ++j) {
//单个数据处理
var item = new cls();
for (var key_1 in loaded[j]) {
if (Object.prototype.hasOwnProperty.call(loaded[j], key_1)) {
var element = loaded[j][key_1];
item[key_1] = element;
}
}
_this._map[key][item.ID] = item;
if (item.ID > maxId) {
maxId = item.ID;
}
}
_this._map[key]["#count"] = loaded.length;
_this._map[key]["#maxId"] = maxId;
}, this);
this.m_temp.clear();
};
/**
* 根据表名和ID取出数据
* @param tableName
* @param id
* @returns 找不到则返回null
*/
ConfigManager.prototype.getConfig = function (tableName, id) {
if (this._map[tableName] && this._map[tableName][id]) {
return this._map[tableName][id];
}
else {
return null;
}
};
return ConfigManager;
}());
config.ConfigManager = ConfigManager;
})(config = slib.config || (slib.config = {}));
})(slib || (slib = {}));
(function (slib) {
var pool;
(function (pool) {
/**池元素基类,若继承此基类,则不可在构造函数中传入参数,避免池管理器创建对象时出错 */
var PoolUnit = /** @class */ (function () {
function PoolUnit() {
this.inPool = false;
}
/**从池中取出时的初始化接口,建议子类复写,在此时初始化数据 */
PoolUnit.prototype.onInitFromPool = function () {
this.inPool = false;
};
/**回收到池中时调用的接口,建议子类复写,在此时清除内部相关引用 */
PoolUnit.prototype.onRecoverToPool = function () {
this.inPool = true;
};
return PoolUnit;
}());
pool.PoolUnit = PoolUnit;
})(pool = slib.pool || (slib.pool = {}));
})(slib || (slib = {}));
(function (slib) {
var pool;
(function (pool) {
var PoolManager = /** @class */ (function () {
function PoolManager(T) {
this._pool = [];
this._warnNum = 200;
this._class = T;
}
Object.defineProperty(PoolManager.prototype, "warnNum", {
/**警告数量,若池中元素数量超过设定值则打印警告,默认为200 */
get: function () { return this._warnNum; },
enumerable: false,
configurable: true
});
/**
* 设置警告数量,查看warnNum属性
* @param num
*/
PoolManager.prototype.setWarnNum = function (num) {
this._warnNum = num;
};
PoolManager.prototype.getUnit = function () {
if (this._pool.length >= this._warnNum) {
slib.logW("pool:{0}, unit num warn:{1}", this._class.name, this._pool.length);
}
var target = null;
for (var i = 0; i < this._pool.length; ++i) {
if (this._pool[i].inPool) {
target = this._pool[i];
break;
}
}
if (target == null) {
target = new this._class();
this._pool.push(target);
}
target.onInitFromPool();
return target;
};
/**注意,若不是由PoolManager创建的,也会回收到池中 */
PoolManager.prototype.recoverUnit = function (unit) {
unit.onRecoverToPool();
if (this._pool.indexOf(unit) < 0) {
this._pool.push(unit);
}
};
return PoolManager;
}());
pool.PoolManager = PoolManager;
})(pool = slib.pool || (slib.pool = {}));
})(slib || (slib = {}));
/// <reference path="../pool/PoolUnit.ts"/>
/// <reference path="../pool/PoolManager.ts"/>
/// <reference path="../pool/PoolUnit.ts"/>
/// <reference path="../pool/PoolManager.ts"/>
(function (slib) {
var event;
(function (event_1) {
var Listener = /** @class */ (function (_super) {
__extends(Listener, _super);
function Listener() {
return _super !== null && _super.apply(this, arguments) || this;
}
Listener.prototype.onRecoverToPool = function () {
_super.prototype.onRecoverToPool.call(this);
this.thisObj = null;
this.callBack = null;
};
return Listener;
}(slib.pool.PoolUnit));
/**事件派发器 */
var EventDispatcher = /** @class */ (function () {
function EventDispatcher() {
this._map = new Map();
}
EventDispatcher.prototype.addEventListener = function (event, callBack, thisObj) {
if (!this._map.has(event)) {
this._map.set(event, []);
}
var list = this._map.get(event);
var listener = EventDispatcher._pool.getUnit();
listener.thisObj = thisObj;
listener.callBack = callBack;
list.push(listener);
};
EventDispatcher.prototype.removeEventListener = function (event, callBack) {
var list = this._map.get(event);
if (list) {
for (var i = 0; i < list.length; ++i) {
if (list[i].callBack == callBack) {
var listener = list[i];
list.splice(i, 1);
EventDispatcher._pool.recoverUnit(listener);
break;
}
}
}
};
EventDispatcher.prototype.dispatchEvent = function (event, arg) {
var list = this._map.get(event);
if (list) {
for (var i = 0; i < list.length; ++i) {
if (arg) {
//TODO 若此时移除了监听,会造成循环错乱
list[i].callBack.call(list[i].thisObj, arg);
}
else {
list[i].callBack.call(list[i].thisObj);
}
}
}
};
/**清除所有监听 */
EventDispatcher.prototype.clearAll = function () {
for (var key in this._map) {
var list = this._map.get(key);
for (var i = 0; i < list.length; ++i) {
EventDispatcher._pool.recoverUnit(list[i]);
}
list.length = 0;
this._map.delete[key];
}
};
EventDispatcher._pool = new slib.pool.PoolManager(Listener);
return EventDispatcher;
}());
event_1.EventDispatcher = EventDispatcher;
})(event = slib.event || (slib.event = {}));
})(slib || (slib = {}));
(function (slib) {
var frame;
(function (frame) {
var Battle = /** @class */ (function () {
function Battle() {
this.destroyed = false;
this._battleLastTime = 0;
this._moduleList = [];
this._lazyUpdateDelta = 0;
this._lazyUpdateFlag = true;
}
Object.defineProperty(Battle.prototype, "battleLastTime", {
/**战斗持续时间 */
get: function () { return this._battleLastTime; },
enumerable: false,
configurable: true
});
Battle.prototype.addModule = function (module) {
this._moduleList.push(module);
};
Battle.prototype.init = function () {
for (var i = 0; i < this._moduleList.length; i++) {
var element = this._moduleList[i];
element.initModule();
}
};
Battle.prototype.start = function () {
this._battleLastTime = 0;
for (var i = 0; i < this._moduleList.length; i++) {
var element = this._moduleList[i];
element.startModule();
}
};
Battle.prototype.update = function (delta) {
this._battleLastTime += delta;
for (var i = 0; i < this._moduleList.length; i++) {
var element = this._moduleList[i];
element.update(delta);
}
for (var i = 0; i < this._moduleList.length; i++) {
var element = this._moduleList[i];
element.lateUpdate(delta);
}
this._lazyUpdateDelta += delta;
this._lazyUpdateFlag = !this._lazyUpdateFlag;
if (this._lazyUpdateFlag) {
for (var i = 0; i < this._moduleList.length; i++) {
var element = this._moduleList[i];
element.lazyUpdate(this._lazyUpdateDelta);
}
this._lazyUpdateDelta = 0;
}
};
Battle.prototype.stop = function () {
for (var i = 0; i < this._moduleList.length; i++) {
var element = this._moduleList[i];
element.stopModule();
}
};
Battle.prototype.destroy = function () {
for (var i = 0; i < this._moduleList.length; i++) {
var element = this._moduleList[i];
element.destroy();
}
this._moduleList.length = 0;
};
return Battle;
}());
frame.Battle = Battle;
})(frame = slib.frame || (slib.frame = {}));
})(slib || (slib = {}));
(function (slib) {
var frame;
(function (frame) {
var BattleBaseModule = /** @class */ (function () {
function BattleBaseModule(battle) {
this._battle = battle;
}
Object.defineProperty(BattleBaseModule.prototype, "battle", {
get: function () { return this._battle; },
enumerable: false,
configurable: true
});
BattleBaseModule.prototype.initModule = function () { };
BattleBaseModule.prototype.startModule = function () { };
BattleBaseModule.prototype.stopModule = function () { };
BattleBaseModule.prototype.update = function (delta) { };
/**在所有module的update结束后进行 */
BattleBaseModule.prototype.lateUpdate = function (delta) { };
/**两帧调用一次,可处理一些并不需要频繁更新的逻辑 */
BattleBaseModule.prototype.lazyUpdate = function (delta) { };
BattleBaseModule.prototype.destroy = function () { this._battle = null; };
return BattleBaseModule;
}());
frame.BattleBaseModule = BattleBaseModule;
})(frame = slib.frame || (slib.frame = {}));
})(slib || (slib = {}));
(function (slib) {
var frame;
(function (frame) {
/**可控制心跳的timer */
var FrameTimer = /** @class */ (function () {
function FrameTimer() {
this._intervalCount = 0;
this._running = false;
}
Object.defineProperty(FrameTimer.prototype, "running", {
/**是否正在运行 */
get: function () { return this._running; },
enumerable: false,
configurable: true
});
/**
* @param interval 间隔,需要与update中传入的参数为同一单位
* @param callBack
* @param callBackThisObj
* @param count 0为无限
*/
FrameTimer.prototype.init = function (interval, callBack, callBackThisObj, count) {
if (count === void 0) { count = 1; }
this._interval = interval;
this._callBack = callBack;
this._callBackThisObj = callBackThisObj;
this._count = count;
};
FrameTimer.prototype.start = function () {
this._running = true;
};
FrameTimer.prototype.update = function (delta) {
if (!this._running)
return;
this._intervalCount += delta;
if (this._intervalCount >= this._interval) {
this._intervalCount -= this._interval;
if (this._count >= 1) {
--this._count;
if (this._count == 0) {
this._running = false;
}
this._callBack.call(this._callBackThisObj);
}
else if (this._count == 0) {
this._callBack.call(this._callBackThisObj);
}
}
};
FrameTimer.prototype.stop = function () {
this._running = false;
};
FrameTimer.prototype.destroy = function () {
this._running = false;
this._callBack = null;
this._callBackThisObj = null;
};
return FrameTimer;
}());
frame.FrameTimer = FrameTimer;
})(frame = slib.frame || (slib.frame = {}));
})(slib || (slib = {}));
(function (slib) {
var math;
(function (math) {
/**最小二叉堆,根节点为最小值,父节点总是小于或等于子节点,在一个频繁修改的数组中,若总是需要获取最小值,使用二叉堆性能较高 */
var BinaryHeap = /** @class */ (function () {
/**
*
* @param scoreFunction 排序值函数,二叉树根据此函数返回的值进行排序
*/
function BinaryHeap(scoreFunction) {
this._content = [];
this._scoreFunction = scoreFunction;
}
Object.defineProperty(BinaryHeap.prototype, "size", {
/**所有节点数量 */
get: function () { return this._content.length; },
enumerable: false,
configurable: true
});
/**添加节点 */
BinaryHeap.prototype.push = function (element) {
this._content.push(element);
this.sinkDown(this._content.length - 1);
};
/**取最小值节点 */
BinaryHeap.prototype.pop = function () {
var result = this._content[0];
var end = this._content.pop(); //末尾数据
if (this._content.length > 0) {
this._content[0] = end; //将末尾数据插入头部
this.bubbleUp(0);
}
return result;
};
/**删除所有子节点 */
BinaryHeap.prototype.clear = function () {
this._content.length = 0;
};
/**移除某个节点,二叉堆会在移除的节点处重新排序 */
BinaryHeap.prototype.remove = function (node) {
var i = this._content.indexOf(node);
if (i > -1) {
//目标节点在二叉堆中
var end = this._content.pop();
if (end == node) {
//移除的是最后一个节点,什么都不用做
}
else {
this._content[i] = end; //将最后一个节点填充到删除的位置
var reduce = (this._scoreFunction(end) < this._scoreFunction(node)); //是否变小了
if (reduce) {
this.sinkDown(i);
}
else {
this.bubbleUp(i);
}
}
}
};
/**
* 某个节点的值改变了,重排二叉堆
* @param node 需要重排的节点
* @param reduce true表示减小,false表示增加
*/
BinaryHeap.prototype.rescoreElement = function (node, reduce) {
if (reduce === void 0) { reduce = true; }
if (reduce) {
this.sinkDown(this._content.indexOf(node));
}
else {
this.bubbleUp(this._content.indexOf(node));
}
};
/**从索引n开始向上查找,将大的父节点下沉 */
BinaryHeap.prototype.sinkDown = function (n) {
var element = this._content[n];
while (n > 0) {
var parentN = ((n + 1) >> 1) - 1; //父节点坐标
var parent_1 = this._content[parentN];
if (this._scoreFunction(element) < this._scoreFunction(parent_1)) {
this._content[parentN] = element;
this._content[n] = parent_1;
n = parentN;
}
else {
break; //已经符合,不需要继续排序了
}
}
};
/**从索引n开始向下查找,将小的子节点上浮 */
BinaryHeap.prototype.bubbleUp = function (n) {
var length = this._content.length;
var element = this._content[n];
var elemScore = this._scoreFunction(element);
while (true) {
var child2N = (n + 1) << 1;
var child1N = child2N - 1;
var swap = null;
var child1Score = null;
if (child1N < length) {
//如果子节点1存在
var child1 = this._content[child1N];
child1Score = this._scoreFunction(child1);
if (child1Score < elemScore) {
//子节点小,需要上浮
swap = child1N;
}
}
if (child2N < length) {
//如果子节点2存在
var child2 = this._content[child2N];
var child2Score = this._scoreFunction(child2);
if (child2Score < (swap == null ? elemScore : child1Score)) {
//子节点小,需要上浮
swap = child2N;
}
}
if (swap != null) {
this._content[n] = this._content[swap];
this._content[swap] = element;
n = swap;
}
else {
//不需要交换,结束
break;
}
}
};
return BinaryHeap;
}());
math.BinaryHeap = BinaryHeap;
})(math = slib.math || (slib.math = {}));
})(slib || (slib = {}));
(function (slib) {
var math;
(function (math) {
/**2d向量简易封装 */
var Vector = /** @class */ (function (_super) {
__extends(Vector, _super);
function Vector(x, y) {
var _this = _super.call(this) || this;
_this._x = 0;
_this._y = 0;
_this._x = x;
_this._y = y;
return _this;
}
/**从对象池中获取 */
Vector.get = function () {
if (!this._inited) {
this._inited = true;
this._poolManager.setWarnNum(9999);
}
return this._poolManager.getUnit();
};
Vector.recover = function (p) {
this._poolManager.recoverUnit(p);
};
Object.defineProperty(Vector.prototype, "x", {
get: function () { return this._x; },
enumerable: false,
configurable: true
});
Object.defineProperty(Vector.prototype, "y", {
get: function () { return this._y; },
enumerable: false,
configurable: true
});
Object.defineProperty(Vector.prototype, "magnitude", {
/**模长 */
get: function () { return Math.sqrt(this.sqrMagnitude); },
enumerable: false,
configurable: true
});
Object.defineProperty(Vector.prototype, "sqrMagnitude", {
/**模长的平方数,在需要比较向量长度时,可比较两向量的模长平方数,省去开方计算 */
get: function () { return this._x * this._x + this._y * this._y; },
enumerable: false,
configurable: true
});
Vector.prototype.setValue = function (valueX, valueY) {
this.setX(valueX);
this.setY(valueY);
return this;
// this._x = x;
// this._y = y;
};
Vector.prototype.setX = function (valueX) {
if (slib.MathUtil.isValidNumber(valueX)) {
this._x = valueX;
}
};
Vector.prototype.setY = function (valueY) {
if (slib.MathUtil.isValidNumber(valueY)) {
this._y = valueY;
}
};
/**当x,y都为0时,不会产生变化 */
Vector.prototype.normalize = function () {
if (this._x != 0 || this._y != 0) {
this.scale(1 / this.magnitude);
}
};
Vector.prototype.scale = function (value) {
this._x *= value;
this._y *= value;
};
Vector._poolManager = new slib.pool.PoolManager(Vector);
Vector._inited = false;
return Vector;
}(slib.pool.PoolUnit));
math.Vector = Vector;
})(math = slib.math || (slib.math = {}));
})(slib || (slib = {}));
/**2d移动模型 */
/**2d移动模型 */
(function (slib) {
var move2d;
(function (move2d) {
var Dir;
(function (Dir) {
Dir[Dir["LEFT"] = 0] = "LEFT";
Dir[Dir["RIGHT"] = 1] = "RIGHT";
Dir[Dir["UP"] = 2] = "UP";
Dir[Dir["DOWN"] = 3] = "DOWN";
})(Dir = move2d.Dir || (move2d.Dir = {}));
/**2d移动模型,可根据设置速度、加速度、方向进行移动,需由外部调用update接口更新 */
var ActorMove2d = /** @class */ (function () {
function ActorMove2d(x, y) {
this.destroyed = false;
this._speed = 0;
this._accelerate = 0;
this._dirNormal = new slib.math.Vector(0, 0);
this._stepVec = new slib.math.Vector(0, 0);
/**步进模长 每次调用onFrameUpdate后更新*/
this._stepLen = 0;
this._pos = new slib.math.Vector(0, 0);
this.active = true;
this._pos.setValue(x, y);
}
Object.defineProperty(ActorMove2d.prototype, "speed", {
/**速度 */
get: function () { return this._speed; },
enumerable: false,
configurable: true
});
Object.defineProperty(ActorMove2d.prototype, "accelerate", {
/**加速度*/
get: function () { return this._accelerate; },
enumerable: false,
configurable: true
});
Object.defineProperty(ActorMove2d.prototype, "dirNormal", {
/**方向法向量 */
get: function () { return this._dirNormal; },
enumerable: false,
configurable: true
});
Object.defineProperty(ActorMove2d.prototype, "stepVec", {
/**步进向量 每次调用onFrameUpdate后更新*/
get: function () { return this._stepVec; },
enumerable: false,
configurable: true
});
Object.defineProperty(ActorMove2d.prototype, "pos", {
get: function () { return this._pos; },
enumerable: false,
configurable: true
});
/**设置前进方向 */
ActorMove2d.prototype.setDir = function (x, y) {
this._dirNormal.setValue(x, y);
this._dirNormal.normalize();
};
/**直接设置前进方向的法向量,可节省一次归一运算 */
ActorMove2d.prototype.setDirNormal = function (x, y) {
this._dirNormal.setValue(x, y);
};
/**步进接口,更新一帧数据
* @param delta 单位秒
*/
ActorMove2d.prototype.update = function (delta) {
if (!this.active)
return;
//先更新速度
if (this._accelerate != 0) {
var speedDelta = delta * this._accelerate;
this._speed += speedDelta;
this._speed < 0 && (this._speed = 0);
}
// let realDis = 0;
if ((this._dirNormal.x != 0 || this._dirNormal.y != 0) && this._speed > 0) {
//需要进行位移
var dis = delta * this._speed;
this._stepVec.setValue(this._dirNormal.x, this._dirNormal.y);
this._stepVec.scale(dis);
this._stepLen = this._stepVec.magnitude;
this.setPos(this._pos.x + this._stepVec.x, this._pos.y + this._stepVec.y);
this.callBallBack(this._stepLen);
}
else {
this._stepVec.setValue(0, 0);
this._stepLen = 0;
}
};
/**手动设置位置,不会触发位移回调 */
ActorMove2d.prototype.setPos = function (x, y) {
this._pos.setValue(x, y);
};
/**
* 当调用onFrameUpdate时会触发此接口,调用posChangeCallBack,子类可复写此方法,控制调用的时机
* @param dis 前进的距离
*/
ActorMove2d.prototype.callBallBack = function (dis) {
if (this._posChangeCallBack) {
this._posChangeCallBack.call(this._posChangeCallBackThisObj, dis);
}
};
/**
* 绑定位移回调函数,在产生位移时才会触发回调
* @param func 回调函数,参数 dis:number,前进的距离
*/
ActorMove2d.prototype.bindPosChangeCallback = function (callBack, thisObj) {
this._posChangeCallBack = callBack;
this._posChangeCallBackThisObj = thisObj;
};
/**
* 设置速度,若设置负值,速度会变为0
* @param value
*/
ActorMove2d.prototype.setSpeed = function (value) {
if (this._speed != value) {
this._speed = value;
if (this._speed < 0) {
this._speed = 0;
}
}
};
/**设置加速度,若设置负值,则产生减速效果 */
ActorMove2d.prototype.setAcceleration = function (value) {
if (value != this._accelerate) {
this._accelerate = value;
}
};
ActorMove2d.prototype.destroy = function () {
this.destroyed = true;
slib.math.Vector.recover(this._stepVec);
this._stepVec = null;
this._posChangeCallBack = null;
this._posChangeCallBackThisObj = null;
slib.math.Vector.recover(this.dirNormal);
this._dirNormal = null;
slib.math.Vector.recover(this._pos);
this._pos = null;
};
return ActorMove2d;
}());
move2d.ActorMove2d = ActorMove2d;
})(move2d = slib.move2d || (slib.move2d = {}));
})(slib || (slib = {}));
(function (slib) {
var move2d;
(function (move2d) {
/**在ActorMove2d的基础上改为根据路径列表前进,setDir接口会失效 */
var ActorMove2dPath = /** @class */ (function (_super) {
__extends(ActorMove2dPath, _super);
function ActorMove2dPath() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this._pathIndex = -1;
_this._changePointCallBack = null;
_this._changePointCallBackThisObj = null;
_this._posChangeCallBackFlag = false;
return _this;
}
Object.defineProperty(ActorMove2dPath.prototype, "pathList", {
get: function () { return this._pathList; },
enumerable: false,
configurable: true
});
Object.defineProperty(ActorMove2dPath.prototype, "pathIndex", {
/**当前正在追逐的节点索引 */
get: function () { return this._pathIndex; },
enumerable: false,
configurable: true
});
/**
* 注意,若设置了加速度,只在行走时加速度才参与计算
* @param value 加速度
*/
ActorMove2dPath.prototype.setAcceleration = function (value) {
_super.prototype.setAcceleration.call(this, value);
};
/**
* 设置路径列表,若路径点与当前位置相同,则会跳到下一个路径点,直到位置不同
* @param pathList 传入的路径列表,内部只会引用此数组,不修改其中的数据
*/
ActorMove2dPath.prototype.start = function (pathList) {
if (pathList && pathList.length > 0) {
this._pathList = pathList;
this._pathIndex = -1;
do {
this.checkNextPathPoint();
} while (this._curTarget && this.pos.x == this._curTarget.x && this.pos.y == this._curTarget.y); //就在当前位置上,切换下一点
if (this._curTarget == null) {
slib.logW("pathlist donot contain valid pos");
}
}
};
/**
* 绑定路径点切换时的回调函数,当走过一个点时会触发一次,若出发后此实例的pathIndex属性为-1,则表示路径已走完
* @param callBack
* @param thisObj
*/
ActorMove2dPath.prototype.bindPointChangeCallBack = function (callBack, thisObj) {
this._changePointCallBack = callBack;
this._changePointCallBackThisObj = thisObj;
};
ActorMove2dPath.prototype.setDir = function () {
//屏蔽此方法,避免外部修改方向
};
ActorMove2dPath.prototype.stop = function () {
this._curTarget = null;
this._pathIndex = -1;
_super.prototype.setDir.call(this, 0, 0);
};
ActorMove2dPath.prototype.gotoPathPoint = function (moveLen) {
if (this._curTarget) {
/**目标点距离 平方数 */
var targetLenSqr = (this._curTarget.x - this.pos.x) * (this._curTarget.x - this.pos.x) + (this._curTarget.y - this.pos.y) * (this._curTarget.y - this.pos.y);
var moveLenSqr = moveLen * moveLen;
if (moveLenSqr >= targetLenSqr) {
//超过了目标点
this.setPos(this._curTarget.x, this._curTarget.y); //移动到目标点
this.checkNextPathPoint(); //切换下一目标点
this.onGoToNextPoint();
return Math.sqrt(targetLenSqr);
}
else {
//到不了目标点
var step = slib.math.Vector.get();
step.setValue(this.dirNormal.x, this.dirNormal.y);
step.scale(moveLen);
this.setPos(this.pos.x + step.x, this.pos.y + step.y);
slib.math.Vector.recover(step);
return moveLen;
}
}
return 0;
};
ActorMove2dPath.prototype.update = function (delta) {
if (this._curTarget && this.speed > 0) {
this._posChangeCallBackFlag = false;
var tempPos = slib.math.Vector.get();
tempPos.setValue(this.pos.x, this.pos.y);
_super.prototype.update.call(this, delta); //先让计算父类位移
this.setPos(tempPos.x, tempPos.y); //重置位置
slib.math.Vector.recover(tempPos);
var stepCount = this._stepLen; //要把它走完或是走到终点
var moveLen = 0;
while (stepCount > 0) {
var stepLen = this.gotoPathPoint(stepCount);
stepCount -= stepLen;
moveLen += stepLen;
if (stepLen == 0) {
break; //没有路径点了
}
}
this._posChangeCallBackFlag = true;
this.callBallBack(moveLen);
}
};
ActorMove2dPath.prototype.callBallBack = function (dis) {
if (this._posChangeCallBackFlag) {
_super.prototype.callBallBack.call(this, dis);
}
};
/**
* 当前路径点切换时调用
* @returns true为成功,false为路径点已走完了
*/
ActorMove2dPath.prototype.checkNextPathPoint = function () {
this._pathIndex += 1;
if (this._pathIndex >= 0 && this._pathIndex < this._pathList.length) {
this._curTarget = this._pathList[this._pathIndex];
_super.prototype.setDir.call(this, this._curTarget.x - this.pos.x, this._curTarget.y - this.pos.y);
return true;
}
else {
this.stop();
return false;
}
};
/**当前路径点切换时调用,供子类复写,第一个点不会触发 */
ActorMove2dPath.prototype.onGoToNextPoint = function () {
if (this._changePointCallBack) {
this._changePointCallBack.call(this._changePointCallBackThisObj);
}
};
ActorMove2dPath.prototype.destroy = function () {
this.stop();
_super.prototype.destroy.call(this);
this._pathList = null;
this._curTarget = null;
this._changePointCallBack = null;
this._changePointCallBackThisObj = null;
};
return ActorMove2dPath;
}(move2d.ActorMove2d));
move2d.ActorMove2dPath = ActorMove2dPath;
})(move2d = slib.move2d || (slib.move2d = {}));
})(slib || (slib = {}));
/**状态机相关 */
/**状态机相关 */
(function (slib) {
var sm;
(function (sm) {
/**
* 单个状态基类
*/
var SMBaseState = /** @class */ (function () {
function SMBaseState() {
this.isDestroy = false;
this.map = new Map();
}
Object.defineProperty(SMBaseState.prototype, "id", {
get: function () { return this.m_stateId; },
set: function (value) { this.m_stateId = value; },
enumerable: false,
configurable: true
});
Object.defineProperty(SMBaseState.prototype, "isRunning", {
get: function () { return this.stateMachine.getCurrentStateID() == this.m_stateId; },
enumerable: false,
configurable: true
});
/**
* 定义事件和状态的跳转关系
* @param trans 事件类型
* @param id 状态类型
*/
SMBaseState.prototype.addTransition = function (trans, id) {
if (trans == SMBaseState.nullTransitionId || id == SMBaseState.nullStateId || this.map.has(trans)) {
return;
}
this.map.set(trans, id);
};
SMBaseState.prototype.deleteTransition = function (trans) {
if (trans == SMBaseState.nullTransitionId) {
return;
}
if (this.map.has(trans)) {
this.map.delete(trans);
}
};
SMBaseState.prototype.getOutputState = function (trans) {
return this.map.has(trans) ? this.map.get(trans) : SMBaseState.nullStateId;
};
/**状态被触发时的调用 */
SMBaseState.prototype.onEnter = function (data) {
if (data === void 0) { data = null; }
};
/**离开状态的调用,状态机destory时,也会调用当前状态的onLeave接口 */
SMBaseState.prototype.onLeave = function () {
};
SMBaseState.prototype.onReason = function (transID, data) {
if (data === void 0) { data = null; }
var shouldChange = false;
if (this.getOutputState(transID) != SMBaseState.nullStateId) {
shouldChange = true;
}
if (shouldChange) {
this.stateMachine.performTransition(transID, data);
}
};
SMBaseState.prototype.onUpdate = function (data) {
if (data === void 0) { data = null; }
};
/**状态机destro时触发 */
SMBaseState.prototype.onDestory = function () {
this.stateMachine = null;
};
SMBaseState.prototype.onGetDebugInfomation = function () {
return null;
};
SMBaseState.nullTransitionId = -1;
SMBaseState.nullStateId = -1;
return SMBaseState;
}());
sm.SMBaseState = SMBaseState;
})(sm = slib.sm || (slib.sm = {}));
})(slib || (slib = {}));
(function (slib) {
var sm;
(function (sm) {
/**
* 状态机控制类
*/
var XStateMachine = /** @class */ (function () {
function XStateMachine() {
this.m_states = [];
//#endregion
}
XStateMachine.prototype.getCurrentStateID = function () { return this.m_currentStateID; };
XStateMachine.prototype.performTransition = function (trans, data) {
if (data === void 0) { data = null; }
if (trans == sm.SMBaseState.nullTransitionId) {
return;
}
var id = this.m_currentState.getOutputState(trans);
if (id == sm.SMBaseState.nullStateId) {
return;
}
var prevState = this.m_currentState;
var newState = null;
for (var i = 0; i < this.m_states.length; i++) {
var element = this.m_states[i];
if (element.id == id) {
newState = element;
break;
}
}
if (newState == null) {
slib.logW("invalid state id:{0}", id);
}
prevState.onLeave();
this.m_oldStateID = this.m_currentStateID;
this.m_currentStateID = id;
this.m_currentState = newState;
if (this.m_currentState != null) {
this.m_currentState.lastStateId = this.m_oldStateID;
}
this.m_currentState.onEnter(data);
this.onStateChange();
if (this.m_currentStateID != this.m_currentState.id) {
slib.logW("set state failed");
}
};
XStateMachine.prototype.addState = function (s) {
if (this.m_states.length == 0) {
this.m_states.push(s);
this.m_currentState = s;
this.m_currentStateID = s.id;
return;
}
for (var i = 0; i < this.m_states.length; i++) {
var element = this.m_states[i];
if (element.id == s.id) {
return;
}
}
this.m_states.push(s);
};
XStateMachine.prototype.getStateById = function (id) {
var _state = null;
for (var i = 0; i < this.m_states.length; i++) {
var element = this.m_states[i];
if (element.id == id) {
_state = element;
break;
}
}
return _state;
};
XStateMachine.prototype.onStateChange = function () {
};
XStateMachine.prototype.destory = function () {
this.onDestory();
};
XStateMachine.prototype.onDestory = function () {
if (this.m_currentState != null) {
this.m_currentState.isDestroy = true;
this.m_currentState.onLeave();
this.m_currentState = null;
this.m_currentStateID = 0;
}
if (this.m_states != null) {
for (var i = 0; i < this.m_states.length; i++) {
var element = this.m_states[i];
element.onDestory();
}
}
};
return XStateMachine;
}());
sm.XStateMachine = XStateMachine;
})(sm = slib.sm || (slib.sm = {}));
})(slib || (slib = {}));