Skip to content

Instantly share code, notes, and snippets.

@yoggy
Last active January 20, 2024 07:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoggy/60a5b401fdee02ffcb12708e93fa5417 to your computer and use it in GitHub Desktop.
Save yoggy/60a5b401fdee02ffcb12708e93fa5417 to your computer and use it in GitHub Desktop.
const particle = $.subNode("particle");
const se1 = $.audio("EC5227CW-chime");
const se2 = $.audio("beep");
// 初期設定
$.onStart(() => {
$.state.lastUpdatedTime = 0;
$.state.remainingTime = 0;
});
// 残り時間が0になったらパーティクルを止める
$.onUpdate(deltaTime => {
remainingTime = $.state.remainingTime;
if (remainingTime > 0) {
remainingTime -= deltaTime;
if (remainingTime <= 0) {
remainingTime = 0;
particle.setEnabled(false);
}
$.state.remainingTime = remainingTime
}
});
// ボタンを押したときの処理
$.onInteract(() => {
// ボタン連打防止。10秒以上まつこと
if (new Date().getTime() - $.state.lastUpdatedTime < 10 * 1000) {
$.log("No button mashing!");
$.state.lastUpdatedTime = new Date().getTime();
se2.play();
return;
}
$.log("callExternal()");
// callExternal用のデータを用意する
let msg = {message_type:"pingpong"};
let request_data = JSON.stringify(msg)
$.callExternal(request_data, "meta_data"); // meta_dataはonExternalCallEndで見分ける時に使うデータ
// ボタンを押した時刻を記録
$.state.lastUpdatedTime = new Date().getTime();
// 音を鳴らす
se1.play();
// パーティクルを有効にする
particle.setEnabled(true);
// 4秒後にパーティクルを停止する
$.state.remainingTime = 4.0;
});
// サーバからのレスポンスを受信する関数
$.onExternalCallEnd((response, meta, errorReason) => {
$.log("response="+response+",meta="+meta+",errorReason="+errorReason);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment