module MKXPZTimeStep DEFAULT_FRAME_RATE = 60.0 MAX_FRAME_SCALE = 4.0 module_function def current_frame_rate rate = Graphics.frame_rate.to_f rescue DEFAULT_FRAME_RATE rate = DEFAULT_FRAME_RATE if rate <= 0.0 rate end def default_delta 1.0 / current_frame_rate end def monotonic_now Process.clock_gettime(Process::CLOCK_MONOTONIC) rescue StandardError Time.now.to_f end def sanitize_delta(delta) return default_delta if delta.nil? return default_delta if delta <= 0.0 return default_delta unless delta.finite? [delta, 0.25].min end def frame_scale scale = Graphics.delta.to_f * current_frame_rate return 1.0 if scale <= 0.0 return 1.0 unless scale.finite? [scale, MAX_FRAME_SCALE].min end def adjust_duration(value) return 0 if value.nil? || value <= 0 adjusted = value + 1.0 - frame_scale return 1 if adjusted > 0 && adjusted < 1 [adjusted, 0].max end end class << Graphics alias mkxpz_time_step_update update def update(*args, &block) now = MKXPZTimeStep.monotonic_now last = @mkxpz_time_step_last_tick @mkxpz_time_step_last_tick = now @mkxpz_time_step_delta = if last MKXPZTimeStep.sanitize_delta(now - last) else MKXPZTimeStep.default_delta end mkxpz_time_step_update(*args, &block) end def delta @mkxpz_time_step_delta || MKXPZTimeStep.default_delta end def frame_time_scale MKXPZTimeStep.frame_scale end end class Game_CharacterBase alias mkxpz_time_step_distance_per_frame distance_per_frame def distance_per_frame(*args, &block) mkxpz_time_step_distance_per_frame(*args, &block) * Graphics.frame_time_scale end end class Spriteset_Weather def update_sprite_rain(sprite) scale = Graphics.frame_time_scale sprite.bitmap = @rain_bitmap sprite.x -= scale sprite.y += scale * 6 sprite.opacity -= scale * 12 end def update_sprite_storm(sprite) scale = Graphics.frame_time_scale sprite.bitmap = @storm_bitmap sprite.x -= scale * 3 sprite.y += scale * 6 sprite.opacity -= scale * 12 end def update_sprite_snow(sprite) scale = Graphics.frame_time_scale sprite.bitmap = @snow_bitmap sprite.x -= scale sprite.y += scale * 3 sprite.opacity -= scale * 12 end end class Game_Map alias mkxpz_time_step_scroll_distance scroll_distance def scroll_distance(*args, &block) mkxpz_time_step_scroll_distance(*args, &block) * Graphics.frame_time_scale end def update_parallax scale = Graphics.frame_time_scale sx = (@parallax_sx / 64.0) * scale sy = (@parallax_sy / 64.0) * scale @parallax_x += sx if @parallax_loop_x @parallax_y += sy if @parallax_loop_y end end class Game_Picture alias mkxpz_time_step_update_move update_move alias mkxpz_time_step_update_tone_change update_tone_change def update_move(*args, &block) active = @duration != 0 mkxpz_time_step_update_move(*args, &block) return unless active return if @duration == 0 @duration = MKXPZTimeStep.adjust_duration(@duration) end def update_tone_change(*args, &block) active = @tone_duration != 0 mkxpz_time_step_update_tone_change(*args, &block) return unless active return if @tone_duration == 0 @tone_duration = MKXPZTimeStep.adjust_duration(@tone_duration) end def update_rotate return if @rotate_speed == 0 @angle += (@rotate_speed / 2.0) * Graphics.frame_time_scale @angle += 360 while @angle < 0 @angle %= 360 end end class Game_Screen alias mkxpz_time_step_update_fadeout update_fadeout alias mkxpz_time_step_update_fadein update_fadein alias mkxpz_time_step_update_tone update_tone alias mkxpz_time_step_update_flash update_flash alias mkxpz_time_step_update_shake update_shake def update_fadeout(*args, &block) active = @fadeout_duration != 0 mkxpz_time_step_update_fadeout(*args, &block) return unless active return if @fadeout_duration == 0 @fadeout_duration = MKXPZTimeStep.adjust_duration(@fadeout_duration) end def update_fadein(*args, &block) active = @fadein_duration != 0 mkxpz_time_step_update_fadein(*args, &block) return unless active return if @fadein_duration == 0 @fadein_duration = MKXPZTimeStep.adjust_duration(@fadein_duration) end def update_tone(*args, &block) active = @tone_duration != 0 mkxpz_time_step_update_tone(*args, &block) return unless active return if @tone_duration == 0 @tone_duration = MKXPZTimeStep.adjust_duration(@tone_duration) end def update_flash(*args, &block) active = @flash_duration != 0 mkxpz_time_step_update_flash(*args, &block) return unless active return if @flash_duration == 0 @flash_duration = MKXPZTimeStep.adjust_duration(@flash_duration) end def update_shake(*args, &block) active = @shake_duration != 0 mkxpz_time_step_update_shake(*args, &block) return unless active return if @shake_duration == 0 @shake_duration = MKXPZTimeStep.adjust_duration(@shake_duration) end def update_weather return unless @weather_duration > 0 duration = @weather_duration.to_f @weather_power = (@weather_power * (duration - 1.0) + @weather_power_target) / duration @weather_duration = MKXPZTimeStep.adjust_duration(@weather_duration) @weather_type = :none if @weather_duration <= 0 && @weather_power_target == 0 end end class Game_Timer def update return unless @working return if @chkMode return unless @count > 0 @count -= Graphics.frame_time_scale off if @count <= 0 end def sec seconds = @count.to_f / MKXPZTimeStep.current_frame_rate [seconds.ceil, 0].max end end class Window_Message unless method_defined?(:mkxpz_time_step_update_fiber) alias mkxpz_time_step_update_fiber update_fiber end def update_fiber(*args, &block) @mkxpz_message_char_budget = [@mkxpz_message_char_budget.to_f, 0.0].max + Graphics.frame_time_scale mkxpz_time_step_update_fiber(*args, &block) end def wait(duration) return if timeout? remaining = duration.to_f while remaining > 0.0 remaining -= Graphics.frame_time_scale Fiber.yield if remaining > 0.0 end end def wait_for_one_character update_show_fast return if @show_fast || @line_show_fast @mkxpz_message_char_budget = [@mkxpz_message_char_budget.to_f, 1.0].max - 1.0 Fiber.yield if @mkxpz_message_char_budget < 1.0 end end