新增arcadeRigidbody组件, 可以用于简单的,街机风格的物理学

This commit is contained in:
yhh
2020-12-07 11:48:42 +08:00
parent 2a0d4ef4dd
commit bddae046a0
12 changed files with 835 additions and 82 deletions

View File

@@ -1,27 +1,12 @@
module es {
/**
* 用于包装事件的一个小类
*/
export class FuncPack {
/** 函数 */
public func: Function;
/** 上下文 */
public context: any;
constructor(func: Function, context: any) {
this.func = func;
this.context = context;
}
}
/**
* 用于事件管理
*/
export class Emitter<T> {
private _messageTable: Map<T, FuncPack[]>;
private _messageTable: Map<T, Function[]>;
constructor() {
this._messageTable = new Map<T, FuncPack[]>();
this._messageTable = new Map<T, Function[]>();
}
/**
@@ -31,15 +16,17 @@ module es {
* @param context 监听上下文
*/
public addObserver(eventType: T, handler: Function, context: any) {
let list: FuncPack[] = this._messageTable.get(eventType);
handler.bind(context);
let list: Function[] = this._messageTable.get(eventType);
if (!list) {
list = [];
this._messageTable.set(eventType, list);
}
if (list.findIndex(funcPack => funcPack.func == handler) != -1)
if (new linq.List(list).contains(handler))
console.warn("您试图添加相同的观察者两次");
list.push(new FuncPack(handler, context));
list.push(handler);
}
/**
@@ -49,9 +36,7 @@ module es {
*/
public removeObserver(eventType: T, handler: Function) {
let messageData = this._messageTable.get(eventType);
let index = messageData.findIndex(data => data.func == handler);
if (index != -1)
new linq.List(messageData).removeAt(index);
new linq.List(messageData).remove(handler);
}
/**
@@ -60,10 +45,10 @@ module es {
* @param data 事件数据
*/
public emit(eventType: T, data?: any) {
let list: FuncPack[] = this._messageTable.get(eventType);
let list: Function[] = this._messageTable.get(eventType);
if (list) {
for (let i = list.length - 1; i >= 0; i--)
list[i].func.call(list[i].context, data);
list[i](data);
}
}
}