KModTimer VX

KModTimer VX
Version 1.0.0

by Kyonides
​


Introduction

This is a simple scriptlet that provides some extra features for your timer sprite like custom font name and size and even a colored background. The last feature can be deactivated as well.

The embedded comments include a list of the newly available script calls.

The Script

Ruby:
# * KModTimer VX * #
#   Scripter : Kyonides Arkanthes
#   v1.0.0 - 2023-05-02

# * Script Calls * #

# - Reset Timer's Setup
# $game_system.reset_timer

# - Change Timer's X Alignment - Positions: :left, :center or :right
# $game_system.timer_pos_x = X-Alignment

# $game_system.timer_font_name = "Font Name"
# $game_system.timer_font_size = Number
# $game_system.timer_font("Font Name", Number)

# - Change the Font Color
# $game_system.timer_font_color(Red, Green, Blue)
# $game_system.timer_font_color(Red, Green, Blue, Alpha)

# - Change the Background Color
# $game_system.timer_bg_color(Red, Green, Blue)
# $game_system.timer_bg_color(Red, Green, Blue, Alpha)

# - Toggle Background Color - Boolean: true or false
# $game_system.timer_use_bg = Boolean

module KModTimer
  # Initial Values
  FONT_NAME = "Arial"
  FONT_SIZE = 32
  # Color Ranges (0..255) [Red, Green, Blue, Alpha]
  FONT_COLOR = [255, 255, 255, 255]
  BG_COLOR   = [0, 0, 0, 80]
  USE_BG_COLOR = true
  # Choose the Position: :left, :center or :right
  POSITION_X = :left
# * End of Setup Section * #
  extend self
  attr_accessor :refresh
  class TimerData
    def initialize() reset_settings end
    def reset_settings
      @font_name = FONT_NAME.dup
      @font_size = FONT_SIZE
      @font_color = FONT_COLOR.dup
      @bg_color = BG_COLOR.dup
      @use_bg = USE_BG_COLOR
      @pos_x = POSITION_X
    end
    attr_accessor :font_name, :font_size, :font_color
    attr_accessor :bg_color, :use_bg, :pos_x
  end
end

class Sprite_Timer
  def initialize(viewport)
    super(viewport)
    @data = $game_system.timer_setup
    self.bitmap = Bitmap.new(88, 48)
    bg_setup
    font_setup
    update_position
    self.y = 0
    self.z = 500
    update
  end

  def bg_setup
    @bg_color = Color.new(*@data.bg_color)
  end

  def font_setup
    font = self.bitmap.font
    font.name = @data.font_name
    font.size = @data.font_size
    font.color.set(*@data.font_color)
  end

  def update_x
    case @data.pos_x
    when :left
      self.x = 0
    when :center
      self.x = (Graphics.width - self.bitmap.width) / 2
    when :right
      self.x = Graphics.width - self.bitmap.width
    end
  end

  def update_position
    update_x
    self.y = 0
    self.z = 200
  end

  def redraw_backdrop
    bm = self.bitmap
    @data.use_bg ? bm.fill_rect(bm.rect, @bg_color) : bm.clear
  end

  def timer_text
    min = @total_sec / 60
    sec = @total_sec % 60
    sprintf("%02d:%02d", min, sec)
  end

  def redraw
    if KModTimer.refresh
      KModTimer.refresh = nil
      bg_setup
      font_setup
    end
    redraw_backdrop
    bm = self.bitmap
    bm.draw_text(bm.rect, timer_text, 1)
  end

  def update_bitmap
    if $game_system.timer / Graphics.frame_rate != @total_sec
      @total_sec = $game_system.timer / Graphics.frame_rate
      redraw
    end
  end

  def update
    super
    self.visible = $game_system.timer_working
    update_x
    update_bitmap
  end
end

class Game_System
  alias :kyon_mod_timer_gm_sys_init :initialize
  def initialize
    kyon_mod_timer_gm_sys_init
    @timer_setup = KModTimer::TimerData.new
  end

  def reset_timer
    @timer_setup.reset_settings
  end

  def timer_pos_x=(new_x)
    @timer_setup.pos_x = new_x
  end

  def timer_font(new_name, new_size)
    KModTimer.refresh = true
    @timer_setup.font_name = new_name
    @timer_setup.font_size = new_size
  end

  def timer_font_name=(new_name)
    KModTimer.refresh = true
    @timer_setup.font_name = new_name
  end

  def timer_font_size=(new_size)
    KModTimer.refresh = true
    @timer_setup.font_size = new_size
  end

  def timer_font_color(red, green, blue, alpha=255)
    KModTimer.refresh = true
    @timer_setup.font_color = [red, green, blue, alpha]
  end

  def timer_bg_color(red, green, blue, alpha=255)
    KModTimer.refresh = true
    @timer_setup.bg_color = [red, green, blue, alpha]
  end

  def timer_use_bg=(bool)
    KModTimer.refresh = true
    @timer_setup.use_bg = bool
  end
  attr_reader :timer_setup
end

Terms & Conditions

Feel free to use it.
Optionally, you could include my nickname in your game credits.
That's it.
New Features Are Available!​

From now on, all of the RGSS based engines allow you to change the timer's settings during gameplay! 😮

The script calls for RMXP and RMVX are identical. 😉

VX Ace's calls are totally different because of the existence of the Game_Timer class. He, he.

Original thread: https://forums.rpgmakerweb.com/threads/kmodtimer-vx.157110/