Solarus Forum

Community => Your scripts => Topic started by: ffomega on December 03, 2016, 12:59:49 pm

Title: [Script] Slight change to the Rupee counter
Post by: ffomega on December 03, 2016, 12:59:49 pm
I thought I'd try poking around in the rupee script and came up with a neat little alternate counter.  You'll need to do the following:

1. download the gray and yellow digits fonts and place them in your /data/fonts folder (RIGHT-CLICK > SAVE AS):

gray digits (http://solarus_resource.site88.net/tutorial/download/gray_digits.png) - yellow digits (http://solarus_resource.site88.net/tutorial/download/yellow_digits.png)

Make sure you add the fonts to your game from WITHIN the quest editor as well otherwise your game may not run!!

2. copy this code and paste it into the rupees script located in the /scripts/hud folder:

Code: (lua) [Select]
-- The money counter shown in the game screen.

local rupees_builder = {}

local rupee_icon_img = sol.surface.create("hud/rupee_icon.png")

function rupees_builder:new(game, config)

  local rupees = {}

  local digits_text = sol.text_surface.create({
    font = "white_digits",
    horizontal_alignment = "left",
    vertical_alignment = "bottom",
  })
  local money_displayed = game:get_money()

  local dst_x, dst_y = config.x, config.y

  function rupees:on_draw(dst_surface)

    local x, y = dst_x, dst_y
    local width, height = dst_surface:get_size()
    if x < 0 then
      x = width + x
    end
    if y < 0 then
      y = height + y
    end

    rupee_icon_img:draw(dst_surface, x , y)
    digits_text:draw(dst_surface, x + 12, y + 8)
  end

  -- Checks whether the view displays correct information
  -- and updates it if necessary.
  local function check()

    local need_rebuild = false
    local money = game:get_money()
    local max_money = game:get_max_money()

    -- Current money.
    if money ~= money_displayed then

      need_rebuild = true
      if money_displayed < money then
        money_displayed = money_displayed + 1
      else
        money_displayed = money_displayed - 1
      end

      if money_displayed == money  -- The final value was just reached.
          or money_displayed % 3 == 0 then  -- Otherwise, play sound "rupee_counter_end" every 3 values.
        sol.audio.play_sound("rupee_counter_end")
      end
    end

    if digits_text:get_text() == "" then
      need_rebuild = true
    end

    -- Update the text if something has changed.
    if need_rebuild then
      digits_text:set_text(string.format("%04d", money_displayed))

      -- Show in gray if the 0 is reached.
      if money_displayed  == 0 then
        digits_text:set_font("gray_digits")
      end
      -- Show in yellow if 1/4 of the maximum is reached.
      if money_displayed <= max_money / 4 and money_displayed > 0 then
        digits_text:set_font("yellow_digits")
      end
      -- Show in white if the current is grater than 1/4 of the maximum.
      if money_displayed > max_money / 4 then
        digits_text:set_font("white_digits")
      end
      -- Show in green if the maximum is reached.
      if money_displayed == max_money then
        digits_text:set_font("green_digits")
      end

    end

    return true  -- Repeat the timer.
  end

  -- Periodically check.
  check()
  sol.timer.start(game, 40, check)

  return rupees
end

return rupees_builder


Rupees will display in the following format:

0 rupees = dark gray
1 - {max rupees / 4) - yellow
current rupees greater than {max rupees / 4) - white
max rupees - green

(http://solarus_resource.site88.net/tutorial/download/rupee_counter.gif)

Happy mapping!
Title: Re: [Script] Slight change to the Rupee counter
Post by: ffomega on December 05, 2016, 02:37:46 am
For an added bonus, if you want to set your rupee counter up similar to how I had it in my old project, you can add or remove the leading zeros in the rupee script.  Just open it and go down to line 65 in the script.

Code: ( lua) [Select]
      digits_text:set_text(string.format("%04d", money_displayed))
Change ("%04d") to (%01d")