base/timer
쿨다운과 반복 타이머. (원본 base/timer.js 의 Cooldown, RepeatTimer)
- 시간은 초 단위 f64 다. (엔진 TimeManager::get_time_delta 와 같은 타입)
- 발동은 두 엔진 모두 activate() 다. (예약어 use 를 이름으로 쓰지 않는다)
- 원본 기본 인자(duration = 1, interval = 1, elapsed_event = null)는 Default 와 new 의 인자로 옮겼다.
- RepeatTimer 의 콜백은
Box<dyn FnMut()>다. (씬의 set_first_touch_callback 과 같은 꼴) 게임 상태를 바꾸려면Rc<Cell>/Rc<RefCell>을 잡아 넣는다.
Cooldown and repeat timer. (Cooldown and RepeatTimer of the original base/timer.js)
- Time is f64 in seconds. (the same type as the engine's TimeManager::get_time_delta)
- Triggering is activate() in both engines. (the reserved word use is not used as a name)
- The original default arguments (duration = 1, interval = 1, elapsed_event = null) are carried over to Default and the arguments of new.
- The callback of RepeatTimer is
Box<dyn FnMut()>. (the same form as set_first_touch_callback of a scene) To change game state, capture and pass in anRc<Cell>/Rc<RefCell>.
Cooldownstruct traits Clone, Copy, Debug, PartialEq, Default
쿨다운.
- "timer -= dt; if (timer <= 0) ..." 를 매번 손으로 쓰던 것을 표준화했다.
- activate() 로 발동하면 지정한 시간 동안 is_ready() 가 거짓이 된다.
Cooldown.
- Standardizes the "timer -= dt; if (timer <= 0) ..." that was written by hand every time.
- After activate() triggers it, is_ready() is false for the given time.
use vanilla::Cooldown;
let mut cooldown = Cooldown::new(0.5);
assert!(cooldown.activate());
assert!(!cooldown.is_ready());
assert!(!cooldown.activate());
cooldown.tick(0.25);
assert_eq!(cooldown.get_remain_seconds(), 0.25);
assert_eq!(cooldown.get_progress(), 0.5);
cooldown.tick(0.25);
assert!(cooldown.is_ready());생성. (duration 은 초, 0 보다 작으면 0)
Constructor. (duration in seconds, 0 if less than 0)
- durationf64
- 쿨다운 시간. (초, 0 보다 작으면 0)Cooldown time. (seconds, 0 if less than 0)
반환ReturnsCooldown 준비된 상태의 쿨다운. (남은 시간 0)A cooldown in the ready state. (remaining time 0)
짝Counterpart: vanilla.js Cooldown.constructor
갱신.
Update.
- time_deltaf64
- 지난 프레임과의 간격. (초, 남은 시간에서 빼고 0 아래로는 내려가지 않는다)Interval since the last frame. (seconds, subtracted from the remaining time, which does not go below 0)
짝Counterpart: vanilla.js Cooldown.tick
발동 가능 여부.
Whether it can be triggered.
반환Returnsbool 남은 시간이 0 이하면 true.true if the remaining time is 0 or less.
짝Counterpart: vanilla.js Cooldown.isReady
발동한다. (원본 activate, 준비되어 있으면 쿨다운을 시작하고 참을 돌려준다)
Triggers. (the original activate; if ready, starts the cooldown and returns true)
반환Returnsbool 발동했으면 true. 아직 쿨다운 중이면 false. (쿨다운 시간이 0 이면 발동해도 곧바로 준비 상태다)true if triggered. false if still cooling down. (if the cooldown time is 0, it is ready again right after triggering)
짝Counterpart: vanilla.js Cooldown.activate
곧바로 준비 상태로 되돌린다.
Immediately returns to the ready state.
짝Counterpart: vanilla.js Cooldown.reset
쿨다운 시간 설정. (0 보다 작으면 0)
Sets the cooldown time. (0 if less than 0)
- durationf64
- 쿨다운 시간. (초, 지금 남은 시간은 바꾸지 않고 다음 발동부터 쓴다)Cooldown time. (seconds; does not change the current remaining time, used from the next trigger)
짝Counterpart: vanilla.js Cooldown.setDuration
쿨다운 시간 반환.
Returns the cooldown time.
반환Returnsf64 쿨다운 시간. (초)Cooldown time. (seconds)
짝Counterpart: vanilla.js Cooldown.getDuration
남은 시간 반환.
Returns the remaining time.
반환Returnsf64 다시 준비될 때까지 남은 시간. (초, 준비되어 있으면 0)Time remaining until ready again. (seconds, 0 if ready)
짝Counterpart: vanilla.js Cooldown.getRemainSeconds
진행 비율 반환. (0 = 방금 발동, 1 = 준비 완료)
Returns the progress ratio. (0 = just triggered, 1 = ready)
반환Returnsf64 1 - 남은 시간 / 쿨다운 시간. (쿨다운 시간이 0 이면 1, 발동 뒤 set_duration 으로 남은 시간보다 짧게 바꾸면 0 보다 작을 수 있다)1 - remaining time / cooldown time. (1 if the cooldown time is 0; can be less than 0 if set_duration after triggering makes it shorter than the remaining time)
짝Counterpart: vanilla.js Cooldown.getProgress
RepeatTimerstruct traits Default
반복 타이머.
- 일정 간격마다 콜백을 부른다. 프레임이 길어져 여러 번 지나쳤으면 밀린 만큼 몰아서 부른다.
Repeating timer.
- Calls the callback at every interval. If several intervals passed because a frame ran long, calls it for all the missed ones at once.
use std::cell::Cell;
use std::rc::Rc;
use vanilla::RepeatTimer;
let spawn_count = Rc::new(Cell::new(0));
let spawn_count_in_event = spawn_count.clone();
let mut repeat_timer = RepeatTimer::new(0.25, Some(Box::new(move || {
spawn_count_in_event.set(spawn_count_in_event.get() + 1);
})));
repeat_timer.tick(0.125);
assert_eq!(spawn_count.get(), 0);
repeat_timer.tick(0.625);
assert_eq!(spawn_count.get(), 3);
repeat_timer.pause();
repeat_timer.tick(1.0);
assert_eq!(spawn_count.get(), 3);생성. (interval 은 초, elapsed_event 는 간격마다 부르는 콜백, 만들자마자 돌아간다)
Constructor. (interval in seconds, elapsed_event is the callback called every interval, runs as soon as it is created)
- intervalf64
- 간격. (초, 0.000001 보다 작으면 0.000001)Interval. (seconds, 0.000001 if less than 0.000001)
- elapsed_eventOption<Box<dyn FnMut()>>
- 간격마다 부르는 콜백. None 이면 시간만 흐른다.Callback called every interval. If None, only time passes.
반환ReturnsRepeatTimer 돌아가는 상태의 반복 타이머. (누적 시간 0)A repeat timer in the running state. (accumulated time 0)
짝Counterpart: vanilla.js RepeatTimer.constructor
갱신. (지난 간격 수만큼 콜백을 부른다)
Update. (calls the callback once for each interval passed)
- time_deltaf64
- 지난 프레임과의 간격. (초, 멈춰 있으면 무시한다)Interval since the last frame. (seconds, ignored while paused)
짝Counterpart: vanilla.js RepeatTimer.tick
정지. (누적 시간도 비운다)
Stops. (also clears the accumulated time)
짝Counterpart: vanilla.js RepeatTimer.stop
일시 정지. (누적 시간은 남긴다)
Pauses. (keeps the accumulated time)
짝Counterpart: vanilla.js RepeatTimer.pause
간격 설정. (하한 0.000001 초)
Sets the interval. (lower limit 0.000001 seconds)
- intervalf64
- 간격. (초, 쌓아 둔 누적 시간은 그대로 둔다)Interval. (seconds, the accumulated time is kept as is)
짝Counterpart: vanilla.js RepeatTimer.setInterval
간격 반환.
Returns the interval.
반환Returnsf64 간격. (초)Interval. (seconds)
짝Counterpart: vanilla.js RepeatTimer.getInterval
콜백 설정. (None 이면 콜백 없이 시간만 흐른다)
Sets the callback. (if None, only time passes without a callback)
- elapsed_eventOption<Box<dyn FnMut()>>
- 간격마다 부를 콜백. (앞의 콜백을 바꾼다)Callback called every interval. (replaces the previous callback)
짝Counterpart: vanilla.js RepeatTimer.setElapsedEvent
동작 중 여부.
Whether it is running.
반환Returnsbool 돌아가는 중이면 true. (만든 뒤와 start 뒤 true, stop 과 pause 뒤 false)true if running. (true after creation and after start, false after stop and pause)
짝Counterpart: vanilla.js RepeatTimer.isRunning