This commit is contained in:
yhh
2020-11-20 16:30:09 +08:00
parent 7eab688d9c
commit 0fd6a24f5a
605 changed files with 1944 additions and 184497 deletions

View File

@@ -1,146 +0,0 @@
module es {
export class TiledMapRenderer extends RenderableComponent {
public tiledMap: TmxMap;
public physicsLayer: Ref<number> = new Ref(1 << 0);
/**
* 如果空,所有层将被渲染
*/
public layerIndicesToRender: number[];
private toContainer: boolean = false;
public get width() {
return this.tiledMap.width * this.tiledMap.tileWidth;
}
public get height() {
return this.tiledMap.height * this.tiledMap.tileHeight;
}
public collisionLayer: TmxLayer;
public _shouldCreateColliders: boolean;
public _colliders: Collider[];
constructor(tiledMap: TmxMap, collisionLayerName: string = null, shouldCreateColliders: boolean = true) {
super();
this.tiledMap = tiledMap;
this._shouldCreateColliders = shouldCreateColliders;
this.displayObject = new egret.DisplayObjectContainer();
if (collisionLayerName) {
this.collisionLayer = tiledMap.tileLayers.find(layer => layer.name == collisionLayerName);
}
}
/**
* 将此组件设置为只渲染单层
* @param layerName
*/
public setLayerToRender(layerName: string) {
this.layerIndicesToRender = [];
this.layerIndicesToRender[0] = this.getLayerIndex(layerName);
}
/**
* 设置该组件应该按名称呈现哪些层。如果你知道索引你可以直接设置layerIndicesToRender
* @param layerNames
*/
public setLayersToRender(...layerNames: string[]) {
this.layerIndicesToRender = [];
for (let i = 0; i < layerNames.length; i++)
this.layerIndicesToRender[i] = this.getLayerIndex(layerNames[i]);
}
private getLayerIndex(layerName: string) {
let index = 0;
let layerType = this.tiledMap.getLayer(layerName);
for (let layer in this.tiledMap.layers) {
if (this.tiledMap.layers.hasOwnProperty(layer) &&
this.tiledMap.layers[layer] == layerType) {
return index;
}
}
return -1;
}
public getRowAtWorldPosition(yPos: number): number {
yPos -= this.entity.transform.position.y + this._localOffset.y;
return this.tiledMap.worldToTilePositionY(yPos);
}
public getColumnAtWorldPosition(xPos: number): number {
xPos -= this.entity.transform.position.x + this._localOffset.x;
return this.tiledMap.worldToTilePositionX(xPos);
}
public onEntityTransformChanged(comp: transform.Component) {
// 这里我们只处理位置变化。平铺地图不能缩放
if (this._shouldCreateColliders && comp == transform.Component.position) {
this.removeColliders();
this.addColliders();
}
}
public onAddedToEntity() {
this.addColliders();
}
public onRemovedFromEntity() {
this.removeColliders();
}
public update() {
this.tiledMap.update();
}
public render(camera: es.Camera) {
this.sync(camera);
if (!this.layerIndicesToRender) {
TiledRendering.renderMap(this.tiledMap, !this.toContainer ? this.displayObject as egret.DisplayObjectContainer : null, Vector2.add(this.entity.transform.position, this._localOffset),
this.transform.scale, this.renderLayer);
} else {
for (let i = 0; i < this.tiledMap.layers.length; i++) {
if (this.tiledMap.layers[i].visible && this.layerIndicesToRender.contains(i))
TiledRendering.renderLayerRenderCamera(this.tiledMap.layers[i] as TmxLayer, !this.toContainer ? this.displayObject as egret.DisplayObjectContainer : null, Vector2.add(this.entity.transform.position, this._localOffset),
this.transform.scale, this.renderLayer, camera.bounds);
}
}
if (!this.toContainer){
this.displayObject.cacheAsBitmap = true;
this.toContainer = true;
}
}
public addColliders() {
if (!this.collisionLayer || !this._shouldCreateColliders)
return;
// 获取冲突层及其冲突的矩形
let collisionRects = this.collisionLayer.getCollisionRectangles();
// 为我们收到的矩形创建碰撞器
this._colliders = [];
for (let i = 0; i < collisionRects.length; i++) {
let collider = new BoxCollider(collisionRects[i].x + this._localOffset.x,
collisionRects[i].y + this._localOffset.y, collisionRects[i].width, collisionRects[i].height);
collider.physicsLayer = this.physicsLayer;
collider.entity = this.entity;
this._colliders[i] = collider;
Physics.addCollider(collider);
}
}
public removeColliders() {
if (this._colliders == null)
return;
for (let collider of this._colliders) {
Physics.removeCollider(collider);
}
this._colliders = null;
}
}
}

View File

@@ -1,131 +0,0 @@
// module es {
// /**
// * 用来存放来自移动调用的所有冲突信息
// */
// export class CollisionState {
// public right: boolean;
// public left: boolean;
// public above: boolean;
// public below: boolean;
// public becameGroundedThisFrame: boolean;
// public wasGroundedLastFrame: boolean;
// public isGroundedOnOnewayPlatform: boolean;
// public slopAngle: number;
// public get hasCollision(): boolean {
// return this.below || this.right || this.left || this.above;
// }
// public _movementRemainderX: SubpixelNumber;
// public _movementRemainderY: SubpixelNumber;
// public reset() {
// this.becameGroundedThisFrame = this.isGroundedOnOnewayPlatform = this.right = this.left = this.above = this.below = false;
// this.slopAngle = 0;
// }
// public toString() {
// return `[CollisionState] r: ${this.right}, l: ${this.left}, a: ${this.above}, b: ${this.below}, angle: ${this.slopAngle}, wasGroundedLastFrame: ${this.wasGroundedLastFrame}, becameGroundedThisFrame: ${this.becameGroundedThisFrame}`;
// }
// }
// /**
// * TiledMapMover是在基于重力的平铺地图中移动对象的助手。
// * 它要求它所在的实体有一个BoxCollider。BoxCollider将与colliderHorizontal/VerticalInset用于所有碰撞检测。
// *
// * 一种方法是通过将你的Transform向下移动1个像素并调用CollisionState.clearLastGroundTile来跳过平台。
// */
// export class TiledMapMover extends Component {
// /**
// * 在垂直移动时BoxCollider将根据水平面上的嵌入缩小
// */
// public colliderHorizontalInset = 2;
// /**
// * 垂直平面上的嵌入在水平移动时BoxCollider将根据该嵌入缩小
// */
// public colliderVerticalInset = 6;
// /**
// * 临时存储
// */
// public _boxColliderBounds: Rectangle;
// constructor() {
// super();
// }
// /**
// * 测试碰撞,然后移动实体
// * @param motion
// * @param boxColliderBounds
// * @param collisionState
// */
// public testCollisions(motion: Vector2, boxColliderBounds: Rectangle, collisionState: CollisionState) {
// this._boxColliderBounds = boxColliderBounds;
// // 保存我们当前的接地状态我们将用于wasGroundedLastFrame和becameGroundedThisFrame
// collisionState.wasGroundedLastFrame = collisionState.below;
// // 重置碰撞状态
// collisionState.reset();
// // 获取圆角值用于我们的实际检测
// let motionX = motion.x;
// let motionY = motion.y;
// // 首先,检查水平方向中的移动
// if (motionX != 0){
// let direction = motionX > 0 ? Edge.right : Edge.left;
// let sweptBounds = this.collisionRectForSide(direction, motionX);
// let collisionResponse: number = 0;
// if (this.testMapCollision(sweptBounds, direction, collisionState, collisionResponse)){
// motion.x = collisionResponse - RectangleExt.getSide(boxColliderBounds, direction);
// collisionState.left = direction == Edge.left;
// collisionState.right = direction == Edge.right;
// collisionState._movementRemainderX.reset();
// }else{
// collisionState.left = false;
// collisionState.right = false;
// }
// }
// }
// public testMapCollision(collisionRect: Rectangle, direction: Edge, collisionState: CollisionState, collisionResponse: number){
// let side = EdgeExt.oppositeEdge(direction);
// let perpindicularPosition = EdgeExt.isVertical(side) ? collisionRect.center.x : collisionRect.center.y;
// let leadingPosition = RectangleExt.getSide(collisionRect, direction);
// let shouldTestSlopes = EdgeExt.isVertical(side);
// }
// /**
// * 获取展开以考虑运动的给定边的碰撞矩形
// * @param side
// * @param motion
// */
// public collisionRectForSide(side: Edge, motion: number){
// let bounds: Rectangle;
// // 对于水平碰撞检查我们只使用一个条块作为边界。Vertical得到矩形的一半就可以正确地向上推
// // 当相交的斜率,忽略以进行水平移动。
// if (EdgeExt.isHorizontal(side)){
// bounds = RectangleExt.getRectEdgePortion(this._boxColliderBounds, side);
// }else {
// bounds = RectangleExt.getHalfRect(this._boxColliderBounds, side);
// }
// // 我们水平收缩用于垂直移动,垂直收缩用于水平移动
// if (EdgeExt.isVertical(side)){
// RectangleExt.contract(bounds, this.colliderHorizontalInset, 0);
// }else {
// RectangleExt.contract(bounds, 0, this.colliderVerticalInset);
// }
// // 最后在移动方向上扩展边
// RectangleExt.expandSide(bounds, side, motion);
// return bounds;
// }
// }
// }