新增虚拟输入类 VirtualInput
This commit is contained in:
@@ -8,12 +8,24 @@ module es {
|
||||
/**
|
||||
* 零参数构造函数要求RenderableComponent在实体上,这样碰撞器可以在实体被添加到场景时调整自身的大小。
|
||||
*/
|
||||
constructor() {
|
||||
constructor(x?: number, y?: number, width?: number, height?: number) {
|
||||
super();
|
||||
|
||||
// 我们在这里插入一个1x1框作为占位符,直到碰撞器在下一阵被添加到实体并可以获得更精确的自动调整大小数据
|
||||
this.shape = new Box(1, 1);
|
||||
this._colliderRequiresAutoSizing = true;
|
||||
if (x == undefined && y == undefined){
|
||||
if (width == undefined && height == undefined){
|
||||
// 我们在这里插入一个1x1框作为占位符,直到碰撞器在下一阵被添加到实体并可以获得更精确的自动调整大小数据
|
||||
this.shape = new Box(1, 1);
|
||||
this._colliderRequiresAutoSizing = true;
|
||||
}else if (width != undefined && height != undefined){
|
||||
x = -width / 2;
|
||||
y = -height / 2;
|
||||
this._localOffset = new Vector2(x + width / 2, y + height / 2);
|
||||
this.shape = new Box(width, height);
|
||||
}
|
||||
}else if (x != undefined && y != undefined && width != undefined && height != undefined){
|
||||
this._localOffset = new Vector2(x + width / 2, y + height / 2);
|
||||
this.shape = new Box(width, height);
|
||||
}
|
||||
}
|
||||
|
||||
public get width() {
|
||||
@@ -32,20 +44,6 @@ module es {
|
||||
this.setHeight(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个BoxCollider并使用x/y组件作为localOffset
|
||||
* @param x
|
||||
* @param y
|
||||
* @param width
|
||||
* @param height
|
||||
*/
|
||||
public createBoxRect(x: number, y: number, width: number, height: number): BoxCollider{
|
||||
this._localOffset = new Vector2(x + width / 2, y + width / 2);
|
||||
this.shape = new Box(width, height);
|
||||
this._colliderRequiresAutoSizing = false;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置BoxCollider的大小
|
||||
* @param width
|
||||
@@ -110,11 +108,11 @@ module es {
|
||||
if (!this.pixelShape2.parent)
|
||||
this.debugDisplayObject.addChild(this.pixelShape2);
|
||||
|
||||
// this.hollowShape.graphics.clear();
|
||||
// this.hollowShape.graphics.beginFill(Colors.colliderBounds, 0);
|
||||
// this.hollowShape.graphics.lineStyle(Size.lineSizeMultiplier, Colors.colliderBounds);
|
||||
// this.hollowShape.graphics.drawRect(this.bounds.x, this.bounds.y, this.bounds.width, this.bounds.height);
|
||||
// this.hollowShape.graphics.endFill();
|
||||
this.hollowShape.graphics.clear();
|
||||
this.hollowShape.graphics.beginFill(Colors.colliderBounds, 0);
|
||||
this.hollowShape.graphics.lineStyle(Size.lineSizeMultiplier, Colors.colliderBounds);
|
||||
this.hollowShape.graphics.drawRect(this.bounds.x, this.bounds.y, this.bounds.width, this.bounds.height);
|
||||
this.hollowShape.graphics.endFill();
|
||||
|
||||
this.polygonShape.graphics.clear();
|
||||
if (poly.points.length >= 2){
|
||||
|
||||
@@ -13,11 +13,14 @@ module es {
|
||||
constructor(radius?: number) {
|
||||
super();
|
||||
|
||||
if (radius)
|
||||
if (radius == undefined){
|
||||
// 我们在这里插入一个1px的圆圈作为占位符
|
||||
// 直到碰撞器被添加到实体并可以获得更精确的自动调整大小数据的下一帧
|
||||
this.shape = new Circle(1);
|
||||
this._colliderRequiresAutoSizing = true;
|
||||
// 我们在这里插入一个1px的圆圈作为占位符
|
||||
// 直到碰撞器被添加到实体并可以获得更精确的自动调整大小数据的下一帧
|
||||
this.shape = new Circle(radius ? radius : 1);
|
||||
}else{
|
||||
this.shape = new Circle(radius);
|
||||
}
|
||||
}
|
||||
|
||||
public get radius(): number {
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
module es {
|
||||
export abstract class Collider extends Component {
|
||||
public debug
|
||||
/**
|
||||
* 对撞机的基本形状
|
||||
*/
|
||||
@@ -12,12 +11,12 @@ module es {
|
||||
/**
|
||||
* 在处理冲突时,physicsLayer可以用作过滤器。Flags类有帮助位掩码的方法
|
||||
*/
|
||||
public physicsLayer = 1 << 0;
|
||||
public physicsLayer = new Ref(1 << 0);
|
||||
/**
|
||||
* 碰撞器在使用移动器移动时应该碰撞的层
|
||||
* 默认为所有层
|
||||
*/
|
||||
public collidesWithLayers = Physics.allLayers;
|
||||
public collidesWithLayers: Ref<number> = new Ref(Physics.allLayers);
|
||||
/**
|
||||
* 如果为true,碰撞器将根据附加的变换缩放和旋转
|
||||
*/
|
||||
@@ -124,8 +123,8 @@ module es {
|
||||
let renderableBounds = renderable.bounds;
|
||||
|
||||
// 这里我们需要大小*反尺度,因为当我们自动调整碰撞器的大小时,它需要没有缩放的渲染
|
||||
let width = renderableBounds.width / this.entity.scale.x;
|
||||
let height = renderableBounds.height / this.entity.scale.y;
|
||||
let width = renderableBounds.width / this.entity.transform.scale.x;
|
||||
let height = renderableBounds.height / this.entity.transform.scale.y;
|
||||
// 圆碰撞器需要特别注意原点
|
||||
if (this instanceof CircleCollider) {
|
||||
this.radius = Math.max(width, height) * 0.5;
|
||||
@@ -214,7 +213,7 @@ module es {
|
||||
public collidesWith(collider: Collider, motion: Vector2, result: CollisionResult): boolean {
|
||||
// 改变形状的位置,使它在移动后的位置,这样我们可以检查重叠
|
||||
let oldPosition = this.entity.position;
|
||||
this.entity.position = this.entity.position.add(motion);
|
||||
this.entity.position.add(motion);
|
||||
|
||||
let didCollide = this.shape.collidesWithShape(collider.shape, result);
|
||||
if (didCollide)
|
||||
@@ -225,15 +224,5 @@ module es {
|
||||
|
||||
return didCollide;
|
||||
}
|
||||
|
||||
public clone(): Component {
|
||||
let collider = ObjectUtils.clone<Collider>(this);
|
||||
collider.entity = null;
|
||||
|
||||
if (this.shape)
|
||||
collider.shape = this.shape.clone();
|
||||
|
||||
return collider;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ module es {
|
||||
let bounds = collider.bounds;
|
||||
bounds.x += motion.x;
|
||||
bounds.y += motion.y;
|
||||
let neighbors = Physics.boxcastBroadphaseExcludingSelf(collider, bounds, collider.collidesWithLayers);
|
||||
let neighbors = Physics.boxcastBroadphaseExcludingSelf(collider, bounds, collider.collidesWithLayers.value);
|
||||
|
||||
for (let j = 0; j < neighbors.length; j++) {
|
||||
let neighbor = neighbors[j];
|
||||
|
||||
@@ -24,13 +24,12 @@ module es {
|
||||
let didCollide = false;
|
||||
|
||||
// 获取我们在新位置可能发生碰撞的任何东西
|
||||
this.entity.position = Vector2.add(this.entity.position, motion);
|
||||
this.entity.position.add(motion);
|
||||
|
||||
// 获取任何可能在新位置发生碰撞的东西
|
||||
let neighbors = Physics.boxcastBroadphase(this._collider.bounds, this._collider.collidesWithLayers);
|
||||
for (let i = 0; i < neighbors.length; i++) {
|
||||
let neighbor = neighbors[i];
|
||||
if (this._collider.overlaps(neighbor) && neighbor.enabled) {
|
||||
let neighbors = Physics.boxcastBroadphase(this._collider.bounds, this._collider.collidesWithLayers.value);
|
||||
for (let neighbor of neighbors){
|
||||
if (this._collider.overlaps(neighbor) && neighbor.enabled){
|
||||
didCollide = true;
|
||||
this.notifyTriggerListeners(this._collider, neighbor);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user