vanilladocs
vanillapunch.com
API / core/timemanager

core/timemanager

시간 관리자. (원본 core/timemanager.js)

  • 엔진이 프레임마다 경과 초를 넣고, 누적 시간, 프레임 간격, 초당 프레임 수, 시간 배율을 센다.
  • 씬에서는 engine.get_time_manager() 로 읽고 engine.get_time_manager_mut() 로 배율과 상한을 바꾼다.

Time manager. (the original core/timemanager.js)

  • The engine feeds the elapsed seconds every frame, and it tracks the accumulated time, frame interval, frames per second and time scale.
  • In a scene, read it with engine.get_time_manager() and change the scale and limit with engine.get_time_manager_mut().

TimeManagerstruct traits Default

짝Counterpart: vanilla.js TimeManager소스source

시간 관리자.

  • 원본은 request_animation_frame 의 timestamp(밀리초)를 받는다. 여기서는 엔진 시작 뒤 경과 초를 받는다.
  • 한 프레임 간격의 상한은 원본처럼 0.25 초가 기본이고 set_maximum_time_delta 로 바꾼다.

Time manager.

  • The original receives the request_animation_frame timestamp (milliseconds). Here it receives the seconds elapsed since the engine started.
  • The limit of one frame interval defaults to 0.25 seconds as in the original and is changed with set_maximum_time_delta.
예제Example경과 초를 넣고 프레임 간격과 배율 읽기Feed elapsed seconds and read the frame interval and scale
use vanilla::TimeManager;

let mut time_manager = TimeManager::new();
time_manager.update(0.0);
time_manager.update(0.1);
assert!((time_manager.get_unscale_delta_time() - 0.1).abs() < 1e-6);
time_manager.set_time_scale(0.5);
assert!((time_manager.get_time_delta() - 0.05).abs() < 1e-6);
time_manager.update(2.0);
assert_eq!(time_manager.get_unscale_delta_time(), 0.25);
assert!((time_manager.get_time() - 0.35).abs() < 1e-9);
예제Example씬에서 느리게 돌리고 프레임 간격 읽기Run slowly in a scene and read the frame interval
use vanilla::{Engine, EngineConfiguration, Scene, SceneBase};

struct MainScene {
	scene_base: SceneBase,
}

impl Scene for MainScene {
	fn get_scene_base(&self) -> &SceneBase {
		&self.scene_base
	}

	fn get_scene_base_mut(&mut self) -> &mut SceneBase {
		&mut self.scene_base
	}

	fn load(&mut self, engine: &Engine) {
		let mut time_manager = engine.get_time_manager_mut();
		time_manager.set_time_scale(0.5);
		time_manager.set_maximum_time_delta(0.1);
	}

	fn tick(&mut self, time_delta: f64) {
		let engine = Engine::get_engine().unwrap();
		self.scene_base.tick(time_delta);
		let time_manager = engine.get_time_manager();
		let unscaled_time_delta = time_manager.get_unscale_delta_time();
		let frame_per_second = time_manager.get_frame_per_second();
		println!("{} {} {}", time_delta, unscaled_time_delta, frame_per_second);
	}
}

let engine_configuration = EngineConfiguration::new();
let engine = Engine::new(engine_configuration);
let main_scene = MainScene {
	scene_base: SceneBase::new(),
};
engine.run(Box::new(main_scene)).unwrap();
생성자constructorpub fn new() -> TimeManager#0.3.2 부터since 0.3.2소스source

생성.

Constructor.

반환ReturnsTimeManager 새 시간 관리자. (시간 0, 배율 1, 상한 0.25 초, 첫 update 를 기다린다)A new time manager. (time 0, scale 1, limit 0.25 seconds, waiting for the first update)

짝Counterpart: vanilla.js TimeManager.constructor

메서드methodpub fn update(&mut self, seconds_since_startup: f64)#0.3.2 부터since 0.3.2소스source

갱신. (seconds_since_startup: 엔진 시작 뒤 경과 초)

  • 첫 호출은 그 시각을 시작으로 삼아 간격 0 이다. 그 뒤로는 지난 호출과의 차를 0 ~ 상한으로 잘라 누적 시간에 더한다.
  • 지난 초당 프레임 수 확정 뒤 1 초가 지나면 그동안 센 프레임 수를 초당 프레임 수로 확정한다.

Update. (seconds_since_startup: seconds elapsed since the engine started)

  • The first call takes that time as the start, with an interval of 0. After that, the difference from the last call is clamped to 0 ~ limit and added to the accumulated time.
  • When 1 second has passed since the last frames per second was settled, the frames counted meanwhile are settled as the frames per second.
seconds_since_startupf64
엔진 시작 뒤 경과 시간. (초, 프레임마다 늘어나는 값)Time elapsed since the engine started. (seconds, a value that increases every frame)

짝Counterpart: vanilla.js TimeManager.update

예제Example첫 호출은 간격 0, 큰 간격은 상한에서 잘린다The first call has an interval of 0, and large intervals are clamped at the limit
use vanilla::TimeManager;

