新增事件发送接收器

This commit is contained in:
yhh
2020-06-15 12:16:23 +08:00
parent 16892eb7af
commit c3120d791f
15 changed files with 141 additions and 123 deletions

View File

@@ -8,9 +8,9 @@ class Vector2 {
* @param x 二维空间中的x坐标
* @param y 二维空间的y坐标
*/
constructor(x: number, y?: number){
this.x = x;
this.y = y ? y : x;
constructor(x? : number, y?: number){
this.x = x ? x : 0;
this.y = y ? y : this.x;
}
public static add(value1: Vector2, value2: Vector2){

View File

@@ -120,7 +120,7 @@ class Polygon extends Shape {
for (let i = 0; i < vertCount; i++) {
let a = 2 * Math.PI * (i / vertCount);
verts[i] = new Vector2(Math.cos(a), Math.sign(a) * radius);
verts[i] = new Vector2(Math.cos(a), Math.sin(a) * radius);
}
return verts;

View File

@@ -2,7 +2,14 @@ class ShapeCollisions {
public static polygonToPolygon(first: Polygon, second: Polygon){
let result = new CollisionResult();
let isIntersecting = true;
// let firstEdges = first.ed
let firstEdges = first.edgeNormals;
let secondEdges = second.edgeNormals;
let minIntervalDistance = Number.POSITIVE_INFINITY;
let translationAxis = new Vector2();
let polygonOffset = Vector2.subtract(first.position, second.position);
let axis: Vector2;
}
public static circleToPolygon(circle: Circle, polygon: Polygon){

View File

@@ -1,19 +0,0 @@
class Particle {
public position: Vector2 = new Vector2(0, 0);
public lastPosition: Vector2 = new Vector2(0, 0);
public isPinned: boolean;
public pinnedPosition;
public acceleration: Vector2 = new Vector2(0, 0);
public mass: number = 1;
public radius: number = 0;
public collidesWithColliders = true;
constructor(position: Vector2){
this.position = position;
this.lastPosition = position;
}
public applyForce(force: Vector2){
this.acceleration = Vector2.add(this.acceleration, new Vector2(force.x / this.mass, force.y / this.mass));
}
}

View File

@@ -0,0 +1,29 @@
class Emitter<T> {
private _messageTable: Map<T, Function[]>;
constructor(){
this._messageTable = new Map<T, Function[]>();
}
public addObserver(eventType: T, handler: Function){
let list: Function[] = this._messageTable.get(eventType);
if (!list){
list = [];
this._messageTable.set(eventType, list);
}
if (list.contains(handler))
console.warn("您试图添加相同的观察者两次");
list.push(handler);
}
public removeObserver(eventType: T, handler: Function){
this._messageTable.get(eventType).remove(handler);
}
public emit(eventType: T, data: any){
let list: Function[] = this._messageTable.get(eventType);
for (let i = list.length - 1; i >= 0; i --)
list[i](data);
}
}