新增fastList、注释完善

This commit is contained in:
yhh
2020-10-27 18:08:49 +08:00
parent 0e7b0bc45c
commit fc6a8a0803
19 changed files with 595 additions and 205 deletions
+51 -8
View File
@@ -102,7 +102,7 @@ declare module es {
} }
} }
declare module es { declare module es {
class Vector2 { class Vector2 implements IEquatable<Vector2> {
x: number; x: number;
y: number; y: number;
constructor(x?: number, y?: number); constructor(x?: number, y?: number);
@@ -131,7 +131,7 @@ declare module es {
length(): number; length(): number;
lengthSquared(): number; lengthSquared(): number;
round(): Vector2; round(): Vector2;
equals(other: Vector2): boolean; equals(other: Vector2 | object): boolean;
} }
} }
declare module es { declare module es {
@@ -483,7 +483,7 @@ declare module es {
} }
} }
declare module es { declare module es {
class CameraInset { interface CameraInset {
left: number; left: number;
right: number; right: number;
top: number; top: number;
@@ -578,7 +578,12 @@ declare module es {
} }
} }
declare module es { declare module es {
class IUpdatableComparer { interface IUpdatable {
enabled: boolean;
updateOrder: number;
update(): any;
}
class IUpdatableComparer implements IComparer<IUpdatable> {
compare(a: Component, b: Component): number; compare(a: Component, b: Component): number;
} }
} }
@@ -982,7 +987,7 @@ declare module es {
class ComponentList { class ComponentList {
static compareUpdatableOrder: IUpdatableComparer; static compareUpdatableOrder: IUpdatableComparer;
_entity: Entity; _entity: Entity;
_components: Component[]; _components: FastList<Component>;
_componentsToAdd: Component[]; _componentsToAdd: Component[];
_componentsToRemove: Component[]; _componentsToRemove: Component[];
_tempBufferList: Component[]; _tempBufferList: Component[];
@@ -1022,7 +1027,7 @@ declare module es {
_entitiesToRemove: Entity[]; _entitiesToRemove: Entity[];
_isEntityListUnsorted: boolean; _isEntityListUnsorted: boolean;
_entityDict: Map<number, Entity[]>; _entityDict: Map<number, Entity[]>;
_unsortedTags: number[]; _unsortedTags: Set<number>;
_addToSceneEntityList: Entity[]; _addToSceneEntityList: Entity[];
frameAllocate: boolean; frameAllocate: boolean;
maxAllocate: number; maxAllocate: number;
@@ -1050,7 +1055,7 @@ declare module es {
} }
declare module es { declare module es {
class EntityProcessorList { class EntityProcessorList {
private _processors; protected _processors: EntitySystem[];
add(processor: EntitySystem): void; add(processor: EntitySystem): void;
remove(processor: EntitySystem): void; remove(processor: EntitySystem): void;
onComponentAdded(entity: Entity): void; onComponentAdded(entity: Entity): void;
@@ -1066,6 +1071,22 @@ declare module es {
protected removeFromProcessors(entity: Entity): void; protected removeFromProcessors(entity: Entity): void;
} }
} }
declare module es {
class FastList<T> {
buffer: T[];
length: number;
constructor(size?: number);
clear(): void;
reset(): void;
add(item: T): void;
remove(item: T): void;
removeAt(index: number): void;
contains(item: T): boolean;
ensureCapacity(additionalItemCount?: number): void;
addRange(array: T[]): void;
sort(comparer: IComparer<T>): void;
}
}
declare module es { declare module es {
class Matcher { class Matcher {
protected allSet: BitSet; protected allSet: BitSet;
@@ -2149,6 +2170,13 @@ declare module es {
static repeat<T>(element: T, count: number): any[]; static repeat<T>(element: T, count: number): any[];
} }
} }
declare module es {
class EqualityComparer<T> implements IEqualityComparer {
static default<T>(): EqualityComparer<T>;
protected constructor();
equals(x: T, y: T): boolean;
}
}
declare module es { declare module es {
class GlobalManager { class GlobalManager {
_enabled: boolean; _enabled: boolean;
@@ -2159,6 +2187,21 @@ declare module es {
update(): void; update(): void;
} }
} }
declare module es {
interface IComparer<T> {
compare(x: T, y: T): number;
}
}
declare module es {
interface IEqualityComparer {
equals(x: any, y: any): boolean;
}
}
declare module es {
interface IEquatable<T> {
equals(other: T): boolean;
}
}
declare module es { declare module es {
class ListPool { class ListPool {
private static readonly _objectQueue; private static readonly _objectQueue;
@@ -2170,7 +2213,7 @@ declare module es {
} }
} }
declare module es { declare module es {
class Pair<T> { class Pair<T> implements IEquatable<Pair<T>> {
first: T; first: T;
second: T; second: T;
constructor(first: T, second: T); constructor(first: T, second: T);
+120 -54
View File
@@ -733,10 +733,9 @@ var es;
return Math.acos(es.MathHelper.clamp(Vector2.dot(from, to), -1, 1)) * es.MathHelper.Rad2Deg; return Math.acos(es.MathHelper.clamp(Vector2.dot(from, to), -1, 1)) * es.MathHelper.Rad2Deg;
}; };
Vector2.negate = function (value) { Vector2.negate = function (value) {
var result = new Vector2(); value.x = -value.x;
result.x = -value.x; value.y = -value.y;
result.y = -value.y; return value;
return result;
}; };
Vector2.prototype.add = function (value) { Vector2.prototype.add = function (value) {
this.x += value.x; this.x += value.x;
@@ -773,7 +772,10 @@ var es;
return new Vector2(Math.round(this.x), Math.round(this.y)); return new Vector2(Math.round(this.x), Math.round(this.y));
}; };
Vector2.prototype.equals = function (other) { Vector2.prototype.equals = function (other) {
return other.x == this.x && other.y == this.y; if (other instanceof Vector2) {
return other.x == this.x && other.y == this.y;
}
return false;
}; };
return Vector2; return Vector2;
}()); }());
@@ -1056,7 +1058,6 @@ var es;
return __generator(this, function (_a) { return __generator(this, function (_a) {
switch (_a.label) { switch (_a.label) {
case 0: case 0:
this.startDebugDraw(es.Time.deltaTime);
if (!this._sceneTransition) return [3, 4]; if (!this._sceneTransition) return [3, 4];
this._sceneTransition.preRender(); this._sceneTransition.preRender();
if (!(this._scene && !this._sceneTransition.hasPreviousSceneRender)) return [3, 2]; if (!(this._scene && !this._sceneTransition.hasPreviousSceneRender)) return [3, 2];
@@ -1083,9 +1084,7 @@ var es;
this._scene.postRender(); this._scene.postRender();
} }
_a.label = 5; _a.label = 5;
case 5: case 5: return [2];
this.endDebugDraw();
return [2];
} }
}); });
}); });
@@ -2287,21 +2286,11 @@ var es;
})(es || (es = {})); })(es || (es = {}));
var es; var es;
(function (es) { (function (es) {
var CameraInset = (function () {
function CameraInset() {
this.left = 0;
this.right = 0;
this.top = 0;
this.bottom = 0;
}
return CameraInset;
}());
es.CameraInset = CameraInset;
var Camera = (function (_super) { var Camera = (function (_super) {
__extends(Camera, _super); __extends(Camera, _super);
function Camera() { function Camera() {
var _this = _super.call(this) || this; var _this = _super.call(this) || this;
_this._inset = new CameraInset(); _this._inset = { left: 0, right: 0, top: 0, bottom: 0 };
_this._areMatrixedDirty = true; _this._areMatrixedDirty = true;
_this._areBoundsDirty = true; _this._areBoundsDirty = true;
_this._isProjectionMatrixDirty = true; _this._isProjectionMatrixDirty = true;
@@ -2444,11 +2433,7 @@ var es;
configurable: true configurable: true
}); });
Camera.prototype.setInset = function (left, right, top, bottom) { Camera.prototype.setInset = function (left, right, top, bottom) {
this._inset = new CameraInset(); this._inset = { left: left, right: right, top: top, bottom: bottom };
this._inset.left = left;
this._inset.right = right;
this._inset.top = top;
this._inset.bottom = bottom;
this._areBoundsDirty = true; this._areBoundsDirty = true;
return this; return this;
}; };
@@ -4513,7 +4498,7 @@ var es;
(function (es) { (function (es) {
var ComponentList = (function () { var ComponentList = (function () {
function ComponentList(entity) { function ComponentList(entity) {
this._components = []; this._components = new es.FastList();
this._componentsToAdd = []; this._componentsToAdd = [];
this._componentsToRemove = []; this._componentsToRemove = [];
this._tempBufferList = []; this._tempBufferList = [];
@@ -4528,7 +4513,7 @@ var es;
}); });
Object.defineProperty(ComponentList.prototype, "buffer", { Object.defineProperty(ComponentList.prototype, "buffer", {
get: function () { get: function () {
return this._components; return this._components.buffer;
}, },
enumerable: true, enumerable: true,
configurable: true configurable: true
@@ -4541,7 +4526,7 @@ var es;
}; };
ComponentList.prototype.remove = function (component) { ComponentList.prototype.remove = function (component) {
if (this._componentsToRemove.contains(component)) if (this._componentsToRemove.contains(component))
console.warn("You are trying to remove a Component (" + component + ") that you already removed"); console.warn("\u60A8\u6B63\u5728\u5C1D\u8BD5\u5220\u9664\u4E00\u4E2A\u60A8\u5DF2\u7ECF\u5220\u9664\u7684\u7EC4\u4EF6(" + component + ")");
if (this._componentsToAdd.contains(component)) { if (this._componentsToAdd.contains(component)) {
this._componentsToAdd.remove(component); this._componentsToAdd.remove(component);
return; return;
@@ -4552,13 +4537,13 @@ var es;
for (var i = 0; i < this._components.length; i++) { for (var i = 0; i < this._components.length; i++) {
this.handleRemove(this._components[i]); this.handleRemove(this._components[i]);
} }
this._components.length = 0; this._components.clear();
this._componentsToAdd.length = 0; this._componentsToAdd.length = 0;
this._componentsToRemove.length = 0; this._componentsToRemove.length = 0;
}; };
ComponentList.prototype.deregisterAllComponents = function () { ComponentList.prototype.deregisterAllComponents = function () {
for (var i = 0; i < this._components.length; i++) { for (var i = 0; i < this._components.length; i++) {
var component = this._components[i]; var component = this._components.buffer[i];
if (component instanceof es.RenderableComponent) { if (component instanceof es.RenderableComponent) {
if (component.displayObject.parent) if (component.displayObject.parent)
component.displayObject.parent.removeChild(component.displayObject); component.displayObject.parent.removeChild(component.displayObject);
@@ -4572,7 +4557,7 @@ var es;
}; };
ComponentList.prototype.registerAllComponents = function () { ComponentList.prototype.registerAllComponents = function () {
for (var i = 0; i < this._components.length; i++) { for (var i = 0; i < this._components.length; i++) {
var component = this._components[i]; var component = this._components.buffer[i];
if (component instanceof es.RenderableComponent) { if (component instanceof es.RenderableComponent) {
if (!this._entity.scene.dynamicBatch) if (!this._entity.scene.dynamicBatch)
this._entity.scene.addChild(component.displayObject); this._entity.scene.addChild(component.displayObject);
@@ -4606,7 +4591,7 @@ var es;
this._entity.scene.addChild(component.debugDisplayObject); this._entity.scene.addChild(component.debugDisplayObject);
this._entity.componentBits.set(es.ComponentTypeManager.getIndexFor(component)); this._entity.componentBits.set(es.ComponentTypeManager.getIndexFor(component));
this._entity.scene.entityProcessors.onComponentAdded(this._entity); this._entity.scene.entityProcessors.onComponentAdded(this._entity);
this._components.push(component); this._components.add(component);
this._tempBufferList.push(component); this._tempBufferList.push(component);
} }
if (this._entity.scene.dynamicBatch) if (this._entity.scene.dynamicBatch)
@@ -4623,7 +4608,7 @@ var es;
this._tempBufferList.length = 0; this._tempBufferList.length = 0;
} }
if (this._isComponentListUnsorted) { if (this._isComponentListUnsorted) {
this._components.sort(ComponentList.compareUpdatableOrder.compare); this._components.sort(ComponentList.compareUpdatableOrder);
this._isComponentListUnsorted = false; this._isComponentListUnsorted = false;
} }
}; };
@@ -4642,7 +4627,7 @@ var es;
}; };
ComponentList.prototype.getComponent = function (type, onlyReturnInitializedComponents) { ComponentList.prototype.getComponent = function (type, onlyReturnInitializedComponents) {
for (var i = 0; i < this._components.length; i++) { for (var i = 0; i < this._components.length; i++) {
var component = this._components[i]; var component = this._components.buffer[i];
if (component instanceof type) if (component instanceof type)
return component; return component;
} }
@@ -4659,7 +4644,7 @@ var es;
if (!components) if (!components)
components = []; components = [];
for (var i = 0; i < this._components.length; i++) { for (var i = 0; i < this._components.length; i++) {
var component = this._components[i]; var component = this._components.buffer[i];
if (typeof (typeName) == "string") { if (typeof (typeName) == "string") {
if (egret.is(component, typeName)) { if (egret.is(component, typeName)) {
components.push(component); components.push(component);
@@ -4689,7 +4674,7 @@ var es;
ComponentList.prototype.update = function () { ComponentList.prototype.update = function () {
this.updateLists(); this.updateLists();
for (var i = 0; i < this._components.length; i++) { for (var i = 0; i < this._components.length; i++) {
var updatableComponent = this._components[i]; var updatableComponent = this._components.buffer[i];
if (updatableComponent.enabled && if (updatableComponent.enabled &&
(updatableComponent.updateInterval == 1 || (updatableComponent.updateInterval == 1 ||
es.Time.frameCount % updatableComponent.updateInterval == 0)) es.Time.frameCount % updatableComponent.updateInterval == 0))
@@ -4698,8 +4683,8 @@ var es;
}; };
ComponentList.prototype.onEntityTransformChanged = function (comp) { ComponentList.prototype.onEntityTransformChanged = function (comp) {
for (var i = 0; i < this._components.length; i++) { for (var i = 0; i < this._components.length; i++) {
if (this._components[i].enabled) if (this._components.buffer[i].enabled)
this._components[i].onEntityTransformChanged(comp); this._components.buffer[i].onEntityTransformChanged(comp);
} }
for (var i = 0; i < this._componentsToAdd.length; i++) { for (var i = 0; i < this._componentsToAdd.length; i++) {
if (this._componentsToAdd[i].enabled) if (this._componentsToAdd[i].enabled)
@@ -4708,16 +4693,16 @@ var es;
}; };
ComponentList.prototype.onEntityEnabled = function () { ComponentList.prototype.onEntityEnabled = function () {
for (var i = 0; i < this._components.length; i++) for (var i = 0; i < this._components.length; i++)
this._components[i].onEnabled(); this._components.buffer[i].onEnabled();
}; };
ComponentList.prototype.onEntityDisabled = function () { ComponentList.prototype.onEntityDisabled = function () {
for (var i = 0; i < this._components.length; i++) for (var i = 0; i < this._components.length; i++)
this._components[i].onDisabled(); this._components.buffer[i].onDisabled();
}; };
ComponentList.prototype.debugRender = function (camera) { ComponentList.prototype.debugRender = function (camera) {
for (var i = 0; i < this._components.length; i++) { for (var i = 0; i < this._components.length; i++) {
if (this._components[i].enabled) if (this._components.buffer[i].enabled)
this._components[i].debugRender(camera); this._components.buffer[i].debugRender(camera);
} }
}; };
ComponentList.compareUpdatableOrder = new es.IUpdatableComparer(); ComponentList.compareUpdatableOrder = new es.IUpdatableComparer();
@@ -4755,7 +4740,7 @@ var es;
this._entitiesToAdded = []; this._entitiesToAdded = [];
this._entitiesToRemove = []; this._entitiesToRemove = [];
this._entityDict = new Map(); this._entityDict = new Map();
this._unsortedTags = []; this._unsortedTags = new Set();
this._addToSceneEntityList = []; this._addToSceneEntityList = [];
this.frameAllocate = false; this.frameAllocate = false;
this.maxAllocate = 10; this.maxAllocate = 10;
@@ -4779,7 +4764,7 @@ var es;
this._isEntityListUnsorted = true; this._isEntityListUnsorted = true;
}; };
EntityList.prototype.markTagUnsorted = function (tag) { EntityList.prototype.markTagUnsorted = function (tag) {
this._unsortedTags.push(tag); this._unsortedTags.add(tag);
}; };
EntityList.prototype.add = function (entity) { EntityList.prototype.add = function (entity) {
if (this._entitiesToAdded.indexOf(entity) == -1) if (this._entitiesToAdded.indexOf(entity) == -1)
@@ -4787,7 +4772,7 @@ var es;
}; };
EntityList.prototype.remove = function (entity) { EntityList.prototype.remove = function (entity) {
if (!this._entitiesToRemove.contains(entity)) { if (!this._entitiesToRemove.contains(entity)) {
console.warn("You are trying to remove an entity (" + entity.name + ") that you already removed"); console.warn("\u60A8\u6B63\u5728\u5C1D\u8BD5\u5220\u9664\u5DF2\u7ECF\u5220\u9664\u7684\u5B9E\u4F53(" + entity.name + ")");
return; return;
} }
if (this._entitiesToAdded.contains(entity)) { if (this._entitiesToAdded.contains(entity)) {
@@ -4798,7 +4783,7 @@ var es;
this._entitiesToRemove.push(entity); this._entitiesToRemove.push(entity);
}; };
EntityList.prototype.removeAllEntities = function () { EntityList.prototype.removeAllEntities = function () {
this._unsortedTags.length = 0; this._unsortedTags.clear();
this._entitiesToAdded.length = 0; this._entitiesToAdded.length = 0;
this._isEntityListUnsorted = false; this._isEntityListUnsorted = false;
this.updateLists(); this.updateLists();
@@ -4826,8 +4811,7 @@ var es;
var list = this.getTagList(entity.tag); var list = this.getTagList(entity.tag);
if (list.findIndex(function (e) { return e.id == entity.id; }) == -1) { if (list.findIndex(function (e) { return e.id == entity.id; }) == -1) {
list.push(entity); list.push(entity);
if (!this._unsortedTags.contains(entity.tag)) this._unsortedTags.add(entity.tag);
this._unsortedTags.push(entity.tag);
} }
}; };
EntityList.prototype.removeFromTagList = function (entity) { EntityList.prototype.removeFromTagList = function (entity) {
@@ -4844,6 +4828,7 @@ var es;
} }
}; };
EntityList.prototype.updateLists = function () { EntityList.prototype.updateLists = function () {
var _this = this;
if (this._entitiesToRemove.length > 0) { if (this._entitiesToRemove.length > 0) {
for (var _i = 0, _a = this._entitiesToRemove; _i < _a.length; _i++) { for (var _i = 0, _a = this._entitiesToRemove; _i < _a.length; _i++) {
var entity = _a[_i]; var entity = _a[_i];
@@ -4880,14 +4865,13 @@ var es;
}); });
this._isEntityListUnsorted = false; this._isEntityListUnsorted = false;
} }
if (this._addToSceneEntityList.length == 0 && this._unsortedTags.length > 0) { if (this._addToSceneEntityList.length == 0 && this._unsortedTags.size > 0) {
for (var _b = 0, _c = this._unsortedTags; _b < _c.length; _b++) { this._unsortedTags.forEach(function (tag) {
var tag = _c[_b]; _this._entityDict.get(tag).sort(function (a, b) {
this._entityDict.get(tag).sort(function (a, b) {
return a.compareTo(b); return a.compareTo(b);
}); });
} });
this._unsortedTags.length = 0; this._unsortedTags.clear();
} }
}; };
EntityList.prototype.perEntityAddToScene = function () { EntityList.prototype.perEntityAddToScene = function () {
@@ -4910,6 +4894,7 @@ var es;
EntityList.prototype.entitiesWithTag = function (tag) { EntityList.prototype.entitiesWithTag = function (tag) {
var list = this.getTagList(tag); var list = this.getTagList(tag);
var returnList = es.ListPool.obtain(); var returnList = es.ListPool.obtain();
returnList.length = this._entities.length;
for (var i = 0; i < list.length; i++) for (var i = 0; i < list.length; i++)
returnList.push(list[i]); returnList.push(list[i]);
return returnList; return returnList;
@@ -5023,6 +5008,67 @@ var es;
es.EntityProcessorList = EntityProcessorList; es.EntityProcessorList = EntityProcessorList;
})(es || (es = {})); })(es || (es = {}));
var es; var es;
(function (es) {
var FastList = (function () {
function FastList(size) {
if (size === void 0) { size = 5; }
this.length = 0;
this.buffer = new Array(size);
}
FastList.prototype.clear = function () {
this.buffer.length = 0;
this.length = 0;
};
FastList.prototype.reset = function () {
this.length = 0;
};
FastList.prototype.add = function (item) {
if (this.length == this.buffer.length)
this.buffer.length = Math.max(this.buffer.length << 1, 10);
this.buffer[this.length++] = item;
};
FastList.prototype.remove = function (item) {
var comp = es.EqualityComparer.default();
for (var i = 0; i < this.length; ++i) {
if (comp.equals(this.buffer[i], item)) {
this.removeAt(i);
return;
}
}
};
FastList.prototype.removeAt = function (index) {
if (index >= this.length)
throw new Error("index超出范围!");
this.length--;
this.buffer.removeAt(index);
};
FastList.prototype.contains = function (item) {
var comp = es.EqualityComparer.default();
for (var i = 0; i < this.length; ++i) {
if (comp.equals(this.buffer[i], item))
return true;
}
return false;
};
FastList.prototype.ensureCapacity = function (additionalItemCount) {
if (additionalItemCount === void 0) { additionalItemCount = 1; }
if (this.length + additionalItemCount >= this.buffer.length)
this.buffer.length = Math.max(this.buffer.length << 1, this.length + additionalItemCount);
};
FastList.prototype.addRange = function (array) {
for (var _i = 0, array_1 = array; _i < array_1.length; _i++) {
var item = array_1[_i];
this.add(item);
}
};
FastList.prototype.sort = function (comparer) {
this.buffer.sort(comparer.compare);
};
return FastList;
}());
es.FastList = FastList;
})(es || (es = {}));
var es;
(function (es) { (function (es) {
var Matcher = (function () { var Matcher = (function () {
function Matcher() { function Matcher() {
@@ -10332,6 +10378,26 @@ var es;
es.Enumerable = Enumerable; es.Enumerable = Enumerable;
})(es || (es = {})); })(es || (es = {}));
var es; var es;
(function (es) {
var EqualityComparer = (function () {
function EqualityComparer() {
}
EqualityComparer.default = function () {
return new EqualityComparer();
};
EqualityComparer.prototype.equals = function (x, y) {
if (typeof x["equals"] == 'function') {
return x["equals"](y);
}
else {
return x === y;
}
};
return EqualityComparer;
}());
es.EqualityComparer = EqualityComparer;
})(es || (es = {}));
var es;
(function (es) { (function (es) {
var GlobalManager = (function () { var GlobalManager = (function () {
function GlobalManager() { function GlobalManager() {
File diff suppressed because one or more lines are too long
+51 -8
View File
@@ -102,7 +102,7 @@ declare module es {
} }
} }
declare module es { declare module es {
class Vector2 { class Vector2 implements IEquatable<Vector2> {
x: number; x: number;
y: number; y: number;
constructor(x?: number, y?: number); constructor(x?: number, y?: number);
@@ -131,7 +131,7 @@ declare module es {
length(): number; length(): number;
lengthSquared(): number; lengthSquared(): number;
round(): Vector2; round(): Vector2;
equals(other: Vector2): boolean; equals(other: Vector2 | object): boolean;
} }
} }
declare module es { declare module es {
@@ -483,7 +483,7 @@ declare module es {
} }
} }
declare module es { declare module es {
class CameraInset { interface CameraInset {
left: number; left: number;
right: number; right: number;
top: number; top: number;
@@ -578,7 +578,12 @@ declare module es {
} }
} }
declare module es { declare module es {
class IUpdatableComparer { interface IUpdatable {
enabled: boolean;
updateOrder: number;
update(): any;
}
class IUpdatableComparer implements IComparer<IUpdatable> {
compare(a: Component, b: Component): number; compare(a: Component, b: Component): number;
} }
} }
@@ -982,7 +987,7 @@ declare module es {
class ComponentList { class ComponentList {
static compareUpdatableOrder: IUpdatableComparer; static compareUpdatableOrder: IUpdatableComparer;
_entity: Entity; _entity: Entity;
_components: Component[]; _components: FastList<Component>;
_componentsToAdd: Component[]; _componentsToAdd: Component[];
_componentsToRemove: Component[]; _componentsToRemove: Component[];
_tempBufferList: Component[]; _tempBufferList: Component[];
@@ -1022,7 +1027,7 @@ declare module es {
_entitiesToRemove: Entity[]; _entitiesToRemove: Entity[];
_isEntityListUnsorted: boolean; _isEntityListUnsorted: boolean;
_entityDict: Map<number, Entity[]>; _entityDict: Map<number, Entity[]>;
_unsortedTags: number[]; _unsortedTags: Set<number>;
_addToSceneEntityList: Entity[]; _addToSceneEntityList: Entity[];
frameAllocate: boolean; frameAllocate: boolean;
maxAllocate: number; maxAllocate: number;
@@ -1050,7 +1055,7 @@ declare module es {
} }
declare module es { declare module es {
class EntityProcessorList { class EntityProcessorList {
private _processors; protected _processors: EntitySystem[];
add(processor: EntitySystem): void; add(processor: EntitySystem): void;
remove(processor: EntitySystem): void; remove(processor: EntitySystem): void;
onComponentAdded(entity: Entity): void; onComponentAdded(entity: Entity): void;
@@ -1066,6 +1071,22 @@ declare module es {
protected removeFromProcessors(entity: Entity): void; protected removeFromProcessors(entity: Entity): void;
} }
} }
declare module es {
class FastList<T> {
buffer: T[];
length: number;
constructor(size?: number);
clear(): void;
reset(): void;
add(item: T): void;
remove(item: T): void;
removeAt(index: number): void;
contains(item: T): boolean;
ensureCapacity(additionalItemCount?: number): void;
addRange(array: T[]): void;
sort(comparer: IComparer<T>): void;
}
}
declare module es { declare module es {
class Matcher { class Matcher {
protected allSet: BitSet; protected allSet: BitSet;
@@ -2149,6 +2170,13 @@ declare module es {
static repeat<T>(element: T, count: number): any[]; static repeat<T>(element: T, count: number): any[];
} }
} }
declare module es {
class EqualityComparer<T> implements IEqualityComparer {
static default<T>(): EqualityComparer<T>;
protected constructor();
equals(x: T, y: T): boolean;
}
}
declare module es { declare module es {
class GlobalManager { class GlobalManager {
_enabled: boolean; _enabled: boolean;
@@ -2159,6 +2187,21 @@ declare module es {
update(): void; update(): void;
} }
} }
declare module es {
interface IComparer<T> {
compare(x: T, y: T): number;
}
}
declare module es {
interface IEqualityComparer {
equals(x: any, y: any): boolean;
}
}
declare module es {
interface IEquatable<T> {
equals(other: T): boolean;
}
}
declare module es { declare module es {
class ListPool { class ListPool {
private static readonly _objectQueue; private static readonly _objectQueue;
@@ -2170,7 +2213,7 @@ declare module es {
} }
} }
declare module es { declare module es {
class Pair<T> { class Pair<T> implements IEquatable<Pair<T>> {
first: T; first: T;
second: T; second: T;
constructor(first: T, second: T); constructor(first: T, second: T);
+120 -54
View File
@@ -733,10 +733,9 @@ var es;
return Math.acos(es.MathHelper.clamp(Vector2.dot(from, to), -1, 1)) * es.MathHelper.Rad2Deg; return Math.acos(es.MathHelper.clamp(Vector2.dot(from, to), -1, 1)) * es.MathHelper.Rad2Deg;
}; };
Vector2.negate = function (value) { Vector2.negate = function (value) {
var result = new Vector2(); value.x = -value.x;
result.x = -value.x; value.y = -value.y;
result.y = -value.y; return value;
return result;
}; };
Vector2.prototype.add = function (value) { Vector2.prototype.add = function (value) {
this.x += value.x; this.x += value.x;
@@ -773,7 +772,10 @@ var es;
return new Vector2(Math.round(this.x), Math.round(this.y)); return new Vector2(Math.round(this.x), Math.round(this.y));
}; };
Vector2.prototype.equals = function (other) { Vector2.prototype.equals = function (other) {
return other.x == this.x && other.y == this.y; if (other instanceof Vector2) {
return other.x == this.x && other.y == this.y;
}
return false;
}; };
return Vector2; return Vector2;
}()); }());
@@ -1056,7 +1058,6 @@ var es;
return __generator(this, function (_a) { return __generator(this, function (_a) {
switch (_a.label) { switch (_a.label) {
case 0: case 0:
this.startDebugDraw(es.Time.deltaTime);
if (!this._sceneTransition) return [3, 4]; if (!this._sceneTransition) return [3, 4];
this._sceneTransition.preRender(); this._sceneTransition.preRender();
if (!(this._scene && !this._sceneTransition.hasPreviousSceneRender)) return [3, 2]; if (!(this._scene && !this._sceneTransition.hasPreviousSceneRender)) return [3, 2];
@@ -1083,9 +1084,7 @@ var es;
this._scene.postRender(); this._scene.postRender();
} }
_a.label = 5; _a.label = 5;
case 5: case 5: return [2];
this.endDebugDraw();
return [2];
} }
}); });
}); });
@@ -2287,21 +2286,11 @@ var es;
})(es || (es = {})); })(es || (es = {}));
var es; var es;
(function (es) { (function (es) {
var CameraInset = (function () {
function CameraInset() {
this.left = 0;
this.right = 0;
this.top = 0;
this.bottom = 0;
}
return CameraInset;
}());
es.CameraInset = CameraInset;
var Camera = (function (_super) { var Camera = (function (_super) {
__extends(Camera, _super); __extends(Camera, _super);
function Camera() { function Camera() {
var _this = _super.call(this) || this; var _this = _super.call(this) || this;
_this._inset = new CameraInset(); _this._inset = { left: 0, right: 0, top: 0, bottom: 0 };
_this._areMatrixedDirty = true; _this._areMatrixedDirty = true;
_this._areBoundsDirty = true; _this._areBoundsDirty = true;
_this._isProjectionMatrixDirty = true; _this._isProjectionMatrixDirty = true;
@@ -2444,11 +2433,7 @@ var es;
configurable: true configurable: true
}); });
Camera.prototype.setInset = function (left, right, top, bottom) { Camera.prototype.setInset = function (left, right, top, bottom) {
this._inset = new CameraInset(); this._inset = { left: left, right: right, top: top, bottom: bottom };
this._inset.left = left;
this._inset.right = right;
this._inset.top = top;
this._inset.bottom = bottom;
this._areBoundsDirty = true; this._areBoundsDirty = true;
return this; return this;
}; };
@@ -4513,7 +4498,7 @@ var es;
(function (es) { (function (es) {
var ComponentList = (function () { var ComponentList = (function () {
function ComponentList(entity) { function ComponentList(entity) {
this._components = []; this._components = new es.FastList();
this._componentsToAdd = []; this._componentsToAdd = [];
this._componentsToRemove = []; this._componentsToRemove = [];
this._tempBufferList = []; this._tempBufferList = [];
@@ -4528,7 +4513,7 @@ var es;
}); });
Object.defineProperty(ComponentList.prototype, "buffer", { Object.defineProperty(ComponentList.prototype, "buffer", {
get: function () { get: function () {
return this._components; return this._components.buffer;
}, },
enumerable: true, enumerable: true,
configurable: true configurable: true
@@ -4541,7 +4526,7 @@ var es;
}; };
ComponentList.prototype.remove = function (component) { ComponentList.prototype.remove = function (component) {
if (this._componentsToRemove.contains(component)) if (this._componentsToRemove.contains(component))
console.warn("You are trying to remove a Component (" + component + ") that you already removed"); console.warn("\u60A8\u6B63\u5728\u5C1D\u8BD5\u5220\u9664\u4E00\u4E2A\u60A8\u5DF2\u7ECF\u5220\u9664\u7684\u7EC4\u4EF6(" + component + ")");
if (this._componentsToAdd.contains(component)) { if (this._componentsToAdd.contains(component)) {
this._componentsToAdd.remove(component); this._componentsToAdd.remove(component);
return; return;
@@ -4552,13 +4537,13 @@ var es;
for (var i = 0; i < this._components.length; i++) { for (var i = 0; i < this._components.length; i++) {
this.handleRemove(this._components[i]); this.handleRemove(this._components[i]);
} }
this._components.length = 0; this._components.clear();
this._componentsToAdd.length = 0; this._componentsToAdd.length = 0;
this._componentsToRemove.length = 0; this._componentsToRemove.length = 0;
}; };
ComponentList.prototype.deregisterAllComponents = function () { ComponentList.prototype.deregisterAllComponents = function () {
for (var i = 0; i < this._components.length; i++) { for (var i = 0; i < this._components.length; i++) {
var component = this._components[i]; var component = this._components.buffer[i];
if (component instanceof es.RenderableComponent) { if (component instanceof es.RenderableComponent) {
if (component.displayObject.parent) if (component.displayObject.parent)
component.displayObject.parent.removeChild(component.displayObject); component.displayObject.parent.removeChild(component.displayObject);
@@ -4572,7 +4557,7 @@ var es;
}; };
ComponentList.prototype.registerAllComponents = function () { ComponentList.prototype.registerAllComponents = function () {
for (var i = 0; i < this._components.length; i++) { for (var i = 0; i < this._components.length; i++) {
var component = this._components[i]; var component = this._components.buffer[i];
if (component instanceof es.RenderableComponent) { if (component instanceof es.RenderableComponent) {
if (!this._entity.scene.dynamicBatch) if (!this._entity.scene.dynamicBatch)
this._entity.scene.addChild(component.displayObject); this._entity.scene.addChild(component.displayObject);
@@ -4606,7 +4591,7 @@ var es;
this._entity.scene.addChild(component.debugDisplayObject); this._entity.scene.addChild(component.debugDisplayObject);
this._entity.componentBits.set(es.ComponentTypeManager.getIndexFor(component)); this._entity.componentBits.set(es.ComponentTypeManager.getIndexFor(component));
this._entity.scene.entityProcessors.onComponentAdded(this._entity); this._entity.scene.entityProcessors.onComponentAdded(this._entity);
this._components.push(component); this._components.add(component);
this._tempBufferList.push(component); this._tempBufferList.push(component);
} }
if (this._entity.scene.dynamicBatch) if (this._entity.scene.dynamicBatch)
@@ -4623,7 +4608,7 @@ var es;
this._tempBufferList.length = 0; this._tempBufferList.length = 0;
} }
if (this._isComponentListUnsorted) { if (this._isComponentListUnsorted) {
this._components.sort(ComponentList.compareUpdatableOrder.compare); this._components.sort(ComponentList.compareUpdatableOrder);
this._isComponentListUnsorted = false; this._isComponentListUnsorted = false;
} }
}; };
@@ -4642,7 +4627,7 @@ var es;
}; };
ComponentList.prototype.getComponent = function (type, onlyReturnInitializedComponents) { ComponentList.prototype.getComponent = function (type, onlyReturnInitializedComponents) {
for (var i = 0; i < this._components.length; i++) { for (var i = 0; i < this._components.length; i++) {
var component = this._components[i]; var component = this._components.buffer[i];
if (component instanceof type) if (component instanceof type)
return component; return component;
} }
@@ -4659,7 +4644,7 @@ var es;
if (!components) if (!components)
components = []; components = [];
for (var i = 0; i < this._components.length; i++) { for (var i = 0; i < this._components.length; i++) {
var component = this._components[i]; var component = this._components.buffer[i];
if (typeof (typeName) == "string") { if (typeof (typeName) == "string") {
if (egret.is(component, typeName)) { if (egret.is(component, typeName)) {
components.push(component); components.push(component);
@@ -4689,7 +4674,7 @@ var es;
ComponentList.prototype.update = function () { ComponentList.prototype.update = function () {
this.updateLists(); this.updateLists();
for (var i = 0; i < this._components.length; i++) { for (var i = 0; i < this._components.length; i++) {
var updatableComponent = this._components[i]; var updatableComponent = this._components.buffer[i];
if (updatableComponent.enabled && if (updatableComponent.enabled &&
(updatableComponent.updateInterval == 1 || (updatableComponent.updateInterval == 1 ||
es.Time.frameCount % updatableComponent.updateInterval == 0)) es.Time.frameCount % updatableComponent.updateInterval == 0))
@@ -4698,8 +4683,8 @@ var es;
}; };
ComponentList.prototype.onEntityTransformChanged = function (comp) { ComponentList.prototype.onEntityTransformChanged = function (comp) {
for (var i = 0; i < this._components.length; i++) { for (var i = 0; i < this._components.length; i++) {
if (this._components[i].enabled) if (this._components.buffer[i].enabled)
this._components[i].onEntityTransformChanged(comp); this._components.buffer[i].onEntityTransformChanged(comp);
} }
for (var i = 0; i < this._componentsToAdd.length; i++) { for (var i = 0; i < this._componentsToAdd.length; i++) {
if (this._componentsToAdd[i].enabled) if (this._componentsToAdd[i].enabled)
@@ -4708,16 +4693,16 @@ var es;
}; };
ComponentList.prototype.onEntityEnabled = function () { ComponentList.prototype.onEntityEnabled = function () {
for (var i = 0; i < this._components.length; i++) for (var i = 0; i < this._components.length; i++)
this._components[i].onEnabled(); this._components.buffer[i].onEnabled();
}; };
ComponentList.prototype.onEntityDisabled = function () { ComponentList.prototype.onEntityDisabled = function () {
for (var i = 0; i < this._components.length; i++) for (var i = 0; i < this._components.length; i++)
this._components[i].onDisabled(); this._components.buffer[i].onDisabled();
}; };
ComponentList.prototype.debugRender = function (camera) { ComponentList.prototype.debugRender = function (camera) {
for (var i = 0; i < this._components.length; i++) { for (var i = 0; i < this._components.length; i++) {
if (this._components[i].enabled) if (this._components.buffer[i].enabled)
this._components[i].debugRender(camera); this._components.buffer[i].debugRender(camera);
} }
}; };
ComponentList.compareUpdatableOrder = new es.IUpdatableComparer(); ComponentList.compareUpdatableOrder = new es.IUpdatableComparer();
@@ -4755,7 +4740,7 @@ var es;
this._entitiesToAdded = []; this._entitiesToAdded = [];
this._entitiesToRemove = []; this._entitiesToRemove = [];
this._entityDict = new Map(); this._entityDict = new Map();
this._unsortedTags = []; this._unsortedTags = new Set();
this._addToSceneEntityList = []; this._addToSceneEntityList = [];
this.frameAllocate = false; this.frameAllocate = false;
this.maxAllocate = 10; this.maxAllocate = 10;
@@ -4779,7 +4764,7 @@ var es;
this._isEntityListUnsorted = true; this._isEntityListUnsorted = true;
}; };
EntityList.prototype.markTagUnsorted = function (tag) { EntityList.prototype.markTagUnsorted = function (tag) {
this._unsortedTags.push(tag); this._unsortedTags.add(tag);
}; };
EntityList.prototype.add = function (entity) { EntityList.prototype.add = function (entity) {
if (this._entitiesToAdded.indexOf(entity) == -1) if (this._entitiesToAdded.indexOf(entity) == -1)
@@ -4787,7 +4772,7 @@ var es;
}; };
EntityList.prototype.remove = function (entity) { EntityList.prototype.remove = function (entity) {
if (!this._entitiesToRemove.contains(entity)) { if (!this._entitiesToRemove.contains(entity)) {
console.warn("You are trying to remove an entity (" + entity.name + ") that you already removed"); console.warn("\u60A8\u6B63\u5728\u5C1D\u8BD5\u5220\u9664\u5DF2\u7ECF\u5220\u9664\u7684\u5B9E\u4F53(" + entity.name + ")");
return; return;
} }
if (this._entitiesToAdded.contains(entity)) { if (this._entitiesToAdded.contains(entity)) {
@@ -4798,7 +4783,7 @@ var es;
this._entitiesToRemove.push(entity); this._entitiesToRemove.push(entity);
}; };
EntityList.prototype.removeAllEntities = function () { EntityList.prototype.removeAllEntities = function () {
this._unsortedTags.length = 0; this._unsortedTags.clear();
this._entitiesToAdded.length = 0; this._entitiesToAdded.length = 0;
this._isEntityListUnsorted = false; this._isEntityListUnsorted = false;
this.updateLists(); this.updateLists();
@@ -4826,8 +4811,7 @@ var es;
var list = this.getTagList(entity.tag); var list = this.getTagList(entity.tag);
if (list.findIndex(function (e) { return e.id == entity.id; }) == -1) { if (list.findIndex(function (e) { return e.id == entity.id; }) == -1) {
list.push(entity); list.push(entity);
if (!this._unsortedTags.contains(entity.tag)) this._unsortedTags.add(entity.tag);
this._unsortedTags.push(entity.tag);
} }
}; };
EntityList.prototype.removeFromTagList = function (entity) { EntityList.prototype.removeFromTagList = function (entity) {
@@ -4844,6 +4828,7 @@ var es;
} }
}; };
EntityList.prototype.updateLists = function () { EntityList.prototype.updateLists = function () {
var _this = this;
if (this._entitiesToRemove.length > 0) { if (this._entitiesToRemove.length > 0) {
for (var _i = 0, _a = this._entitiesToRemove; _i < _a.length; _i++) { for (var _i = 0, _a = this._entitiesToRemove; _i < _a.length; _i++) {
var entity = _a[_i]; var entity = _a[_i];
@@ -4880,14 +4865,13 @@ var es;
}); });
this._isEntityListUnsorted = false; this._isEntityListUnsorted = false;
} }
if (this._addToSceneEntityList.length == 0 && this._unsortedTags.length > 0) { if (this._addToSceneEntityList.length == 0 && this._unsortedTags.size > 0) {
for (var _b = 0, _c = this._unsortedTags; _b < _c.length; _b++) { this._unsortedTags.forEach(function (tag) {
var tag = _c[_b]; _this._entityDict.get(tag).sort(function (a, b) {
this._entityDict.get(tag).sort(function (a, b) {
return a.compareTo(b); return a.compareTo(b);
}); });
} });
this._unsortedTags.length = 0; this._unsortedTags.clear();
} }
}; };
EntityList.prototype.perEntityAddToScene = function () { EntityList.prototype.perEntityAddToScene = function () {
@@ -4910,6 +4894,7 @@ var es;
EntityList.prototype.entitiesWithTag = function (tag) { EntityList.prototype.entitiesWithTag = function (tag) {
var list = this.getTagList(tag); var list = this.getTagList(tag);
var returnList = es.ListPool.obtain(); var returnList = es.ListPool.obtain();
returnList.length = this._entities.length;
for (var i = 0; i < list.length; i++) for (var i = 0; i < list.length; i++)
returnList.push(list[i]); returnList.push(list[i]);
return returnList; return returnList;
@@ -5023,6 +5008,67 @@ var es;
es.EntityProcessorList = EntityProcessorList; es.EntityProcessorList = EntityProcessorList;
})(es || (es = {})); })(es || (es = {}));
var es; var es;
(function (es) {
var FastList = (function () {
function FastList(size) {
if (size === void 0) { size = 5; }
this.length = 0;
this.buffer = new Array(size);
}
FastList.prototype.clear = function () {
this.buffer.length = 0;
this.length = 0;
};
FastList.prototype.reset = function () {
this.length = 0;
};
FastList.prototype.add = function (item) {
if (this.length == this.buffer.length)
this.buffer.length = Math.max(this.buffer.length << 1, 10);
this.buffer[this.length++] = item;
};
FastList.prototype.remove = function (item) {
var comp = es.EqualityComparer.default();
for (var i = 0; i < this.length; ++i) {
if (comp.equals(this.buffer[i], item)) {
this.removeAt(i);
return;
}
}
};
FastList.prototype.removeAt = function (index) {
if (index >= this.length)
throw new Error("index超出范围!");
this.length--;
this.buffer.removeAt(index);
};
FastList.prototype.contains = function (item) {
var comp = es.EqualityComparer.default();
for (var i = 0; i < this.length; ++i) {
if (comp.equals(this.buffer[i], item))
return true;
}
return false;
};
FastList.prototype.ensureCapacity = function (additionalItemCount) {
if (additionalItemCount === void 0) { additionalItemCount = 1; }
if (this.length + additionalItemCount >= this.buffer.length)
this.buffer.length = Math.max(this.buffer.length << 1, this.length + additionalItemCount);
};
FastList.prototype.addRange = function (array) {
for (var _i = 0, array_1 = array; _i < array_1.length; _i++) {
var item = array_1[_i];
this.add(item);
}
};
FastList.prototype.sort = function (comparer) {
this.buffer.sort(comparer.compare);
};
return FastList;
}());
es.FastList = FastList;
})(es || (es = {}));
var es;
(function (es) { (function (es) {
var Matcher = (function () { var Matcher = (function () {
function Matcher() { function Matcher() {
@@ -10332,6 +10378,26 @@ var es;
es.Enumerable = Enumerable; es.Enumerable = Enumerable;
})(es || (es = {})); })(es || (es = {}));
var es; var es;
(function (es) {
var EqualityComparer = (function () {
function EqualityComparer() {
}
EqualityComparer.default = function () {
return new EqualityComparer();
};
EqualityComparer.prototype.equals = function (x, y) {
if (typeof x["equals"] == 'function') {
return x["equals"](y);
}
else {
return x === y;
}
};
return EqualityComparer;
}());
es.EqualityComparer = EqualityComparer;
})(es || (es = {}));
var es;
(function (es) { (function (es) {
var GlobalManager = (function () { var GlobalManager = (function () {
function GlobalManager() { function GlobalManager() {
+1 -1
View File
File diff suppressed because one or more lines are too long
+4 -11
View File
@@ -1,13 +1,10 @@
module es { module es {
export class CameraInset { export interface CameraInset {
public left: number = 0; left: number, right: number, top: number, bottom: number
public right: number = 0;
public top: number = 0;
public bottom: number = 0;
} }
export class Camera extends Component { export class Camera extends Component {
public _inset: CameraInset = new CameraInset(); public _inset: CameraInset = {left: 0, right: 0, top: 0, bottom: 0};
public _areMatrixedDirty: boolean = true; public _areMatrixedDirty: boolean = true;
public _areBoundsDirty: boolean = true; public _areBoundsDirty: boolean = true;
public _isProjectionMatrixDirty = true; public _isProjectionMatrixDirty = true;
@@ -209,11 +206,7 @@ module es {
* @param bottom * @param bottom
*/ */
public setInset(left: number, right: number, top: number, bottom: number): Camera { public setInset(left: number, right: number, top: number, bottom: number): Camera {
this._inset = new CameraInset(); this._inset = {left: left, right: right, top: top, bottom: bottom};
this._inset.left = left;
this._inset.right = right;
this._inset.top = top;
this._inset.bottom = bottom;
this._areBoundsDirty = true; this._areBoundsDirty = true;
return this; return this;
} }
@@ -1,8 +1,14 @@
module es { module es {
export interface IUpdatable {
enabled: boolean;
updateOrder: number;
update();
}
/** /**
* *
*/ */
export class IUpdatableComparer { export class IUpdatableComparer implements IComparer<IUpdatable> {
public compare(a: Component, b: Component) { public compare(a: Component, b: Component) {
return a.updateOrder - b.updateOrder; return a.updateOrder - b.updateOrder;
} }
+2 -2
View File
@@ -155,7 +155,7 @@ module es {
} }
public async draw() { public async draw() {
this.startDebugDraw(Time.deltaTime); // this.startDebugDraw(Time.deltaTime);
if (this._sceneTransition) { if (this._sceneTransition) {
this._sceneTransition.preRender(); this._sceneTransition.preRender();
@@ -182,7 +182,7 @@ module es {
this._scene.postRender(); this._scene.postRender();
} }
this.endDebugDraw(); // this.endDebugDraw();
} }
public startDebugUpdate() { public startDebugUpdate() {
+18 -18
View File
@@ -1,4 +1,4 @@
///<reference path="../Components/IUpdatableComparer.ts" /> ///<reference path="../Components/IUpdatable.ts" />
module es { module es {
export class ComponentList { export class ComponentList {
/** /**
@@ -10,7 +10,7 @@ module es {
/** /**
* *
*/ */
public _components: Component[] = []; public _components: FastList<Component> = new FastList<Component>();
/** /**
* *
*/ */
@@ -34,7 +34,7 @@ module es {
} }
public get buffer() { public get buffer() {
return this._components; return this._components.buffer;
} }
public markEntityListUnsorted() { public markEntityListUnsorted() {
@@ -47,7 +47,7 @@ module es {
public remove(component: Component) { public remove(component: Component) {
if (this._componentsToRemove.contains(component)) if (this._componentsToRemove.contains(component))
console.warn(`You are trying to remove a Component (${component}) that you already removed`); console.warn(`您正在尝试删除一个您已经删除的组件(${component})`);
// 这可能不是一个活动的组件,所以我们必须注意它是否还没有被处理,它可能正在同一帧中被删除 // 这可能不是一个活动的组件,所以我们必须注意它是否还没有被处理,它可能正在同一帧中被删除
if (this._componentsToAdd.contains(component)) { if (this._componentsToAdd.contains(component)) {
@@ -66,14 +66,14 @@ module es {
this.handleRemove(this._components[i]); this.handleRemove(this._components[i]);
} }
this._components.length = 0; this._components.clear();
this._componentsToAdd.length = 0; this._componentsToAdd.length = 0;
this._componentsToRemove.length = 0; this._componentsToRemove.length = 0;
} }
public deregisterAllComponents() { public deregisterAllComponents() {
for (let i = 0; i < this._components.length; i++) { for (let i = 0; i < this._components.length; i++) {
let component = this._components[i]; let component = this._components.buffer[i];
// 处理渲染层列表 // 处理渲染层列表
if (component instanceof RenderableComponent) { if (component instanceof RenderableComponent) {
@@ -92,7 +92,7 @@ module es {
public registerAllComponents() { public registerAllComponents() {
for (let i = 0; i < this._components.length; i++) { for (let i = 0; i < this._components.length; i++) {
let component = this._components[i]; let component = this._components.buffer[i];
if (component instanceof RenderableComponent) { if (component instanceof RenderableComponent) {
if (!this._entity.scene.dynamicBatch) this._entity.scene.addChild(component.displayObject); if (!this._entity.scene.dynamicBatch) this._entity.scene.addChild(component.displayObject);
@@ -131,7 +131,7 @@ module es {
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component)); this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component));
this._entity.scene.entityProcessors.onComponentAdded(this._entity); this._entity.scene.entityProcessors.onComponentAdded(this._entity);
this._components.push(component); this._components.add(component);
this._tempBufferList.push(component); this._tempBufferList.push(component);
} }
if (this._entity.scene.dynamicBatch) this._entity.scene.dynamicInBatch(); if (this._entity.scene.dynamicBatch) this._entity.scene.dynamicInBatch();
@@ -155,7 +155,7 @@ module es {
} }
if (this._isComponentListUnsorted) { if (this._isComponentListUnsorted) {
this._components.sort(ComponentList.compareUpdatableOrder.compare); this._components.sort(ComponentList.compareUpdatableOrder);
this._isComponentListUnsorted = false; this._isComponentListUnsorted = false;
} }
} }
@@ -187,7 +187,7 @@ module es {
*/ */
public getComponent<T extends Component>(type, onlyReturnInitializedComponents: boolean): T { public getComponent<T extends Component>(type, onlyReturnInitializedComponents: boolean): T {
for (let i = 0; i < this._components.length; i++) { for (let i = 0; i < this._components.length; i++) {
let component = this._components[i]; let component = this._components.buffer[i];
if (component instanceof type) if (component instanceof type)
return component as T; return component as T;
} }
@@ -214,7 +214,7 @@ module es {
components = []; components = [];
for (let i = 0; i < this._components.length; i++) { for (let i = 0; i < this._components.length; i++) {
let component = this._components[i]; let component = this._components.buffer[i];
if (typeof (typeName) == "string") { if (typeof (typeName) == "string") {
if (egret.is(component, typeName)) { if (egret.is(component, typeName)) {
components.push(component); components.push(component);
@@ -245,7 +245,7 @@ module es {
public update() { public update() {
this.updateLists(); this.updateLists();
for (let i = 0; i < this._components.length; i++) { for (let i = 0; i < this._components.length; i++) {
let updatableComponent = this._components[i]; let updatableComponent = this._components.buffer[i];
if (updatableComponent.enabled && if (updatableComponent.enabled &&
(updatableComponent.updateInterval == 1 || (updatableComponent.updateInterval == 1 ||
@@ -256,8 +256,8 @@ module es {
public onEntityTransformChanged(comp: transform.Component) { public onEntityTransformChanged(comp: transform.Component) {
for (let i = 0; i < this._components.length; i++) { for (let i = 0; i < this._components.length; i++) {
if (this._components[i].enabled) if (this._components.buffer[i].enabled)
this._components[i].onEntityTransformChanged(comp); this._components.buffer[i].onEntityTransformChanged(comp);
} }
for (let i = 0; i < this._componentsToAdd.length; i++) { for (let i = 0; i < this._componentsToAdd.length; i++) {
@@ -268,18 +268,18 @@ module es {
public onEntityEnabled() { public onEntityEnabled() {
for (let i = 0; i < this._components.length; i++) for (let i = 0; i < this._components.length; i++)
this._components[i].onEnabled(); this._components.buffer[i].onEnabled();
} }
public onEntityDisabled() { public onEntityDisabled() {
for (let i = 0; i < this._components.length; i++) for (let i = 0; i < this._components.length; i++)
this._components[i].onDisabled(); this._components.buffer[i].onDisabled();
} }
public debugRender(camera: Camera){ public debugRender(camera: Camera){
for (let i = 0; i < this._components.length; i ++){ for (let i = 0; i < this._components.length; i ++){
if (this._components[i].enabled) if (this._components.buffer[i].enabled)
this._components[i].debugRender(camera); this._components.buffer[i].debugRender(camera);
} }
} }
} }
+30 -26
View File
@@ -2,26 +2,26 @@ module es {
export class EntityList { export class EntityList {
public scene: Scene; public scene: Scene;
/** /**
* *
*/ */
public _entities: Entity[] = []; public _entities: Entity[] = [];
/** /**
* 便 * 便
*/ */
public _entitiesToAdded: Entity[] = []; public _entitiesToAdded: Entity[] = [];
/** /**
* 便 * 便
*/ */
public _entitiesToRemove: Entity[] = []; public _entitiesToRemove: Entity[] = [];
/** /**
* *
*/ */
public _isEntityListUnsorted: boolean; public _isEntityListUnsorted: boolean;
/** /**
* 便 * 便
*/ */
public _entityDict: Map<number, Entity[]> = new Map<number, Entity[]>(); public _entityDict: Map<number, Entity[]> = new Map<number, Entity[]>();
public _unsortedTags: number[] = []; public _unsortedTags: Set<number> = new Set<number>();
public _addToSceneEntityList: Entity[] = []; public _addToSceneEntityList: Entity[] = [];
/** 是否使用分帧处理 */ /** 是否使用分帧处理 */
public frameAllocate: boolean = false; public frameAllocate: boolean = false;
@@ -45,11 +45,11 @@ module es {
} }
public markTagUnsorted(tag: number) { public markTagUnsorted(tag: number) {
this._unsortedTags.push(tag); this._unsortedTags.add(tag);
} }
/** /**
* *
* @param entity * @param entity
*/ */
public add(entity: Entity) { public add(entity: Entity) {
@@ -58,12 +58,12 @@ module es {
} }
/** /**
* *
* @param entity * @param entity
*/ */
public remove(entity: Entity) { public remove(entity: Entity) {
if (!this._entitiesToRemove.contains(entity)) { if (!this._entitiesToRemove.contains(entity)) {
console.warn(`You are trying to remove an entity (${entity.name}) that you already removed`); console.warn(`您正在尝试删除已经删除的实体(${entity.name})`);
return; return;
} }
@@ -81,12 +81,12 @@ module es {
* *
*/ */
public removeAllEntities() { public removeAllEntities() {
this._unsortedTags.length = 0; this._unsortedTags.clear();
this._entitiesToAdded.length = 0; this._entitiesToAdded.length = 0;
this._isEntityListUnsorted = false; this._isEntityListUnsorted = false;
// 为什么我们要在这里更新列表?主要用于处理场景切换前分离的实体。 // 为什么我们要在这里更新列表主要是为了处理场景切换前分离的实体。
// 它们仍然在_entitiesToRemove列表中,该列表将由更新列表处理。 // 它们仍然在_entitiesToRemove列表中,这将由updateLists处理。
this.updateLists(); this.updateLists();
for (let i = 0; i < this._entities.length; i++) { for (let i = 0; i < this._entities.length; i++) {
@@ -100,7 +100,7 @@ module es {
} }
/** /**
* EntityList管理 * EntityList管理
* @param entity * @param entity
*/ */
public contains(entity: Entity): boolean { public contains(entity: Entity): boolean {
@@ -122,8 +122,7 @@ module es {
let list = this.getTagList(entity.tag); let list = this.getTagList(entity.tag);
if (list.findIndex(e => e.id == entity.id) == -1) { if (list.findIndex(e => e.id == entity.id) == -1) {
list.push(entity); list.push(entity);
if (!this._unsortedTags.contains(entity.tag)) this._unsortedTags.add(entity.tag);
this._unsortedTags.push(entity.tag);
} }
} }
@@ -157,7 +156,7 @@ module es {
this._entitiesToRemove.length = 0; this._entitiesToRemove.length = 0;
} }
// 现在所有实体都被添加到场景中,我们再次循环并调用onAddedToScene // 现在所有实体都被添加到场景中,我们再次循环并调用onAddedToScene
while (this._addToSceneEntityList.length > 0){ while (this._addToSceneEntityList.length > 0){
let entity = this._addToSceneEntityList.shift(); let entity = this._addToSceneEntityList.shift();
entity.onAddedToScene(); entity.onAddedToScene();
@@ -187,14 +186,15 @@ module es {
this._isEntityListUnsorted = false; this._isEntityListUnsorted = false;
} }
if (this._addToSceneEntityList.length == 0 && this._unsortedTags.length > 0) { // 根据需要对标签列表进行排序
for (const tag of this._unsortedTags) { if (this._addToSceneEntityList.length == 0 && this._unsortedTags.size > 0) {
this._unsortedTags.forEach((tag)=>{
this._entityDict.get(tag).sort((a, b) => { this._entityDict.get(tag).sort((a, b) => {
return a.compareTo(b); return a.compareTo(b);
}); });
} });
this._unsortedTags.length = 0; this._unsortedTags.clear();
} }
} }
@@ -214,7 +214,7 @@ module es {
} }
/** /**
* null * name的实体null
* @param name * @param name
*/ */
public findEntity(name: string) { public findEntity(name: string) {
@@ -227,13 +227,15 @@ module es {
} }
/** /**
* ListPool.free将返回的列表放回池中 *
* List可以通过ListPool.free放回池中
* @param tag * @param tag
*/ */
public entitiesWithTag(tag: number) { public entitiesWithTag(tag: number) {
let list = this.getTagList(tag); let list = this.getTagList(tag);
let returnList = ListPool.obtain<Entity>(); let returnList = ListPool.obtain<Entity>();
returnList.length = this._entities.length;
for (let i = 0; i < list.length; i++) for (let i = 0; i < list.length; i++)
returnList.push(list[i]); returnList.push(list[i]);
@@ -241,7 +243,8 @@ module es {
} }
/** /**
* t类型的所有实体的列表ListPool.free放回池中 * T类型的所有实体的列表
* List可以通过ListPool.free放回池中
* @param type * @param type
*/ */
public entitiesOfType<T extends Entity>(type): T[] { public entitiesOfType<T extends Entity>(type): T[] {
@@ -259,7 +262,7 @@ module es {
} }
/** /**
* T的场景中找到的第一个组件 * T类型的组件
* @param type * @param type
*/ */
public findComponentOfType<T extends Component>(type): T { public findComponentOfType<T extends Component>(type): T {
@@ -284,7 +287,8 @@ module es {
} }
/** /**
* t的场景中找到的所有组件ListPool.free放回池中 * T类型的组件
* List可以通过ListPool.free放回池中
* @param type * @param type
*/ */
public findComponentsOfType<T extends Component>(type): T[] { public findComponentsOfType<T extends Component>(type): T[] {
+1 -1
View File
@@ -1,6 +1,6 @@
module es { module es {
export class EntityProcessorList { export class EntityProcessorList {
private _processors: EntitySystem[] = []; protected _processors: EntitySystem[] = [];
public add(processor: EntitySystem) { public add(processor: EntitySystem) {
this._processors.push(processor); this._processors.push(processor);
+116
View File
@@ -0,0 +1,116 @@
module es {
/**
*
* 访使FastList.length字段
*
* @tutorial
* for( var i = 0; i <= list.length; i++ )
* var item = list.buffer[i];
*/
export class FastList<T> {
/**
* 访
* 使buffer.Length! 使FastList.length
*/
public buffer: T[];
/**
* 访
*/
public length: number = 0;
constructor(size: number = 5) {
this.buffer = new Array(size);
}
/**
*
*/
public clear() {
this.buffer.length = 0;
this.length = 0;
}
/**
* clear的工作原理一样
*/
public reset() {
this.length = 0;
}
/**
*
* @param item
*/
public add(item: T) {
if (this.length == this.buffer.length)
this.buffer.length = Math.max(this.buffer.length << 1, 10);
this.buffer[this.length++] = item;
}
/**
*
* @param item
*/
public remove(item: T){
let comp = EqualityComparer.default<T>();
for (let i = 0; i < this.length; ++i){
if (comp.equals(this.buffer[i], item)){
this.removeAt(i);
return;
}
}
}
/**
*
* @param index
*/
public removeAt(index: number){
if (index >= this.length)
throw new Error("index超出范围!");
this.length --;
this.buffer.removeAt(index);
}
/**
* FastList中
* @param item
*/
public contains(item: T){
let comp = EqualityComparer.default<T>();
for (let i = 0; i < this.length; ++ i){
if (comp.equals(this.buffer[i], item))
return true;
}
return false;
}
/**
* ItemCount
* @param additionalItemCount
*/
public ensureCapacity(additionalItemCount: number = 1){
if (this.length + additionalItemCount >= this.buffer.length)
this.buffer.length = Math.max(this.buffer.length << 1, this.length + additionalItemCount);
}
/**
*
* @param array
*/
public addRange(array: T[]){
for (let item of array)
this.add(item);
}
/**
*
*/
public sort(comparer: IComparer<T>){
this.buffer.sort(comparer.compare);
}
}
}
+35 -18
View File
@@ -1,6 +1,6 @@
module es { module es {
/** 2d 向量 */ /** 2d 向量 */
export class Vector2 { export class Vector2 implements IEquatable<Vector2> {
public x: number = 0; public x: number = 0;
public y: number = 0; public y: number = 0;
@@ -111,7 +111,7 @@ module es {
} }
/** /**
* *
* @param value1 * @param value1
* @param min * @param min
* @param max * @param max
@@ -122,17 +122,18 @@ module es {
} }
/** /**
* 线 * Vector2线
* @param value1 * @param value1
* @param value2 * @param value2
* @param amount (0.01.0) * @param amount (0.0-1.0)
* @returns 线
*/ */
public static lerp(value1: Vector2, value2: Vector2, amount: number) { public static lerp(value1: Vector2, value2: Vector2, amount: number) {
return new Vector2(MathHelper.lerp(value1.x, value2.x, amount), MathHelper.lerp(value1.y, value2.y, amount)); return new Vector2(MathHelper.lerp(value1.x, value2.x, amount), MathHelper.lerp(value1.y, value2.y, amount));
} }
/** /**
* * Vector2Vector2包含了通过指定的Matrix进行的二维向量变换
* @param position * @param position
* @param matrix * @param matrix
*/ */
@@ -145,8 +146,9 @@ module es {
* *
* @param value1 * @param value1
* @param value2 * @param value2
* @returns
*/ */
public static distance(value1: Vector2, value2: Vector2) { public static distance(value1: Vector2, value2: Vector2): number {
let v1 = value1.x - value2.x, v2 = value1.y - value2.y; let v1 = value1.x - value2.x, v2 = value1.y - value2.y;
return Math.sqrt((v1 * v1) + (v2 * v2)); return Math.sqrt((v1 * v1) + (v2 * v2));
} }
@@ -163,15 +165,15 @@ module es {
} }
/** /**
* * Vector2
* @param value * @param value
* @returns
*/ */
public static negate(value: Vector2) { public static negate(value: Vector2) {
let result: Vector2 = new Vector2(); value.x = -value.x;
result.x = -value.x; value.y = -value.y;
result.y = -value.y;
return result; return value;
} }
/** /**
@@ -205,8 +207,9 @@ module es {
} }
/** /**
* * Vector2减去一个Vector2
* @param value * @param value Vector2
* @returns Vector2
*/ */
public subtract(value: Vector2) { public subtract(value: Vector2) {
this.x -= value.x; this.x -= value.x;
@@ -214,7 +217,9 @@ module es {
return this; return this;
} }
/** 变成一个方向相同的单位向量 */ /**
* Vector2变成一个方向相同的单位向量
*/
public normalize() { public normalize() {
let val = 1 / Math.sqrt((this.x * this.x) + (this.y * this.y)); let val = 1 / Math.sqrt((this.x * this.x) + (this.y * this.y));
this.x *= val; this.x *= val;
@@ -227,19 +232,31 @@ module es {
} }
/** /**
* * Vector2的平方长度
* @returns Vector2的平方长度
*/ */
public lengthSquared(): number { public lengthSquared(): number {
return (this.x * this.x) + (this.y * this.y); return (this.x * this.x) + (this.y * this.y);
} }
/** 对x和y值四舍五入 */ /**
* X和Y值
*/
public round(): Vector2 { public round(): Vector2 {
return new Vector2(Math.round(this.x), Math.round(this.y)); return new Vector2(Math.round(this.x), Math.round(this.y));
} }
public equals(other: Vector2) { /**
return other.x == this.x && other.y == this.y; *
* @param other
* @returns true false
*/
public equals(other: Vector2 | object): boolean {
if (other instanceof Vector2){
return other.x == this.x && other.y == this.y;
}
return false;
} }
} }
} }
+17
View File
@@ -0,0 +1,17 @@
module es {
export class EqualityComparer<T> implements IEqualityComparer {
public static default<T>(){
return new EqualityComparer<T>();
}
protected constructor(){ }
public equals(x: T, y: T): boolean{
if (typeof x["equals"] == 'function'){
return x["equals"](y);
} else {
return x === y;
}
}
}
}
+5
View File
@@ -0,0 +1,5 @@
module es {
export interface IComparer<T>{
compare(x: T, y: T): number;
}
}
+5
View File
@@ -0,0 +1,5 @@
module es {
export interface IEqualityComparer {
equals(x: any, y: any): boolean;
}
}
+8
View File
@@ -0,0 +1,8 @@
module es {
/**
*
*/
export interface IEquatable<T> {
equals(other: T): boolean;
}
}
+3 -2
View File
@@ -2,7 +2,7 @@ module es {
/** /**
* DTO * DTO
*/ */
export class Pair<T> { export class Pair<T> implements IEquatable<Pair<T>> {
public first: T; public first: T;
public second: T; public second: T;
@@ -15,7 +15,8 @@ module es {
this.first = this.second = null; this.first = this.second = null;
} }
public equals(other: Pair<T>) { public equals(other: Pair<T>): boolean {
// 这两种方法在功能上应该是等价的
return this.first == other.first && this.second == other.second; return this.first == other.first && this.second == other.second;
} }
} }