新增Physics.linecast/linecastAll方法

This commit is contained in:
YHH
2020-08-22 12:21:40 +08:00
parent 73f0f54ba7
commit 6c1cfec928
16 changed files with 537 additions and 145 deletions

View File

@@ -0,0 +1,105 @@
module samples {
import RenderableComponent = es.RenderableComponent;
import Vector2 = es.Vector2;
import Physics = es.Physics;
export class LineCaster extends RenderableComponent {
private _lastPosition = new Vector2(101, 101);
private _collisionPosition = new Vector2(-1, -1);
private _pixelShape1: egret.Shape;
private _pixelShape2: egret.Shape;
private _lineShape: egret.Shape;
private _pixelShape3: egret.Shape;
private _delayTime = 1;
private _pressTime = 0;
private _canTouch = true;
public get width(){
return 1000;
}
public get height(){
return 1000;
}
constructor() {
super();
this._pixelShape1 = new egret.Shape();
this._pixelShape2 = new egret.Shape();
this._lineShape = new egret.Shape();
this._pixelShape3 = new egret.Shape();
this.displayObject = new egret.DisplayObjectContainer();
let displayContainer = this.displayObject as egret.DisplayObjectContainer;
displayContainer.addChild(this._pixelShape1);
displayContainer.addChild(this._pixelShape2);
displayContainer.addChild(this._pixelShape3);
displayContainer.addChild(this._lineShape);
}
public onAddedToEntity(): void {
KeyboardUtils.registerKey(KeyboardUtils.SPACE, this.spaceDown, this, KeyboardUtils.TYPE_KEY_DOWN);
}
public onRemovedFromEntity(): void {
KeyboardUtils.unregisterKey(KeyboardUtils.SPACE, KeyboardUtils.TYPE_KEY_DOWN);
}
private spaceDown(){
let hit = Physics.linecast(this._lastPosition, this.transform.position);
if (hit.collider){
this._collisionPosition = hit.point;
}
}
public render(camera: es.Camera): any {
this._pixelShape1.graphics.clear();
this._pixelShape1.graphics.beginFill(0xffff00);
this._pixelShape1.graphics.lineStyle(4, 0xffff00);
this._pixelShape1.graphics.lineTo(this._lastPosition.x, this._lastPosition.y);
this._pixelShape1.graphics.endFill();
this._pixelShape2.graphics.clear();
this._pixelShape2.graphics.beginFill(0xffffff);
this._pixelShape2.graphics.lineStyle(4, 0xffffff);
this._pixelShape2.graphics.lineTo(this.transform.position.x, this.transform.position.y);
this._pixelShape2.graphics.endFill();
this._lineShape.graphics.clear();
this._lineShape.graphics.beginFill(0xffffff);
this._lineShape.graphics.lineStyle(1, 0xffffff);
this._lineShape.graphics.lineTo(this._lastPosition.x, this._lastPosition.y);
this._lineShape.graphics.lineTo(this.transform.position.x, this.transform.position.y);
this._lineShape.graphics.endFill();
this._pixelShape3.graphics.clear();
if (this._collisionPosition.x > 0 && this._collisionPosition.y > 0){
this._pixelShape3.graphics.beginFill(0xff0000);
this._pixelShape3.graphics.lineStyle(10, 0xff0000);
this._pixelShape3.graphics.lineTo(this._collisionPosition.x, this._collisionPosition.y);
this._pixelShape3.graphics.endFill();
}
}
public update(): void {
if (!this._canTouch){
if (this._pressTime > this._delayTime){
this._canTouch = true;
this._pressTime = 0;
}else{
this._pressTime += es.Time.deltaTime;
}
return;
}
if (!es.Input.touchPosition.equals(Vector2.zero)){
this._lastPosition = this.transform.position;
this.transform.position = es.Input.touchPosition;
this._collisionPosition = new Vector2(-1, -1);
this._canTouch = false;
}
}
}
}

View File

@@ -0,0 +1,25 @@
module samples {
import Vector2 = es.Vector2;
import SpriteRenderer = es.SpriteRenderer;
import BoxCollider = es.BoxCollider;
export class LineCastingScene extends SampleScene {
public initialize(): void {
super.initialize();
this.content.loadRes("moon_png").then((moonTex: egret.Texture) => {
let playerEntity = this.createEntity("player");
playerEntity.position = new Vector2(es.Core.scene.width / 2, es.Core.scene.height / 2);
playerEntity.addComponent(new SpriteRenderer(moonTex));
let coll = new BoxCollider().setSize(moonTex.textureWidth, moonTex.textureHeight);
playerEntity.addComponent(coll);
playerEntity.position = new Vector2(200, 100);
let lineCaster = this.createEntity("linecaster").addComponent(new LineCaster());
lineCaster.transform.position = new Vector2(300, 100);
manager.AlterManager.alter_tips("lineCasting加载成功");
});
}
}
}