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

ffomega

  • Full Member
  • ***
  • Posts: 198
    • 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
  1. -- The money counter shown in the game screen.
  2.  
  3. local rupees_builder = {}
  4.  
  5. local rupee_icon_img = sol.surface.create("hud/rupee_icon.png")
  6.  
  7. function rupees_builder:new(game, config)
  8.  
  9.   local rupees = {}
  10.  
  11.   local digits_text = sol.text_surface.create({
  12.     font = "white_digits",
  13.     horizontal_alignment = "left",
  14.     vertical_alignment = "bottom",
  15.   })
  16.   local money_displayed = game:get_money()
  17.  
  18.   local dst_x, dst_y = config.x, config.y
  19.  
  20.   function rupees:on_draw(dst_surface)
  21.  
  22.     local x, y = dst_x, dst_y
  23.     local width, height = dst_surface:get_size()
  24.     if x < 0 then
  25.       x = width + x
  26.     end
  27.     if y < 0 then
  28.       y = height + y
  29.     end
  30.  
  31.     rupee_icon_img:draw(dst_surface, x , y)
  32.     digits_text:draw(dst_surface, x + 12, y + 8)
  33.   end
  34.  
  35.   -- Checks whether the view displays correct information
  36.   -- and updates it if necessary.
  37.   local function check()
  38.  
  39.     local need_rebuild = false
  40.     local money = game:get_money()
  41.     local max_money = game:get_max_money()
  42.  
  43.     -- Current money.
  44.     if money ~= money_displayed then
  45.  
  46.       need_rebuild = true
  47.       if money_displayed < money then
  48.         money_displayed = money_displayed + 1
  49.       else
  50.         money_displayed = money_displayed - 1
  51.       end
  52.  
  53.       if money_displayed == money  -- The final value was just reached.
  54.           or money_displayed % 3 == 0 then  -- Otherwise, play sound "rupee_counter_end" every 3 values.
  55.         sol.audio.play_sound("rupee_counter_end")
  56.       end
  57.     end
  58.  
  59.     if digits_text:get_text() == "" then
  60.       need_rebuild = true
  61.     end
  62.  
  63.     -- Update the text if something has changed.
  64.     if need_rebuild then
  65.       digits_text:set_text(string.format("%04d", money_displayed))
  66.  
  67.       -- Show in gray if the 0 is reached.
  68.       if money_displayed  == 0 then
  69.         digits_text:set_font("gray_digits")
  70.       end
  71.       -- Show in yellow if 1/4 of the maximum is reached.
  72.       if money_displayed <= max_money / 4 and money_displayed > 0 then
  73.         digits_text:set_font("yellow_digits")
  74.       end
  75.       -- Show in white if the current is grater than 1/4 of the maximum.
  76.       if money_displayed > max_money / 4 then
  77.         digits_text:set_font("white_digits")
  78.       end
  79.       -- Show in green if the maximum is reached.
  80.       if money_displayed == max_money then
  81.         digits_text:set_font("green_digits")
  82.       end
  83.  
  84.     end
  85.  
  86.     return true  -- Repeat the timer.
  87.   end
  88.  
  89.   -- Periodically check.
  90.   check()
  91.   sol.timer.start(game, 40, check)
  92.  
  93.   return rupees
  94. end
  95.  
  96. return rupees_builder
  97.  
  98.  

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 »

ffomega

  • Full Member
  • ***
  • Posts: 198
    • 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
  1.       digits_text:set_text(string.format("%04d", money_displayed))

Change ("%04d") to (%01d")