Skip to content

Instantly share code, notes, and snippets.

@hobione2k
Last active May 31, 2023 04:11
Show Gist options
  • Save hobione2k/e6a6e87cc095e7b34ed3a8737cd6e805 to your computer and use it in GitHub Desktop.
Save hobione2k/e6a6e87cc095e7b34ed3a8737cd6e805 to your computer and use it in GitHub Desktop.
ClusterScript Timer
const TIMER_KEY = (name) => `timer_${name}`;
const isTimerActive = (name) => $.state[TIMER_KEY(name)] >= 0;
const startTimer = (name, duration) => {
const key = TIMER_KEY(name);
if (isTimerActive(name)) return false; // この行を消すと再実行でタイマーを延長できる
$.state[key] = duration;
return true;
};
const isTimerEnd = (name, deltaTime) => {
const key = TIMER_KEY(name);
if (!isTimerActive(name)) return false;
$.state[key] -= deltaTime;
if ($.state[key] > 0) return false;
$.state[key] = -1;
return true;
};
@hobione2k
Copy link
Author

hobione2k commented May 30, 2023

使い方

$.onInteract(() => {
  startTimer("Timer1", 2);  // Timer1を2秒にセット
});

$.onUpdate((deltaTime) => {
  if (isTimerEnd("Timer1", deltaTime)) {
    // 2秒経ったらなんか実行
  }
  if(isTimerActive("Timer1")) {
    // タイマーが動いている2秒間の間なんか実行
  }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment