ecs适配egret

This commit is contained in:
yhh
2020-06-29 15:41:02 +08:00
parent a63d8598d8
commit a4f1ae351f
33 changed files with 647 additions and 871 deletions

View File

@@ -1,12 +1,8 @@
abstract class Component {
abstract class Component extends egret.DisplayObjectContainer {
public entity: Entity;
private _enabled: boolean = true;
public updateInterval: number = 1;
public get transform(){
return this.entity.transform;
}
public get enabled(){
return this.entity ? this.entity.enabled && this._enabled : this._enabled;
}

View File

@@ -4,7 +4,6 @@ class Camera extends Component {
private _origin: Vector2 = Vector2.zero;
private _transformMatrix: Matrix2D = new Matrix2D();
private _inverseTransformMatrix = new Matrix2D();
private _projectionMatrix = new Matrix2D();
private _minimumZoom = 0.3;
private _maximumZoom = 3;
@@ -12,7 +11,6 @@ class Camera extends Component {
private _inset: CameraInset = new CameraInset();
private _bounds: Rectangle = new Rectangle();
private _areBoundsDirty = true;
private _isProjectionMatrixDirty = true;
public get bounds(){
if (this._areMatrixesDirty)
@@ -23,7 +21,7 @@ class Camera extends Component {
let topLeft = this.screenToWorldPoint(new Vector2(this._inset.left, this._inset.top));
let bottomRight = this.screenToWorldPoint(new Vector2(stage.stageWidth - this._inset.right, stage.stageHeight - this._inset.bottom));
if (this.entity.transform.rotation != 0){
if (this.entity.rotation != 0){
let topRight = this.screenToWorldPoint(new Vector2(stage.stageWidth - this._inset.right, this._inset.top));
let bottomLeft = this.screenToWorldPoint(new Vector2(this._inset.left, stage.stageHeight - this._inset.bottom));
@@ -89,11 +87,11 @@ class Camera extends Component {
}
public get position(){
return this.entity.transform.position;
return this.entity.position;
}
public set position(value: Vector2){
this.entity.transform.position = value;
this.entity.position = value;
}
public get transformMatrix(){
@@ -115,11 +113,10 @@ class Camera extends Component {
}
public onSceneSizeChanged(newWidth: number, newHeight: number){
this._isProjectionMatrixDirty = true;
let oldOrigin = this._origin;
this.origin = new Vector2(newWidth / 2, newHeight / 2);
this.entity.transform.position = Vector2.add(this.entity.transform.position, Vector2.subtract(this._origin, oldOrigin));
this.entity.position = Vector2.add(this.entity.position, Vector2.subtract(this._origin, oldOrigin));
}
public setMinimumZoom(minZoom: number): Camera{
@@ -154,7 +151,7 @@ class Camera extends Component {
}
public setPosition(position: Vector2){
this.entity.transform.setPosition(position);
this.entity.position = position;
return this;
}
@@ -168,13 +165,13 @@ class Camera extends Component {
return;
let tempMat: Matrix2D;
this._transformMatrix = Matrix2D.createTranslation(-this.entity.transform.position.x, -this.entity.transform.position.y);
this._transformMatrix = Matrix2D.createTranslation(-this.entity.position.x, -this.entity.position.y);
if (this._zoom != 1){
tempMat = Matrix2D.createScale(this._zoom, this._zoom);
this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat);
}
if (this.entity.transform.rotation != 0){
if (this.entity.rotation != 0){
tempMat = Matrix2D.createRotation(this.entity.rotation);
this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat);
}

View File

@@ -55,11 +55,11 @@ class FollowCamera extends Component {
this.updateFollow();
this.camera.position = Vector2.lerp(this.camera.position, Vector2.add(this.camera.position, this._desiredPositionDelta), this.followLerp);
this.camera.entity.transform.roundPosition();
this.camera.entity.roundPosition();
if (this.mapLockEnabled){
this.camera.position = this.clampToMapSize(this.camera.position);
this.camera.entity.transform.roundPosition();
this.camera.entity.roundPosition();
}
}
@@ -74,8 +74,8 @@ class FollowCamera extends Component {
this._desiredPositionDelta.x = this._desiredPositionDelta.y = 0;
if (this._cameraStyle == CameraStyle.lockOn){
let targetX = this._targetEntity.transform.position.x;
let targetY = this._targetEntity.transform.position.y;
let targetX = this._targetEntity.position.x;
let targetY = this._targetEntity.position.y;
if (this._worldSpaceDeadZone.x > targetX)
this._desiredPositionDelta.x = targetX - this._worldSpaceDeadZone.x;

View File

@@ -82,15 +82,15 @@ abstract class Collider extends Component{
if (renderable){
let renderbaleBounds = renderable.bounds;
let width = renderbaleBounds.width / this.entity.transform.scale.x;
let height = renderbaleBounds.height / this.entity.transform.scale.y;
let width = renderbaleBounds.width / this.entity.scale.x;
let height = renderbaleBounds.height / this.entity.scale.y;
if (this instanceof BoxCollider){
let boxCollider = this as BoxCollider;
boxCollider.width = width;
boxCollider.height = height;
this.localOffset = Vector2.subtract(renderbaleBounds.center, this.entity.transform.position);
this.localOffset = Vector2.subtract(renderbaleBounds.center, this.entity.position);
}
}
}

View File

@@ -46,7 +46,7 @@ class Mover extends Component {
}
public applyMovement(motion: Vector2){
this.entity.transform.position = Vector2.add(this.entity.transform.position, motion);
this.entity.position = Vector2.add(this.entity.position, motion);
if (this._triggerHelper)
this._triggerHelper.update();

View File

@@ -31,13 +31,7 @@ abstract class RenderableComponent extends Component implements IRenderable {
}
public get bounds(): Rectangle{
if (this._areBoundsDirty){
this._bounds.calculateBounds(this.entity.transform.position, this._localOffset, new Vector2(0, 0),
this.entity.transform.scale, this.entity.transform.rotation, this.width, this.height);
this._areBoundsDirty = false;
}
return this._bounds;
return new Rectangle(this.getBounds().x, this.getBounds().y, this.getBounds().width, this.getBounds().height);
}
protected getWidth(){

View File

@@ -1,39 +1,5 @@
class SpriteRenderer extends RenderableComponent {
private _sprite: Sprite;
private _origin: Vector2;
private _bitmap: egret.Bitmap;
public get bounds(){
if (this._areBoundsDirty){
if (this._sprite){
this._bounds.calculateBounds(this.entity.transform.position, this._localOffset, this._origin,
this.entity.transform.scale, this.entity.transform.rotation, this._sprite.texture2D.textureWidth,
this._sprite.texture2D.textureHeight);
this._areBoundsDirty = false;
}
}
return this._bounds;
}
public get sprite(){
return this._sprite;
}
public set sprite(value: Sprite){
this.setSprite(value);
}
public setSprite(sprite: Sprite): SpriteRenderer{
this._sprite = sprite;
if (this._sprite)
this._origin = sprite.origin;
this._bitmap = new egret.Bitmap(sprite.texture2D);
this.scene.addChild(this._bitmap);
return this;
}
public get origin(){
return this._origin;
@@ -49,6 +15,11 @@ class SpriteRenderer extends RenderableComponent {
return this;
}
public setSprite(sprite: Sprite){
this.removeChildren();
this.addChild(new egret.Bitmap(sprite.texture2D));
}
public setColor(color: number){
let colorMatrix = [
1, 0, 0, 0, 0,
@@ -60,31 +31,22 @@ class SpriteRenderer extends RenderableComponent {
colorMatrix[6] = Math.floor(color / 256 % 256) / 255;
colorMatrix[12] = color % 256 / 255;
let colorFilter = new egret.ColorMatrixFilter(colorMatrix);
this._bitmap.filters = [colorFilter];
this.filters = [colorFilter];
}
public isVisibleFromCamera(camera: Camera): boolean{
let topLeft = camera.screenToWorldPoint(new Vector2(0, 0));
this.isVisible = new Rectangle(topLeft.x, topLeft.y, this.stage.stageWidth, this.stage.stageHeight).intersects(this.bounds);
this._bitmap.visible = this.isVisible;
this.visible = this.isVisible;
return this.isVisible;
}
public render(camera: Camera){
if (!this.sprite)
return;
this._bitmap.x = this.entity.transform.position.x - camera.transform.position.x + camera.origin.x;
this._bitmap.y = this.entity.transform.position.y - camera.transform.position.y + camera.origin.y;
this._bitmap.rotation = this.entity.transform.rotation + camera.transform.rotation;
this._bitmap.anchorOffsetX = this._origin.x;
this._bitmap.anchorOffsetY = this._origin.y;
this._bitmap.scaleX = this.entity.transform.scale.x * camera.transform.scale.x;
this._bitmap.scaleY = this.entity.transform.scale.y * camera.transform.scale.y;
}
public onRemovedFromEntity(){
if (this._bitmap)
this.scene.removeChild(this._bitmap);
if (this.parent)
this.parent.removeChild(this);
}
}

View File

@@ -1,12 +1,10 @@
class Entity {
class Entity extends egret.DisplayObjectContainer {
private static _idGenerator: number;
public name: string;
public readonly id: number;
/** 当前实体所属的场景 */
public scene: Scene;
/** 封装实体的位置/旋转/缩放,并允许设置一个高层结构 */
public readonly transform: Transform;
/** 当前附加到此实体的所有组件的列表 */
public readonly components: ComponentList;
private _updateOrder: number = 0;
@@ -16,92 +14,26 @@ class Entity {
public componentBits: BitSet;
public get parent(){
return this.transform.parent;
}
public set parent(value: Transform){
this.transform.setParent(value);
public get isDestoryed(){
return this._isDestoryed;
}
public get position(){
return this.transform.position;
return new Vector2(this.x, this.y);
}
public set position(value: Vector2){
this.transform.setPosition(value);
}
public get localPosition(){
return this.transform.localPosition;
}
public set localPosition(value: Vector2){
this.transform.setLocalPosition(value);
}
public get rotation(){
return this.transform.rotation;
}
public set rotation(value: number){
this.transform.setRotation(value);
}
public get rotationDegrees(){
return this.transform.rotationDegrees;
}
public set rotationDegrees(value: number){
this.transform.setRotationDegrees(value);
}
public get localRotation(){
return this.transform.localRotation;
}
public set localRotation(value: number){
this.transform.setLocalRotation(value);
}
public get localRotationDegrees(){
return this.transform.localRotationDegrees;
}
public set localRotationDegrees(value: number){
this.transform.setLocalRotationDegrees(value);
this.x = value.x;
this.y = value.y;
}
public get scale(){
return this.transform.scale;
return new Vector2(this.scaleX, this.scaleY);
}
public set scale(value: Vector2){
this.transform.setScale(value);
}
public get localScale(){
return this.transform.scale;
}
public set localScale(value: Vector2){
this.transform.setScale(value);
}
public get worldInverseTransform(){
return this.transform.worldInverseTransform;
}
public get localToWorldTransform(){
return this.transform.localToWorldTransform;
}
public get worldToLocalTransform(){
return this.transform.worldToLocalTransform;
}
public get isDestoryed(){
return this._isDestoryed;
this.scaleX = value.x;
this.scaleY = value.y;
}
public get enabled(){
@@ -136,8 +68,8 @@ class Entity {
}
constructor(name: string){
super();
this.name = name;
this.transform = new Transform(this);
this.components = new ComponentList(this);
this.id = Entity._idGenerator ++;
@@ -152,6 +84,10 @@ class Entity {
this.setUpdateOrder(value);
}
public roundPosition(){
this.position = Vector2Ext.round(this.position);
}
public setUpdateOrder(updateOrder: number){
if (this._updateOrder != updateOrder){
this._updateOrder = updateOrder;
@@ -182,8 +118,8 @@ class Entity {
newScene.entities.add(this);
this.components.registerAllComponents();
for (let i = 0; i < this.transform.childCount; i ++){
this.transform.getChild(i).entity.attachToScene(newScene);
for (let i = 0; i < this.numChildren; i ++){
(this.getChildAt(i) as Component).entity.attachToScene(newScene);
}
}
@@ -191,13 +127,14 @@ class Entity {
this.scene.entities.remove(this);
this.components.deregisterAllComponents();
for (let i = 0; i < this.transform.childCount; i ++)
this.transform.getChild(i).entity.detachFromScene();
for (let i = 0; i < this.numChildren; i ++)
(this.getChildAt(i) as Component).entity.detachFromScene();
}
public addComponent<T extends Component>(component: T): T{
component.entity = this;
this.components.add(component);
this.addChild(component);
component.initialize();
return component;
}
@@ -260,14 +197,14 @@ class Entity {
this.components.onEntityTransformChanged(comp);
}
public destory(){
public destroy(){
this._isDestoryed = true;
this.scene.entities.remove(this);
this.transform.parent = null;
this.removeChildren();
for (let i = this.transform.childCount - 1; i >= 0; i --){
let child = this.transform.getChild(i);
child.entity.destory();
for (let i = this.numChildren - 1; i >= 0; i --){
let child = this.getChildAt(i);
(child as Component).entity.destroy();
}
}
}

View File

@@ -6,9 +6,6 @@ class Scene extends egret.DisplayObjectContainer {
public readonly content: ContentManager;
public enablePostProcessing = true;
private _projectionMatrix: Matrix2D;
private _transformMatrix: Matrix2D;
private _matrixTransformMatrix: Matrix2D;
private _renderers: Renderer[] = [];
private _postProcessors: PostProcessor[] = [];
private _didSceneBegin;
@@ -17,7 +14,6 @@ class Scene extends egret.DisplayObjectContainer {
constructor() {
super();
this._projectionMatrix = new Matrix2D(0, 0, 0, 0, 0, 0);
this.entityProcessors = new EntityProcessorList();
this.renderableComponents = new RenderableComponentList();
this.entities = new EntityList(this);
@@ -29,23 +25,24 @@ class Scene extends egret.DisplayObjectContainer {
public createEntity(name: string) {
let entity = new Entity(name);
entity.transform.position = new Vector2(0, 0);
entity.position = new Vector2(0, 0);
return this.addEntity(entity);
}
public addEntity(entity: Entity) {
this.entities.add(entity);
entity.scene = this;
this.addChild(entity);
for (let i = 0; i < entity.transform.childCount; i++)
this.addEntity(entity.transform.getChild(i).entity);
for (let i = 0; i < entity.numChildren; i++)
this.addEntity((entity.getChildAt(i) as Component).entity);
return entity;
}
public destroyAllEntities() {
for (let i = 0; i < this.entities.count; i++) {
this.entities.buffer[i].destory();
this.entities.buffer[i].destroy();
}
}
@@ -134,6 +131,7 @@ class Scene extends egret.DisplayObjectContainer {
}
this.entities.removeAllEntities();
this.removeChildren();
Physics.clear();

View File

@@ -50,7 +50,7 @@ class SceneManager {
for (let i = 0; i < SceneManager._scene.entities.buffer.length; i++) {
let entity = SceneManager._scene.entities.buffer[i];
entity.destory();
entity.destroy();
}
SceneManager._scene = SceneManager._nextScene;

View File

@@ -102,6 +102,9 @@ class ComponentList {
}
private handleRemove(component: Component){
if (component instanceof RenderableComponent)
this._entity.scene.renderableComponents.remove(component);
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component), false);
this._entity.scene.entityProcessors.onComponentRemoved(this._entity);