新增renderablecomponent显示

优化返回值
This commit is contained in:
yhh
2020-07-27 16:10:36 +08:00
parent 149a3e5833
commit 506f8ddc0f
28 changed files with 631 additions and 512 deletions

View File

@@ -122,7 +122,7 @@ module es {
this._bounds.location = new Vector2(minX, minY);
this._bounds.width = maxX - minX;
this._bounds.height = maxX - minY;
this._bounds.height = maxY - minY;
} else {
this._bounds.location = topLeft;
this._bounds.width = bottomRight.x - topLeft.x;

View File

@@ -205,7 +205,7 @@ module es {
* 检查这个形状是否与物理系统中的其他对撞机重叠
* @param other
*/
public overlaps(other: Collider) {
public overlaps(other: Collider): boolean {
return this.shape.overlaps(other.shape);
}
@@ -213,20 +213,21 @@ module es {
* 检查这个与运动应用的碰撞器(移动向量)是否与碰撞器碰撞。如果是这样将返回true并且结果将填充碰撞数据。
* @param collider
* @param motion
* @param result
*/
public collidesWith(collider: Collider, motion: Vector2) {
public collidesWith(collider: Collider, motion: Vector2, result: CollisionResult): boolean {
// 改变形状的位置,使它在移动后的位置,这样我们可以检查重叠
let oldPosition = this.entity.position;
this.entity.position = Vector2.add(this.entity.position, motion);
this.entity.position.add(motion);
let result = this.shape.collidesWithShape(collider.shape);
if (result)
let didCollide = this.shape.collidesWithShape(collider.shape, result);
if (didCollide)
result.collider = collider;
// 将图形位置返回到检查前的位置
this.entity.position = oldPosition;
return result;
return didCollide;
}
public clone(): Component{

View File

@@ -16,12 +16,11 @@ module es {
/**
* 计算修改运动矢量的运动,以考虑移动时可能发生的碰撞
* @param motion
* @param collisionResult
*/
public calculateMovement(motion: Vector2){
let collisionResult = new CollisionResult();
public calculateMovement(motion: Vector2, collisionResult: CollisionResult): boolean{
if (!this.entity.getComponent(Collider) || !this._triggerHelper){
return null;
return false;
}
// 移动所有的非触发碰撞器并获得最近的碰撞
@@ -37,9 +36,7 @@ module es {
let bounds = collider.bounds;
bounds.x += motion.x;
bounds.y += motion.y;
let boxcastResult = Physics.boxcastBroadphaseExcludingSelf(collider, bounds, collider.collidesWithLayers);
bounds = boxcastResult.bounds;
let neighbors = boxcastResult.tempHashSet;
let neighbors = Physics.boxcastBroadphaseExcludingSelf(collider, bounds, collider.collidesWithLayers);
for (let j = 0; j < neighbors.length; j ++){
let neighbor = neighbors[j];
@@ -47,13 +44,13 @@ module es {
if (neighbor.isTrigger)
continue;
let _internalcollisionResult = collider.collidesWith(neighbor, motion);
if (_internalcollisionResult){
let _internalcollisionResult: CollisionResult = new CollisionResult();
if (collider.collidesWith(neighbor, motion, _internalcollisionResult)){
// 如果碰撞 则退回之前的移动量
motion = Vector2.subtract(motion, _internalcollisionResult.minimumTranslationVector);
motion.subtract(_internalcollisionResult.minimumTranslationVector);
// 如果我们碰到多个对象,为了简单起见,只取第一个。
if (_internalcollisionResult.collider){
if (_internalcollisionResult.collider != null){
collisionResult = _internalcollisionResult;
}
}
@@ -62,7 +59,7 @@ module es {
ListPool.free(colliders);
return {collisionResult: collisionResult, motion: motion};
return collisionResult.collider != null;
}
/**
@@ -81,15 +78,12 @@ module es {
/**
* 通过调用calculateMovement和applyMovement来移动考虑碰撞的实体;
* @param motion
* @param collisionResult
*/
public move(motion: Vector2){
let movementResult = this.calculateMovement(motion);
let collisionResult = movementResult.collisionResult;
motion = movementResult.motion;
public move(motion: Vector2, collisionResult: CollisionResult){
this.calculateMovement(motion, collisionResult);
this.applyMovement(motion);
return collisionResult;
return collisionResult.collider != null;
}
}
}

View File

@@ -28,8 +28,8 @@ module es {
// 获取任何可能在新位置发生碰撞的东西
let neighbors = Physics.boxcastBroadphase(this._collider.bounds, this._collider.collidesWithLayers);
for (let i = 0; i < neighbors.colliders.length; i ++){
let neighbor = neighbors.colliders[i];
for (let i = 0; i < neighbors.length; i ++){
let neighbor = neighbors[i];
if (this._collider.overlaps(neighbor) && neighbor.enabled){
didCollide = true;
this.notifyTriggerListeners(this._collider, neighbor);

View File

@@ -4,6 +4,10 @@ module es {
* 所有可渲染组件的基类
*/
export abstract class RenderableComponent extends Component implements IRenderable {
/**
* 用于装载egret显示对象
*/
public displayObject: egret.DisplayObject = new egret.DisplayObject();
/**
* renderableComponent的宽度
* 如果你不重写bounds属性则需要实现这个
@@ -107,6 +111,7 @@ module es {
* 如果渲染器不适用isVisibleFromCamera进行剔除检查 这些方法不会被调用
*/
protected onBecameVisible() {
this.displayObject.visible = this.isVisible;
}
/**
@@ -114,6 +119,7 @@ module es {
* 如果渲染器不适用isVisibleFromCamera进行剔除检查 这些方法不会被调用
*/
protected onBecameInvisible() {
this.displayObject.visible = this.isVisible;
}
/**
@@ -165,6 +171,17 @@ module es {
return this;
}
/**
* 进行状态同步
*/
public sync(camera: Camera){
this.displayObject.x = this.entity.position.x + this.localOffset.x - camera.position.x + camera.origin.x;
this.displayObject.y = this.entity.position.y + this.localOffset.y - camera.position.y + camera.origin.y;
this.displayObject.scaleX = this.entity.scale.x;
this.displayObject.scaleY = this.entity.scale.y;
this.displayObject.rotation = this.entity.rotation;
}
public toString(){
return `[RenderableComponent] renderLayer: ${this.renderLayer}`;
}

View File

@@ -1,4 +1,6 @@
module es {
import Bitmap = egret.Bitmap;
export class SpriteRenderer extends RenderableComponent {
public get bounds() {
if (this._areBoundsDirty) {
@@ -8,9 +10,9 @@ module es {
this._sprite.sourceRect.height);
this._areBoundsDirty = false;
}
return this._bounds;
}
return this._bounds;
}
/**
@@ -83,7 +85,10 @@ module es {
this._sprite = sprite;
if (this._sprite) {
this._origin = this._sprite.origin;
this.displayObject.anchorOffsetX = this._origin.x;
this.displayObject.anchorOffsetY = this._origin.y;
}
this.displayObject = new Bitmap(sprite.texture2D);
return this;
}
@@ -95,6 +100,8 @@ module es {
public setOrigin(origin: Vector2): SpriteRenderer {
if (this._origin != origin) {
this._origin = origin;
this.displayObject.anchorOffsetX = this._origin.x;
this.displayObject.anchorOffsetY = this._origin.y;
this._areBoundsDirty = true;
}
@@ -113,7 +120,7 @@ module es {
}
public render(camera: Camera) {
// TODO: render
this.sync(camera);
}
}
}

View File

@@ -81,8 +81,8 @@ module es {
this.addEventListener(egret.Event.RESIZE, this.onGraphicsDeviceReset, this);
this.addEventListener(egret.StageOrientationEvent.ORIENTATION_CHANGE, this.onOrientationChanged, this);
this.addEventListener(egret.Event.ENTER_FRAME, this.update, this);
this.addEventListener(egret.Event.RENDER, this.draw, this);
Input.initialize();
this.initialize();
}
@@ -135,6 +135,8 @@ module es {
}
// this.endDebugUpdate();
await this.draw();
}
public async draw() {

View File

@@ -7,6 +7,8 @@ module transform {
}
module es {
import HashObject = egret.HashObject;
export enum DirtyType {
clean,
positionDirty,
@@ -14,7 +16,7 @@ module es {
rotationDirty,
}
export class Transform {
export class Transform extends HashObject {
/** 与此转换关联的实体 */
public readonly entity: Entity;
/**
@@ -243,6 +245,7 @@ module es {
public _children: Transform[];
constructor(entity: Entity) {
super();
this.entity = entity;
this.scale = Vector2.one;
this._children = [];
@@ -261,7 +264,7 @@ module es {
* @param parent
*/
public setParent(parent: Transform): Transform {
if (this._parent == parent)
if (this._parent.equals(parent))
return this;
if (!this._parent) {
@@ -282,7 +285,7 @@ module es {
*/
public setPosition(x: number, y: number): Transform {
let position = new Vector2(x, y);
if (position == this._position)
if (position.equals(this._position))
return this;
this._position = position;
@@ -301,7 +304,7 @@ module es {
* @param localPosition
*/
public setLocalPosition(localPosition: Vector2): Transform {
if (localPosition == this._localPosition)
if (localPosition.equals(this._localPosition))
return this;
this._localPosition = localPosition;
@@ -493,5 +496,9 @@ module es {
scale: ${this.scale}, localPosition: ${this._localPosition}, localRotation: ${this._localRotation},
localScale: ${this._localScale}]`;
}
public equals(other: Transform){
return other.hashCode == this.hashCode;
}
}
}

View File

@@ -76,8 +76,11 @@ module es {
let component = this._components[i];
// 处理渲染层列表
if (component instanceof RenderableComponent)
if (component instanceof RenderableComponent){
this._entity.scene.removeChild(component.displayObject);
this._entity.scene.renderableComponents.remove(component);
}
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component), false);
this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
@@ -88,8 +91,10 @@ module es {
for (let i = 0; i < this._components.length; i++) {
let component = this._components[i];
if (component instanceof RenderableComponent)
if (component instanceof RenderableComponent){
this._entity.scene.addChild(component.displayObject);
this._entity.scene.renderableComponents.add(component);
}
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component));
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
@@ -112,8 +117,11 @@ module es {
if (this._componentsToAdd.length > 0) {
for (let i = 0, count = this._componentsToAdd.length; i < count; i++) {
let component = this._componentsToAdd[i];
if (component instanceof RenderableComponent)
if (component instanceof RenderableComponent){
this._entity.scene.addChild(component.displayObject);
this._entity.scene.renderableComponents.add(component);
}
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component));
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
@@ -148,8 +156,11 @@ module es {
public handleRemove(component: Component) {
// 处理渲染层列表
if (component instanceof RenderableComponent)
if (component instanceof RenderableComponent){
this._entity.scene.removeChild(component.displayObject);
this._entity.scene.renderableComponents.remove(component);
}
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component), false);
this._entity.scene.entityProcessors.onComponentRemoved(this._entity);