reformat code
This commit is contained in:
@@ -12,6 +12,46 @@ module es {
|
||||
}
|
||||
|
||||
export class Camera extends Component {
|
||||
public _inset: CameraInset = new CameraInset();
|
||||
public _areMatrixedDirty: boolean = true;
|
||||
public _areBoundsDirty: boolean = true;
|
||||
public _isProjectionMatrixDirty = true;
|
||||
/**
|
||||
* 如果相机模式为cameraWindow 则会进行缓动移动
|
||||
* 该值为移动速度
|
||||
*/
|
||||
public followLerp = 0.1;
|
||||
/**
|
||||
* 在cameraWindow模式下,宽度/高度被用做边界框,允许在不移动相机的情况下移动
|
||||
* 在lockOn模式下,只使用deadZone的x/y值 你可以通过直接setCenteredDeadzone重写它来自定义deadZone
|
||||
*/
|
||||
public deadzone: Rectangle = new Rectangle();
|
||||
/**
|
||||
* 相机聚焦于屏幕中心的偏移
|
||||
*/
|
||||
public focusOffset: Vector2 = Vector2.zero;
|
||||
/**
|
||||
* 如果为true 相机位置则不会超出地图矩形(0, 0, mapwidth, mapheight)
|
||||
*/
|
||||
public mapLockEnabled: boolean = false;
|
||||
/**
|
||||
* 當前地圖映射的寬度和高度
|
||||
*/
|
||||
public mapSize: Vector2 = Vector2.zero;
|
||||
public _targetEntity: Entity;
|
||||
public _targetCollider: Collider;
|
||||
public _desiredPositionDelta: Vector2 = new Vector2();
|
||||
public _cameraStyle: CameraStyle;
|
||||
public _worldSpaceDeadZone: Rectangle = new Rectangle();
|
||||
|
||||
constructor(targetEntity: Entity = null, cameraStyle: CameraStyle = CameraStyle.lockOn) {
|
||||
super();
|
||||
|
||||
this._targetEntity = targetEntity;
|
||||
this._cameraStyle = cameraStyle;
|
||||
this.setZoom(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对entity.transform.position的快速访问
|
||||
*/
|
||||
@@ -42,6 +82,8 @@ module es {
|
||||
this.entity.transform.rotation = value;
|
||||
}
|
||||
|
||||
public _zoom;
|
||||
|
||||
/**
|
||||
* 缩放值应该在-1和1之间、然后将该值从minimumZoom转换为maximumZoom。
|
||||
* 允许你设置适当的最小/最大值,然后使用更直观的-1到1的映射来更改缩放
|
||||
@@ -65,6 +107,8 @@ module es {
|
||||
this.setZoom(value);
|
||||
}
|
||||
|
||||
public _minimumZoom = 0.3;
|
||||
|
||||
/**
|
||||
* 相机变焦可以达到的最小非缩放值(0-number.max)。默认为0.3
|
||||
*/
|
||||
@@ -80,6 +124,8 @@ module es {
|
||||
this.setMinimumZoom(value);
|
||||
}
|
||||
|
||||
public _maximumZoom = 3;
|
||||
|
||||
/**
|
||||
* 相机变焦可以达到的最大非缩放值(0-number.max)。默认为3
|
||||
*/
|
||||
@@ -95,20 +141,22 @@ module es {
|
||||
this.setMaximumZoom(value);
|
||||
}
|
||||
|
||||
public _bounds: Rectangle = new Rectangle();
|
||||
|
||||
/**
|
||||
* 相机的世界-空间边界
|
||||
*/
|
||||
public get bounds(){
|
||||
public get bounds() {
|
||||
if (this._areMatrixedDirty)
|
||||
this.updateMatrixes();
|
||||
|
||||
if (this._areBoundsDirty){
|
||||
if (this._areBoundsDirty) {
|
||||
// 旋转或非旋转的边界都需要左上角和右下角
|
||||
let topLeft = this.screenToWorldPoint(new Vector2(this._inset.left, this._inset.top));
|
||||
let bottomRight = this.screenToWorldPoint(new Vector2(Core.graphicsDevice.viewport.width - this._inset.right,
|
||||
Core.graphicsDevice.viewport.height - this._inset.bottom));
|
||||
|
||||
if (this.entity.transform.rotation != 0){
|
||||
if (this.entity.transform.rotation != 0) {
|
||||
// 特别注意旋转的边界。我们需要找到绝对的最小/最大值并从中创建边界
|
||||
let topRight = this.screenToWorldPoint(new Vector2(Core.graphicsDevice.viewport.width - this._inset.right,
|
||||
this._inset.top));
|
||||
@@ -135,6 +183,8 @@ module es {
|
||||
return this._bounds;
|
||||
}
|
||||
|
||||
public _transformMatrix: Matrix2D = new Matrix2D().identity();
|
||||
|
||||
/**
|
||||
* 用于从世界坐标转换到屏幕
|
||||
*/
|
||||
@@ -144,6 +194,8 @@ module es {
|
||||
return this._transformMatrix;
|
||||
}
|
||||
|
||||
public _inverseTransformMatrix: Matrix2D = new Matrix2D().identity();
|
||||
|
||||
/**
|
||||
* 用于从屏幕坐标到世界的转换
|
||||
*/
|
||||
@@ -153,6 +205,8 @@ module es {
|
||||
return this._inverseTransformMatrix;
|
||||
}
|
||||
|
||||
public _origin: Vector2 = Vector2.zero;
|
||||
|
||||
public get origin() {
|
||||
return this._origin;
|
||||
}
|
||||
@@ -164,56 +218,6 @@ module es {
|
||||
}
|
||||
}
|
||||
|
||||
public _zoom;
|
||||
public _minimumZoom = 0.3;
|
||||
public _maximumZoom = 3;
|
||||
public _bounds: Rectangle = new Rectangle();
|
||||
public _inset: CameraInset = new CameraInset();
|
||||
public _transformMatrix: Matrix2D = new Matrix2D().identity();
|
||||
public _inverseTransformMatrix: Matrix2D = new Matrix2D().identity();
|
||||
public _origin: Vector2 = Vector2.zero;
|
||||
|
||||
public _areMatrixedDirty: boolean = true;
|
||||
public _areBoundsDirty: boolean = true;
|
||||
public _isProjectionMatrixDirty = true;
|
||||
|
||||
/**
|
||||
* 如果相机模式为cameraWindow 则会进行缓动移动
|
||||
* 该值为移动速度
|
||||
*/
|
||||
public followLerp = 0.1;
|
||||
/**
|
||||
* 在cameraWindow模式下,宽度/高度被用做边界框,允许在不移动相机的情况下移动
|
||||
* 在lockOn模式下,只使用deadZone的x/y值 你可以通过直接setCenteredDeadzone重写它来自定义deadZone
|
||||
*/
|
||||
public deadzone: Rectangle = new Rectangle();
|
||||
/**
|
||||
* 相机聚焦于屏幕中心的偏移
|
||||
*/
|
||||
public focusOffset: Vector2 = Vector2.zero;
|
||||
/**
|
||||
* 如果为true 相机位置则不会超出地图矩形(0, 0, mapwidth, mapheight)
|
||||
*/
|
||||
public mapLockEnabled: boolean = false;
|
||||
/**
|
||||
* 當前地圖映射的寬度和高度
|
||||
*/
|
||||
public mapSize: Vector2 = Vector2.zero;
|
||||
|
||||
public _targetEntity: Entity;
|
||||
public _targetCollider: Collider;
|
||||
public _desiredPositionDelta: Vector2 = new Vector2();
|
||||
public _cameraStyle: CameraStyle;
|
||||
public _worldSpaceDeadZone: Rectangle = new Rectangle();
|
||||
|
||||
constructor(targetEntity: Entity = null, cameraStyle: CameraStyle = CameraStyle.lockOn) {
|
||||
super();
|
||||
|
||||
this._targetEntity = targetEntity;
|
||||
this._cameraStyle = cameraStyle;
|
||||
this.setZoom(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当场景渲染目标的大小发生变化时,我们会更新相机的原点并调整它的位置以保持它原来的位置
|
||||
* @param newWidth
|
||||
@@ -226,33 +230,6 @@ module es {
|
||||
this.entity.transform.position = Vector2.add(this.entity.transform.position, Vector2.subtract(this._origin, oldOrigin));
|
||||
}
|
||||
|
||||
protected updateMatrixes(){
|
||||
if (!this._areMatrixedDirty)
|
||||
return;
|
||||
|
||||
let tempMat: Matrix2D;
|
||||
this._transformMatrix = Matrix2D.create().translate(-this.entity.transform.position.x, -this.entity.transform.position.y);
|
||||
|
||||
if (this._zoom != 1){
|
||||
tempMat = Matrix2D.create().scale(this._zoom, this._zoom);
|
||||
this._transformMatrix = this._transformMatrix.multiply(tempMat);
|
||||
}
|
||||
|
||||
if (this.entity.transform.rotation != 0){
|
||||
tempMat = Matrix2D.create().rotate(this.entity.transform.rotation);
|
||||
this._transformMatrix = this._transformMatrix.multiply(tempMat);
|
||||
}
|
||||
|
||||
tempMat = Matrix2D.create().translate(this._origin.x, this._origin.y);
|
||||
this._transformMatrix =this._transformMatrix.multiply(tempMat);
|
||||
|
||||
this._inverseTransformMatrix = this._transformMatrix.invert();
|
||||
|
||||
// 无论何时矩阵改变边界都是无效的
|
||||
this._areBoundsDirty = true;
|
||||
this._areMatrixedDirty = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置用于从视口边缘插入摄像机边界的量
|
||||
* @param left
|
||||
@@ -357,7 +334,7 @@ module es {
|
||||
* 将一个点从世界坐标转换到屏幕
|
||||
* @param worldPosition
|
||||
*/
|
||||
public worldToScreenPoint(worldPosition: Vector2): Vector2{
|
||||
public worldToScreenPoint(worldPosition: Vector2): Vector2 {
|
||||
this.updateMatrixes();
|
||||
worldPosition = Vector2.transform(worldPosition, this._transformMatrix);
|
||||
return worldPosition;
|
||||
@@ -367,7 +344,7 @@ module es {
|
||||
* 将点从屏幕坐标转换为世界坐标
|
||||
* @param screenPosition
|
||||
*/
|
||||
public screenToWorldPoint(screenPosition: Vector2): Vector2{
|
||||
public screenToWorldPoint(screenPosition: Vector2): Vector2 {
|
||||
this.updateMatrixes();
|
||||
screenPosition = Vector2.transform(screenPosition, this._inverseTransformMatrix);
|
||||
return screenPosition;
|
||||
@@ -376,7 +353,7 @@ module es {
|
||||
/**
|
||||
* 返回鼠标在世界空间中的位置
|
||||
*/
|
||||
public mouseToWorldPoint(): Vector2{
|
||||
public mouseToWorldPoint(): Vector2 {
|
||||
return this.screenToWorldPoint(Input.touchPosition);
|
||||
}
|
||||
|
||||
@@ -476,5 +453,32 @@ module es {
|
||||
public setCenteredDeadzone(width: number, height: number) {
|
||||
this.deadzone = new Rectangle((this.bounds.width - width) / 2, (this.bounds.height - height) / 2, width, height);
|
||||
}
|
||||
|
||||
protected updateMatrixes() {
|
||||
if (!this._areMatrixedDirty)
|
||||
return;
|
||||
|
||||
let tempMat: Matrix2D;
|
||||
this._transformMatrix = Matrix2D.create().translate(-this.entity.transform.position.x, -this.entity.transform.position.y);
|
||||
|
||||
if (this._zoom != 1) {
|
||||
tempMat = Matrix2D.create().scale(this._zoom, this._zoom);
|
||||
this._transformMatrix = this._transformMatrix.multiply(tempMat);
|
||||
}
|
||||
|
||||
if (this.entity.transform.rotation != 0) {
|
||||
tempMat = Matrix2D.create().rotate(this.entity.transform.rotation);
|
||||
this._transformMatrix = this._transformMatrix.multiply(tempMat);
|
||||
}
|
||||
|
||||
tempMat = Matrix2D.create().translate(this._origin.x, this._origin.y);
|
||||
this._transformMatrix = this._transformMatrix.multiply(tempMat);
|
||||
|
||||
this._inverseTransformMatrix = this._transformMatrix.invert();
|
||||
|
||||
// 无论何时矩阵改变边界都是无效的
|
||||
this._areBoundsDirty = true;
|
||||
this._areMatrixedDirty = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
module es {
|
||||
export class ComponentPool<T extends PooledComponent>{
|
||||
export class ComponentPool<T extends PooledComponent> {
|
||||
private _cache: T[];
|
||||
private _type: any;
|
||||
|
||||
constructor(typeClass: any){
|
||||
constructor(typeClass: any) {
|
||||
this._type = typeClass;
|
||||
this._cache = [];
|
||||
}
|
||||
|
||||
public obtain(): T{
|
||||
public obtain(): T {
|
||||
try {
|
||||
return this._cache.length > 0 ? this._cache.shift() : new this._type();
|
||||
} catch(err){
|
||||
} catch (err) {
|
||||
throw new Error(this._type + err);
|
||||
}
|
||||
}
|
||||
|
||||
public free(component: T){
|
||||
public free(component: T) {
|
||||
component.reset();
|
||||
this._cache.push(component);
|
||||
}
|
||||
|
||||
@@ -3,8 +3,8 @@ module es {
|
||||
* 用于比较组件更新排序
|
||||
*/
|
||||
export class IUpdatableComparer {
|
||||
public compare(a: Component, b: Component){
|
||||
return a.updateOrder - b.updateOrder;
|
||||
}
|
||||
public compare(a: Component, b: Component) {
|
||||
return a.updateOrder - b.updateOrder;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,13 +3,13 @@ module es {
|
||||
export class Mesh extends RenderableComponent {
|
||||
private _mesh: egret.Mesh;
|
||||
|
||||
constructor(){
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this._mesh = new egret.Mesh();
|
||||
}
|
||||
|
||||
public setTexture(texture: egret.Texture): Mesh{
|
||||
public setTexture(texture: egret.Texture): Mesh {
|
||||
this._mesh.texture = texture;
|
||||
|
||||
return this;
|
||||
|
||||
@@ -1,26 +1,10 @@
|
||||
///<reference path="./Collider.ts" />
|
||||
module es {
|
||||
export class BoxCollider extends Collider {
|
||||
public get width(){
|
||||
return (this.shape as Box).width;
|
||||
}
|
||||
|
||||
public set width(value: number){
|
||||
this.setWidth(value);
|
||||
}
|
||||
|
||||
public get height(){
|
||||
return (this.shape as Box).height;
|
||||
}
|
||||
|
||||
public set height(value: number){
|
||||
this.setHeight(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 零参数构造函数要求RenderableComponent在实体上,这样碰撞器可以在实体被添加到场景时调整自身的大小。
|
||||
*/
|
||||
constructor(){
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
// 我们在这里插入一个1x1框作为占位符,直到碰撞器在下一阵被添加到实体并可以获得更精确的自动调整大小数据
|
||||
@@ -28,15 +12,31 @@ module es {
|
||||
this._colliderRequiresAutoSizing = true;
|
||||
}
|
||||
|
||||
public get width() {
|
||||
return (this.shape as Box).width;
|
||||
}
|
||||
|
||||
public set width(value: number) {
|
||||
this.setWidth(value);
|
||||
}
|
||||
|
||||
public get height() {
|
||||
return (this.shape as Box).height;
|
||||
}
|
||||
|
||||
public set height(value: number) {
|
||||
this.setHeight(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置BoxCollider的大小
|
||||
* @param width
|
||||
* @param height
|
||||
*/
|
||||
public setSize(width: number, height: number){
|
||||
public setSize(width: number, height: number) {
|
||||
this._colliderRequiresAutoSizing = false;
|
||||
let box = this.shape as Box;
|
||||
if (width != box.width || height != box.height){
|
||||
if (width != box.width || height != box.height) {
|
||||
// 更新框,改变边界,如果我们需要更新物理系统中的边界
|
||||
box.updateBox(width, height);
|
||||
if (this.entity && this._isParentEntityAddedToScene)
|
||||
@@ -50,10 +50,10 @@ module es {
|
||||
* 设置BoxCollider的宽度
|
||||
* @param width
|
||||
*/
|
||||
public setWidth(width: number): BoxCollider{
|
||||
public setWidth(width: number): BoxCollider {
|
||||
this._colliderRequiresAutoSizing = false;
|
||||
let box = this.shape as Box;
|
||||
if (width != box.width){
|
||||
if (width != box.width) {
|
||||
// 更新框,改变边界,如果我们需要更新物理系统中的边界
|
||||
box.updateBox(width, box.height);
|
||||
if (this.entity && this._isParentEntityAddedToScene)
|
||||
@@ -67,10 +67,10 @@ module es {
|
||||
* 设置BoxCollider的高度
|
||||
* @param height
|
||||
*/
|
||||
public setHeight(height: number){
|
||||
public setHeight(height: number) {
|
||||
this._colliderRequiresAutoSizing = false;
|
||||
let box = this.shape as Box;
|
||||
if (height != box.height){
|
||||
if (height != box.height) {
|
||||
// 更新框,改变边界,如果我们需要更新物理系统中的边界
|
||||
box.updateBox(box.width, height);
|
||||
if (this.entity && this._isParentEntityAddedToScene)
|
||||
@@ -78,7 +78,7 @@ module es {
|
||||
}
|
||||
}
|
||||
|
||||
public toString(){
|
||||
public toString() {
|
||||
return `[BoxCollider: bounds: ${this.bounds}]`;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,5 @@
|
||||
module es {
|
||||
export class CircleCollider extends Collider {
|
||||
public get radius(): number {
|
||||
return (this.shape as Circle).radius;
|
||||
}
|
||||
|
||||
public set radius(value: number) {
|
||||
this.setRadius(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个有半径的圆
|
||||
*
|
||||
@@ -23,6 +15,14 @@ module es {
|
||||
this.shape = new Circle(radius ? radius : 1);
|
||||
}
|
||||
|
||||
public get radius(): number {
|
||||
return (this.shape as Circle).radius;
|
||||
}
|
||||
|
||||
public set radius(value: number) {
|
||||
this.setRadius(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置圆的半径
|
||||
* @param radius
|
||||
|
||||
@@ -4,23 +4,40 @@ module es {
|
||||
* 对撞机的基本形状
|
||||
*/
|
||||
public shape: Shape;
|
||||
|
||||
/**
|
||||
* 将localOffset添加到实体。获取碰撞器几何图形的最终位置。
|
||||
* 允许向一个实体添加多个碰撞器并分别定位,还允许你设置缩放/旋转
|
||||
* 如果这个碰撞器是一个触发器,它将不会引起碰撞,但它仍然会触发事件
|
||||
*/
|
||||
public get localOffset(): Vector2 {
|
||||
return this._localOffset;
|
||||
}
|
||||
|
||||
public isTrigger: boolean;
|
||||
/**
|
||||
* 将localOffset添加到实体。获取碰撞器几何图形的最终位置。
|
||||
* 允许向一个实体添加多个碰撞器并分别定位,还允许你设置缩放/旋转
|
||||
* @param value
|
||||
* 在处理冲突时,physicsLayer可以用作过滤器。Flags类有帮助位掩码的方法
|
||||
*/
|
||||
public set localOffset(value: Vector2) {
|
||||
this.setLocalOffset(value);
|
||||
}
|
||||
public physicsLayer = 1 << 0;
|
||||
/**
|
||||
* 碰撞器在使用移动器移动时应该碰撞的层
|
||||
* 默认为所有层
|
||||
*/
|
||||
public collidesWithLayers = Physics.allLayers;
|
||||
/**
|
||||
* 如果为true,碰撞器将根据附加的变换缩放和旋转
|
||||
*/
|
||||
public shouldColliderScaleAndRotateWithTransform = true;
|
||||
/**
|
||||
* 这个对撞机在物理系统注册时的边界。
|
||||
* 存储这个允许我们始终能够安全地从物理系统中移除对撞机,即使它在试图移除它之前已经被移动了。
|
||||
*/
|
||||
public registeredPhysicsBounds: Rectangle = new Rectangle();
|
||||
public _localOffsetLength: number;
|
||||
public _isPositionDirty: boolean = true;
|
||||
public _isRotationDirty: boolean = true;
|
||||
protected _colliderRequiresAutoSizing;
|
||||
/**
|
||||
* 标记来跟踪我们的实体是否被添加到场景中
|
||||
*/
|
||||
protected _isParentEntityAddedToScene;
|
||||
/**
|
||||
* 标记来记录我们是否注册了物理系统
|
||||
*/
|
||||
protected _isColliderRegistered;
|
||||
|
||||
/**
|
||||
* 镖师碰撞器的绝对位置
|
||||
@@ -39,27 +56,8 @@ module es {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果这个碰撞器是一个触发器,它将不会引起碰撞,但它仍然会触发事件
|
||||
*/
|
||||
public isTrigger: boolean;
|
||||
/**
|
||||
* 在处理冲突时,physicsLayer可以用作过滤器。Flags类有帮助位掩码的方法
|
||||
*/
|
||||
public physicsLayer = 1 << 0;
|
||||
/**
|
||||
* 碰撞器在使用移动器移动时应该碰撞的层
|
||||
* 默认为所有层
|
||||
*/
|
||||
public collidesWithLayers = Physics.allLayers;
|
||||
|
||||
/**
|
||||
* 如果为true,碰撞器将根据附加的变换缩放和旋转
|
||||
*/
|
||||
public shouldColliderScaleAndRotateWithTransform = true;
|
||||
|
||||
public get bounds(): Rectangle {
|
||||
if (this._isPositionDirty || this._isRotationDirty){
|
||||
if (this._isPositionDirty || this._isRotationDirty) {
|
||||
this.shape.recalculateBounds(this);
|
||||
this._isPositionDirty = this._isRotationDirty = false;
|
||||
}
|
||||
@@ -67,26 +65,24 @@ module es {
|
||||
return this.shape.bounds;
|
||||
}
|
||||
|
||||
/**
|
||||
* 这个对撞机在物理系统注册时的边界。
|
||||
* 存储这个允许我们始终能够安全地从物理系统中移除对撞机,即使它在试图移除它之前已经被移动了。
|
||||
*/
|
||||
public registeredPhysicsBounds: Rectangle = new Rectangle();
|
||||
protected _colliderRequiresAutoSizing;
|
||||
protected _localOffset: Vector2 = Vector2.zero;
|
||||
public _localOffsetLength: number;
|
||||
|
||||
/**
|
||||
* 标记来跟踪我们的实体是否被添加到场景中
|
||||
* 将localOffset添加到实体。获取碰撞器几何图形的最终位置。
|
||||
* 允许向一个实体添加多个碰撞器并分别定位,还允许你设置缩放/旋转
|
||||
*/
|
||||
protected _isParentEntityAddedToScene;
|
||||
/**
|
||||
* 标记来记录我们是否注册了物理系统
|
||||
*/
|
||||
protected _isColliderRegistered;
|
||||
public get localOffset(): Vector2 {
|
||||
return this._localOffset;
|
||||
}
|
||||
|
||||
public _isPositionDirty: boolean = true;
|
||||
public _isRotationDirty: boolean = true;
|
||||
/**
|
||||
* 将localOffset添加到实体。获取碰撞器几何图形的最终位置。
|
||||
* 允许向一个实体添加多个碰撞器并分别定位,还允许你设置缩放/旋转
|
||||
* @param value
|
||||
*/
|
||||
public set localOffset(value: Vector2) {
|
||||
this.setLocalOffset(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将localOffset添加到实体。获取碰撞器的最终位置。
|
||||
@@ -229,7 +225,7 @@ module es {
|
||||
return didCollide;
|
||||
}
|
||||
|
||||
public clone(): Component{
|
||||
public clone(): Component {
|
||||
let collider = ObjectUtils.clone<Collider>(this);
|
||||
collider.entity = null;
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
module es{
|
||||
module es {
|
||||
/**
|
||||
* 当添加到组件时,每当实体上的冲突器与另一个组件重叠/退出时,将调用这些方法。
|
||||
* ITriggerListener方法将在实现接口的触发器实体上的任何组件上调用。
|
||||
|
||||
@@ -9,7 +9,7 @@ module es {
|
||||
export class Mover extends Component {
|
||||
private _triggerHelper: ColliderTriggerHelper;
|
||||
|
||||
public onAddedToEntity(){
|
||||
public onAddedToEntity() {
|
||||
this._triggerHelper = new ColliderTriggerHelper(this.entity);
|
||||
}
|
||||
|
||||
@@ -18,14 +18,14 @@ module es {
|
||||
* @param motion
|
||||
* @param collisionResult
|
||||
*/
|
||||
public calculateMovement(motion: Vector2, collisionResult: CollisionResult): boolean{
|
||||
if (!this.entity.getComponent(Collider) || !this._triggerHelper){
|
||||
public calculateMovement(motion: Vector2, collisionResult: CollisionResult): boolean {
|
||||
if (!this.entity.getComponent(Collider) || !this._triggerHelper) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 移动所有的非触发碰撞器并获得最近的碰撞
|
||||
let colliders: Collider[] = this.entity.getComponents(Collider);
|
||||
for (let i = 0; i < colliders.length; i ++){
|
||||
for (let i = 0; i < colliders.length; i++) {
|
||||
let collider = colliders[i];
|
||||
|
||||
// 不检测触发器 在我们移动后会重新访问它
|
||||
@@ -38,19 +38,19 @@ module es {
|
||||
bounds.y += motion.y;
|
||||
let neighbors = Physics.boxcastBroadphaseExcludingSelf(collider, bounds, collider.collidesWithLayers);
|
||||
|
||||
for (let j = 0; j < neighbors.length; j ++){
|
||||
for (let j = 0; j < neighbors.length; j++) {
|
||||
let neighbor = neighbors[j];
|
||||
// 不检测触发器
|
||||
if (neighbor.isTrigger)
|
||||
continue;
|
||||
|
||||
let _internalcollisionResult: CollisionResult = new CollisionResult();
|
||||
if (collider.collidesWith(neighbor, motion, _internalcollisionResult)){
|
||||
if (collider.collidesWith(neighbor, motion, _internalcollisionResult)) {
|
||||
// 如果碰撞 则退回之前的移动量
|
||||
motion = motion.subtract(_internalcollisionResult.minimumTranslationVector);
|
||||
|
||||
// 如果我们碰到多个对象,为了简单起见,只取第一个。
|
||||
if (_internalcollisionResult.collider != null){
|
||||
if (_internalcollisionResult.collider != null) {
|
||||
collisionResult = _internalcollisionResult;
|
||||
}
|
||||
}
|
||||
@@ -66,7 +66,7 @@ module es {
|
||||
* 将calculatemomovement应用到实体并更新triggerHelper
|
||||
* @param motion
|
||||
*/
|
||||
public applyMovement(motion: Vector2){
|
||||
public applyMovement(motion: Vector2) {
|
||||
// 移动实体到它的新位置,如果我们有一个碰撞,否则移动全部数量。当碰撞发生时,运动被更新
|
||||
this.entity.position = Vector2.add(this.entity.position, motion);
|
||||
|
||||
@@ -80,7 +80,7 @@ module es {
|
||||
* @param motion
|
||||
* @param collisionResult
|
||||
*/
|
||||
public move(motion: Vector2, collisionResult: CollisionResult){
|
||||
public move(motion: Vector2, collisionResult: CollisionResult) {
|
||||
this.calculateMovement(motion, collisionResult);
|
||||
this.applyMovement(motion);
|
||||
return collisionResult.collider != null;
|
||||
|
||||
@@ -7,7 +7,7 @@ module es {
|
||||
private _tempTriggerList: ITriggerListener[] = [];
|
||||
private _collider: Collider;
|
||||
|
||||
public onAddedToEntity(){
|
||||
public onAddedToEntity() {
|
||||
this._collider = this.entity.getComponent<Collider>(Collider);
|
||||
if (!this._collider)
|
||||
console.warn("ProjectileMover has no Collider. ProjectilMover requires a Collider!");
|
||||
@@ -17,7 +17,7 @@ module es {
|
||||
* 移动考虑碰撞的实体
|
||||
* @param motion
|
||||
*/
|
||||
public move(motion: Vector2): boolean{
|
||||
public move(motion: Vector2): boolean {
|
||||
if (!this._collider)
|
||||
return false;
|
||||
|
||||
@@ -28,9 +28,9 @@ module es {
|
||||
|
||||
// 获取任何可能在新位置发生碰撞的东西
|
||||
let neighbors = Physics.boxcastBroadphase(this._collider.bounds, this._collider.collidesWithLayers);
|
||||
for (let i = 0; i < neighbors.length; i ++){
|
||||
for (let i = 0; i < neighbors.length; i++) {
|
||||
let neighbor = neighbors[i];
|
||||
if (this._collider.overlaps(neighbor) && neighbor.enabled){
|
||||
if (this._collider.overlaps(neighbor) && neighbor.enabled) {
|
||||
didCollide = true;
|
||||
this.notifyTriggerListeners(this._collider, neighbor);
|
||||
}
|
||||
@@ -39,17 +39,17 @@ module es {
|
||||
return didCollide;
|
||||
}
|
||||
|
||||
private notifyTriggerListeners(self: Collider, other: Collider){
|
||||
private notifyTriggerListeners(self: Collider, other: Collider) {
|
||||
// 通知我们重叠的碰撞器实体上的任何侦听器
|
||||
other.entity.getComponents("ITriggerListener", this._tempTriggerList);
|
||||
for (let i = 0; i < this._tempTriggerList.length; i ++){
|
||||
for (let i = 0; i < this._tempTriggerList.length; i++) {
|
||||
this._tempTriggerList[i].onTriggerEnter(self, other);
|
||||
}
|
||||
this._tempTriggerList.length = 0;
|
||||
|
||||
// 通知此实体上的任何侦听器
|
||||
this.entity.getComponents("ITriggerListener", this._tempTriggerList);
|
||||
for (let i = 0; i < this._tempTriggerList.length; i ++){
|
||||
for (let i = 0; i < this._tempTriggerList.length; i++) {
|
||||
this._tempTriggerList[i].onTriggerEnter(other, self);
|
||||
}
|
||||
this._tempTriggerList.length = 0;
|
||||
|
||||
@@ -8,6 +8,12 @@ module es {
|
||||
* 用于装载egret显示对象
|
||||
*/
|
||||
public displayObject: egret.DisplayObject = new egret.DisplayObject();
|
||||
/**
|
||||
* 用于着色器处理精灵
|
||||
*/
|
||||
public color: number = 0x000000;
|
||||
protected _areBoundsDirty = true;
|
||||
|
||||
/**
|
||||
* renderableComponent的宽度
|
||||
* 如果你不重写bounds属性则需要实现这个
|
||||
@@ -24,11 +30,43 @@ module es {
|
||||
return this.bounds.height;
|
||||
}
|
||||
|
||||
protected _localOffset: Vector2 = Vector2.zero;
|
||||
|
||||
/**
|
||||
* 从父实体的偏移量。用于向需要特定定位的实体
|
||||
*/
|
||||
public get localOffset(): Vector2 {
|
||||
return this._localOffset;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从父实体的偏移量。用于向需要特定定位的实体
|
||||
* @param value
|
||||
*/
|
||||
public set localOffset(value: Vector2) {
|
||||
this.setLocalOffset(value);
|
||||
}
|
||||
|
||||
protected _renderLayer: number = 0;
|
||||
|
||||
/**
|
||||
* 较低的渲染层在前面,较高的在后面
|
||||
*/
|
||||
public get renderLayer(): number {
|
||||
return this._renderLayer;
|
||||
}
|
||||
|
||||
public set renderLayer(value: number) {
|
||||
|
||||
}
|
||||
|
||||
protected _bounds: Rectangle = new Rectangle();
|
||||
|
||||
/**
|
||||
* 这个物体的AABB, 用于相机剔除
|
||||
*/
|
||||
public get bounds(): Rectangle {
|
||||
if (this._areBoundsDirty){
|
||||
if (this._areBoundsDirty) {
|
||||
this._bounds.calculateBounds(this.entity.transform.position, this._localOffset, Vector2.zero,
|
||||
this.entity.transform.scale, this.entity.transform.rotation, this.width, this.height);
|
||||
this._areBoundsDirty = false;
|
||||
@@ -37,36 +75,7 @@ module es {
|
||||
return this._bounds;
|
||||
}
|
||||
|
||||
/**
|
||||
* 较低的渲染层在前面,较高的在后面
|
||||
*/
|
||||
public get renderLayer(): number{
|
||||
return this._renderLayer;
|
||||
}
|
||||
|
||||
public set renderLayer(value: number){
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于着色器处理精灵
|
||||
*/
|
||||
public color: number = 0x000000;
|
||||
|
||||
/**
|
||||
* 从父实体的偏移量。用于向需要特定定位的实体
|
||||
*/
|
||||
public get localOffset(): Vector2{
|
||||
return this._localOffset;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从父实体的偏移量。用于向需要特定定位的实体
|
||||
* @param value
|
||||
*/
|
||||
public set localOffset(value: Vector2){
|
||||
this.setLocalOffset(value);
|
||||
}
|
||||
private _isVisible: boolean;
|
||||
|
||||
/**
|
||||
* 可渲染的可见性。状态的改变会调用onBecameVisible/onBecameInvisible方法
|
||||
@@ -80,7 +89,7 @@ module es {
|
||||
* @param value
|
||||
*/
|
||||
public set isVisible(value: boolean) {
|
||||
if (this._isVisible != value){
|
||||
if (this._isVisible != value) {
|
||||
this._isVisible = value;
|
||||
|
||||
if (this._isVisible)
|
||||
@@ -90,12 +99,6 @@ module es {
|
||||
}
|
||||
}
|
||||
|
||||
protected _localOffset: Vector2 = Vector2.zero;
|
||||
protected _renderLayer: number = 0;
|
||||
protected _bounds: Rectangle = new Rectangle();
|
||||
private _isVisible: boolean;
|
||||
protected _areBoundsDirty = true;
|
||||
|
||||
public onEntityTransformChanged(comp: transform.Component) {
|
||||
this._areBoundsDirty = true;
|
||||
}
|
||||
@@ -106,22 +109,6 @@ module es {
|
||||
*/
|
||||
public abstract render(camera: Camera);
|
||||
|
||||
/**
|
||||
* 当renderableComponent进入相机框架时调用
|
||||
* 如果渲染器不适用isVisibleFromCamera进行剔除检查 这些方法不会被调用
|
||||
*/
|
||||
protected onBecameVisible() {
|
||||
this.displayObject.visible = this.isVisible;
|
||||
}
|
||||
|
||||
/**
|
||||
* 当renderableComponent离开相机框架时调用
|
||||
* 如果渲染器不适用isVisibleFromCamera进行剔除检查 这些方法不会被调用
|
||||
*/
|
||||
protected onBecameInvisible() {
|
||||
this.displayObject.visible = this.isVisible;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果renderableComponent的边界与camera.bounds相交 返回true
|
||||
* 用于处理isVisible标志的状态开关
|
||||
@@ -137,8 +124,8 @@ module es {
|
||||
* 较低的渲染层在前面,较高的在后面
|
||||
* @param renderLayer
|
||||
*/
|
||||
public setRenderLayer(renderLayer: number): RenderableComponent{
|
||||
if (renderLayer != this._renderLayer){
|
||||
public setRenderLayer(renderLayer: number): RenderableComponent {
|
||||
if (renderLayer != this._renderLayer) {
|
||||
let oldRenderLayer = this._renderLayer;
|
||||
this._renderLayer = renderLayer;
|
||||
|
||||
@@ -154,7 +141,7 @@ module es {
|
||||
* 用于着色器处理精灵
|
||||
* @param color
|
||||
*/
|
||||
public setColor(color: number): RenderableComponent{
|
||||
public setColor(color: number): RenderableComponent {
|
||||
this.color = color;
|
||||
return this;
|
||||
}
|
||||
@@ -163,8 +150,8 @@ module es {
|
||||
* 从父实体的偏移量。用于向需要特定定位的实体
|
||||
* @param offset
|
||||
*/
|
||||
public setLocalOffset(offset: Vector2): RenderableComponent{
|
||||
if (this._localOffset != offset){
|
||||
public setLocalOffset(offset: Vector2): RenderableComponent {
|
||||
if (this._localOffset != offset) {
|
||||
this._localOffset = offset;
|
||||
}
|
||||
|
||||
@@ -174,7 +161,7 @@ module es {
|
||||
/**
|
||||
* 进行状态同步
|
||||
*/
|
||||
public sync(camera: Camera){
|
||||
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;
|
||||
@@ -182,8 +169,24 @@ module es {
|
||||
this.displayObject.rotation = this.entity.rotation;
|
||||
}
|
||||
|
||||
public toString(){
|
||||
public toString() {
|
||||
return `[RenderableComponent] renderLayer: ${this.renderLayer}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 当renderableComponent进入相机框架时调用
|
||||
* 如果渲染器不适用isVisibleFromCamera进行剔除检查 这些方法不会被调用
|
||||
*/
|
||||
protected onBecameVisible() {
|
||||
this.displayObject.visible = this.isVisible;
|
||||
}
|
||||
|
||||
/**
|
||||
* 当renderableComponent离开相机框架时调用
|
||||
* 如果渲染器不适用isVisibleFromCamera进行剔除检查 这些方法不会被调用
|
||||
*/
|
||||
protected onBecameInvisible() {
|
||||
this.displayObject.visible = this.isVisible;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ module es {
|
||||
private _scrollX = 0;
|
||||
private _scrollY = 0;
|
||||
|
||||
public update(){
|
||||
public update() {
|
||||
this._scrollX += this.scrollSpeedX * Time.deltaTime;
|
||||
this._scrollY += this.scroolSpeedY * Time.deltaTime;
|
||||
this.sourceRect.x = this._scrollX;
|
||||
|
||||
@@ -3,7 +3,7 @@ module es {
|
||||
public readonly sprites: Sprite[];
|
||||
public readonly frameRate: number;
|
||||
|
||||
constructor(sprites: Sprite[], frameRate: number){
|
||||
constructor(sprites: Sprite[], frameRate: number) {
|
||||
this.sprites = sprites;
|
||||
this.frameRate = frameRate;
|
||||
}
|
||||
|
||||
@@ -45,6 +45,12 @@ module es {
|
||||
* 当前动画的精灵数组中当前帧的索引
|
||||
*/
|
||||
public currentFrame: number;
|
||||
public _elapsedTime: number = 0;
|
||||
public _loopMode: LoopMode;
|
||||
|
||||
constructor(sprite?: Sprite) {
|
||||
super(sprite);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查当前动画是否正在运行
|
||||
@@ -53,19 +59,13 @@ module es {
|
||||
return this.animationState == State.running;
|
||||
}
|
||||
|
||||
private _animations: Map<string, SpriteAnimation> = new Map<string, SpriteAnimation>();
|
||||
|
||||
/** 提供对可用动画列表的访问 */
|
||||
public get animations() {
|
||||
return this._animations;
|
||||
}
|
||||
|
||||
private _animations: Map<string, SpriteAnimation> = new Map<string, SpriteAnimation>();
|
||||
public _elapsedTime: number = 0;
|
||||
public _loopMode: LoopMode;
|
||||
|
||||
constructor(sprite?: Sprite) {
|
||||
super(sprite);
|
||||
}
|
||||
|
||||
public update() {
|
||||
if (this.animationState != State.running || !this.currentAnimation) return;
|
||||
|
||||
|
||||
@@ -2,6 +2,14 @@ module es {
|
||||
import Bitmap = egret.Bitmap;
|
||||
|
||||
export class SpriteRenderer extends RenderableComponent {
|
||||
constructor(sprite: Sprite | egret.Texture = null) {
|
||||
super();
|
||||
if (sprite instanceof Sprite)
|
||||
this.setSprite(sprite);
|
||||
else if (sprite instanceof egret.Texture)
|
||||
this.setSprite(new Sprite(sprite));
|
||||
}
|
||||
|
||||
public get bounds() {
|
||||
if (this._areBoundsDirty) {
|
||||
if (this._sprite) {
|
||||
@@ -15,21 +23,6 @@ module es {
|
||||
return this._bounds;
|
||||
}
|
||||
|
||||
/**
|
||||
* 精灵的原点。这是在设置精灵时自动设置的
|
||||
*/
|
||||
public get origin(): Vector2 {
|
||||
return this._origin;
|
||||
}
|
||||
|
||||
/**
|
||||
* 精灵的原点。这是在设置精灵时自动设置的
|
||||
* @param value
|
||||
*/
|
||||
public set origin(value: Vector2) {
|
||||
this.setOrigin(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用归一化方法设置原点
|
||||
* x/y 均为 0-1
|
||||
@@ -49,6 +42,25 @@ module es {
|
||||
value.y * this.height / this.entity.transform.scale.y));
|
||||
}
|
||||
|
||||
protected _origin: Vector2;
|
||||
|
||||
/**
|
||||
* 精灵的原点。这是在设置精灵时自动设置的
|
||||
*/
|
||||
public get origin(): Vector2 {
|
||||
return this._origin;
|
||||
}
|
||||
|
||||
/**
|
||||
* 精灵的原点。这是在设置精灵时自动设置的
|
||||
* @param value
|
||||
*/
|
||||
public set origin(value: Vector2) {
|
||||
this.setOrigin(value);
|
||||
}
|
||||
|
||||
protected _sprite: Sprite;
|
||||
|
||||
/**
|
||||
* 应该由这个精灵显示的精灵
|
||||
* 当设置时,精灵的原点也被设置为精灵的origin
|
||||
@@ -66,17 +78,6 @@ module es {
|
||||
this.setSprite(value);
|
||||
}
|
||||
|
||||
protected _origin: Vector2;
|
||||
protected _sprite: Sprite;
|
||||
|
||||
constructor(sprite: Sprite | egret.Texture = null) {
|
||||
super();
|
||||
if (sprite instanceof Sprite)
|
||||
this.setSprite(sprite);
|
||||
else if (sprite instanceof egret.Texture)
|
||||
this.setSprite(new Sprite(sprite));
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置精灵并更新精灵的原点以匹配sprite.origin
|
||||
* @param sprite
|
||||
|
||||
@@ -8,19 +8,6 @@ module es {
|
||||
protected leftTexture: egret.Bitmap;
|
||||
protected rightTexture: egret.Bitmap;
|
||||
|
||||
public get scrollX() {
|
||||
return this.sourceRect.x;
|
||||
}
|
||||
public set scrollX(value: number) {
|
||||
this.sourceRect.x = value;
|
||||
}
|
||||
public get scrollY() {
|
||||
return this.sourceRect.y;
|
||||
}
|
||||
public set scrollY(value: number) {
|
||||
this.sourceRect.y = value;
|
||||
}
|
||||
|
||||
constructor(sprite: Sprite) {
|
||||
super(sprite);
|
||||
|
||||
@@ -33,6 +20,22 @@ module es {
|
||||
this.sourceRect = sprite.sourceRect;
|
||||
}
|
||||
|
||||
public get scrollX() {
|
||||
return this.sourceRect.x;
|
||||
}
|
||||
|
||||
public set scrollX(value: number) {
|
||||
this.sourceRect.x = value;
|
||||
}
|
||||
|
||||
public get scrollY() {
|
||||
return this.sourceRect.y;
|
||||
}
|
||||
|
||||
public set scrollY(value: number) {
|
||||
this.sourceRect.y = value;
|
||||
}
|
||||
|
||||
public render(camera: es.Camera) {
|
||||
if (!this.sprite)
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user