Author Topic: [Script] Slight change to the Rupee counter  (Read 1913 times)

ffomega

  • Full Member
  • ***
  • Posts: 223
    • View Profile
    • Solarus Resource & Tutorial Site
[Script] Slight change to the Rupee counter
« 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 - yellow digits

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



Happy mapping!
« Last Edit: December 05, 2016, 02:41:41 am by ffomega »
My tilesets page can be found here:
http://absolute-hyrule-tutorials.solarus-games.org/

ffomega

  • Full Member
  • ***
  • Posts: 223
    • View Profile
    • Solarus Resource & Tutorial Site
Re: [Script] Slight change to the Rupee counter
« Reply #1 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")
My tilesets page can be found here:
http://absolute-hyrule-tutorials.solarus-games.org/