Created
August 11, 2020 11:43
-
-
Save flushpot1125/a903ca19a9a40cccf03d00327b1d6b26 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Mesh,PhysicsImpostor } from "@babylonjs/core"; | |
import { fromScene} from "../tools"; | |
export default class block extends Mesh { | |
@fromScene("ball") | |
_ball :Mesh;//1つ上の行の"@fromScene"の引数が、Hierarchy View上のオブジェクト名。そのオブジェクトを_ballに対応させる | |
private callFlag : boolean = false; | |
//block生成時に1度だけ呼ばれる | |
public onStart(): void { | |
//ここでブロックに物理エンジンをつけている。mass:0にしないと、ブロックが浮いてしまう。friction(摩擦係数):0, restitution(反発係数):1により弾性衝突を実現 | |
this.physicsImpostor = new PhysicsImpostor(this, PhysicsImpostor.BoxImpostor, { mass: 0, friction: 0, restitution: 1.0 }); | |
} | |
//毎フレームごとに呼ばれる | |
public onUpdate(): void { | |
//intersectsMeshを使うと、引数のオブジェクトと衝突したら、という処理が作れる | |
if ((this.intersectsMesh(this._ball)) && this.callFlag== false){ | |
this.callFlag=true; | |
console.log("collisioned!"); | |
// this.dispose();//dispose = Unityのdestroyと同じ。ここで呼べば、ボールが衝突した瞬間に消えるので、ボールが貫通してしまう | |
this.blockDispose(); | |
} | |
} | |
//JavaScript、TypeScriptのasync/await、Promiseを使って、非同期で処理を遅れて実行させる (Unityのコルーチンに似てる) | |
private async blockDispose():Promise<void>{ | |
const sleep = (second) => new Promise(resolve => setTimeout(resolve, second )); | |
await sleep(200); | |
this.dispose();//1つ上の行で200msec待ったらこれを実行 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment