新增Time/String/Array/Keyboard/Random/Object/Texture辅助类

This commit is contained in:
yhh
2020-07-17 14:34:42 +08:00
parent e686ba64d7
commit c96e8b3a04
18 changed files with 7173 additions and 92 deletions

View File

@@ -0,0 +1,241 @@
class ArrayUtils {
/**
* 执行冒泡排序
* @param ary
* 算法参考 -- http://www.hiahia.org/datastructure/paixu/paixu8.3.1.1-1.htm
*/
public static bubbleSort(ary: number[]): void {
let isExchange: Boolean = false;
for (let i: number = 0; i < ary.length; i++) {
isExchange = false;
for (let j: number = ary.length - 1; j > i; j--) {
if (ary[j] < ary[j - 1]) {
let temp: number = ary[j];
ary[j] = ary[j - 1];
ary[j - 1] = temp;
isExchange = true;
}
}
if (!isExchange)
break;
}
}
/**
* 执行插入排序
* @param ary
*/
public static insertionSort(ary: number[]): void {
let len: number = ary.length;
for (let i: number = 1; i < len; i++) {
let val: number = ary[i];
for (var j: number = i; j > 0 && ary[j - 1] > val; j--) {
ary[j] = ary[j - 1];
}
ary[j] = val;
}
}
/**
* 执行二分搜索
* @param ary 搜索的数组(必须排序过)
* @param value 需要搜索的值
* @return 返回匹配结果的数组索引
*/
public static binarySearch(ary: number[], value: number): number {
let startIndex: number = 0;
let endIndex: number = ary.length;
let sub: number = (startIndex + endIndex) >> 1;
while (startIndex < endIndex) {
if (value <= ary[sub]) endIndex = sub;
else if (value >= ary[sub]) startIndex = sub + 1;
sub = (startIndex + endIndex) >> 1;
}
if (ary[startIndex] == value) return startIndex;
return -1;
}
/**
* 返回匹配项的索引
* @param ary
* @param num
* @return 返回匹配项的索引
*/
public static findElementIndex(ary: any[], num: any): any {
let len: number = ary.length;
for (let i: number = 0; i < len; ++i) {
if (ary[i] == num)
return i;
}
return null;
}
/**
* 返回数组中最大值的索引
* @param ary
* @return 返回数组中最大值的索引
*/
public static getMaxElementIndex(ary: number[]): number {
let matchIndex: number = 0;
let len: number = ary.length;
for (let j: number = 1; j < len; j++) {
if (ary[j] > ary[matchIndex])
matchIndex = j;
}
return matchIndex;
}
/**
* 返回数组中最小值的索引
* @param ary
* @return 返回数组中最小值的索引
*/
public static getMinElementIndex(ary: number[]): number {
let matchIndex: number = 0;
let len: number = ary.length;
for (let j: number = 1; j < len; j++) {
if (ary[j] < ary[matchIndex])
matchIndex = j;
}
return matchIndex;
}
/**
* 返回一个"唯一性"数组
* @param ary 需要唯一性的数组
* @return 唯一性的数组
* 比如: [1, 2, 2, 3, 4]
* 返回: [1, 2, 3, 4]
*/
public static getUniqueAry(ary: number[]): number[] {
let uAry: number[] = [];
let newAry: number[] = [];
let count = ary.length;
for (let i: number = 0; i < count; ++i) {
let value: number = ary[i];
if (uAry.indexOf(value) == -1) uAry.push(value);
}
count = uAry.length;
for (let i: number = count - 1; i >= 0; --i) {
newAry.unshift(uAry[i]);
}
return newAry;
}
/**
* 返回2个数组中不同的部分
* 比如数组A = [1, 2, 3, 4, 6]
* 数组B = [0, 2, 1, 3, 4]
* 返回[6, 0]
* @param aryA
* @param aryB
* @return
*/
public static getDifferAry(aryA: number[], aryB: number[]): number[] {
aryA = this.getUniqueAry(aryA);
aryB = this.getUniqueAry(aryB);
let ary: number[] = aryA.concat(aryB);
let uObj: Object = new Object();
let newAry: number[] = [];
let count: number = ary.length;
for (let j: number = 0; j < count; ++j) {
if (!uObj[ary[j]]) {
uObj[ary[j]] = new Object();
uObj[ary[j]].count = 0;
uObj[ary[j]].key = ary[j];
uObj[ary[j]].count++;
}
else {
if (uObj[ary[j]] instanceof Object) {
uObj[ary[j]].count++;
}
}
}
for (let i in uObj) {
if (uObj[i].count != 2) {
newAry.unshift(uObj[i].key);
}
}
return newAry;
}
/**
* 交换数组元素
* @param array 目标数组
* @param index1 交换后的索引
* @param index2 交换前的索引
*/
public static swap(array: any[], index1: number, index2: number): void {
let temp: any = array[index1];
array[index1] = array[index2];
array[index2] = temp;
}
/**
* 清除列表
* @param ary 列表
*/
public static clearList(ary: any[]): void {
if (!ary) return;
let length: number = ary.length;
for (let i: number = length - 1; i >= 0; i -= 1) {
ary.splice(i, 1);
}
}
/**
* 克隆一个数组
* @param ary 需要克隆的数组
* @return 克隆的数组
*/
public static cloneList(ary: any[]): any[] {
if (!ary) return null;
return ary.slice(0, ary.length);
}
/**
* 判断2个数组是否相同
* @param ary1 数组1
* @param ary2 数组2
* @return 是否相同
*/
public static equals(ary1: number[], ary2: number[]): Boolean {
if (ary1 == ary2) return true;
let length: number = ary1.length;
if (length != ary2.length) return false;
while (length--) {
if (ary1[length] != ary2[length])
return false;
}
return true;
}
/**
* 根据索引插入元素,索引和索引后的元素都向后移动一位
* @param index 插入索引
* @param value 插入的元素
* @return 插入的元素 未插入则返回空
*/
public static insert(ary: any[], index: number, value: any): any {
if (!ary) return null;
let length: number = ary.length;
if (index > length) index = length;
if (index < 0) index = 0;
if (index == length) ary.push(value); //插入最后
else if (index == 0) ary.unshift(value); //插入头
else {
for (let i: number = length - 1; i >= index; i -= 1) {
ary[i + 1] = ary[i];
}
ary[index] = value;
}
return value;
}
}

