新增Physics.linecast/linecastAll方法
This commit is contained in:
105
demo/src/Scenes/LineCasting/LineCaster.ts
Normal file
105
demo/src/Scenes/LineCasting/LineCaster.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
25
demo/src/Scenes/LineCasting/LineCastingScene.ts
Normal file
25
demo/src/Scenes/LineCasting/LineCastingScene.ts
Normal 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加载成功");
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ module sc {
|
||||
private _sceneList: SceneData[] = [
|
||||
new SceneData("空白场景", samples.BasicScene),
|
||||
new SceneData("Tiled Tiles", samples.AnimatedTilesScene),
|
||||
new SceneData("Linecasting", samples.LineCastingScene),
|
||||
];
|
||||
|
||||
constructor() {
|
||||
@@ -32,6 +33,7 @@ module sc {
|
||||
private scItemOnClick(evt: egret.Event){
|
||||
let data = evt.currentTarget.data;
|
||||
es.Core.scene = new data();
|
||||
es.Core.scene.camera.position = es.Vector2.zero;
|
||||
}
|
||||
|
||||
public destroy() {
|
||||
|
||||
Reference in New Issue
Block a user