let mut time_manager = TimeManager::new();
time_manager.update(10.0);
assert_eq!(time_manager.get_unscale_delta_time(), 0.0);
time_manager.update(15.0);
assert_eq!(time_manager.get_unscale_delta_time(), 0.25);
assert_eq!(time_manager.get_realtime_since_startup(), 15.0);
메서드methodpub fn get_realtime_since_startup(&self) -> f64#0.3.2 부터since 0.3.2소스source

엔진 시작 뒤 경과 시간. (초)

Time elapsed since the engine started. (seconds)

반환Returnsf64 마지막 update 에 넣은 경과 초. (update 전에는 0)Elapsed seconds fed to the last update. (0 before update)

짝Counterpart: vanilla.js TimeManager.getRealtimeSinceStartup

메서드methodpub fn get_time(&self) -> f64#0.3.2 부터since 0.3.2소스source

누적 게임 시간. (초, 프레임 간격 제한을 거친 값의 합)

Accumulated game time. (seconds, sum of values after the frame interval limit)

반환Returnsf64 누적 시간. (초, 배율을 곱하지 않는다)Accumulated time. (seconds, not multiplied by the scale)

짝Counterpart: vanilla.js TimeManager.getTime

메서드methodpub fn get_unscale_delta_time(&self) -> f64#0.3.2 부터since 0.3.2소스source

배율을 곱하지 않은 프레임 간격. (초)

Frame interval not multiplied by the scale. (seconds)

반환Returnsf64 이번 프레임 간격. (초, 0 ~ 상한)This frame's interval. (seconds, 0 ~ limit)

짝Counterpart: vanilla.js TimeManager.getUnscaleDeltaTime

메서드methodpub fn get_time_delta(&self) -> f64#0.3.2 부터since 0.3.2소스source

배율을 곱한 프레임 간격. (초)

Frame interval multiplied by the time scale. (seconds)

반환Returnsf64 이번 프레임 간격 곱하기 시간 배율. (초, 배율이 음수면 음수)This frame's interval times the time scale. (seconds, negative if the scale is negative)

짝Counterpart: vanilla.js TimeManager.getTimeDelta

메서드methodpub fn get_frame_per_second(&self) -> u32#0.3.2 부터since 0.3.2소스source

지난 1초 동안의 프레임 수.

Number of frames in the last 1 second.

반환Returnsu32 초당 프레임 수. (첫 1 초가 지나기 전에는 0)Frames per second. (0 before the first 1 second passes)

짝Counterpart: vanilla.js TimeManager.getFramePerSecond

메서드methodpub fn set_time_scale(&mut self, time_scale: f64)#0.3.2 부터since 0.3.2소스source

시간 배율 설정. (원본처럼 받은 값 그대로, 음수면 get_time_delta 도 음수다)

Sets the time scale. (the value as given, as in the original, get_time_delta is negative too if it is negative)

time_scalef64
시간 배율. (1 이면 그대로, 0.5 면 절반 속도, 0 이면 멈춤, 기본 1)Time scale. (1 is normal, 0.5 is half speed, 0 stops, default 1)

짝Counterpart: vanilla.js TimeManager.setTimeScale

메서드methodpub fn get_time_scale(&self) -> f64#0.3.2 부터since 0.3.2소스source

시간 배율 반환.

Returns the time scale.

반환Returnsf64 지금 시간 배율. (기본 1)Current time scale. (default 1)

짝Counterpart: vanilla.js TimeManager.getTimeScale

메서드methodpub fn set_maximum_time_delta(&mut self, seconds: f64)#0.4.0 부터since 0.4.0소스source

한 프레임 간격의 상한 설정. (초, 기본 0.25. 0 보다 작거나 유한하지 않으면 0.25 로 둔다)

  • 창이 가려졌다 돌아오거나 오래 멈춘 뒤의 첫 프레임 간격을 이 값으로 자른다. 한 번에 크게 나아가면 안 되는 게임은 0.1, 0.05 처럼 줄인다.

Sets the limit of one frame interval. (seconds, default 0.25. Set to 0.25 if less than 0 or not finite)

  • The first frame interval after the window is hidden and returns or after a long stall is clamped to this value. Games that must not advance a lot at once lower it, like 0.1 or 0.05.
secondsf64
한 프레임 간격의 상한. (초, 0 이상의 유한한 값)Limit of one frame interval. (seconds, a finite value of 0 or more)

짝Counterpart: vanilla.js TimeManager.setMaximumTimeDelta

예제Example상한을 줄이고, 잘못된 값은 기본값으로Lower the limit, and invalid values fall back to the default
use vanilla::TimeManager;

let mut time_manager = TimeManager::new();
time_manager.set_maximum_time_delta(0.05);
assert_eq!(time_manager.get_maximum_time_delta(), 0.05);
time_manager.set_maximum_time_delta(-1.0);
assert_eq!(time_manager.get_maximum_time_delta(), 0.25);
메서드methodpub fn get_maximum_time_delta(&self) -> f64#0.4.0 부터since 0.4.0소스source

한 프레임 간격의 상한 반환. (초)

Returns the upper limit of one frame interval. (seconds)

반환Returnsf64 지금 상한. (초, 기본 0.25)Current limit. (seconds, default 0.25)

짝Counterpart: vanilla.js TimeManager.getMaximumTimeDelta