View File

@@ -1,5 +1,3 @@
declare class fui {}
class ContentManager {
protected loadedAssets: Map<string, any> = new Map<string, any>();
@@ -29,7 +27,7 @@ class ContentManager {
reject(err);
});
}
})
});
}
public dispose() {

View File

@@ -0,0 +1,239 @@
class KeyboardUtils {
/**
* 键盘事件类型
*/
public static TYPE_KEY_DOWN: number = 0;
public static TYPE_KEY_UP: number = 1;
//存放按下注册数据的字典
private static keyDownDict: Object;
//存放按起注册数据的字典
private static keyUpDict: Object;
/**
* 键值字符串枚举
*/
public static A: string = "A";
public static B: string = "B";
public static C: string = "C";
public static D: string = "D";
public static E: string = "E";
public static F: string = "F";
public static G: string = "G";
public static H: string = "H";
public static I: string = "I";
public static J: string = "J";
public static K: string = "K";
public static L: string = "L";
public static M: string = "M";
public static N: string = "N";
public static O: string = "O";
public static P: string = "P";
public static Q: string = "Q";
public static R: string = "R";
public static S: string = "S";
public static T: string = "T";
public static U: string = "U";
public static V: string = "V";
public static W: string = "W";
public static X: string = "X";
public static Y: string = "Y";
public static Z: string = "Z";
public static ESC: string = "Esc";
public static F1: string = "F1";
public static F2: string = "F2";
public static F3: string = "F3";
public static F4: string = "F4";
public static F5: string = "F5";
public static F6: string = "F6";
public static F7: string = "F7";
public static F8: string = "F8";
public static F9: string = "F9";
public static F10: string = "F10";
public static F11: string = "F11";
public static F12: string = "F12";
public static NUM_1: string = "1";
public static NUM_2: string = "2";
public static NUM_3: string = "3";
public static NUM_4: string = "4";
public static NUM_5: string = "5";
public static NUM_6: string = "6";
public static NUM_7: string = "7";
public static NUM_8: string = "8";
public static NUM_9: string = "9";
public static NUM_0: string = "0";
public static TAB: string = "Tab";
public static CTRL: string = "Ctrl";
public static ALT: string = "Alt";
public static SHIFT: string = "Shift";
public static CAPS_LOCK: string = "Caps Lock";
public static ENTER: string = "Enter";
public static SPACE: string = "Space";
public static BACK_SPACE: string = "Back Space";
public static INSERT: string = "Insert";
public static DELETE: string = "Page Down";
public static HOME: string = "Home";
public static END: string = "Page Down";
public static PAGE_UP: string = "Page Up";
public static PAGE_DOWN: string = "Page Down";
public static LEFT: string = "Left";
public static RIGHT: string = "Right";
public static UP: string = "Up";
public static DOWN: string = "Down";
public static PAUSE_BREAK: string = "Pause Break";
public static NUM_LOCK: string = "Num Lock";
public static SCROLL_LOCK: string = "Scroll Lock";
public static WINDOWS: string = "Windows";
public static init(): void {
this.keyDownDict = {};
this.keyUpDict = {};
document.addEventListener("keydown", this.onKeyDonwHander);
document.addEventListener("keyup", this.onKeyUpHander);
}
private static onKeyDonwHander(event: KeyboardEvent): void {
if (!this.keyDownDict) return;
var key: string = this.keyCodeToString(event.keyCode);
var o: Object = this.keyDownDict[key];
if (o) {
var fun: Function = o["fun"];
var thisObj: any = o["thisObj"];
var args: any = o["args"];
fun.apply(thisObj, args);
}
}
private static onKeyUpHander(event: KeyboardEvent): void {
if (!this.keyUpDict) return;
var key: string = this.keyCodeToString(event.keyCode);
var o: Object = this.keyUpDict[key];
if (o) {
var fun: Function = o["fun"];
var thisObj: any = o["thisObj"];
var args: any = o["args"];
fun.apply(thisObj, args);
}
}
/**
* 注册按键
* @param key 键值
* @param fun 回调方法
* @param type 按键类型 TYPE_KEY_DOWN、TYPE_KEY_UP
*/
public static registerKey(key: string, fun: Function, thisObj: any, type: number = 0, ...args): void {
var keyDict: Object = type ? this.keyUpDict : this.keyDownDict;
keyDict[key] = { "fun": fun, args: args, "thisObj": thisObj };
}
/**
* 注销按键
* @param key 键值
* @param type 注销的类型
*/
public static unregisterKey(key: string, type: number = 0): void {
var keyDict: Object = type ? this.keyUpDict : this.keyDownDict;
delete keyDict[key];
}
/**
* 根据keyCode或charCode获取相应的字符串代号
* @param keyCode
* @return 键盘所指字符串代号
*/
private static keyCodeToString(keyCode: number): string {
switch (keyCode) {
case 8:
return this.BACK_SPACE;
case 9:
return this.TAB;
case 13:
return this.ENTER;
case 16:
return this.SHIFT;
case 17:
return this.CTRL;
case 19:
return this.PAUSE_BREAK;
case 20:
return this.CAPS_LOCK;
case 27:
return this.ESC;
case 32:
return this.SPACE;
case 33:
return this.PAGE_UP;
case 34:
return this.PAGE_DOWN;
case 35:
return this.END;
case 36:
return this.HOME;
case 37:
return this.LEFT;
case 38:
return this.UP;
case 39:
return this.RIGHT;
case 40:
return this.DOWN;
case 45:
return this.INSERT;
case 46:
return this.DELETE;
case 91:
return this.WINDOWS;
case 112:
return this.F1;
case 113:
return this.F2;
case 114:
return this.F3;
case 115:
return this.F4;
case 116:
return this.F5;
case 117:
return this.F6;
case 118:
return this.F7;
case 119:
return this.F8;
case 120:
return this.F9;
case 122:
return this.F11;
case 123:
return this.F12;
case 144:
return this.NUM_LOCK;
case 145:
return this.SCROLL_LOCK;
default:
return String.fromCharCode(keyCode);
}
}
/**
* 销毁方法
*/
public static destroy(): void {
this.keyDownDict = null;
this.keyUpDict = null;
document.removeEventListener("keydown", this.onKeyDonwHander);
document.removeEventListener("keyup", this.onKeyUpHander);
}
}

View File

@@ -0,0 +1,134 @@
class RandomUtils {
/**
* 在 start 与 stop之间取一个随机整数可以用step指定间隔 但不包括较大的端点start与stop较大的一个
* 如
* this.randrange(1, 10, 3)
* 则返回的可能是 1 或 4 或 7 , 注意 这里面不会返回10因为是10是大端点
*
* @param start
* @param stop
* @param step
* @return 假设 start < stop, [start, stop) 区间内的随机整数
*
*/
public static randrange(start: number, stop: number, step: number = 1): number {
if (step == 0)
throw new Error('step 不能为 0');
let width: number = stop - start;
if (width == 0)
throw new Error('没有可用的范围(' + start + ',' + stop + ')');
if (width < 0)
width = start - stop;
let n: number = Math.floor((width + step - 1) / step);
return Math.floor(this.random() * n) * step + Math.min(start, stop);
}
/**
* 返回a 到 b直间的随机整数包括 a 和 b
* @param a
* @param b
* @return [a, b] 直接的随机整数
*
*/
public static randint(a: number, b: number): number {
a = Math.floor(a);
b = Math.floor(b);
if (a > b)
a++;
else
b++;
return this.randrange(a, b);
}
/**
* 返回 a - b之间的随机数不包括 Math.max(a, b)
* @param a
* @param b
* @return 假设 a < b, [a, b)
*/
public static randnum(a: number, b: number): number {
return this.random() * (b - a) + a;
}
/**
* 打乱数组
* @param array
* @return
*/
public static shuffle(array: any[]): any[] {
array.sort(this._randomCompare);
return array;
}
private static _randomCompare(a: Object, b: Object): number {
return (this.random() > .5) ? 1 : -1;
}
/**
* 从序列中随机取一个元素
* @param sequence 可以是 数组、 vector等只要是有length属性并且可以用数字索引获取元素的对象
* 另外,字符串也是允许的。
* @return 序列中的某一个元素
*
*/
public static choice(sequence: any): any {
if (!sequence.hasOwnProperty("length"))
throw new Error('无法对此对象执行此操作');
let index: number = Math.floor(this.random() * sequence.length);
if (sequence instanceof String)
return String(sequence).charAt(index);
else
return sequence[index];
}
/**
* 对列表中的元素进行随机采æ ?
* <pre>
* this.sample([1, 2, 3, 4, 5], 3) // Choose 3 elements
* [4, 1, 5]
* </pre>
* @param sequence
* @param num
* @return
*
*/
public static sample(sequence: any[], num: number): any[] {
let len: number = sequence.length;
if (num <= 0 || len < num)
throw new Error("采样数量不够");
let selected: any[] = [];
let indices: any[] = [];
for (let i: number = 0; i < num; i++) {
let index: number = Math.floor(this.random() * len);
while (indices.indexOf(index) >= 0)
index = Math.floor(this.random() * len);
selected.push(sequence[index]);
indices.push(index);
}
return selected;
}
/**
* 返回 0.0 - 1.0 之间的随机数,等同于 Math.random()
* @return Math.random()
*
*/
public static random(): number {
return Math.random();
}
/**
* 计算概率
* @param chance 概率
* @return
*/
public static boolean(chance: number = .5): boolean {
return (this.random() < chance) ? true : false;
}
}