新增事件发送接收器

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

@@ -576,7 +576,7 @@ declare class Rectangle {
declare class Vector2 {
x: number;
y: number;
constructor(x: number, y?: number);
constructor(x?: number, y?: number);
static add(value1: Vector2, value2: Vector2): Vector2;
static divide(value1: Vector2, value2: Vector2): Vector2;
static multiply(value1: Vector2, value2: Vector2): Vector2;
@@ -675,18 +675,6 @@ declare class ShapeCollisions {
static closestPointOnLine(lineA: Vector2, lineB: Vector2, closestTo: Vector2): Vector2;
static pointToPoly(point: Vector2, poly: Polygon): CollisionResult;
}
declare class Particle {
position: Vector2;
lastPosition: Vector2;
isPinned: boolean;
pinnedPosition: any;
acceleration: Vector2;
mass: number;
radius: number;
collidesWithColliders: boolean;
constructor(position: Vector2);
applyForce(force: Vector2): void;
}
declare class SpatialHash {
gridBounds: Rectangle;
private _raycastParser;
@@ -714,6 +702,13 @@ declare class NumberDictionary {
getAllObjects(): Collider[];
clear(): void;
}
declare class Emitter<T> {
private _messageTable;
constructor();
addObserver(eventType: T, handler: Function): void;
removeObserver(eventType: T, handler: Function): void;
emit(eventType: T, data: any): void;
}
declare class Triangulator {
triangleIndices: number[];
private _triPrev;

View File

@@ -2719,8 +2719,8 @@ var Vector2 = (function () {
function Vector2(x, y) {
this.x = 0;
this.y = 0;
this.x = x;
this.y = y ? y : x;
this.x = x ? x : 0;
this.y = y ? y : this.x;
}
Vector2.add = function (value1, value2) {
var result = new Vector2(0, 0);
@@ -3026,7 +3026,7 @@ var Polygon = (function (_super) {
var verts = new Vector2[vertCount];
for (var i = 0; i < vertCount; i++) {
var 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;
};
@@ -3086,6 +3086,12 @@ var ShapeCollisions = (function () {
ShapeCollisions.polygonToPolygon = function (first, second) {
var result = new CollisionResult();
var isIntersecting = true;
var firstEdges = first.edgeNormals;
var secondEdges = second.edgeNormals;
var minIntervalDistance = Number.POSITIVE_INFINITY;
var translationAxis = new Vector2();
var polygonOffset = Vector2.subtract(first.position, second.position);
var axis;
};
ShapeCollisions.circleToPolygon = function (circle, polygon) {
var result = new CollisionResult();
@@ -3156,22 +3162,6 @@ var ShapeCollisions = (function () {
};
return ShapeCollisions;
}());
var Particle = (function () {
function Particle(position) {
this.position = new Vector2(0, 0);
this.lastPosition = new Vector2(0, 0);
this.acceleration = new Vector2(0, 0);
this.mass = 1;
this.radius = 0;
this.collidesWithColliders = true;
this.position = position;
this.lastPosition = position;
}
Particle.prototype.applyForce = function (force) {
this.acceleration = Vector2.add(this.acceleration, new Vector2(force.x / this.mass, force.y / this.mass));
};
return Particle;
}());
var SpatialHash = (function () {
function SpatialHash(cellSize) {
if (cellSize === void 0) { cellSize = 100; }
@@ -3284,6 +3274,30 @@ var NumberDictionary = (function () {
};
return NumberDictionary;
}());
var Emitter = (function () {
function Emitter() {
this._messageTable = new Map();
}
Emitter.prototype.addObserver = function (eventType, handler) {
var list = this._messageTable.get(eventType);
if (!list) {
list = [];
this._messageTable.set(eventType, list);
}
if (list.contains(handler))
console.warn("您试图添加相同的观察者两次");
list.push(handler);
};
Emitter.prototype.removeObserver = function (eventType, handler) {
this._messageTable.get(eventType).remove(handler);
};
Emitter.prototype.emit = function (eventType, data) {
var list = this._messageTable.get(eventType);
for (var i = list.length - 1; i >= 0; i--)
list[i](data);
};
return Emitter;
}());
var Triangulator = (function () {
function Triangulator() {
this.triangleIndices = [];

File diff suppressed because one or more lines are too long