Tool.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. import { _IProperty, _ITime } from "../Data/CommonDataType";
  2. import Tool_Tween from "./Tool/Tool_Tween";
  3. import Tool2D from "./Tool2D";
  4. import Tool3D from "./Tool3D";
  5. /**
  6. * 工具类
  7. */
  8. export default class Tool {
  9. /**缓动动画工具类 */
  10. private static tween: Tool_Tween;
  11. /**2D工具类 */
  12. private static tool2D: Tool2D;
  13. /**3D工具类 */
  14. private static tool3D: Tool3D;
  15. private constructor () {
  16. }
  17. /**缓动动画工具 */
  18. public static get Tween (): Tool_Tween {
  19. if (this.tween == null) {
  20. this.tween = new Tool_Tween();
  21. }
  22. return this.tween;
  23. }
  24. /**2D工具 */
  25. public static get Tool2D (): Tool2D {
  26. if (this.tool2D == null) {
  27. this.tool2D = new Tool2D();
  28. }
  29. return this.tool2D;
  30. }
  31. /**3D工具 */
  32. public static get Tool3D (): Tool3D {
  33. if (this.tool3D == null) {
  34. this.tool3D = new Tool3D();
  35. }
  36. return this.tool3D;
  37. }
  38. //////////////////////////////////////////////// Log ////////////////////////////////////////////////
  39. public static isLog: boolean = true;
  40. public static log (...args): void {
  41. if (this.isLog == true) {
  42. console.log(...args);
  43. }
  44. }
  45. public static error (...args): void {
  46. if (this.isLog == true) {
  47. console.error(...args);
  48. }
  49. }
  50. //////////////////////////////////////////////// Data ////////////////////////////////////////////////
  51. /**数据比对覆盖,只对inData存在的值进行覆盖 */
  52. public static dataProofreadCover (inData: any, outData: any): any {
  53. if (inData != null && outData != null) {
  54. for (let index in inData) {
  55. if (outData[index] !== undefined) {
  56. if (typeof inData[index] == "object" && outData[index] != null) {
  57. this.dataProofreadCover(inData[index], outData[index])
  58. }
  59. else {
  60. inData[index] = outData[index];
  61. }
  62. }
  63. }
  64. }
  65. return inData;
  66. }
  67. /**数据补充覆盖,只对inData不存在的值进行补充 */
  68. public static dataSupplementCover (inData: any, outData: any): any {
  69. if (inData != null && outData != null) {
  70. for (let index in outData) {
  71. if (inData[index] == undefined) {
  72. if (typeof inData[index] == "object" && outData[index] != null) {
  73. inData[index] = Array.isArray(outData[index]) ? [] : {};
  74. this.dataSupplementCover(inData[index], outData[index])
  75. }
  76. else {
  77. inData[index] = outData[index];
  78. }
  79. }
  80. }
  81. }
  82. return inData;
  83. }
  84. /**数据完全覆盖,对inData进行完全覆盖 */
  85. public static dataAllCover (inData: any, outData: any): any {
  86. if (inData != null && outData != null) {
  87. for (let index in outData) {
  88. if (typeof outData[index] == "object" && outData[index] != null) {
  89. inData[index] = Array.isArray(outData[index]) ? [] : {};
  90. this.dataAllCover(inData[index], outData[index])
  91. }
  92. else {
  93. inData[index] = outData[index];
  94. }
  95. }
  96. }
  97. return inData;
  98. }
  99. /**寻找key值 */
  100. public static findKey (data: Object, value: any, compare = (a: any, b: any) => a === b): any {
  101. return Object.keys(data).find(k => compare(data[k], value));
  102. }
  103. //////////////////////////////////////////////// Math ////////////////////////////////////////////////
  104. /**随机数 */
  105. public static random (number: number): number {
  106. return Math.round(Math.random()*number+0.4-0.4);
  107. }
  108. /**随机数组 */
  109. public static randomArray (sort: Array<number>, count: number = 10000): Array<number> {
  110. let func = (sort: Array<number>) => {
  111. let temp = [];
  112. let length = sort.length;
  113. for (let j = 0; j < length; j++) {
  114. let random = Math.round(Math.random()*(sort.length-1));
  115. temp[j] = sort[random];
  116. sort.splice(random, 1);
  117. }
  118. sort = temp;
  119. return sort;
  120. }
  121. for (let i = 0; i < count; i++) {
  122. sort = func(sort);
  123. sort = func(sort);
  124. }
  125. return sort;
  126. }
  127. /**数值限制 */
  128. public static clamp (min: number, max: number, data: number): number {
  129. if (data < min) {
  130. data = min;
  131. }
  132. else if (data > max) {
  133. data = max;
  134. }
  135. return data;
  136. }
  137. /**数值限制 */
  138. public static clampProperty (inData: _IProperty, outData: _IProperty, data: _IProperty, isClamp: boolean = true): _IProperty {
  139. if ((inData.max != NaN && outData.max != NaN) && (inData.max != null && outData.max != null)) {
  140. data.max = inData.max + outData.max;
  141. }
  142. if ((inData.min != NaN && outData.min != NaN) && (inData.min != null && outData.min != null)) {
  143. data.min = inData.min + outData.min;
  144. }
  145. if ((inData.value != NaN && outData.value != NaN) && (inData.value != null && outData.value != null)) {
  146. data.value = inData.value + outData.value;
  147. if (isClamp == true) {
  148. data.value = this.clamp(data.min, data.max, data.value);
  149. }
  150. }
  151. return data;
  152. }
  153. /**修改属性 */
  154. public static setPropertyValue (property: _IProperty, value: number): _IProperty {
  155. property.value = this.clamp(property.min, property.max, property.value+value);
  156. return property;
  157. }
  158. /**科学计数法 */
  159. public static formatter (value: number, fix?: number): string {
  160. let bits = ["K","M","B","T","aa","ab","ac","ad","ae","af","bb","bc","bd"];
  161. if (value >= 1000) {
  162. for (let i = bits.length; i > 0; i--) {
  163. if (value >= Math.pow(1000,i)) {
  164. let j = (value/Math.pow(1000,i)).toFixed(fix);
  165. j += bits[i-1];
  166. return `${j}`;
  167. }
  168. }
  169. }
  170. return value+"";
  171. }
  172. /**获得夹角 */
  173. public static getAngle (px: number, py: number, mx: number, my: number): number {
  174. var x = Math.abs(px-mx);
  175. var y = Math.abs(py-my);
  176. var z = Math.sqrt(Math.pow(x,2)+Math.pow(y,2));
  177. var cos = y/z;
  178. var radina = Math.acos(cos);//用反三角函数求弧度
  179. var angle = Math.floor(180/(Math.PI/radina));//将弧度转换成角度
  180. if(mx>px&&my>py){//鼠标在第四象限
  181. angle = 180 - angle;
  182. }
  183. if(mx==px&&my>py){//鼠标在y轴负方向上
  184. angle = 180;
  185. }
  186. if(mx>px&&my==py){//鼠标在x轴正方向上
  187. angle = 90;
  188. }
  189. if(mx<px&&my>py){//鼠标在第三象限
  190. angle = 180+angle;
  191. }
  192. if(mx<px&&my==py){//鼠标在x轴负方向
  193. angle = 270;
  194. }
  195. if(mx<px&&my<py){//鼠标在第二象限
  196. angle = 360 - angle;
  197. }
  198. return angle;
  199. }
  200. /**角度转向量 */
  201. public static getVector (angle: number): cc.Vec2 {
  202. // 将传入的角度转为弧度
  203. let radian = Math.PI / 180 * angle;
  204. // 邻边 / 斜边
  205. let cos = Math.cos(radian);
  206. // 对边 / 斜边
  207. let sin = Math.sin(radian);
  208. // 结合在一起并归一化
  209. let vec = cc.v2(cos, sin).normalize();
  210. return vec;
  211. }
  212. /**贝塞尔曲线 */
  213. public static bezier3 (a1, a2, a3, t) {
  214. return ((1 - t) * (1 - t)) * a1 + 2 * t * (1 - t) * a2 + t * t * a3;
  215. }
  216. //////////////////////////////////////////////// String ////////////////////////////////////////////////
  217. /**是否有中文 */
  218. public static isChinese (string: string): boolean {
  219. let reg= /[\u4e00-\u9fa5]+/;
  220. if (!reg.test(string)) {
  221. return false
  222. }
  223. return true;
  224. }
  225. /**将数字转换为汉字 */
  226. public static convertNumberToChinese (data: number): string {
  227. let Chinese: string = "";
  228. let list_1: Array<string> = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
  229. let list_2: Array<string> = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
  230. let list_3: Array<string> = ["十", "百", "千", "万", "十万", "百万", "千万", "亿"];
  231. let temp: Array<string> = (""+data).split("").reverse();
  232. let zore: boolean = false;
  233. for (let i: number = 0; i < temp.length; i++) {
  234. let index: number = list_1.indexOf(temp[i]);
  235. if (i == 0) {
  236. if (index != 0) {
  237. Chinese = list_2[index];
  238. }
  239. }
  240. else {
  241. if (index != 0) {
  242. Chinese = list_3[i-1] + Chinese;
  243. Chinese = list_2[index] + Chinese;
  244. zore = false;
  245. }
  246. else if (zore == false) {
  247. Chinese = list_2[index] + Chinese;
  248. zore = true;
  249. }
  250. }
  251. }
  252. if (Chinese.substring(0, 2) == "一十") {
  253. Chinese = Chinese.replace("一十", "十");
  254. }
  255. if (Chinese[Chinese.length-1] == list_2[0]) {
  256. Chinese = Chinese.substring(0, Chinese.length-1);
  257. }
  258. return Chinese;
  259. }
  260. /**将数字转换为真实日期 */
  261. public static converNumberToDate (time: number): string {
  262. let _time: _ITime = this.converNumberToTime(time);
  263. let _month = _time.month < 10?('0'+_time.month):_time.month;
  264. let _day = _time.day < 10?('0'+_time.day):_time.day;
  265. let _hour = (_time.hour) < 10?('0'+_time.hour):_time.hour;
  266. let _minute = (_time.minute) < 10?('0'+_time.minute):_time.minute;
  267. let _second = (_time.second) < 10?('0'+_time.second):_time.second;
  268. return _time.year+"-"+_month+"-"+_day+" "+_hour+":"+_minute+":"+_second;
  269. }
  270. /**将数字转换为时间 */
  271. public static converNumberToTime (time: number): _ITime {
  272. let d = new Date(Math.round(time)*1000);
  273. let year = d.getFullYear();
  274. let month = (d.getMonth() + 1);
  275. let day = d.getDate();
  276. let hour = d.getHours();
  277. let minute = d.getMinutes();
  278. let seconds = d.getSeconds();
  279. return {
  280. year: year,
  281. month: month,
  282. day: day,
  283. hour: hour,
  284. minute: minute,
  285. second: seconds,
  286. };
  287. }
  288. /**将数字转换为时间文本 */
  289. public static converNumberToTimeText (time: number, isChinese: boolean = false, isClearZero: boolean = false): string {
  290. time = time<0? 0:time;
  291. let t = Math.ceil(time);
  292. let h = Math.floor(t / 3600);
  293. let m = Math.floor(t % 3600 / 60);
  294. let s = Math.floor(t % 60);
  295. let hs: string = (h>=10)? h+"":("0"+h);
  296. let ms: string = (m>=10)? m+"":("0"+m);
  297. let ss: string = (s>=10)? s+"":("0"+s);
  298. let str: string = "";
  299. if (h > 0) {
  300. if (isClearZero == true) {
  301. if (hs.charAt(0) == "0") {
  302. hs = hs.substring(1, hs.length);;
  303. }
  304. }
  305. if (isChinese == true) {
  306. str = hs + "时" + ms + "分" + ss + "秒";
  307. }
  308. else {
  309. str = hs + ":" + ms + ":" + ss;
  310. }
  311. }
  312. else {
  313. if (isClearZero == true) {
  314. if (ms.charAt(0) == "0") {
  315. ms = ms.substring(1, ms.length);
  316. }
  317. }
  318. if (isChinese == true) {
  319. str = ms + "分" + ss +"秒";
  320. }
  321. else {
  322. str = ms + ":" + ss;
  323. }
  324. }
  325. return str;
  326. }
  327. }