新增componentlist管理组件列表

This commit is contained in:
YHH
2020-06-08 23:04:57 +08:00
parent 11af0a31a7
commit 262e16bb88
12 changed files with 659 additions and 29 deletions

View File

@@ -24,6 +24,11 @@ declare abstract class Component {
enabled: boolean; enabled: boolean;
setEnabled(isEnabled: boolean): this; setEnabled(isEnabled: boolean): this;
abstract initialize(): any; abstract initialize(): any;
onAddedToEntity(): void;
onRemovedFromEntity(): void;
onEnabled(): void;
onDisabled(): void;
onEntityTransformChanged(comp: ComponentTransform): void;
update(): void; update(): void;
bind(displayRender: egret.DisplayObject): this; bind(displayRender: egret.DisplayObject): this;
registerComponent(): void; registerComponent(): void;
@@ -33,19 +38,25 @@ declare class Entity {
name: string; name: string;
scene: Scene; scene: Scene;
readonly transform: Transform; readonly transform: Transform;
readonly components: Component[]; readonly components: ComponentList;
private _updateOrder; private _updateOrder;
private _enabled; private _enabled;
private _isDestoryed;
componentBits: BitSet; componentBits: BitSet;
readonly isDestoryed: boolean;
enabled: boolean; enabled: boolean;
setEnabled(isEnabled: boolean): this; setEnabled(isEnabled: boolean): this;
constructor(name: string); constructor(name: string);
updateOrder: number; updateOrder: number;
setUpdateOrder(updateOrder: number): this; setUpdateOrder(updateOrder: number): this;
attachToScene(newScene: Scene): void; attachToScene(newScene: Scene): void;
detachFromScene(): void;
addComponent<T extends Component>(component: T): T; addComponent<T extends Component>(component: T): T;
getComponent<T extends Component>(type: any): T; getComponent<T extends Component>(type: any): T;
update(): void; update(): void;
onAddedToScene(): void;
onRemovedFromScene(): void;
onTransformChanged(comp: ComponentTransform): void;
destory(): void; destory(): void;
} }
declare class Scene extends egret.DisplayObjectContainer { declare class Scene extends egret.DisplayObjectContainer {
@@ -85,6 +96,11 @@ declare enum DirtyType {
scaleDirty = 2, scaleDirty = 2,
rotationDirty = 3 rotationDirty = 3
} }
declare enum ComponentTransform {
position = 0,
scale = 1,
rotation = 2
}
declare class Transform { declare class Transform {
readonly entity: Entity; readonly entity: Entity;
private _children; private _children;
@@ -119,6 +135,7 @@ declare class Transform {
localPosition: Vector2; localPosition: Vector2;
setLocalPosition(localPosition: Vector2): this; setLocalPosition(localPosition: Vector2): this;
setPosition(position: Vector2): this; setPosition(position: Vector2): this;
setDirty(dirtyFlagType: DirtyType): void;
updateTransform(): void; updateTransform(): void;
} }
declare class Camera extends Component { declare class Camera extends Component {
@@ -176,6 +193,25 @@ declare class BitSet {
nextSetBit(from: number): number; nextSetBit(from: number): number;
set(pos: number, value?: boolean): void; set(pos: number, value?: boolean): void;
} }
declare class ComponentList {
private _entity;
private _components;
private _componentsToAdd;
private _componentsToRemove;
private _tempBufferList;
constructor(entity: Entity);
readonly buffer: Component[];
add(component: Component): void;
remove(component: Component): void;
removeAllComponents(): void;
deregisterAllComponents(): void;
registerAllComponents(): void;
updateLists(): void;
private handleRemove;
getComponent<T extends Component>(type: any, onlyReturnInitializedComponents: boolean): T;
update(): void;
onEntityTransformChanged(comp: any): void;
}
declare class ComponentTypeManager { declare class ComponentTypeManager {
private static _componentTypesMask; private static _componentTypesMask;
static add(type: any): void; static add(type: any): void;

View File

@@ -258,6 +258,16 @@ var Component = (function () {
} }
return this; return this;
}; };
Component.prototype.onAddedToEntity = function () {
};
Component.prototype.onRemovedFromEntity = function () {
};
Component.prototype.onEnabled = function () {
};
Component.prototype.onDisabled = function () {
};
Component.prototype.onEntityTransformChanged = function (comp) {
};
Component.prototype.update = function () { Component.prototype.update = function () {
}; };
Component.prototype.bind = function (displayRender) { Component.prototype.bind = function (displayRender) {
@@ -280,9 +290,16 @@ var Entity = (function () {
this._enabled = true; this._enabled = true;
this.name = name; this.name = name;
this.transform = new Transform(this); this.transform = new Transform(this);
this.components = []; this.components = new ComponentList(this);
this.componentBits = new BitSet(); this.componentBits = new BitSet();
} }
Object.defineProperty(Entity.prototype, "isDestoryed", {
get: function () {
return this._isDestoryed;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Entity.prototype, "enabled", { Object.defineProperty(Entity.prototype, "enabled", {
get: function () { get: function () {
return this._enabled; return this._enabled;
@@ -320,25 +337,41 @@ var Entity = (function () {
Entity.prototype.attachToScene = function (newScene) { Entity.prototype.attachToScene = function (newScene) {
this.scene = newScene; this.scene = newScene;
newScene.entities.add(this); newScene.entities.add(this);
this.components.forEach(function (component) { return component.registerComponent(); }); this.components.registerAllComponents();
for (var i = 0; i < this.transform.childCount; i++) { for (var i = 0; i < this.transform.childCount; i++) {
this.transform.getChild(i).entity.attachToScene(newScene); this.transform.getChild(i).entity.attachToScene(newScene);
} }
}; };
Entity.prototype.detachFromScene = function () {
this.scene.entities.remove(this);
this.components.deregisterAllComponents();
for (var i = 0; i < this.transform.childCount; i++)
this.transform.getChild(i).entity.detachFromScene();
};
Entity.prototype.addComponent = function (component) { Entity.prototype.addComponent = function (component) {
component.entity = this; component.entity = this;
this.components.push(component); this.components.add(component);
component.initialize(); component.initialize();
return component; return component;
}; };
Entity.prototype.getComponent = function (type) { Entity.prototype.getComponent = function (type) {
return this.components.firstOrDefault(function (component) { return component instanceof type; }); return this.components.getComponent(type, false);
}; };
Entity.prototype.update = function () { Entity.prototype.update = function () {
this.components.forEach(function (component) { return component.update(); }); this.components.update();
this.transform.updateTransform(); this.transform.updateTransform();
}; };
Entity.prototype.onAddedToScene = function () {
};
Entity.prototype.onRemovedFromScene = function () {
if (this._isDestoryed)
this.components.remove;
};
Entity.prototype.onTransformChanged = function (comp) {
this.components.onEntityTransformChanged(comp);
};
Entity.prototype.destory = function () { Entity.prototype.destory = function () {
this._isDestoryed = true;
this.scene.entities.remove(this); this.scene.entities.remove(this);
this.transform.parent = null; this.transform.parent = null;
for (var i = this.transform.childCount - 1; i >= 0; i--) { for (var i = this.transform.childCount - 1; i >= 0; i--) {
@@ -461,6 +494,12 @@ var DirtyType;
DirtyType[DirtyType["scaleDirty"] = 2] = "scaleDirty"; DirtyType[DirtyType["scaleDirty"] = 2] = "scaleDirty";
DirtyType[DirtyType["rotationDirty"] = 3] = "rotationDirty"; DirtyType[DirtyType["rotationDirty"] = 3] = "rotationDirty";
})(DirtyType || (DirtyType = {})); })(DirtyType || (DirtyType = {}));
var ComponentTransform;
(function (ComponentTransform) {
ComponentTransform[ComponentTransform["position"] = 0] = "position";
ComponentTransform[ComponentTransform["scale"] = 1] = "scale";
ComponentTransform[ComponentTransform["rotation"] = 2] = "rotation";
})(ComponentTransform || (ComponentTransform = {}));
var Transform = (function () { var Transform = (function () {
function Transform(entity) { function Transform(entity) {
this._localRotation = 0; this._localRotation = 0;
@@ -536,6 +575,7 @@ var Transform = (function () {
return this; return this;
this._localPosition = localPosition; this._localPosition = localPosition;
this._localDirty = this._positionDirty = this._localPositionDirty = this._localRotationDirty = this._localScaleDirty = true; this._localDirty = this._positionDirty = this._localPositionDirty = this._localRotationDirty = this._localScaleDirty = true;
this.setDirty(DirtyType.positionDirty);
return this; return this;
}; };
Transform.prototype.setPosition = function (position) { Transform.prototype.setPosition = function (position) {
@@ -548,8 +588,8 @@ var Transform = (function () {
else { else {
this.localPosition = position; this.localPosition = position;
} }
for (var i = 0; i < this.entity.components.length; i++) { for (var i = 0; i < this.entity.components.buffer.length; i++) {
var component = this.entity.components[i]; var component = this.entity.components.buffer[i];
if (component.displayRender) { if (component.displayRender) {
component.displayRender.x = this.entity.scene.camera.transformMatrix.m31 + this.position.x; component.displayRender.x = this.entity.scene.camera.transformMatrix.m31 + this.position.x;
component.displayRender.y = this.entity.scene.camera.transformMatrix.m32 + this.position.y; component.displayRender.y = this.entity.scene.camera.transformMatrix.m32 + this.position.y;
@@ -557,6 +597,27 @@ var Transform = (function () {
} }
return this; return this;
}; };
Transform.prototype.setDirty = function (dirtyFlagType) {
if ((this._hierachyDirty & dirtyFlagType) == 0) {
this._hierachyDirty |= dirtyFlagType;
switch (dirtyFlagType) {
case DirtyType.positionDirty:
this.entity.onTransformChanged(ComponentTransform.position);
break;
case DirtyType.rotationDirty:
this.entity.onTransformChanged(ComponentTransform.rotation);
break;
case DirtyType.scaleDirty:
this.entity.onTransformChanged(ComponentTransform.scale);
break;
}
if (this._children == null)
this._children = [];
for (var i = 0; i < this._children.length; i++) {
this._children[i].setDirty(dirtyFlagType);
}
}
};
Transform.prototype.updateTransform = function () { Transform.prototype.updateTransform = function () {
if (this._hierachyDirty != DirtyType.clean) { if (this._hierachyDirty != DirtyType.clean) {
if (this.parent) if (this.parent)
@@ -617,7 +678,7 @@ var Camera = (function (_super) {
}; };
Camera.prototype.update = function () { Camera.prototype.update = function () {
var _this = this; var _this = this;
SceneManager.getActiveScene().entities.buffer.forEach(function (entity) { return entity.components.forEach(function (component) { SceneManager.getActiveScene().entities.buffer.forEach(function (entity) { return entity.components.buffer.forEach(function (component) {
if (component.displayRender) { if (component.displayRender) {
var has = _this.entity.scene.$children.indexOf(component.displayRender); var has = _this.entity.scene.$children.indexOf(component.displayRender);
if (has == -1) { if (has == -1) {
@@ -826,6 +887,116 @@ var BitSet = (function () {
BitSet.LONG_MASK = 0x3f; BitSet.LONG_MASK = 0x3f;
return BitSet; return BitSet;
}()); }());
var ComponentList = (function () {
function ComponentList(entity) {
this._components = [];
this._componentsToAdd = [];
this._componentsToRemove = [];
this._tempBufferList = [];
this._entity = entity;
}
Object.defineProperty(ComponentList.prototype, "buffer", {
get: function () {
return this._components;
},
enumerable: true,
configurable: true
});
ComponentList.prototype.add = function (component) {
this._componentsToAdd.push(component);
};
ComponentList.prototype.remove = function (component) {
if (this._componentsToAdd.contains(component)) {
this._componentsToAdd.remove(component);
return;
}
this._componentsToRemove.push(component);
};
ComponentList.prototype.removeAllComponents = function () {
for (var i = 0; i < this._components.length; i++) {
this.handleRemove(this._components[i]);
}
this._components.length = 0;
this._componentsToAdd.length = 0;
this._componentsToRemove.length = 0;
};
ComponentList.prototype.deregisterAllComponents = function () {
for (var i = 0; i < this._components.length; i++) {
var component = this._components[i];
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component), false);
this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
}
};
ComponentList.prototype.registerAllComponents = function () {
for (var i = 0; i < this._components.length; i++) {
var component = this._components[i];
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component));
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
}
};
ComponentList.prototype.updateLists = function () {
if (this._componentsToRemove.length > 0) {
for (var i = 0; i < this._componentsToRemove.length; i++) {
this.handleRemove(this._componentsToRemove[i]);
this._components.remove(this._componentsToRemove[i]);
}
this._componentsToRemove.length = 0;
}
if (this._componentsToAdd.length > 0) {
for (var i = 0, count = this._componentsToAdd.length; i < count; i++) {
var component = this._componentsToAdd[i];
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component));
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
this._components.push(component);
this._tempBufferList.push(component);
}
this._componentsToAdd.length = 0;
for (var i = 0; i < this._tempBufferList.length; i++) {
var component = this._tempBufferList[i];
component.onAddedToEntity();
if (component.enabled) {
component.onEnabled();
}
}
this._tempBufferList.length = 0;
}
};
ComponentList.prototype.handleRemove = function (component) {
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component), false);
this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
component.onRemovedFromEntity();
component.entity = null;
};
ComponentList.prototype.getComponent = function (type, onlyReturnInitializedComponents) {
for (var i = 0; i < this._components.length; i++) {
var component = this._components[i];
if (component instanceof type)
return component;
}
if (!onlyReturnInitializedComponents) {
for (var i = 0; i < this._componentsToAdd.length; i++) {
var component = this._componentsToAdd[i];
if (component instanceof type)
return component;
}
}
return null;
};
ComponentList.prototype.update = function () {
this.updateLists();
};
ComponentList.prototype.onEntityTransformChanged = function (comp) {
for (var i = 0; i < this._components.length; i++) {
if (this._components[i].enabled)
this._components[i].onEntityTransformChanged(comp);
}
for (var i = 0; i < this._componentsToAdd.length; i++) {
if (this._componentsToAdd[i].enabled)
this._componentsToAdd[i].onEntityTransformChanged(comp);
}
};
return ComponentList;
}());
var ComponentTypeManager = (function () { var ComponentTypeManager = (function () {
function ComponentTypeManager() { function ComponentTypeManager() {
} }
@@ -921,6 +1092,7 @@ var EntityList = (function () {
entity.scene = _this.scene; entity.scene = _this.scene;
_this.scene.entityProcessors.onEntityAdded(entity); _this.scene.entityProcessors.onEntityAdded(entity);
}); });
this._tempEntityList.forEach(function (entity) { return entity.onAddedToScene(); });
this._tempEntityList.length = 0; this._tempEntityList.length = 0;
} }
}; };

File diff suppressed because one or more lines are too long

View File

@@ -24,6 +24,11 @@ declare abstract class Component {
enabled: boolean; enabled: boolean;
setEnabled(isEnabled: boolean): this; setEnabled(isEnabled: boolean): this;
abstract initialize(): any; abstract initialize(): any;
onAddedToEntity(): void;
onRemovedFromEntity(): void;
onEnabled(): void;
onDisabled(): void;
onEntityTransformChanged(comp: ComponentTransform): void;
update(): void; update(): void;
bind(displayRender: egret.DisplayObject): this; bind(displayRender: egret.DisplayObject): this;
registerComponent(): void; registerComponent(): void;
@@ -33,19 +38,25 @@ declare class Entity {
name: string; name: string;
scene: Scene; scene: Scene;
readonly transform: Transform; readonly transform: Transform;
readonly components: Component[]; readonly components: ComponentList;
private _updateOrder; private _updateOrder;
private _enabled; private _enabled;
private _isDestoryed;
componentBits: BitSet; componentBits: BitSet;
readonly isDestoryed: boolean;
enabled: boolean; enabled: boolean;
setEnabled(isEnabled: boolean): this; setEnabled(isEnabled: boolean): this;
constructor(name: string); constructor(name: string);
updateOrder: number; updateOrder: number;
setUpdateOrder(updateOrder: number): this; setUpdateOrder(updateOrder: number): this;
attachToScene(newScene: Scene): void; attachToScene(newScene: Scene): void;
detachFromScene(): void;
addComponent<T extends Component>(component: T): T; addComponent<T extends Component>(component: T): T;
getComponent<T extends Component>(type: any): T; getComponent<T extends Component>(type: any): T;
update(): void; update(): void;
onAddedToScene(): void;
onRemovedFromScene(): void;
onTransformChanged(comp: ComponentTransform): void;
destory(): void; destory(): void;
} }
declare class Scene extends egret.DisplayObjectContainer { declare class Scene extends egret.DisplayObjectContainer {
@@ -85,6 +96,11 @@ declare enum DirtyType {
scaleDirty = 2, scaleDirty = 2,
rotationDirty = 3 rotationDirty = 3
} }
declare enum ComponentTransform {
position = 0,
scale = 1,
rotation = 2
}
declare class Transform { declare class Transform {
readonly entity: Entity; readonly entity: Entity;
private _children; private _children;
@@ -119,6 +135,7 @@ declare class Transform {
localPosition: Vector2; localPosition: Vector2;
setLocalPosition(localPosition: Vector2): this; setLocalPosition(localPosition: Vector2): this;
setPosition(position: Vector2): this; setPosition(position: Vector2): this;
setDirty(dirtyFlagType: DirtyType): void;
updateTransform(): void; updateTransform(): void;
} }
declare class Camera extends Component { declare class Camera extends Component {
@@ -176,6 +193,25 @@ declare class BitSet {
nextSetBit(from: number): number; nextSetBit(from: number): number;
set(pos: number, value?: boolean): void; set(pos: number, value?: boolean): void;
} }
declare class ComponentList {
private _entity;
private _components;
private _componentsToAdd;
private _componentsToRemove;
private _tempBufferList;
constructor(entity: Entity);
readonly buffer: Component[];
add(component: Component): void;
remove(component: Component): void;
removeAllComponents(): void;
deregisterAllComponents(): void;
registerAllComponents(): void;
updateLists(): void;
private handleRemove;
getComponent<T extends Component>(type: any, onlyReturnInitializedComponents: boolean): T;
update(): void;
onEntityTransformChanged(comp: any): void;
}
declare class ComponentTypeManager { declare class ComponentTypeManager {
private static _componentTypesMask; private static _componentTypesMask;
static add(type: any): void; static add(type: any): void;

View File

@@ -258,6 +258,16 @@ var Component = (function () {
} }
return this; return this;
}; };
Component.prototype.onAddedToEntity = function () {
};
Component.prototype.onRemovedFromEntity = function () {
};
Component.prototype.onEnabled = function () {
};
Component.prototype.onDisabled = function () {
};
Component.prototype.onEntityTransformChanged = function (comp) {
};
Component.prototype.update = function () { Component.prototype.update = function () {
}; };
Component.prototype.bind = function (displayRender) { Component.prototype.bind = function (displayRender) {
@@ -280,9 +290,16 @@ var Entity = (function () {
this._enabled = true; this._enabled = true;
this.name = name; this.name = name;
this.transform = new Transform(this); this.transform = new Transform(this);
this.components = []; this.components = new ComponentList(this);
this.componentBits = new BitSet(); this.componentBits = new BitSet();
} }
Object.defineProperty(Entity.prototype, "isDestoryed", {
get: function () {
return this._isDestoryed;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Entity.prototype, "enabled", { Object.defineProperty(Entity.prototype, "enabled", {
get: function () { get: function () {
return this._enabled; return this._enabled;
@@ -320,25 +337,41 @@ var Entity = (function () {
Entity.prototype.attachToScene = function (newScene) { Entity.prototype.attachToScene = function (newScene) {
this.scene = newScene; this.scene = newScene;
newScene.entities.add(this); newScene.entities.add(this);
this.components.forEach(function (component) { return component.registerComponent(); }); this.components.registerAllComponents();
for (var i = 0; i < this.transform.childCount; i++) { for (var i = 0; i < this.transform.childCount; i++) {
this.transform.getChild(i).entity.attachToScene(newScene); this.transform.getChild(i).entity.attachToScene(newScene);
} }
}; };
Entity.prototype.detachFromScene = function () {
this.scene.entities.remove(this);
this.components.deregisterAllComponents();
for (var i = 0; i < this.transform.childCount; i++)
this.transform.getChild(i).entity.detachFromScene();
};
Entity.prototype.addComponent = function (component) { Entity.prototype.addComponent = function (component) {
component.entity = this; component.entity = this;
this.components.push(component); this.components.add(component);
component.initialize(); component.initialize();
return component; return component;
}; };
Entity.prototype.getComponent = function (type) { Entity.prototype.getComponent = function (type) {
return this.components.firstOrDefault(function (component) { return component instanceof type; }); return this.components.getComponent(type, false);
}; };
Entity.prototype.update = function () { Entity.prototype.update = function () {
this.components.forEach(function (component) { return component.update(); }); this.components.update();
this.transform.updateTransform(); this.transform.updateTransform();
}; };
Entity.prototype.onAddedToScene = function () {
};
Entity.prototype.onRemovedFromScene = function () {
if (this._isDestoryed)
this.components.remove;
};
Entity.prototype.onTransformChanged = function (comp) {
this.components.onEntityTransformChanged(comp);
};
Entity.prototype.destory = function () { Entity.prototype.destory = function () {
this._isDestoryed = true;
this.scene.entities.remove(this); this.scene.entities.remove(this);
this.transform.parent = null; this.transform.parent = null;
for (var i = this.transform.childCount - 1; i >= 0; i--) { for (var i = this.transform.childCount - 1; i >= 0; i--) {
@@ -461,6 +494,12 @@ var DirtyType;
DirtyType[DirtyType["scaleDirty"] = 2] = "scaleDirty"; DirtyType[DirtyType["scaleDirty"] = 2] = "scaleDirty";
DirtyType[DirtyType["rotationDirty"] = 3] = "rotationDirty"; DirtyType[DirtyType["rotationDirty"] = 3] = "rotationDirty";
})(DirtyType || (DirtyType = {})); })(DirtyType || (DirtyType = {}));
var ComponentTransform;
(function (ComponentTransform) {
ComponentTransform[ComponentTransform["position"] = 0] = "position";
ComponentTransform[ComponentTransform["scale"] = 1] = "scale";
ComponentTransform[ComponentTransform["rotation"] = 2] = "rotation";
})(ComponentTransform || (ComponentTransform = {}));
var Transform = (function () { var Transform = (function () {
function Transform(entity) { function Transform(entity) {
this._localRotation = 0; this._localRotation = 0;
@@ -536,6 +575,7 @@ var Transform = (function () {
return this; return this;
this._localPosition = localPosition; this._localPosition = localPosition;
this._localDirty = this._positionDirty = this._localPositionDirty = this._localRotationDirty = this._localScaleDirty = true; this._localDirty = this._positionDirty = this._localPositionDirty = this._localRotationDirty = this._localScaleDirty = true;
this.setDirty(DirtyType.positionDirty);
return this; return this;
}; };
Transform.prototype.setPosition = function (position) { Transform.prototype.setPosition = function (position) {
@@ -548,8 +588,8 @@ var Transform = (function () {
else { else {
this.localPosition = position; this.localPosition = position;
} }
for (var i = 0; i < this.entity.components.length; i++) { for (var i = 0; i < this.entity.components.buffer.length; i++) {
var component = this.entity.components[i]; var component = this.entity.components.buffer[i];
if (component.displayRender) { if (component.displayRender) {
component.displayRender.x = this.entity.scene.camera.transformMatrix.m31 + this.position.x; component.displayRender.x = this.entity.scene.camera.transformMatrix.m31 + this.position.x;
component.displayRender.y = this.entity.scene.camera.transformMatrix.m32 + this.position.y; component.displayRender.y = this.entity.scene.camera.transformMatrix.m32 + this.position.y;
@@ -557,6 +597,27 @@ var Transform = (function () {
} }
return this; return this;
}; };
Transform.prototype.setDirty = function (dirtyFlagType) {
if ((this._hierachyDirty & dirtyFlagType) == 0) {
this._hierachyDirty |= dirtyFlagType;
switch (dirtyFlagType) {
case DirtyType.positionDirty:
this.entity.onTransformChanged(ComponentTransform.position);
break;
case DirtyType.rotationDirty:
this.entity.onTransformChanged(ComponentTransform.rotation);
break;
case DirtyType.scaleDirty:
this.entity.onTransformChanged(ComponentTransform.scale);
break;
}
if (this._children == null)
this._children = [];
for (var i = 0; i < this._children.length; i++) {
this._children[i].setDirty(dirtyFlagType);
}
}
};
Transform.prototype.updateTransform = function () { Transform.prototype.updateTransform = function () {
if (this._hierachyDirty != DirtyType.clean) { if (this._hierachyDirty != DirtyType.clean) {
if (this.parent) if (this.parent)
@@ -617,7 +678,7 @@ var Camera = (function (_super) {
}; };
Camera.prototype.update = function () { Camera.prototype.update = function () {
var _this = this; var _this = this;
SceneManager.getActiveScene().entities.buffer.forEach(function (entity) { return entity.components.forEach(function (component) { SceneManager.getActiveScene().entities.buffer.forEach(function (entity) { return entity.components.buffer.forEach(function (component) {
if (component.displayRender) { if (component.displayRender) {
var has = _this.entity.scene.$children.indexOf(component.displayRender); var has = _this.entity.scene.$children.indexOf(component.displayRender);
if (has == -1) { if (has == -1) {
@@ -826,6 +887,116 @@ var BitSet = (function () {
BitSet.LONG_MASK = 0x3f; BitSet.LONG_MASK = 0x3f;
return BitSet; return BitSet;
}()); }());
var ComponentList = (function () {
function ComponentList(entity) {
this._components = [];
this._componentsToAdd = [];
this._componentsToRemove = [];
this._tempBufferList = [];
this._entity = entity;
}
Object.defineProperty(ComponentList.prototype, "buffer", {
get: function () {
return this._components;
},
enumerable: true,
configurable: true
});
ComponentList.prototype.add = function (component) {
this._componentsToAdd.push(component);
};
ComponentList.prototype.remove = function (component) {
if (this._componentsToAdd.contains(component)) {
this._componentsToAdd.remove(component);
return;
}
this._componentsToRemove.push(component);
};
ComponentList.prototype.removeAllComponents = function () {
for (var i = 0; i < this._components.length; i++) {
this.handleRemove(this._components[i]);
}
this._components.length = 0;
this._componentsToAdd.length = 0;
this._componentsToRemove.length = 0;
};
ComponentList.prototype.deregisterAllComponents = function () {
for (var i = 0; i < this._components.length; i++) {
var component = this._components[i];
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component), false);
this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
}
};
ComponentList.prototype.registerAllComponents = function () {
for (var i = 0; i < this._components.length; i++) {
var component = this._components[i];
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component));
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
}
};
ComponentList.prototype.updateLists = function () {
if (this._componentsToRemove.length > 0) {
for (var i = 0; i < this._componentsToRemove.length; i++) {
this.handleRemove(this._componentsToRemove[i]);
this._components.remove(this._componentsToRemove[i]);
}
this._componentsToRemove.length = 0;
}
if (this._componentsToAdd.length > 0) {
for (var i = 0, count = this._componentsToAdd.length; i < count; i++) {
var component = this._componentsToAdd[i];
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component));
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
this._components.push(component);
this._tempBufferList.push(component);
}
this._componentsToAdd.length = 0;
for (var i = 0; i < this._tempBufferList.length; i++) {
var component = this._tempBufferList[i];
component.onAddedToEntity();
if (component.enabled) {
component.onEnabled();
}
}
this._tempBufferList.length = 0;
}
};
ComponentList.prototype.handleRemove = function (component) {
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component), false);
this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
component.onRemovedFromEntity();
component.entity = null;
};
ComponentList.prototype.getComponent = function (type, onlyReturnInitializedComponents) {
for (var i = 0; i < this._components.length; i++) {
var component = this._components[i];
if (component instanceof type)
return component;
}
if (!onlyReturnInitializedComponents) {
for (var i = 0; i < this._componentsToAdd.length; i++) {
var component = this._componentsToAdd[i];
if (component instanceof type)
return component;
}
}
return null;
};
ComponentList.prototype.update = function () {
this.updateLists();
};
ComponentList.prototype.onEntityTransformChanged = function (comp) {
for (var i = 0; i < this._components.length; i++) {
if (this._components[i].enabled)
this._components[i].onEntityTransformChanged(comp);
}
for (var i = 0; i < this._componentsToAdd.length; i++) {
if (this._componentsToAdd[i].enabled)
this._componentsToAdd[i].onEntityTransformChanged(comp);
}
};
return ComponentList;
}());
var ComponentTypeManager = (function () { var ComponentTypeManager = (function () {
function ComponentTypeManager() { function ComponentTypeManager() {
} }
@@ -921,6 +1092,7 @@ var EntityList = (function () {
entity.scene = _this.scene; entity.scene = _this.scene;
_this.scene.entityProcessors.onEntityAdded(entity); _this.scene.entityProcessors.onEntityAdded(entity);
}); });
this._tempEntityList.forEach(function (entity) { return entity.onAddedToScene(); });
this._tempEntityList.length = 0; this._tempEntityList.length = 0;
} }
}; };

File diff suppressed because one or more lines are too long

View File

@@ -20,6 +20,26 @@ abstract class Component {
public abstract initialize(); public abstract initialize();
public onAddedToEntity(){
}
public onRemovedFromEntity(){
}
public onEnabled(){
}
public onDisabled(){
}
public onEntityTransformChanged(comp: ComponentTransform){
}
public update(){ public update(){
} }

View File

@@ -20,7 +20,7 @@ class Camera extends Component {
} }
public update(){ public update(){
SceneManager.getActiveScene().entities.buffer.forEach(entity => entity.components.forEach(component => { SceneManager.getActiveScene().entities.buffer.forEach(entity => entity.components.buffer.forEach(component => {
if (component.displayRender){ if (component.displayRender){
let has = this.entity.scene.$children.indexOf(component.displayRender) let has = this.entity.scene.$children.indexOf(component.displayRender)
if (has == -1){ if (has == -1){

View File

@@ -5,12 +5,17 @@ class Entity {
/** 封装实体的位置/旋转/缩放,并允许设置一个高层结构 */ /** 封装实体的位置/旋转/缩放,并允许设置一个高层结构 */
public readonly transform: Transform; public readonly transform: Transform;
/** 当前附加到此实体的所有组件的列表 */ /** 当前附加到此实体的所有组件的列表 */
public readonly components: Component[]; public readonly components: ComponentList;
private _updateOrder: number = 0; private _updateOrder: number = 0;
private _enabled: boolean = true; private _enabled: boolean = true;
private _isDestoryed: boolean;
public componentBits: BitSet; public componentBits: BitSet;
public get isDestoryed(){
return this._isDestoryed;
}
public get enabled(){ public get enabled(){
return this._enabled; return this._enabled;
} }
@@ -30,7 +35,7 @@ class Entity {
constructor(name: string){ constructor(name: string){
this.name = name; this.name = name;
this.transform = new Transform(this); this.transform = new Transform(this);
this.components = []; this.components = new ComponentList(this);
this.componentBits = new BitSet(); this.componentBits = new BitSet();
} }
@@ -56,30 +61,52 @@ class Entity {
public attachToScene(newScene: Scene){ public attachToScene(newScene: Scene){
this.scene = newScene; this.scene = newScene;
newScene.entities.add(this); newScene.entities.add(this);
this.components.forEach(component => component.registerComponent()); this.components.registerAllComponents();
for (let i = 0; i < this.transform.childCount; i ++){ for (let i = 0; i < this.transform.childCount; i ++){
this.transform.getChild(i).entity.attachToScene(newScene); this.transform.getChild(i).entity.attachToScene(newScene);
} }
} }
public detachFromScene(){
this.scene.entities.remove(this);
this.components.deregisterAllComponents();
for (let i = 0; i < this.transform.childCount; i ++)
this.transform.getChild(i).entity.detachFromScene();
}
public addComponent<T extends Component>(component: T): T{ public addComponent<T extends Component>(component: T): T{
component.entity = this; component.entity = this;
this.components.push(component); this.components.add(component);
component.initialize(); component.initialize();
return component; return component;
} }
public getComponent<T extends Component>(type): T{ public getComponent<T extends Component>(type): T{
return this.components.firstOrDefault(component => component instanceof type) as T; return this.components.getComponent(type, false) as T;
} }
public update(){ public update(){
this.components.forEach(component => component.update()); this.components.update();
this.transform.updateTransform(); this.transform.updateTransform();
} }
public onAddedToScene(){
}
public onRemovedFromScene(){
if (this._isDestoryed)
this.components.remove
}
public onTransformChanged(comp: ComponentTransform){
this.components.onEntityTransformChanged(comp);
}
public destory(){ public destory(){
this._isDestoryed = true;
this.scene.entities.remove(this); this.scene.entities.remove(this);
this.transform.parent = null; this.transform.parent = null;

View File

@@ -5,6 +5,12 @@ enum DirtyType{
rotationDirty, rotationDirty,
} }
enum ComponentTransform{
position,
scale,
rotation
}
class Transform { class Transform {
/** 相关联的实体 */ /** 相关联的实体 */
public readonly entity: Entity; public readonly entity: Entity;
@@ -105,6 +111,7 @@ class Transform {
this._localPosition = localPosition; this._localPosition = localPosition;
this._localDirty = this._positionDirty = this._localPositionDirty = this._localRotationDirty = this._localScaleDirty = true; this._localDirty = this._positionDirty = this._localPositionDirty = this._localRotationDirty = this._localScaleDirty = true;
this.setDirty(DirtyType.positionDirty);
return this; return this;
} }
@@ -120,8 +127,8 @@ class Transform {
this.localPosition = position; this.localPosition = position;
} }
for (let i = 0; i < this.entity.components.length; i ++){ for (let i = 0; i < this.entity.components.buffer.length; i ++){
let component = this.entity.components[i]; let component = this.entity.components.buffer[i];
if (component.displayRender){ if (component.displayRender){
component.displayRender.x = this.entity.scene.camera.transformMatrix.m31 + this.position.x; component.displayRender.x = this.entity.scene.camera.transformMatrix.m31 + this.position.x;
component.displayRender.y = this.entity.scene.camera.transformMatrix.m32 + this.position.y; component.displayRender.y = this.entity.scene.camera.transformMatrix.m32 + this.position.y;
@@ -131,6 +138,31 @@ class Transform {
return this; return this;
} }
public setDirty(dirtyFlagType: DirtyType){
if ((this._hierachyDirty & dirtyFlagType) == 0){
this._hierachyDirty |= dirtyFlagType;
switch (dirtyFlagType){
case DirtyType.positionDirty:
this.entity.onTransformChanged(ComponentTransform.position);
break;
case DirtyType.rotationDirty:
this.entity.onTransformChanged(ComponentTransform.rotation);
break;
case DirtyType.scaleDirty:
this.entity.onTransformChanged(ComponentTransform.scale);
break;
}
if (this._children == null)
this._children = [];
for (let i = 0; i < this._children.length; i ++){
this._children[i].setDirty(dirtyFlagType);
}
}
}
public updateTransform(){ public updateTransform(){
if (this._hierachyDirty != DirtyType.clean){ if (this._hierachyDirty != DirtyType.clean){
if (this.parent) if (this.parent)

View File

@@ -0,0 +1,134 @@
class ComponentList {
private _entity: Entity;
private _components: Component[] = [];
private _componentsToAdd: Component[] = [];
private _componentsToRemove: Component[] = [];
private _tempBufferList: Component[] = [];
constructor(entity: Entity){
this._entity = entity;
}
public get buffer(){
return this._components;
}
public add(component: Component){
this._componentsToAdd.push(component);
}
public remove(component: Component){
if (this._componentsToAdd.contains(component)){
this._componentsToAdd.remove(component);
return;
}
this._componentsToRemove.push(component);
}
public removeAllComponents(){
for (let i = 0; i < this._components.length; i ++){
this.handleRemove(this._components[i]);
}
this._components.length = 0;
this._componentsToAdd.length = 0;
this._componentsToRemove.length = 0;
}
public deregisterAllComponents(){
for (let i = 0; i < this._components.length; i ++){
let component = this._components[i];
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component), false);
this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
}
}
public registerAllComponents(){
for (let i = 0; i < this._components.length; i ++){
let component = this._components[i];
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component));
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
}
}
public updateLists(){
if (this._componentsToRemove.length > 0){
for (let i = 0; i < this._componentsToRemove.length; i ++){
this.handleRemove(this._componentsToRemove[i]);
this._components.remove(this._componentsToRemove[i]);
}
this._componentsToRemove.length = 0;
}
if (this._componentsToAdd.length > 0){
for (let i = 0, count = this._componentsToAdd.length; i < count; i ++){
let component = this._componentsToAdd[i];
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component));
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
this._components.push(component);
this._tempBufferList.push(component);
}
this._componentsToAdd.length = 0;
for (let i = 0; i < this._tempBufferList.length; i++){
let component = this._tempBufferList[i];
component.onAddedToEntity();
if (component.enabled){
component.onEnabled();
}
}
this._tempBufferList.length = 0;
}
}
private handleRemove(component: Component){
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component), false);
this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
component.onRemovedFromEntity();
component.entity = null;
}
public getComponent<T extends Component>(type, onlyReturnInitializedComponents: boolean): T{
for (let i = 0; i < this._components.length; i ++){
let component = this._components[i];
if (component instanceof type)
return component as T;
}
if (!onlyReturnInitializedComponents){
for (let i = 0; i < this._componentsToAdd.length; i ++){
let component = this._componentsToAdd[i];
if (component instanceof type)
return component as T;
}
}
return null;
}
public update(){
this.updateLists();
}
public onEntityTransformChanged(comp){
for (let i = 0; i < this._components.length; i++){
if (this._components[i].enabled)
this._components[i].onEntityTransformChanged(comp);
}
for (let i = 0; i < this._componentsToAdd.length; i ++){
if (this._componentsToAdd[i].enabled)
this._componentsToAdd[i].onEntityTransformChanged(comp);
}
}
}

View File

@@ -86,6 +86,7 @@ class EntityList{
this.scene.entityProcessors.onEntityAdded(entity) this.scene.entityProcessors.onEntityAdded(entity)
}); });
this._tempEntityList.forEach(entity => entity.onAddedToScene());
this._tempEntityList.length = 0; this._tempEntityList.length = 0;
} }
} }